diff options
Diffstat (limited to 'shared/server-commands/requests')
-rw-r--r-- | shared/server-commands/requests/requests.ts | 27 |
1 files changed, 22 insertions, 5 deletions
diff --git a/shared/server-commands/requests/requests.ts b/shared/server-commands/requests/requests.ts index 85cbc9be9..dc9cf4e01 100644 --- a/shared/server-commands/requests/requests.ts +++ b/shared/server-commands/requests/requests.ts | |||
@@ -3,7 +3,7 @@ | |||
3 | import { decode } from 'querystring' | 3 | import { decode } from 'querystring' |
4 | import request from 'supertest' | 4 | import request from 'supertest' |
5 | import { URL } from 'url' | 5 | import { URL } from 'url' |
6 | import { buildAbsoluteFixturePath } from '@shared/core-utils' | 6 | import { buildAbsoluteFixturePath, pick } from '@shared/core-utils' |
7 | import { HttpStatusCode } from '@shared/models' | 7 | import { HttpStatusCode } from '@shared/models' |
8 | 8 | ||
9 | export type CommonRequestParams = { | 9 | export type CommonRequestParams = { |
@@ -21,10 +21,22 @@ export type CommonRequestParams = { | |||
21 | expectedStatus?: HttpStatusCode | 21 | expectedStatus?: HttpStatusCode |
22 | } | 22 | } |
23 | 23 | ||
24 | function makeRawRequest (url: string, expectedStatus?: HttpStatusCode, range?: string) { | 24 | function makeRawRequest (options: { |
25 | const { host, protocol, pathname } = new URL(url) | 25 | url: string |
26 | token?: string | ||
27 | expectedStatus?: HttpStatusCode | ||
28 | range?: string | ||
29 | query?: { [ id: string ]: string } | ||
30 | }) { | ||
31 | const { host, protocol, pathname } = new URL(options.url) | ||
32 | |||
33 | return makeGetRequest({ | ||
34 | url: `${protocol}//${host}`, | ||
35 | path: pathname, | ||
36 | contentType: undefined, | ||
26 | 37 | ||
27 | return makeGetRequest({ url: `${protocol}//${host}`, path: pathname, expectedStatus, range }) | 38 | ...pick(options, [ 'expectedStatus', 'range', 'token', 'query' ]) |
39 | }) | ||
28 | } | 40 | } |
29 | 41 | ||
30 | function makeGetRequest (options: CommonRequestParams & { | 42 | function makeGetRequest (options: CommonRequestParams & { |
@@ -134,7 +146,12 @@ function unwrapText (test: request.Test): Promise<string> { | |||
134 | function unwrapBodyOrDecodeToJSON <T> (test: request.Test): Promise<T> { | 146 | function unwrapBodyOrDecodeToJSON <T> (test: request.Test): Promise<T> { |
135 | return test.then(res => { | 147 | return test.then(res => { |
136 | if (res.body instanceof Buffer) { | 148 | if (res.body instanceof Buffer) { |
137 | return JSON.parse(new TextDecoder().decode(res.body)) | 149 | try { |
150 | return JSON.parse(new TextDecoder().decode(res.body)) | ||
151 | } catch (err) { | ||
152 | console.error('Cannot decode JSON.', res.body) | ||
153 | throw err | ||
154 | } | ||
138 | } | 155 | } |
139 | 156 | ||
140 | return res.body | 157 | return res.body |