Skip to content
This repository has been archived by the owner on Jul 16, 2024. It is now read-only.

Commit

Permalink
add schemaBodyFormJson tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-smart committed Dec 20, 2023
1 parent ee1d912 commit 221fcfe
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 3 deletions.
11 changes: 11 additions & 0 deletions docs/platform/Http/Body.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Added in v1.0.0
- [text](#text)
- [uint8Array](#uint8array)
- [unsafeJson](#unsafejson)
- [urlParams](#urlparams)
- [errors](#errors)
- [BodyError](#bodyerror)
- [BodyError (interface)](#bodyerror-interface)
Expand Down Expand Up @@ -184,6 +185,16 @@ export declare const unsafeJson: (body: unknown) => Uint8Array
Added in v1.0.0
## urlParams
**Signature**
```ts
export declare const urlParams: (urlParams: UrlParams.UrlParams) => Uint8Array
```
Added in v1.0.0
# errors
## BodyError
Expand Down
92 changes: 92 additions & 0 deletions packages/platform-node/test/HttpServer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,98 @@ describe("HttpServer", () => {
expect(response.status).toEqual(400)
}).pipe(Effect.scoped, runPromise))

it("schemaBodyFormJson", () =>
Effect.gen(function*(_) {
yield* _(
Http.router.empty,
Http.router.post(
"/upload",
Effect.gen(function*(_) {
const result = yield* _(
Http.request.schemaBodyFormJson(Schema.struct({
test: Schema.string
}))("json")
)
expect(result.test).toEqual("content")
return Http.response.empty()
}).pipe(Effect.scoped)
),
Effect.tapErrorCause(Effect.logError),
Http.server.serveEffect()
)
const client = yield* _(makeClient)
const formData = new FormData()
formData.append("json", JSON.stringify({ test: "content" }))
const response = yield* _(
client(HttpC.request.post("/upload", { body: HttpC.body.formData(formData) }))
)
expect(response.status).toEqual(204)
}).pipe(Effect.scoped, runPromise))

it("schemaBodyFormJson file", () =>
Effect.gen(function*(_) {
yield* _(
Http.router.empty,
Http.router.post(
"/upload",
Effect.gen(function*(_) {
const result = yield* _(
Http.request.schemaBodyFormJson(Schema.struct({
test: Schema.string
}))("json")
)
expect(result.test).toEqual("content")
return Http.response.empty()
}).pipe(Effect.scoped)
),
Effect.tapErrorCause(Effect.logError),
Http.server.serveEffect()
)
const client = yield* _(makeClient)
const formData = new FormData()
formData.append(
"json",
new Blob([JSON.stringify({ test: "content" })], { type: "application/json" }),
"test.json"
)
const response = yield* _(
client(HttpC.request.post("/upload", { body: HttpC.body.formData(formData) }))
)
expect(response.status).toEqual(204)
}).pipe(Effect.scoped, runPromise))

it("schemaBodyFormJson url encoded", () =>
Effect.gen(function*(_) {
yield* _(
Http.router.empty,
Http.router.post(
"/upload",
Effect.gen(function*(_) {
const result = yield* _(
Http.request.schemaBodyFormJson(Schema.struct({
test: Schema.string
}))("json")
)
expect(result.test).toEqual("content")
return Http.response.empty()
}).pipe(Effect.scoped)
),
Effect.tapErrorCause(Effect.logError),
Http.server.serveEffect()
)
const client = yield* _(makeClient)
const response = yield* _(
client(
HttpC.request.post("/upload", {
body: HttpC.body.urlParams(HttpC.urlParams.fromInput({
json: JSON.stringify({ test: "content" })
}))
})
)
)
expect(response.status).toEqual(204)
}).pipe(Effect.scoped, runPromise))

it("tracing", () =>
Effect.gen(function*(_) {
yield* _(
Expand Down
7 changes: 7 additions & 0 deletions packages/platform/src/Http/Body.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type * as Effect from "effect/Effect"
import type * as Stream_ from "effect/Stream"
import type * as PlatformError from "../Error.js"
import type * as FileSystem from "../FileSystem.js"
import type * as UrlParams from "../Http/UrlParams.js"
import * as internal from "../internal/http/body.js"

/**
Expand Down Expand Up @@ -167,6 +168,12 @@ export const jsonSchema: <I, A>(
schema: Schema.Schema<I, A>
) => (body: A) => Effect.Effect<never, BodyError, Uint8Array> = internal.jsonSchema

/**
* @since 1.0.0
* @category constructors
*/
export const urlParams: (urlParams: UrlParams.UrlParams) => Uint8Array = internal.urlParams

/**
* @since 1.0.0
* @category models
Expand Down
12 changes: 9 additions & 3 deletions packages/platform/src/internal/http/body.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as Stream_ from "effect/Stream"
import type * as PlatformError from "../../Error.js"
import * as FileSystem from "../../FileSystem.js"
import type * as Body from "../../Http/Body.js"
import * as UrlParams from "../../Http/UrlParams.js"

/** @internal */
export const TypeId: Body.TypeId = Symbol.for(
Expand Down Expand Up @@ -68,13 +69,14 @@ class Uint8ArrayImpl implements Body.Uint8Array {
export const uint8Array = (body: Uint8Array, contentType?: string): Body.Uint8Array =>
new Uint8ArrayImpl(body, contentType ?? "application/octet-stream")

const encoder = new TextEncoder()

/** @internal */
export const text = (body: string, contentType?: string): Body.Uint8Array =>
uint8Array(new TextEncoder().encode(body), contentType ?? "text/plain")
uint8Array(encoder.encode(body), contentType ?? "text/plain")

/** @internal */
export const unsafeJson = (body: unknown): Body.Uint8Array =>
uint8Array(new TextEncoder().encode(JSON.stringify(body)), "application/json")
export const unsafeJson = (body: unknown): Body.Uint8Array => text(JSON.stringify(body), "application/json")

/** @internal */
export const json = (body: unknown): Effect.Effect<never, Body.BodyError, Body.Uint8Array> =>
Expand All @@ -83,6 +85,10 @@ export const json = (body: unknown): Effect.Effect<never, Body.BodyError, Body.U
catch: (error) => BodyError({ _tag: "JsonError", error })
})

/** @internal */
export const urlParams = (urlParams: UrlParams.UrlParams): Body.Uint8Array =>
text(UrlParams.toString(urlParams), "application/x-www-form-urlencoded")

/** @internal */
export const jsonSchema = <I, A>(schema: Schema.Schema<I, A>) => {
const encode = Schema.encode(schema)
Expand Down

0 comments on commit 221fcfe

Please sign in to comment.