Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
v1rtl committed Sep 22, 2021
1 parent 59ef9ee commit 9ae2775
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 161 deletions.
81 changes: 0 additions & 81 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,87 +11,6 @@ Tiny body parser for Deno. Port of the [milliparsec](https://github.com/talentle
- parses only when `Content-Type` header matches the parser's type
- custom callback function

## Examples

### Vanilla

```ts
import { serve } from 'https://deno.land/std@0.89.0/http/server.ts'
import { json, ReqWithBody } from 'https://deno.land/x/parsec/mod.ts'

const s = serve({ port: 3000 })

for await (const req of s) {
await json(req, undefined, (err) => {
if (err) req.respond({ status: 400, body: err.message })
else req.respond({ status: 204 })
})
if (!(req as ReqWithBody).requestBody) {
req.respond({ status: 404, body: 'No body found' })
} else {
const response = JSON.stringify((req as ReqWithBody).requestBody, null, 2)
req.respond({ body: response })
}
}
```

### [tinyhttp](https://github.com/talentlessguy/tinyhttp-deno)

```ts
import { App, Request } from 'https://deno.land/x/tinyhttp/mod.ts'
import { json, ReqWithBody } from 'https://deno.land/x/parsec/mod.ts'

const app = new App<unknown, Request & ReqWithBody>()

app
.use(json)
.post((req, res) => {
res.send(req.requestBody || {})
})
.listen(3000, () => console.log(`Started on :3000`))
```

## [Opine](https://github.com/asos-craigmorten/opine)

```ts
import { opine } from 'https://deno.land/x/opine/mod.ts'
import { json } from 'https://deno.land/x/parsec/mod.ts'

const app = opine()

app
.use(json)
.post('/', (req, res) => {
res.send(req.parsedBody || {})
})
.listen(3000, () => console.log(`Started on :3000`))
```

## [Oak](https://github.com/oakserver/oak)

```ts
import { Application } from 'https://deno.land/x/oak/mod.ts'
import { json, ReqWithBody } from 'https://deno.land/x/parsec/mod.ts'

const app = new Application()

app
.use((ctx, next) => json(ctx.request.serverRequest, undefined, next))
.use((ctx) => {
if (ctx.request.method === 'POST') {
ctx.response.body = (ctx.request.serverRequest as ReqWithBody).parsedBody
}
})

await app.listen({ port: 3000 })
```

Then run:

```sh
curl -X POST localhost:3000 -d '{ "abc": "def" }' -H "Content-Type: application/json"
```

[releases]: https://img.shields.io/github/v/release/deno-libs/parsec?style=flat-square
[docs-badge]: https://img.shields.io/github/v/release/deno-libs/parsec?color=yellow&label=Documentation&logo=deno&style=flat-square
[docs]: https://doc.deno.land/https/deno.land/x/parsec/mod.ts
Expand Down
13 changes: 3 additions & 10 deletions egg.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,9 @@
"entry": "./mod.ts",
"description": "Tiny body parser for Deno. Port of the milliparsec library.",
"homepage": "https://github.com/deno-libs/parsec",
"version": "0.0.6",
"files": [
"mod.ts",
"README.md"
],
"ignore": [
"examples",
".github",
"test"
],
"version": "0.1.0",
"files": ["mod.ts", "README.md"],
"ignore": ["examples", ".github", "test"],
"checkFormat": false,
"checkTests": true,
"checkInstallation": true,
Expand Down
14 changes: 0 additions & 14 deletions examples/oak.ts

This file was deleted.

11 changes: 0 additions & 11 deletions examples/opine.ts

This file was deleted.

11 changes: 0 additions & 11 deletions examples/tinyhttp.ts

This file was deleted.

14 changes: 0 additions & 14 deletions examples/vanilla.ts

This file was deleted.

12 changes: 3 additions & 9 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import * as qs from 'https://deno.land/std@0.106.0/node/querystring.ts'
import { ServerRequest } from 'https://deno.land/std@0.106.0/http/server.ts'
import { readAll } from 'https://deno.land/std@0.106.0/io/mod.ts'

type Req = Pick<ServerRequest, 'body' | 'headers'>
import * as qs from 'https://deno.land/std@0.108.0/node/querystring.ts'

/**
* Request interface extension with additional `parsedBody` property (where parsed body gets stored)
*/
export interface ReqWithBody<T = Record<string, unknown>> extends Req {
export interface ReqWithBody<T = Record<string, unknown>> extends Request {
parsedBody?: T
}

Expand All @@ -20,9 +16,7 @@ const dec = new TextDecoder()
export const bodyParser =
<T>(fn: (body: string) => T) =>
async (req: ReqWithBody<T>) => {
const buf = await readAll(req.body)

const body = dec.decode(buf)
const body = await req.text()

req.parsedBody = fn(body)

Expand Down
24 changes: 13 additions & 11 deletions mod_test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { superdeno } from 'https://deno.land/x/superdeno@4.4.0/mod.ts'
import { ServerRequest } from 'https://deno.land/std@0.106.0/http/server.ts'
import { superdeno } from 'https://deno.land/x/superdeno@4.5.0/mod.ts'
import { bodyParser, json, ReqWithBody, urlencoded } from './mod.ts'
import { describe, it, run } from 'https://deno.land/x/tincan@0.2.1/mod.ts'
import { describe, it, run } from 'https://deno.land/x/tincan@0.2.2/mod.ts'

describe('bodyParser(req)', () => {
it('should decode request body', async () => {
const request = superdeno(async (req: ServerRequest) => {
await req.respond({ body: await bodyParser((x) => x)(req) })
const request = superdeno(async (req) => {
return new Response(await bodyParser((x) => x)(req))
})

await request.post('/').send('Hello World').expect(200, 'Hello World')
Expand All @@ -15,19 +14,22 @@ describe('bodyParser(req)', () => {

describe('json(req)', () => {
it('should parse JSON body', async () => {
const request = superdeno(async (req: ServerRequest & ReqWithBody<Record<string, string>>) => {
const request = superdeno(async (req: Request & ReqWithBody<Record<string, string>>) => {
await json(req)

await req.respond({ body: req.parsedBody?.hello || 'no body', status: 200 })
return new Response(req.parsedBody?.hello || 'no body', { status: 200 })
})

await request.post('/').set('Content-Type', 'application/json').send('{ "hello": "world" }').expect(200, 'world')
})
it('should pass JSON parsing error to next', async () => {
const request = superdeno(async (req: ServerRequest & ReqWithBody<Record<string, string>>) => {
const request = superdeno(async (req: Request & ReqWithBody<Record<string, string>>) => {
let status = 200

await json(req, undefined, async () => {
await req.respond({ status: 400 })
status = 400
})
return new Response(undefined, { status })
})

await request.post('/').set('Content-Type', 'application/json').send('{ "hello": "wor').expect(400)
Expand All @@ -36,10 +38,10 @@ describe('json(req)', () => {

describe('urlencoded', () => {
it('should parse URL encoded form', async () => {
const request = superdeno(async (req: ServerRequest & ReqWithBody<Record<string, string>>) => {
const request = superdeno(async (req: Request & ReqWithBody<Record<string, string>>) => {
await urlencoded(req)

await req.respond({ body: req.parsedBody?.hello || 'no body', status: 200 })
return new Response(req.parsedBody?.hello || 'no body', { status: 200 })
})

await request
Expand Down

0 comments on commit 9ae2775

Please sign in to comment.