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