From 9ae2775f8ae479af17b28e1cab6ef46ebf85af10 Mon Sep 17 00:00:00 2001 From: v1rtl Date: Wed, 22 Sep 2021 13:45:40 +0300 Subject: [PATCH] update tests --- README.md | 81 -------------------------------------------- egg.json | 13 ++----- examples/oak.ts | 14 -------- examples/opine.ts | 11 ------ examples/tinyhttp.ts | 11 ------ examples/vanilla.ts | 14 -------- mod.ts | 12 ++----- mod_test.ts | 24 +++++++------ 8 files changed, 19 insertions(+), 161 deletions(-) delete mode 100644 examples/oak.ts delete mode 100644 examples/opine.ts delete mode 100644 examples/tinyhttp.ts delete mode 100644 examples/vanilla.ts diff --git a/README.md b/README.md index d52b0f8..e601127 100644 --- a/README.md +++ b/README.md @@ -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() - -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 diff --git a/egg.json b/egg.json index 0b5eb01..9516450 100644 --- a/egg.json +++ b/egg.json @@ -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, diff --git a/examples/oak.ts b/examples/oak.ts deleted file mode 100644 index cc51095..0000000 --- a/examples/oak.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { Application } from 'https://deno.land/x/oak/mod.ts' -import { json, ReqWithBody } from '../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 }) diff --git a/examples/opine.ts b/examples/opine.ts deleted file mode 100644 index 7019b44..0000000 --- a/examples/opine.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { opine } from 'https://deno.land/x/opine/mod.ts' -import { json } from '../mod.ts' - -const app = opine() - -app - .use(json) - .post('/', (req, res) => { - res.send(req.parsedBody || {}) - }) - .listen(3000, () => console.log(`Started on :3000`)) diff --git a/examples/tinyhttp.ts b/examples/tinyhttp.ts deleted file mode 100644 index 5e1ac87..0000000 --- a/examples/tinyhttp.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { App, Request } from 'https://deno.land/x/tinyhttp/mod.ts' -import { json, ReqWithBody } from '../mod.ts' - -const app = new App() - -app - .use(json) - .post((req, res) => { - res.send(req.parsedBody || {}) - }) - .listen(3000, () => console.log(`Started on :3000`)) diff --git a/examples/vanilla.ts b/examples/vanilla.ts deleted file mode 100644 index dc9dc00..0000000 --- a/examples/vanilla.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { serve } from 'https://deno.land/std@0.106.0/http/server.ts' -import { json, ReqWithBody } from '../mod.ts' - -const s = serve({ port: 3000 }) - -for await (const req of s) { - await json(req, undefined) - if (!(req as ReqWithBody).parsedBody) { - req.respond({ status: 404, body: 'No body found' }) - } else { - const response = JSON.stringify((req as ReqWithBody).parsedBody, null, 2) - req.respond({ body: response }) - } -} diff --git a/mod.ts b/mod.ts index 825aaa2..eed9617 100644 --- a/mod.ts +++ b/mod.ts @@ -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 +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> extends Req { +export interface ReqWithBody> extends Request { parsedBody?: T } @@ -20,9 +16,7 @@ const dec = new TextDecoder() export const bodyParser = (fn: (body: string) => T) => async (req: ReqWithBody) => { - const buf = await readAll(req.body) - - const body = dec.decode(buf) + const body = await req.text() req.parsedBody = fn(body) diff --git a/mod_test.ts b/mod_test.ts index 1ee5203..dc14121 100644 --- a/mod_test.ts +++ b/mod_test.ts @@ -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') @@ -15,19 +14,22 @@ describe('bodyParser(req)', () => { describe('json(req)', () => { it('should parse JSON body', async () => { - const request = superdeno(async (req: ServerRequest & ReqWithBody>) => { + const request = superdeno(async (req: Request & ReqWithBody>) => { 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>) => { + const request = superdeno(async (req: Request & ReqWithBody>) => { + 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) @@ -36,10 +38,10 @@ describe('json(req)', () => { describe('urlencoded', () => { it('should parse URL encoded form', async () => { - const request = superdeno(async (req: ServerRequest & ReqWithBody>) => { + const request = superdeno(async (req: Request & ReqWithBody>) => { 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