-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.ts
323 lines (238 loc) · 9.53 KB
/
tests.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
import {assert, assertEquals, assertGreater, assertGreaterOrEqual, assertLess, assertLessOrEqual} from 'https://deno.land/std@0.208.0/assert/mod.ts';
import Decurl, {globalInit, globalCleanup, globalSslset} from './decurl.ts';
import {Code, HttpVersion, Sslbackend, Sslset} from './types.ts';
import {isStatus} from 'https://deno.land/std@0.208.0/http/status.ts';
/** This must be the first test. */
Deno.test('Init - SSL backend', () => {
assertEquals(globalSslset(Sslbackend.Bearssl), Sslset.UnknownBackend);
assertEquals(globalSslset(Sslbackend.Openssl), Sslset.Ok);
assertEquals(globalInit(), 0);
globalCleanup();
if (Deno.build.os == 'windows') {
assertEquals(globalSslset(Sslbackend.Schannel), Sslset.Ok);
assertEquals(globalSslset(Sslbackend.Bearssl), Sslset.TooLate);
assertEquals(globalInit(), 0);
globalCleanup();
}
})
Deno.test('Readme', () => {
globalInit()
using decurl = new Decurl()
decurl.setSslVerifypeer(0)
decurl.setUrl('https://example.com')
const curlCode = decurl.perform()
const responseCode = decurl.getResponseCode()
const response = decurl.getWriteFunctionData()
if (response) {
console.log(new TextDecoder().decode(response))
}
console.log(responseCode)
console.log(curlCode)
globalCleanup()
})
Deno.test('GET - Big text', async () => {
globalInit();
using d = new Decurl();
if (Deno.build.os == 'windows')
assertEquals(d.setSslVerifypeer(0), Code.Ok);
assertEquals(d.setUrl('https://html.spec.whatwg.org/'), Code.Ok);
assertEquals(d.perform(), Code.Ok);
consistResponse(d);
const dRes = d.getWriteFunctionData();
assert(dRes);
const fetchRes = await fetch('https://html.spec.whatwg.org/').then(r => r.text());
assertEquals(new TextDecoder().decode(dRes), fetchRes);
globalCleanup();
})
Deno.test('GET - Multiple handles', async () => {
globalInit();
using d1 = new Decurl();
using d2 = new Decurl();
if (Deno.build.os == 'windows')
assertEquals(d1.setSslVerifypeer(0), Code.Ok);
if (Deno.build.os == 'windows')
assertEquals(d2.setSslVerifypeer(0), Code.Ok);
assertEquals(d1.setUrl('https://example.com'), Code.Ok);
assertEquals(d2.setUrl('https://example.com'), Code.Ok);
assertEquals(d1.perform(), Code.Ok);
assertEquals(d2.perform(), Code.Ok);
consistResponse(d1);
consistResponse(d2);
const d1Res = d1.getWriteFunctionData();
const d2Res = d2.getWriteFunctionData();
assert(d1Res);
assert(d2Res);
const fetchRes = await fetch('https://example.com').then(r => r.text());
const txtDec = new TextDecoder();
assertEquals(txtDec.decode(d1Res), txtDec.decode(d2Res));
assertEquals(txtDec.decode(d2Res), fetchRes);
globalCleanup();
})
Deno.test('GET - Not found', async () => {
globalInit();
using d = new Decurl();
if (Deno.build.os == 'windows')
assertEquals(d.setSslVerifypeer(0), Code.Ok);
assertEquals(d.setUrl('https://example.com/not_found'), Code.Ok);
assertEquals(d.perform(), Code.Ok);
consistResponse(d);
assertEquals(d.getResponseCode(), 404);
assertEquals(d.getEffectiveUrl(), 'https://example.com/not_found');
const dRes = d.getWriteFunctionData();
assert(dRes);
const fetchRes = await fetch('https://example.com/not_found').then(r => r.text());
assertEquals(new TextDecoder().decode(dRes), fetchRes);
globalCleanup();
})
Deno.test('GET - PDF', async () => {
globalInit();
using d = new Decurl();
if (Deno.build.os == 'windows')
assertEquals(d.setSslVerifypeer(0), Code.Ok);
assertEquals(d.setUrl('https://html.spec.whatwg.org/print.pdf'), Code.Ok);
assertEquals(d.perform(), Code.Ok);
consistResponse(d);
const dRes = d.getWriteFunctionData();
assert(dRes);
const fetchRes = await fetch('https://html.spec.whatwg.org/print.pdf').then(r => r.arrayBuffer());
const shaDecurl = await crypto.subtle.digest('SHA-256', dRes);
const shaFetch = await crypto.subtle.digest('SHA-256', fetchRes);
assertEquals(new Uint8Array(shaDecurl), new Uint8Array(shaFetch)); // asserting ArrayBuffers directly doesn't work
globalCleanup();
})
Deno.test('GET - Small text', async () => {
globalInit();
using d = new Decurl();
if (Deno.build.os == 'windows')
assertEquals(d.setSslVerifypeer(0), Code.Ok);
assertEquals(d.setUrl('https://example.com'), Code.Ok);
assertEquals(d.perform(), Code.Ok);
consistResponse(d);
assertEquals(d.getResponseCode(), 200);
assertEquals(d.getEffectiveUrl(), 'https://example.com/');
const dRes = d.getWriteFunctionData();
assert(dRes);
const fetchRes = await fetch('https://example.com').then(r => r.text());
assertEquals(new TextDecoder().decode(dRes), fetchRes);
globalCleanup();
})
Deno.test('GET - Headers', async () => {
globalInit();
using d = new Decurl();
if (Deno.build.os == 'windows')
assertEquals(d.setSslVerifypeer(0), Code.Ok);
assertEquals(d.setUrl('https://example.com'), Code.Ok);
assertEquals(d.perform(), Code.Ok);
consistResponse(d);
assertEquals(d.getResponseCode(), 200);
const headers = d.getHeaderFunctionData();
assert(headers);
assert(headers.get('content-length'));
assert(headers.get('age'));
assert(headers.get('date'));
assert(headers.get('x-cache'));
const fetchRes = await fetch('https://example.com');
await fetchRes.arrayBuffer();
assertEquals(headers.get('content-type'), fetchRes.headers.get('content-type'));
globalCleanup();
})
Deno.test('Custom headers', async () => {
globalInit();
using d = new Decurl();
if (Deno.build.os == 'windows')
assertEquals(d.setSslVerifypeer(0), Code.Ok);
d.setHttpheader(new Headers({'custom-header': 'custom value'}));
assertEquals(d.setUrl('https://example.com'), Code.Ok);
assertEquals(d.perform(), Code.Ok);
consistResponse(d);
assertEquals(d.getResponseCode(), 200);
const fetchRes = await fetch('https://example.com');
await fetchRes.arrayBuffer();
globalCleanup();
})
Deno.test('Cookie engine', () => {
globalInit();
using d = new Decurl();
if (Deno.build.os == 'windows')
assertEquals(d.setSslVerifypeer(0), Code.Ok);
assertEquals(d.setFollowlocation(1), Code.Ok);
d.setCookiefile('');
assertEquals(d.setUrl('https://www.google.com/'), Code.Ok);
assertEquals(d.perform(), Code.Ok);
assertEquals(d.getResponseCode(), 200);
assertGreater(d.getCookielist()?.length, 1);
consistResponse(d);
assertEquals(d.perform(), Code.Ok);
assertEquals(d.getResponseCode(), 200);
assertGreater(d.getHeaderFunctionData()?.getSetCookie().length, 1);
assertGreater(d.getCookielist()?.length, 1);
consistResponse(d);
globalCleanup();
})
Deno.test('Error buffer', () => {
globalInit();
using d = new Decurl();
if (Deno.build.os == 'windows')
assertEquals(d.setSslVerifypeer(0), Code.Ok);
const errBuf = new ArrayBuffer(2048);
d.setErrorbuffer(errBuf);
d.perform();
assert(new TextDecoder().decode(errBuf).startsWith('No URL'));
globalCleanup();
})
function consistResponse(d: Decurl) {
assert(d.getHttpVersion() in HttpVersion);
assert(d.getEffectiveMethod() == 'GET' || d.getEffectiveMethod() == 'POST');
assert(d.getScheme()?.includes('HTTP'));
assert(isStatus(d.getResponseCode()));
assert(d.getContentType());
assertGreater(d.getLocalIp()!.length, 3);
assertGreaterOrEqual(d.getLocalPort(), 8000);
assertGreater(d.getEffectiveUrl()!.length, 7);
assertGreater(d.getPrimaryIp()!.length, 7);
assert([80, 443].includes(d.getPrimaryPort()));
const headers = d.getHeaderFunctionData();
assert(headers);
assert(headers.get('content-type'));
assert(headers.get('date'));
/** @todo console.log(d.getCainfo()); */
assertGreaterOrEqual(d.getFiletimeT(), 0);
assertGreaterOrEqual(d.getFiletime(), 0);
assertEquals(d.getFiletimeT(), d.getFiletime()); // what's the difference?
assertGreater(d.getSizeDownloadT(), 10);
assertGreaterOrEqual(d.getSizeUploadT(), 0);
assertGreaterOrEqual(d.getSpeedDownloadT(), 50);
assertLess(d.getSpeedDownloadT(), 10000000);
assertGreaterOrEqual(d.getSpeedUploadT(), 0);
assertGreaterOrEqual(d.getRequestSize(), 10);
assertGreaterOrEqual(d.getHeaderSize(), 0);
assertLessOrEqual(d.getHeaderSize(), 2000);
assertGreaterOrEqual(d.getContentLengthDownloadT(), 0);
assertGreaterOrEqual(d.getContentLengthUploadT(), 0);
assertGreaterOrEqual(d.getTotalTimeT(), 0n);
assertGreaterOrEqual(d.getStarttransferTimeT(), 0n);
assertGreaterOrEqual(d.getPretransferTimeT(), 0n);
assertGreaterOrEqual(d.getAppconnectTimeT(), 0n);
assertGreaterOrEqual(d.getConnectTimeT(), 0n);
assertGreaterOrEqual(d.getNamelookupTimeT(), 0n);
assertGreaterOrEqual(d.getRedirectTimeT(), 0n);
assertGreaterOrEqual(d.getTotalTime(), 0);
assertGreaterOrEqual(d.getStarttransferTime(), 0);
assertGreaterOrEqual(d.getPretransferTime(), 0);
assertGreaterOrEqual(d.getAppconnectTime(), 0);
assertGreaterOrEqual(d.getConnectTime(), 0);
assertGreaterOrEqual(d.getNamelookupTime(), 0);
assertGreaterOrEqual(d.getRedirectTime(), 0);
assertEquals(BigInt(Math.round(d.getTotalTime() * 1000000)), d.getTotalTimeT());
assertEquals(BigInt(Math.round(d.getPretransferTime() * 1000000)), d.getPretransferTimeT());
assertEquals(BigInt(Math.round(d.getAppconnectTime() * 1000000)), d.getAppconnectTimeT());
assertEquals(BigInt(Math.round(d.getConnectTime() * 1000000)), d.getConnectTimeT());
assertEquals(BigInt(Math.round(d.getNamelookupTime() * 1000000)), d.getNamelookupTimeT());
assertEquals(BigInt(Math.round(d.getRedirectTime() * 1000000)), d.getRedirectTimeT());
assertGreaterOrEqual(d.getPretransferTimeT(), d.getAppconnectTimeT());
assertGreaterOrEqual(d.getAppconnectTimeT(), d.getConnectTimeT());
assertGreaterOrEqual(d.getConnectTimeT(), d.getNamelookupTimeT());
assertGreaterOrEqual(d.getPretransferTime(), d.getAppconnectTime());
assertGreaterOrEqual(d.getAppconnectTime(), d.getConnectTime());
assertGreaterOrEqual(d.getConnectTime(), d.getNamelookupTime());
}