-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttpclient.spec.ts
208 lines (188 loc) · 4.87 KB
/
httpclient.spec.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
import {
expect as chaiExpect,
} from 'chai';
import fetchMock, {
enableFetchMocks,
} from "jest-fetch-mock";
import {
AnyObject,
} from "../types";
import {
randomString,
} from "../utils";
import {
clientFactory,
createHttpClient,
HttpClient,
HTTPMethods,
} from './httpclient';
enableFetchMocks();
// Covering for jest-dom
if (!AbortSignal.timeout) {
AbortSignal.timeout = (ms) => {
const controller = new AbortController();
setTimeout(() => controller.abort(new DOMException("TimeoutError")), ms);
return controller.signal;
};
}
if (!AbortSignal.any) {
AbortSignal.any = (signals: AbortSignal[]) => {
return signals[0];
};
}
const testUrl = globalThis.location.href;
// Mock returns request as response
fetchMock.mockResponse(async (request: Request) => {
const headers: AnyObject = {};
for (const [key, val] of request.headers) {
headers[key] = val;
}
const requestJson = request.clone();
const body = JSON.stringify({
request: requestJson,
signal: request.signal,
signalAborted: request.signal.aborted,
});
return {
body,
headers: {
...headers,
'X-Test-Method-Response-Header': request.method.toString(),
},
status: 200,
url: request.url,
};
});
const auth = (getToken?: any) => {
return (api: any) => {
return (middleware: any) => {
return (params: any) => {
const {
action,
} = api;
if (action?.type === 'HTTP_REQUEST') {
const {
requestOptions,
} = params;
const token = getToken(requestOptions);
const init = requestOptions[1] || {};
if (init.headers) {
init.headers.set('Authorization', `Bearer: ${token}`);
} else {
init.headers = new Headers([['Authorization', `Bearer: ${token}`]]);;
}
requestOptions[1] = init;
}
return middleware(params);
};
};
};
};
const respDecorator = (api: any) => {
return (middleware: any) => {
return (params: any) => {
const {
action,
} = api;
if (action?.type === 'HTTP_RESPONSE') {
const {
result,
} = params;
return {
...result,
...{ decorated: true },
};
}
return middleware(params);
};
};
};
describe('httpclient', () => {
beforeEach(() => {
fetchMock.doMock()
})
describe('HttpClient', () => {
it('should instantiate', async () => {
chaiExpect(HttpClient({ request: testUrl })).to.not.be.null;
});
describe('signal', () => {
it('should use the default signal', async () => {
const client = HttpClient({
request: testUrl,
});
await client()
.then(resp => {
const body = JSON.parse(resp.body.toString());
chaiExpect(body.signalAborted).to.be.false;
});
});
it('should us a signal passed as parameter', async () => {
const controller = new AbortController();
controller.abort();
const client = HttpClient({
request: testUrl,
requestInitOptions: {
method: 'GET',
},
});
try {
await client()
.then(resp => {
const body = resp.body();
chaiExpect(body.signalAborted).to.be.true;
});
} catch(err: any) {
chaiExpect(err).to.throw;
}
});
});
describe('middleware', () => {
it('should process request middleware', async () => {
const token = randomString(20);
const client = HttpClient({
middleware: [{ middleware: auth(() => token) }],
requestInitOptions: {
method: 'GET',
},
request: testUrl,
});
await client()
.then(resp => {
const header = resp.headers.get('Authorization');
chaiExpect(header).to.eq('Bearer: ' + token);
});
});
it('should process response middleware', async () => {
const client = HttpClient({
middleware: [{ middleware: respDecorator }],
requestInitOptions: {
method: 'GET',
},
request: testUrl,
});
await client()
.then(resp => {
chaiExpect(resp.decorated).to.be.true;
})
});
});
describe('clientFactory', () => {
it('should instantiate', async () => {
const params = {
request: testUrl,
};
const method = HTTPMethods.GET;
const client = clientFactory(method, params);
chaiExpect(client).to.not.be.null;
});
});
describe('createHttpClient', () => {
it('should instantiate', async () => {
const params = {
request: testUrl,
};
chaiExpect(createHttpClient(params)).to.not.be.null;
});
});
});
});