UNPKG

1.3 kBJavaScriptView Raw
1'use strict'
2
3const t = require('tap')
4const test = t.test
5const { Readable } = require('stream')
6const internals = require('../../lib/contentTypeParser')[Symbol.for('internals')]
7const Request = require('../../lib/request')
8const Reply = require('../../lib/reply')
9
10test('rawBody function', t => {
11 t.plan(2)
12 const body = Buffer.from('你好 世界')
13 const parser = {
14 asString: true,
15 asBuffer: false,
16 fn (req, bodyInString, done) {
17 t.equal(bodyInString, body.toString())
18 t.is(typeof done, 'function')
19 return {
20 then (cb) {
21 cb()
22 }
23 }
24 }
25 }
26 const res = {}
27 res.end = () => {}
28 res.writeHead = () => {}
29
30 res.log = { error: () => {}, info: () => {} }
31 const context = {
32 Reply: Reply,
33 Request: Request,
34 preHandler: [],
35 onSend: [],
36 _parserOptions: {
37 limit: 1024
38 }
39 }
40 const rs = new Readable()
41 rs._read = function () {}
42 rs.headers = { 'content-length': body.length }
43 const request = new Request('params', rs, 'query', { 'content-length': body.length }, 'log')
44 const reply = new Reply(res, context, {})
45 const done = () => {}
46
47 internals.rawBody(
48 request,
49 reply,
50 reply.context._parserOptions,
51 parser,
52 done
53 )
54 rs.emit('data', body.toString())
55 rs.emit('end')
56})