|
1 | | -import http from 'http'; |
| 1 | +import type http from 'http'; |
2 | 2 |
|
3 | | -import type { EndRequest, Headers } from '@algolia/client-common'; |
| 3 | +import type { EndRequest } from '@algolia/client-common'; |
4 | 4 | import type { MockRequest, MockResponse } from 'xhr-mock'; |
5 | 5 | import mock from 'xhr-mock'; |
6 | 6 |
|
7 | 7 | import { createXhrRequester } from '../..'; |
| 8 | +import { |
| 9 | + BASE_URL, |
| 10 | + headers, |
| 11 | + timeoutRequest, |
| 12 | + requestStub, |
| 13 | + getStringifiedBody, |
| 14 | + createTestServer, |
| 15 | +} from '../../../../tests/utils'; |
8 | 16 |
|
9 | 17 | const requester = createXhrRequester(); |
10 | | -const BASE_URL = 'https://algolia-dns.net/foo?x-algolia-header=bar'; |
11 | | - |
12 | | -function getStringifiedBody( |
13 | | - body: Record<string, any> = { foo: 'bar' } |
14 | | -): string { |
15 | | - return JSON.stringify(body); |
16 | | -} |
17 | | - |
18 | | -const headers: Headers = { |
19 | | - 'content-type': 'text/plain', |
20 | | -}; |
21 | | - |
22 | | -const timeoutRequest: EndRequest = { |
23 | | - url: 'missing-url-here', |
24 | | - data: '', |
25 | | - headers: {}, |
26 | | - method: 'GET', |
27 | | - responseTimeout: 2000, |
28 | | - connectTimeout: 1000, |
29 | | -}; |
30 | | - |
31 | | -const requestStub: EndRequest = { |
32 | | - url: BASE_URL, |
33 | | - method: 'POST', |
34 | | - headers, |
35 | | - data: getStringifiedBody(), |
36 | | - responseTimeout: 1000, |
37 | | - connectTimeout: 2000, |
38 | | -}; |
39 | 18 |
|
40 | 19 | describe('status code handling', () => { |
41 | 20 | beforeEach(() => mock.setup()); |
@@ -123,24 +102,7 @@ describe('timeout handling', () => { |
123 | 102 | let server: http.Server; |
124 | 103 | // setup http server to test timeout |
125 | 104 | beforeAll(() => { |
126 | | - server = http.createServer(function (_req, res) { |
127 | | - res.writeHead(200, { |
128 | | - 'content-type': 'text/plain', |
129 | | - 'access-control-allow-origin': '*', |
130 | | - 'x-powered-by': 'nodejs', |
131 | | - }); |
132 | | - |
133 | | - res.write('{"foo":'); |
134 | | - |
135 | | - setTimeout(() => { |
136 | | - res.write(' "bar"'); |
137 | | - }, 1000); |
138 | | - |
139 | | - setTimeout(() => { |
140 | | - res.write('}'); |
141 | | - res.end(); |
142 | | - }, 5000); |
143 | | - }); |
| 105 | + server = createTestServer(); |
144 | 106 |
|
145 | 107 | server.listen('1111'); |
146 | 108 | }); |
|
0 commit comments