-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathrequests.ts
More file actions
126 lines (113 loc) · 3.75 KB
/
requests.ts
File metadata and controls
126 lines (113 loc) · 3.75 KB
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
import { del, get, HttpError, postJSON } from '@tkrotoff/fetch';
// Yes, you can use [Jest expect](https://github.com/facebook/jest/tree/v24.9.0/packages/expect) without Jest, how cool it that!
import { expect } from 'expect';
import assert from 'node:assert';
import { getErrorMessage } from './getErrorMessage';
export async function get200OKExample() {
console.group(get200OKExample.name);
try {
const response = await get('https://jsonplaceholder.typicode.com/posts/1').json();
expect(response).toEqual({
id: 1,
title: expect.any(String),
body: expect.any(String),
userId: 1
});
} catch (e) {
// istanbul ignore next
assert(false, getErrorMessage(e));
}
console.groupEnd();
}
export async function postJSON201CreatedExample() {
console.group(postJSON201CreatedExample.name);
try {
const response = await postJSON('https://jsonplaceholder.typicode.com/posts', {
title: 'foo',
body: 'bar',
userId: 1
}).json();
expect(response).toEqual({ id: 101, title: 'foo', body: 'bar', userId: 1 });
} catch (e) {
// istanbul ignore next
assert(false, getErrorMessage(e));
}
console.groupEnd();
}
export async function del200OKExample() {
console.group(del200OKExample.name);
try {
const response = await del('https://jsonplaceholder.typicode.com/posts/1').json();
expect(response).toEqual({});
} catch (e) {
// istanbul ignore next
assert(false, getErrorMessage(e));
}
console.groupEnd();
}
// [Special handling for CORS preflight headers?](https://github.com/Readify/httpstatus/issues/25)
export async function get404NotFoundExample() {
console.group(get404NotFoundExample.name);
try {
await get('https://httpstat.us/404/cors');
// istanbul ignore next
assert(false, 'Code should not be reached');
} catch (e) {
assert(e instanceof HttpError);
expect(e).toBeInstanceOf(HttpError);
expect(e.name).toEqual('HttpError');
expect(e.message).toEqual('Not Found');
expect(e.response.status).toEqual(404);
expect(await e.response.text()).toEqual('404 Not Found');
}
console.groupEnd();
}
export async function get500InternalServerErrorExample() {
console.group(get500InternalServerErrorExample.name);
try {
await get('https://httpstat.us/500/cors');
// istanbul ignore next
assert(false, 'Code should not be reached');
} catch (e) {
assert(e instanceof HttpError);
expect(e).toBeInstanceOf(HttpError);
expect(e.name).toEqual('HttpError');
expect(e.message).toEqual('Internal Server Error');
expect(e.response.status).toEqual(500);
expect(await e.response.text()).toEqual('500 Internal Server Error');
}
console.groupEnd();
}
export async function getCorsBlockedExample() {
console.group(getCorsBlockedExample.name);
try {
await get('https://postman-echo.com/get?foo1=bar1&foo2=bar2');
// istanbul ignore next
assert(false, 'Code should not be reached');
} catch (e) {
assert(e instanceof TypeError);
expect(e).toBeInstanceOf(TypeError);
expect(e.name).toEqual('TypeError');
expect(e.message).toEqual('Failed to fetch');
}
console.groupEnd();
}
export async function abortRequestExample() {
console.group(abortRequestExample.name);
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), 500);
try {
await get('https://httpbin.org/drip?duration=2&numbytes=10&code=200&delay=2', {
signal: controller.signal
});
// istanbul ignore next
clearTimeout(timeout);
// istanbul ignore next
assert(false, 'Code should not be reached');
} catch (e) {
assert(e instanceof DOMException);
expect(e.name).toEqual('AbortError');
expect(e.message).toEqual('This operation was aborted');
}
console.groupEnd();
}