aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tests/utils/requests
diff options
context:
space:
mode:
Diffstat (limited to 'server/tests/utils/requests')
-rw-r--r--server/tests/utils/requests/check-api-params.ts36
-rw-r--r--server/tests/utils/requests/requests.ts46
2 files changed, 76 insertions, 6 deletions
diff --git a/server/tests/utils/requests/check-api-params.ts b/server/tests/utils/requests/check-api-params.ts
new file mode 100644
index 000000000..fbd660629
--- /dev/null
+++ b/server/tests/utils/requests/check-api-params.ts
@@ -0,0 +1,36 @@
1import { makeGetRequest } from './requests'
2
3function checkBadStartPagination (url: string, path: string) {
4 return makeGetRequest({
5 url,
6 path,
7 query: { start: 'hello' },
8 statusCodeExpected: 400
9 })
10}
11
12function checkBadCountPagination (url: string, path: string) {
13 return makeGetRequest({
14 url,
15 path,
16 query: { count: 'hello' },
17 statusCodeExpected: 400
18 })
19}
20
21function checkBadSortPagination (url: string, path: string) {
22 return makeGetRequest({
23 url,
24 path,
25 query: { sort: 'hello' },
26 statusCodeExpected: 400
27 })
28}
29
30// ---------------------------------------------------------------------------
31
32export {
33 checkBadStartPagination,
34 checkBadCountPagination,
35 checkBadSortPagination
36}
diff --git a/server/tests/utils/requests/requests.ts b/server/tests/utils/requests/requests.ts
index 52b7a4c29..eb02cf9e6 100644
--- a/server/tests/utils/requests/requests.ts
+++ b/server/tests/utils/requests/requests.ts
@@ -1,11 +1,43 @@
1import * as request from 'supertest' 1import * as request from 'supertest'
2 2
3function makeGetRequest (url: string, path: string) { 3function makeGetRequest (options: {
4 return request(url) 4 url: string,
5 .get(path) 5 path: string,
6 query?: any,
7 token?: string,
8 statusCodeExpected?: number
9}) {
10 if (!options.statusCodeExpected) options.statusCodeExpected = 400
11
12 const req = request(options.url)
13 .get(options.path)
6 .set('Accept', 'application/json') 14 .set('Accept', 'application/json')
7 .expect(200) 15
16 if (options.token) req.set('Authorization', 'Bearer ' + options.token)
17 if (options.query) req.query(options.query)
18
19 return req
8 .expect('Content-Type', /json/) 20 .expect('Content-Type', /json/)
21 .expect(options.statusCodeExpected)
22}
23
24function makeDeleteRequest (options: {
25 url: string,
26 path: string,
27 token?: string,
28 statusCodeExpected?: number
29}) {
30 if (!options.statusCodeExpected) options.statusCodeExpected = 400
31
32 const req = request(options.url)
33 .delete(options.path)
34 .set('Accept', 'application/json')
35
36 if (options.token) req.set('Authorization', 'Bearer ' + options.token)
37
38 return req
39 .expect('Content-Type', /json/)
40 .expect(options.statusCodeExpected)
9} 41}
10 42
11function makePostUploadRequest (options: { 43function makePostUploadRequest (options: {
@@ -48,9 +80,10 @@ function makePostBodyRequest (options: {
48 url: string, 80 url: string,
49 path: string, 81 path: string,
50 token?: string, 82 token?: string,
51 fields: { [ fieldName: string ]: any }, 83 fields?: { [ fieldName: string ]: any },
52 statusCodeExpected?: number 84 statusCodeExpected?: number
53}) { 85}) {
86 if (!options.fields) options.fields = {}
54 if (!options.statusCodeExpected) options.statusCodeExpected = 400 87 if (!options.statusCodeExpected) options.statusCodeExpected = 400
55 88
56 const req = request(options.url) 89 const req = request(options.url)
@@ -88,5 +121,6 @@ export {
88 makeGetRequest, 121 makeGetRequest,
89 makePostUploadRequest, 122 makePostUploadRequest,
90 makePostBodyRequest, 123 makePostBodyRequest,
91 makePutBodyRequest 124 makePutBodyRequest,
125 makeDeleteRequest
92} 126}