aboutsummaryrefslogtreecommitdiffhomepage
path: root/shared/extra-utils/requests
diff options
context:
space:
mode:
authorRigel Kent <sendmemail@rigelk.eu>2020-12-07 14:32:36 +0100
committerGitHub <noreply@github.com>2020-12-07 14:32:36 +0100
commit2d53be0267acc49cda46707b885096193a1f4e9c (patch)
tree887061a34bc67f40acbb96a6278f9544bf83caeb /shared/extra-utils/requests
parentadc1f09c0dbd997f34028c1c82d1c118dc8ead80 (diff)
downloadPeerTube-2d53be0267acc49cda46707b885096193a1f4e9c.tar.gz
PeerTube-2d53be0267acc49cda46707b885096193a1f4e9c.tar.zst
PeerTube-2d53be0267acc49cda46707b885096193a1f4e9c.zip
replace numbers with typed http status codes (#3409)
Diffstat (limited to 'shared/extra-utils/requests')
-rw-r--r--shared/extra-utils/requests/check-api-params.ts9
-rw-r--r--shared/extra-utils/requests/requests.ts27
2 files changed, 19 insertions, 17 deletions
diff --git a/shared/extra-utils/requests/check-api-params.ts b/shared/extra-utils/requests/check-api-params.ts
index c34c7c216..7f5ff775c 100644
--- a/shared/extra-utils/requests/check-api-params.ts
+++ b/shared/extra-utils/requests/check-api-params.ts
@@ -1,5 +1,6 @@
1import { makeGetRequest } from './requests' 1import { makeGetRequest } from './requests'
2import { immutableAssign } from '../miscs/miscs' 2import { immutableAssign } from '../miscs/miscs'
3import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
3 4
4function checkBadStartPagination (url: string, path: string, token?: string, query = {}) { 5function checkBadStartPagination (url: string, path: string, token?: string, query = {}) {
5 return makeGetRequest({ 6 return makeGetRequest({
@@ -7,7 +8,7 @@ function checkBadStartPagination (url: string, path: string, token?: string, que
7 path, 8 path,
8 token, 9 token,
9 query: immutableAssign(query, { start: 'hello' }), 10 query: immutableAssign(query, { start: 'hello' }),
10 statusCodeExpected: 400 11 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400
11 }) 12 })
12} 13}
13 14
@@ -17,7 +18,7 @@ async function checkBadCountPagination (url: string, path: string, token?: strin
17 path, 18 path,
18 token, 19 token,
19 query: immutableAssign(query, { count: 'hello' }), 20 query: immutableAssign(query, { count: 'hello' }),
20 statusCodeExpected: 400 21 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400
21 }) 22 })
22 23
23 await makeGetRequest({ 24 await makeGetRequest({
@@ -25,7 +26,7 @@ async function checkBadCountPagination (url: string, path: string, token?: strin
25 path, 26 path,
26 token, 27 token,
27 query: immutableAssign(query, { count: 2000 }), 28 query: immutableAssign(query, { count: 2000 }),
28 statusCodeExpected: 400 29 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400
29 }) 30 })
30} 31}
31 32
@@ -35,7 +36,7 @@ function checkBadSortPagination (url: string, path: string, token?: string, quer
35 path, 36 path,
36 token, 37 token,
37 query: immutableAssign(query, { sort: 'hello' }), 38 query: immutableAssign(query, { sort: 'hello' }),
38 statusCodeExpected: 400 39 statusCodeExpected: HttpStatusCode.BAD_REQUEST_400
39 }) 40 })
40} 41}
41 42
diff --git a/shared/extra-utils/requests/requests.ts b/shared/extra-utils/requests/requests.ts
index 6b00871e0..3e773ee03 100644
--- a/shared/extra-utils/requests/requests.ts
+++ b/shared/extra-utils/requests/requests.ts
@@ -5,12 +5,13 @@ import { buildAbsoluteFixturePath, root } from '../miscs/miscs'
5import { isAbsolute, join } from 'path' 5import { isAbsolute, join } from 'path'
6import { URL } from 'url' 6import { URL } from 'url'
7import { decode } from 'querystring' 7import { decode } from 'querystring'
8import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
8 9
9function get4KFileUrl () { 10function get4KFileUrl () {
10 return 'https://download.cpy.re/peertube/4k_file.txt' 11 return 'https://download.cpy.re/peertube/4k_file.txt'
11} 12}
12 13
13function makeRawRequest (url: string, statusCodeExpected?: number, range?: string) { 14function makeRawRequest (url: string, statusCodeExpected?: HttpStatusCode, range?: string) {
14 const { host, protocol, pathname } = new URL(url) 15 const { host, protocol, pathname } = new URL(url)
15 16
16 return makeGetRequest({ url: `${protocol}//${host}`, path: pathname, statusCodeExpected, range }) 17 return makeGetRequest({ url: `${protocol}//${host}`, path: pathname, statusCodeExpected, range })
@@ -21,12 +22,12 @@ function makeGetRequest (options: {
21 path?: string 22 path?: string
22 query?: any 23 query?: any
23 token?: string 24 token?: string
24 statusCodeExpected?: number 25 statusCodeExpected?: HttpStatusCode
25 contentType?: string 26 contentType?: string
26 range?: string 27 range?: string
27 redirects?: number 28 redirects?: number
28}) { 29}) {
29 if (!options.statusCodeExpected) options.statusCodeExpected = 400 30 if (!options.statusCodeExpected) options.statusCodeExpected = HttpStatusCode.BAD_REQUEST_400
30 if (options.contentType === undefined) options.contentType = 'application/json' 31 if (options.contentType === undefined) options.contentType = 'application/json'
31 32
32 const req = request(options.url).get(options.path) 33 const req = request(options.url).get(options.path)
@@ -44,9 +45,9 @@ function makeDeleteRequest (options: {
44 url: string 45 url: string
45 path: string 46 path: string
46 token?: string 47 token?: string
47 statusCodeExpected?: number 48 statusCodeExpected?: HttpStatusCode
48}) { 49}) {
49 if (!options.statusCodeExpected) options.statusCodeExpected = 400 50 if (!options.statusCodeExpected) options.statusCodeExpected = HttpStatusCode.BAD_REQUEST_400
50 51
51 const req = request(options.url) 52 const req = request(options.url)
52 .delete(options.path) 53 .delete(options.path)
@@ -64,9 +65,9 @@ function makeUploadRequest (options: {
64 token?: string 65 token?: string
65 fields: { [ fieldName: string ]: any } 66 fields: { [ fieldName: string ]: any }
66 attaches?: { [ attachName: string ]: any | any[] } 67 attaches?: { [ attachName: string ]: any | any[] }
67 statusCodeExpected?: number 68 statusCodeExpected?: HttpStatusCode
68}) { 69}) {
69 if (!options.statusCodeExpected) options.statusCodeExpected = 400 70 if (!options.statusCodeExpected) options.statusCodeExpected = HttpStatusCode.BAD_REQUEST_400
70 71
71 let req: request.Test 72 let req: request.Test
72 if (options.method === 'PUT') { 73 if (options.method === 'PUT') {
@@ -110,10 +111,10 @@ function makePostBodyRequest (options: {
110 path: string 111 path: string
111 token?: string 112 token?: string
112 fields?: { [ fieldName: string ]: any } 113 fields?: { [ fieldName: string ]: any }
113 statusCodeExpected?: number 114 statusCodeExpected?: HttpStatusCode
114}) { 115}) {
115 if (!options.fields) options.fields = {} 116 if (!options.fields) options.fields = {}
116 if (!options.statusCodeExpected) options.statusCodeExpected = 400 117 if (!options.statusCodeExpected) options.statusCodeExpected = HttpStatusCode.BAD_REQUEST_400
117 118
118 const req = request(options.url) 119 const req = request(options.url)
119 .post(options.path) 120 .post(options.path)
@@ -130,9 +131,9 @@ function makePutBodyRequest (options: {
130 path: string 131 path: string
131 token?: string 132 token?: string
132 fields: { [ fieldName: string ]: any } 133 fields: { [ fieldName: string ]: any }
133 statusCodeExpected?: number 134 statusCodeExpected?: HttpStatusCode
134}) { 135}) {
135 if (!options.statusCodeExpected) options.statusCodeExpected = 400 136 if (!options.statusCodeExpected) options.statusCodeExpected = HttpStatusCode.BAD_REQUEST_400
136 137
137 const req = request(options.url) 138 const req = request(options.url)
138 .put(options.path) 139 .put(options.path)
@@ -148,7 +149,7 @@ function makeHTMLRequest (url: string, path: string) {
148 return request(url) 149 return request(url)
149 .get(path) 150 .get(path)
150 .set('Accept', 'text/html') 151 .set('Accept', 'text/html')
151 .expect(200) 152 .expect(HttpStatusCode.OK_200)
152} 153}
153 154
154function updateAvatarRequest (options: { 155function updateAvatarRequest (options: {
@@ -170,7 +171,7 @@ function updateAvatarRequest (options: {
170 token: options.accessToken, 171 token: options.accessToken,
171 fields: {}, 172 fields: {},
172 attaches: { avatarfile: filePath }, 173 attaches: { avatarfile: filePath },
173 statusCodeExpected: 200 174 statusCodeExpected: HttpStatusCode.OK_200
174 }) 175 })
175} 176}
176 177