|
1 | 1 | import { strict as assert } from 'node:assert'; |
2 | 2 | import { createReadStream } from 'node:fs'; |
3 | 3 | import { Readable } from 'node:stream'; |
| 4 | +import qs from 'qs'; |
4 | 5 | import { describe, it, beforeAll, afterAll } from 'vitest'; |
5 | 6 | import urllib from '../src'; |
6 | 7 | import { startServer } from './fixtures/server'; |
@@ -42,6 +43,82 @@ describe('options.data.test.ts', () => { |
42 | 43 | assert.equal(url.searchParams.get('data'), '哈哈'); |
43 | 44 | }); |
44 | 45 |
|
| 46 | + it('should GET with data work on nestedQuerystring=true', async () => { |
| 47 | + const response = await urllib.request(_url, { |
| 48 | + method: 'GET', |
| 49 | + data: { |
| 50 | + sql: 'SELECT * from table', |
| 51 | + data: '哈哈', |
| 52 | + foo: { |
| 53 | + bar: 'bar value', |
| 54 | + array: [ 1, 2, 3 ], |
| 55 | + }, |
| 56 | + }, |
| 57 | + nestedQuerystring: true, |
| 58 | + dataType: 'json', |
| 59 | + headers: { |
| 60 | + 'x-qs': 'true', |
| 61 | + }, |
| 62 | + }); |
| 63 | + assert.equal(response.status, 200); |
| 64 | + assert.equal(response.headers['content-type'], 'application/json'); |
| 65 | + assert.equal(response.data.method, 'GET'); |
| 66 | + assert(response.url.startsWith(_url)); |
| 67 | + // console.log(response); |
| 68 | + assert(!response.redirected); |
| 69 | + assert.equal(response.data.url, '/?sql=SELECT%20%2A%20from%20table&data=%E5%93%88%E5%93%88&foo%5Bbar%5D=bar%20value&foo%5Barray%5D%5B0%5D=1&foo%5Barray%5D%5B1%5D=2&foo%5Barray%5D%5B2%5D=3'); |
| 70 | + const query = qs.parse(response.data.url.substring(2)); |
| 71 | + const url = new URL(response.data.href); |
| 72 | + assert.equal(url.searchParams.get('sql'), 'SELECT * from table'); |
| 73 | + assert.equal(url.searchParams.get('data'), '哈哈'); |
| 74 | + assert.equal(url.searchParams.get('foo[bar]'), 'bar value'); |
| 75 | + assert.equal(url.searchParams.get('foo[array][0]'), '1'); |
| 76 | + assert.equal(url.searchParams.get('foo[array][1]'), '2'); |
| 77 | + assert.equal(url.searchParams.get('foo[array][2]'), '3'); |
| 78 | + assert.equal(query.sql, 'SELECT * from table'); |
| 79 | + assert.equal(query.data, '哈哈'); |
| 80 | + assert.deepEqual(query.foo, { bar: 'bar value', array: [ '1', '2', '3' ] }); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should GET /ok?hello=1 with data work on nestedQuerystring=true', async () => { |
| 84 | + const response = await urllib.request(`${_url}ok?hello=1`, { |
| 85 | + method: 'GET', |
| 86 | + data: { |
| 87 | + sql: 'SELECT * from table', |
| 88 | + data: '哈哈', |
| 89 | + foo: { |
| 90 | + bar: 'bar value', |
| 91 | + array: [ 1, 2, 3 ], |
| 92 | + }, |
| 93 | + }, |
| 94 | + nestedQuerystring: true, |
| 95 | + dataType: 'json', |
| 96 | + headers: { |
| 97 | + 'x-qs': 'true', |
| 98 | + }, |
| 99 | + }); |
| 100 | + assert.equal(response.status, 200); |
| 101 | + assert.equal(response.headers['content-type'], 'application/json'); |
| 102 | + assert.equal(response.data.method, 'GET'); |
| 103 | + assert(response.url.startsWith(_url)); |
| 104 | + // console.log(response); |
| 105 | + assert(!response.redirected); |
| 106 | + assert.equal(response.data.url, '/ok?hello=1&sql=SELECT%20%2A%20from%20table&data=%E5%93%88%E5%93%88&foo%5Bbar%5D=bar%20value&foo%5Barray%5D%5B0%5D=1&foo%5Barray%5D%5B1%5D=2&foo%5Barray%5D%5B2%5D=3'); |
| 107 | + const query = qs.parse(response.data.url.substring(4)); |
| 108 | + const url = new URL(response.data.href); |
| 109 | + assert.equal(url.searchParams.get('hello'), '1'); |
| 110 | + assert.equal(url.searchParams.get('sql'), 'SELECT * from table'); |
| 111 | + assert.equal(url.searchParams.get('data'), '哈哈'); |
| 112 | + assert.equal(url.searchParams.get('foo[bar]'), 'bar value'); |
| 113 | + assert.equal(url.searchParams.get('foo[array][0]'), '1'); |
| 114 | + assert.equal(url.searchParams.get('foo[array][1]'), '2'); |
| 115 | + assert.equal(url.searchParams.get('foo[array][2]'), '3'); |
| 116 | + assert.equal(query.hello, '1'); |
| 117 | + assert.equal(query.sql, 'SELECT * from table'); |
| 118 | + assert.equal(query.data, '哈哈'); |
| 119 | + assert.deepEqual(query.foo, { bar: 'bar value', array: [ '1', '2', '3' ] }); |
| 120 | + }); |
| 121 | + |
45 | 122 | it('should HEAD with data and auto convert to query string', async () => { |
46 | 123 | const response = await urllib.request(_url, { |
47 | 124 | method: 'HEAD', |
@@ -182,6 +259,7 @@ describe('options.data.test.ts', () => { |
182 | 259 | assert.equal(response.data.headers['content-type'], 'application/x-www-form-urlencoded;charset=UTF-8'); |
183 | 260 | assert.equal(response.data.requestBody.sql, 'SELECT * from table'); |
184 | 261 | assert.equal(response.data.requestBody.data, '哈哈 PUT'); |
| 262 | + assert.equal(response.data.requestBody.__raw__, 'sql=SELECT+*+from+table&data=%E5%93%88%E5%93%88+PUT'); |
185 | 263 | }); |
186 | 264 |
|
187 | 265 | it('should PATCH with data and auto using application/x-www-form-urlencoded', async () => { |
@@ -226,6 +304,40 @@ describe('options.data.test.ts', () => { |
226 | 304 | assert.equal(response.data.requestBody.sql, 'SELECT * from table'); |
227 | 305 | assert.equal(response.data.requestBody.data, '哈哈 POST'); |
228 | 306 | assert.equal(response.data.requestBody.foo, '[object Object]'); |
| 307 | + assert.equal(response.data.requestBody.__raw__, 'sql=SELECT+*+from+table&data=%E5%93%88%E5%93%88+POST&foo=%5Bobject+Object%5D'); |
| 308 | + }); |
| 309 | + |
| 310 | + it('should POST with application/x-www-form-urlencoded work on nestedQuerystring=true', async () => { |
| 311 | + const response = await urllib.request(_url, { |
| 312 | + method: 'POST', |
| 313 | + data: { |
| 314 | + sql: 'SELECT * from table', |
| 315 | + data: '哈哈', |
| 316 | + foo: { |
| 317 | + bar: 'bar value', |
| 318 | + array: [ 1, 2, 3 ], |
| 319 | + }, |
| 320 | + }, |
| 321 | + nestedQuerystring: true, |
| 322 | + dataType: 'json', |
| 323 | + headers: { |
| 324 | + 'x-qs': 'true', |
| 325 | + }, |
| 326 | + }); |
| 327 | + assert.equal(response.status, 200); |
| 328 | + assert.equal(response.headers['content-type'], 'application/json'); |
| 329 | + assert.equal(response.data.method, 'POST'); |
| 330 | + assert(response.url.startsWith(_url)); |
| 331 | + assert(!response.redirected); |
| 332 | + // console.log(response.data); |
| 333 | + assert.equal(response.data.url, '/'); |
| 334 | + assert.equal(response.data.headers['content-type'], 'application/x-www-form-urlencoded;charset=UTF-8'); |
| 335 | + assert.equal(response.data.requestBody.sql, 'SELECT * from table'); |
| 336 | + assert.equal(response.data.requestBody.data, '哈哈'); |
| 337 | + assert(response.data.requestBody.foo, 'missing requestBody.foo'); |
| 338 | + assert.equal(response.data.requestBody.foo.bar, 'bar value'); |
| 339 | + assert.deepEqual(response.data.requestBody.foo.array, [ '1', '2', '3' ]); |
| 340 | + assert.equal(response.data.requestBody.__raw__, 'sql=SELECT%20%2A%20from%20table&data=%E5%93%88%E5%93%88&foo%5Bbar%5D=bar%20value&foo%5Barray%5D%5B0%5D=1&foo%5Barray%5D%5B1%5D=2&foo%5Barray%5D%5B2%5D=3'); |
229 | 341 | }); |
230 | 342 |
|
231 | 343 | it('should PUT with data and contentType = json', async () => { |
|
0 commit comments