-
Notifications
You must be signed in to change notification settings - Fork 114
/
options.dataType.test.ts
170 lines (156 loc) · 5.91 KB
/
options.dataType.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import { strict as assert } from 'node:assert';
import { describe, it, beforeAll, afterAll } from 'vitest';
import urllib from '../src';
import { startServer } from './fixtures/server';
import { readableToBytes } from './utils';
describe('options.dataType.test.ts', () => {
let close: any;
let _url: string;
beforeAll(async () => {
const { closeServer, url } = await startServer();
close = closeServer;
_url = url;
});
afterAll(async () => {
await close();
});
it('should dataType is buffer', async () => {
const response = await urllib.request(_url);
assert.equal(response.status, 200);
assert.equal(response.headers['content-type'], 'application/json');
assert(response.headers.date);
assert(Buffer.isBuffer(response.data));
assert.match(response.data.toString(), /^{"method":"GET",/);
assert.equal(response.url, _url);
assert(!response.redirected);
});
it('should work with dataType = buffer', async () => {
const response = await urllib.request(_url, {
dataType: 'buffer',
});
assert.equal(response.status, 200);
assert.equal(response.headers['content-type'], 'application/json');
assert(response.headers.date);
assert(Buffer.isBuffer(response.data));
assert.match(response.data.toString(), /^{"method":"GET",/);
assert.equal(response.url, _url);
assert(!response.redirected);
});
it('should work with dataType = text', async () => {
const response = await urllib.request(_url, {
dataType: 'text',
});
assert.equal(response.status, 200);
assert.equal(response.headers['content-type'], 'application/json');
assert(response.headers.date);
assert.equal(typeof response.data, 'string');
assert.match(response.data, /^{"method":"GET",/);
assert.equal(response.url, _url);
assert(!response.redirected);
});
it('should work with dataType = html', async () => {
const response = await urllib.request(_url, {
dataType: 'html',
});
assert.equal(response.status, 200);
assert.equal(response.headers['content-type'], 'application/json');
assert(response.headers.date);
assert.equal(typeof response.data, 'string');
assert.match(response.data, /^{"method":"GET",/);
assert.equal(response.url, _url);
assert(!response.redirected);
});
it('should work with dataType = json', async () => {
const response = await urllib.request(_url, {
dataType: 'json',
});
assert.equal(response.status, 200);
assert.equal(response.headers['content-type'], 'application/json');
assert.equal(typeof response.data, 'object');
assert.equal(response.data.method, 'GET');
});
it('should ignore json parse when response empty', async () => {
const response = await urllib.request(`${_url}mock-bytes?size=0`, {
dataType: 'json',
});
assert.equal(response.status, 200);
assert.equal(response.data, null);
});
it('should keep exists accept headers when dataType = json', async () => {
const response = await urllib.request(_url, {
dataType: 'json',
headers: {
accept: 'foo/json',
},
});
assert.equal(response.status, 200);
assert.equal(response.headers['content-type'], 'application/json');
// console.log(response.data);
assert.equal(response.data.headers.accept, 'foo/json');
});
it('should throw with dataType = json when response json format invaild', async () => {
await assert.rejects(async () => {
await urllib.request(`${_url}wrongjson`, {
dataType: 'json',
});
}, (err: any) => {
// console.error(err);
assert.equal(err.name, 'JSONResponseFormatError');
if (err.message.startsWith('Expected')) {
// new message on Node.js >= 19
assert.equal(err.message, 'Expected \',\' or \'}\' after property value in JSON at position 9 (data json format: "{\\"foo\\":\\"\\"")');
} else {
assert.equal(err.message, 'Unexpected end of JSON input (data json format: "{\\"foo\\":\\"\\"")');
}
assert.equal(err.res.status, 200);
assert.equal(err.res.headers['content-type'], 'application/json');
assert.equal(err.res.size, 9);
return true;
});
});
it('should work with dataType = text when response json format invaild', async () => {
const response = await urllib.request(`${_url}wrongjson`, {
dataType: 'text',
});
assert.equal(response.status, 200);
assert.equal(response.headers['content-type'], 'application/json');
assert.equal(response.data, '{"foo":""');
});
it('should handle GET /wrongjson-gbk with dataType=json and data size > 1024', async () => {
await assert.rejects(async () => {
await urllib.request(`${_url}wrongjson-gbk`, {
dataType: 'json',
});
}, (err: any) => {
// console.error(err);
assert.equal(err.name, 'JSONResponseFormatError');
assert.match(err.message, /\" \.\.\.skip\.\.\. \"/);
assert.equal(err.res.status, 200);
assert.equal(err.res.headers['content-type'], 'application/json');
return true;
});
});
it('should work with dataType = stream', async () => {
const response = await urllib.request(_url, {
dataType: 'stream',
});
assert.equal(response.status, 200);
assert.equal(response.headers['content-type'], 'application/json');
assert(response.res);
const bytes = await readableToBytes(response.res);
const jsonString = bytes.toString();
assert.equal(JSON.parse(jsonString).method, 'GET');
});
it('should work with streaming = true, alias to data = stream', async () => {
const response = await urllib.request(_url, {
streaming: true,
});
assert.equal(response.status, 200);
assert.equal(response.headers['content-type'], 'application/json');
assert(response.res);
assert(response.res);
const bytes = await readableToBytes(response.res);
const jsonString = bytes.toString();
assert.equal(JSON.parse(jsonString).method, 'GET');
});
});