1 | 'use strict'
|
2 |
|
3 | const t = require('tap')
|
4 | const test = t.test
|
5 | const querystring = require('querystring')
|
6 | const sget = require('simple-get').concat
|
7 | const Fastify = require('..')
|
8 |
|
9 | test('Custom querystring parser', t => {
|
10 | t.plan(9)
|
11 |
|
12 | const fastify = Fastify({
|
13 | querystringParser: function (str) {
|
14 | t.strictEqual(str, 'foo=bar&baz=faz')
|
15 | return querystring.parse(str)
|
16 | }
|
17 | })
|
18 |
|
19 | fastify.get('/', (req, reply) => {
|
20 | t.deepEqual(req.query, {
|
21 | foo: 'bar',
|
22 | baz: 'faz'
|
23 | })
|
24 | reply.send({ hello: 'world' })
|
25 | })
|
26 |
|
27 | fastify.listen(0, (err, address) => {
|
28 | t.error(err)
|
29 | t.tearDown(() => fastify.close())
|
30 |
|
31 | sget({
|
32 | method: 'GET',
|
33 | url: `${address}?foo=bar&baz=faz`
|
34 | }, (err, response, body) => {
|
35 | t.error(err)
|
36 | t.strictEqual(response.statusCode, 200)
|
37 | })
|
38 |
|
39 | fastify.inject({
|
40 | method: 'GET',
|
41 | url: `${address}?foo=bar&baz=faz`
|
42 | }, (err, response, body) => {
|
43 | t.error(err)
|
44 | t.strictEqual(response.statusCode, 200)
|
45 | })
|
46 | })
|
47 | })
|
48 |
|
49 | test('Custom querystring parser should be called also if there is nothing to parse', t => {
|
50 | t.plan(9)
|
51 |
|
52 | const fastify = Fastify({
|
53 | querystringParser: function (str) {
|
54 | t.strictEqual(str, '')
|
55 | return querystring.parse(str)
|
56 | }
|
57 | })
|
58 |
|
59 | fastify.get('/', (req, reply) => {
|
60 | t.deepEqual(req.query, {})
|
61 | reply.send({ hello: 'world' })
|
62 | })
|
63 |
|
64 | fastify.listen(0, (err, address) => {
|
65 | t.error(err)
|
66 | t.tearDown(() => fastify.close())
|
67 |
|
68 | sget({
|
69 | method: 'GET',
|
70 | url: address
|
71 | }, (err, response, body) => {
|
72 | t.error(err)
|
73 | t.strictEqual(response.statusCode, 200)
|
74 | })
|
75 |
|
76 | fastify.inject({
|
77 | method: 'GET',
|
78 | url: address
|
79 | }, (err, response, body) => {
|
80 | t.error(err)
|
81 | t.strictEqual(response.statusCode, 200)
|
82 | })
|
83 | })
|
84 | })
|
85 |
|
86 | test('Querystring without value', t => {
|
87 | t.plan(9)
|
88 |
|
89 | const fastify = Fastify({
|
90 | querystringParser: function (str) {
|
91 | t.strictEqual(str, 'foo')
|
92 | return querystring.parse(str)
|
93 | }
|
94 | })
|
95 |
|
96 | fastify.get('/', (req, reply) => {
|
97 | t.deepEqual(req.query, { foo: '' })
|
98 | reply.send({ hello: 'world' })
|
99 | })
|
100 |
|
101 | fastify.listen(0, (err, address) => {
|
102 | t.error(err)
|
103 | t.tearDown(() => fastify.close())
|
104 |
|
105 | sget({
|
106 | method: 'GET',
|
107 | url: `${address}?foo`
|
108 | }, (err, response, body) => {
|
109 | t.error(err)
|
110 | t.strictEqual(response.statusCode, 200)
|
111 | })
|
112 |
|
113 | fastify.inject({
|
114 | method: 'GET',
|
115 | url: `${address}?foo`
|
116 | }, (err, response, body) => {
|
117 | t.error(err)
|
118 | t.strictEqual(response.statusCode, 200)
|
119 | })
|
120 | })
|
121 | })
|
122 |
|
123 | test('Custom querystring parser should be a function', t => {
|
124 | t.plan(1)
|
125 |
|
126 | try {
|
127 | Fastify({
|
128 | querystringParser: 10
|
129 | })
|
130 | t.fail('Should throw')
|
131 | } catch (err) {
|
132 | t.strictEqual(
|
133 | err.message,
|
134 | "querystringParser option should be a function, instead got 'number'"
|
135 | )
|
136 | }
|
137 | })
|