aboutsummaryrefslogtreecommitdiffhomepage
path: root/shared/extra-utils/requests/requests.ts
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/requests.ts
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/requests.ts')
-rw-r--r--shared/extra-utils/requests/requests.ts27
1 files changed, 14 insertions, 13 deletions
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