-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTest.ts
95 lines (87 loc) · 2.6 KB
/
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
import { Fetch } from "./Fetch";
import * as Fs from "fs";
(async () => {
// echo test
try {
const
testText = "hello",
t = new Date().getTime(),
json = await new Fetch("https://postman-echo.com/post").head("X-Time", t).fetch(testText, "text/plain"),
obj = JSON.parse(json);
if (!obj)
console.log("empty response");
else {
if (obj.data !== testText)
console.error("invalid data");
else {
if (!obj.headers || obj.headers["x-time"] != t)
console.error("invalid header");
else
console.info("all ok");
}
}
}
catch (ex) {
console.error(`exception: ${ex.message || ex}`);
}
// error test
try {
await new Fetch("https://postman-echo.com/status/500").fetch();
}
catch (ex) {
if (ex.statusCode !== 500)
console.error("error test failed");
else
console.info("error test ok");
}
// error map
try {
await new Fetch("https://postman-echo.com/status/500")
.exMap((ex, data) => {
if (data && data === "{\"status\":500}")
return new Error("mapping ok");
return ex;
})
.fetch();
}
catch (ex) {
if (ex.message === "mapping ok")
console.info("error map test ok");
else
console.error("error map test failed");
}
// basic auth test
try {
const
json = await new Fetch("https://postman-echo.com/basic-auth").basicAuth("postman", "password").fetch(),
obj = JSON.parse(json);
if (obj && obj.authenticated === true)
console.info("basic auth ok");
else
console.error("basic auth failed");
}
catch (ex) {
console.error(`exception: ${ex.message || ex}`);
}
// stream test
try {
const stream = Fs.createWriteStream("out.txt");
await new Fetch("https://postman-echo.com/stream/5").pipe(stream);
console.info("stream ok");
}
catch (ex) {
console.error(`exception: ${ex.message || ex}`);
}
// timeout
try {
await new Fetch("https://postman-echo.com/delay/10").timeout(1000).fetch();
console.error("timeout failed");
}
catch (ex) {
if (ex.message?.includes("timeout"))
console.info("timeout ok");
else
console.error("got timeout, but invalid ex");
}
// temp. redirect test (307)
})();