aboutsummaryrefslogtreecommitdiffhomepage
path: root/shared/extra-utils/requests/requests.ts
blob: 3e773ee036109e1221a36cdbdca4fccd80043533 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/no-floating-promises */

import * as request from 'supertest'
import { buildAbsoluteFixturePath, root } from '../miscs/miscs'
import { isAbsolute, join } from 'path'
import { URL } from 'url'
import { decode } from 'querystring'
import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'

function get4KFileUrl () {
  return 'https://download.cpy.re/peertube/4k_file.txt'
}

function makeRawRequest (url: string, statusCodeExpected?: HttpStatusCode, range?: string) {
  const { host, protocol, pathname } = new URL(url)

  return makeGetRequest({ url: `${protocol}//${host}`, path: pathname, statusCodeExpected, range })
}

function makeGetRequest (options: {
  url: string
  path?: string
  query?: any
  token?: string
  statusCodeExpected?: HttpStatusCode
  contentType?: string
  range?: string
  redirects?: number
}) {
  if (!options.statusCodeExpected) options.statusCodeExpected = HttpStatusCode.BAD_REQUEST_400
  if (options.contentType === undefined) options.contentType = 'application/json'

  const req = request(options.url).get(options.path)

  if (options.contentType) req.set('Accept', options.contentType)
  if (options.token) req.set('Authorization', 'Bearer ' + options.token)
  if (options.query) req.query(options.query)
  if (options.range) req.set('Range', options.range)
  if (options.redirects) req.redirects(options.redirects)

  return req.expect(options.statusCodeExpected)
}

function makeDeleteRequest (options: {
  url: string
  path: string
  token?: string
  statusCodeExpected?: HttpStatusCode
}) {
  if (!options.statusCodeExpected) options.statusCodeExpected = HttpStatusCode.BAD_REQUEST_400

  const req = request(options.url)
    .delete(options.path)
    .set('Accept', 'application/json')

  if (options.token) req.set('Authorization', 'Bearer ' + options.token)

  return req.expect(options.statusCodeExpected)
}

function makeUploadRequest (options: {
  url: string
  method?: 'POST' | 'PUT'
  path: string
  token?: string
  fields: { [ fieldName: string ]: any }
  attaches?: { [ attachName: string ]: any | any[] }
  statusCodeExpected?: HttpStatusCode
}) {
  if (!options.statusCodeExpected) options.statusCodeExpected = HttpStatusCode.BAD_REQUEST_400

  let req: request.Test
  if (options.method === 'PUT') {
    req = request(options.url).put(options.path)
  } else {
    req = request(options.url).post(options.path)
  }

  req.set('Accept', 'application/json')

  if (options.token) req.set('Authorization', 'Bearer ' + options.token)

  Object.keys(options.fields).forEach(field => {
    const value = options.fields[field]

    if (value === undefined) return

    if (Array.isArray(value)) {
      for (let i = 0; i < value.length; i++) {
        req.field(field + '[' + i + ']', value[i])
      }
    } else {
      req.field(field, value)
    }
  })

  Object.keys(options.attaches || {}).forEach(attach => {
    const value = options.attaches[attach]
    if (Array.isArray(value)) {
      req.attach(attach, buildAbsoluteFixturePath(value[0]), value[1])
    } else {
      req.attach(attach, buildAbsoluteFixturePath(value))
    }
  })

  return req.expect(options.statusCodeExpected)
}

function makePostBodyRequest (options: {
  url: string
  path: string
  token?: string
  fields?: { [ fieldName: string ]: any }
  statusCodeExpected?: HttpStatusCode
}) {
  if (!options.fields) options.fields = {}
  if (!options.statusCodeExpected) options.statusCodeExpected = HttpStatusCode.BAD_REQUEST_400

  const req = request(options.url)
                .post(options.path)
                .set('Accept', 'application/json')

  if (options.token) req.set('Authorization', 'Bearer ' + options.token)

  return req.send(options.fields)
            .expect(options.statusCodeExpected)
}

function makePutBodyRequest (options: {
  url: string
  path: string
  token?: string
  fields: { [ fieldName: string ]: any }
  statusCodeExpected?: HttpStatusCode
}) {
  if (!options.statusCodeExpected) options.statusCodeExpected = HttpStatusCode.BAD_REQUEST_400

  const req = request(options.url)
                .put(options.path)
                .set('Accept', 'application/json')

  if (options.token) req.set('Authorization', 'Bearer ' + options.token)

  return req.send(options.fields)
            .expect(options.statusCodeExpected)
}

function makeHTMLRequest (url: string, path: string) {
  return request(url)
    .get(path)
    .set('Accept', 'text/html')
    .expect(HttpStatusCode.OK_200)
}

function updateAvatarRequest (options: {
  url: string
  path: string
  accessToken: string
  fixture: string
}) {
  let filePath = ''
  if (isAbsolute(options.fixture)) {
    filePath = options.fixture
  } else {
    filePath = join(root(), 'server', 'tests', 'fixtures', options.fixture)
  }

  return makeUploadRequest({
    url: options.url,
    path: options.path,
    token: options.accessToken,
    fields: {},
    attaches: { avatarfile: filePath },
    statusCodeExpected: HttpStatusCode.OK_200
  })
}

function decodeQueryString (path: string) {
  return decode(path.split('?')[1])
}

// ---------------------------------------------------------------------------

export {
  get4KFileUrl,
  makeHTMLRequest,
  makeGetRequest,
  decodeQueryString,
  makeUploadRequest,
  makePostBodyRequest,
  makePutBodyRequest,
  makeDeleteRequest,
  makeRawRequest,
  updateAvatarRequest
}