aboutsummaryrefslogtreecommitdiffhomepage
path: root/shared/extra-utils/requests
diff options
context:
space:
mode:
Diffstat (limited to 'shared/extra-utils/requests')
-rw-r--r--shared/extra-utils/requests/requests.ts25
1 files changed, 23 insertions, 2 deletions
diff --git a/shared/extra-utils/requests/requests.ts b/shared/extra-utils/requests/requests.ts
index c5ee63e05..3f1ac6650 100644
--- a/shared/extra-utils/requests/requests.ts
+++ b/shared/extra-utils/requests/requests.ts
@@ -67,11 +67,17 @@ function makeUploadRequest (options: {
67 method?: 'POST' | 'PUT' 67 method?: 'POST' | 'PUT'
68 path: string 68 path: string
69 token?: string 69 token?: string
70
70 fields: { [ fieldName: string ]: any } 71 fields: { [ fieldName: string ]: any }
71 attaches?: { [ attachName: string ]: any | any[] } 72 attaches?: { [ attachName: string ]: any | any[] }
73
74 headers?: { [ name: string ]: string }
75
72 statusCodeExpected?: HttpStatusCode 76 statusCodeExpected?: HttpStatusCode
73}) { 77}) {
74 if (!options.statusCodeExpected) options.statusCodeExpected = HttpStatusCode.BAD_REQUEST_400 78 if (options.statusCodeExpected === undefined) {
79 options.statusCodeExpected = HttpStatusCode.BAD_REQUEST_400
80 }
75 81
76 let req: request.Test 82 let req: request.Test
77 if (options.method === 'PUT') { 83 if (options.method === 'PUT') {
@@ -84,6 +90,10 @@ function makeUploadRequest (options: {
84 90
85 if (options.token) req.set('Authorization', 'Bearer ' + options.token) 91 if (options.token) req.set('Authorization', 'Bearer ' + options.token)
86 92
93 Object.keys(options.headers || {}).forEach(name => {
94 req.set(name, options.headers[name])
95 })
96
87 Object.keys(options.fields).forEach(field => { 97 Object.keys(options.fields).forEach(field => {
88 const value = options.fields[field] 98 const value = options.fields[field]
89 99
@@ -107,7 +117,11 @@ function makeUploadRequest (options: {
107 } 117 }
108 }) 118 })
109 119
110 return req.expect(options.statusCodeExpected) 120 if (options.statusCodeExpected) {
121 req.expect(options.statusCodeExpected)
122 }
123
124 return req
111} 125}
112 126
113function makePostBodyRequest (options: { 127function makePostBodyRequest (options: {
@@ -115,7 +129,9 @@ function makePostBodyRequest (options: {
115 path: string 129 path: string
116 token?: string 130 token?: string
117 fields?: { [ fieldName: string ]: any } 131 fields?: { [ fieldName: string ]: any }
132 headers?: { [ name: string ]: string }
118 type?: string 133 type?: string
134 xForwardedFor?: string
119 statusCodeExpected?: HttpStatusCode 135 statusCodeExpected?: HttpStatusCode
120}) { 136}) {
121 if (!options.fields) options.fields = {} 137 if (!options.fields) options.fields = {}
@@ -126,8 +142,13 @@ function makePostBodyRequest (options: {
126 .set('Accept', 'application/json') 142 .set('Accept', 'application/json')
127 143
128 if (options.token) req.set('Authorization', 'Bearer ' + options.token) 144 if (options.token) req.set('Authorization', 'Bearer ' + options.token)
145 if (options.xForwardedFor) req.set('X-Forwarded-For', options.xForwardedFor)
129 if (options.type) req.type(options.type) 146 if (options.type) req.type(options.type)
130 147
148 Object.keys(options.headers || {}).forEach(name => {
149 req.set(name, options.headers[name])
150 })
151
131 return req.send(options.fields) 152 return req.send(options.fields)
132 .expect(options.statusCodeExpected) 153 .expect(options.statusCodeExpected)
133} 154}