requests.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/no-floating-promises */
  2. import * as request from 'supertest'
  3. import { buildAbsoluteFixturePath, root } from '../miscs/miscs'
  4. import { isAbsolute, join } from 'path'
  5. import { URL } from 'url'
  6. import { decode } from 'querystring'
  7. import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
  8. function get4KFileUrl () {
  9. return 'https://download.cpy.re/peertube/4k_file.txt'
  10. }
  11. function makeRawRequest (url: string, statusCodeExpected?: HttpStatusCode, range?: string) {
  12. const { host, protocol, pathname } = new URL(url)
  13. return makeGetRequest({ url: `${protocol}//${host}`, path: pathname, statusCodeExpected, range })
  14. }
  15. function makeGetRequest (options: {
  16. url: string
  17. path?: string
  18. query?: any
  19. token?: string
  20. statusCodeExpected?: HttpStatusCode
  21. contentType?: string
  22. range?: string
  23. redirects?: number
  24. }) {
  25. if (!options.statusCodeExpected) options.statusCodeExpected = HttpStatusCode.BAD_REQUEST_400
  26. if (options.contentType === undefined) options.contentType = 'application/json'
  27. const req = request(options.url).get(options.path)
  28. if (options.contentType) req.set('Accept', options.contentType)
  29. if (options.token) req.set('Authorization', 'Bearer ' + options.token)
  30. if (options.query) req.query(options.query)
  31. if (options.range) req.set('Range', options.range)
  32. if (options.redirects) req.redirects(options.redirects)
  33. return req.expect(options.statusCodeExpected)
  34. }
  35. function makeDeleteRequest (options: {
  36. url: string
  37. path: string
  38. token?: string
  39. statusCodeExpected?: HttpStatusCode
  40. }) {
  41. if (!options.statusCodeExpected) options.statusCodeExpected = HttpStatusCode.BAD_REQUEST_400
  42. const req = request(options.url)
  43. .delete(options.path)
  44. .set('Accept', 'application/json')
  45. if (options.token) req.set('Authorization', 'Bearer ' + options.token)
  46. return req.expect(options.statusCodeExpected)
  47. }
  48. function makeUploadRequest (options: {
  49. url: string
  50. method?: 'POST' | 'PUT'
  51. path: string
  52. token?: string
  53. fields: { [ fieldName: string ]: any }
  54. attaches?: { [ attachName: string ]: any | any[] }
  55. statusCodeExpected?: HttpStatusCode
  56. }) {
  57. if (!options.statusCodeExpected) options.statusCodeExpected = HttpStatusCode.BAD_REQUEST_400
  58. let req: request.Test
  59. if (options.method === 'PUT') {
  60. req = request(options.url).put(options.path)
  61. } else {
  62. req = request(options.url).post(options.path)
  63. }
  64. req.set('Accept', 'application/json')
  65. if (options.token) req.set('Authorization', 'Bearer ' + options.token)
  66. Object.keys(options.fields).forEach(field => {
  67. const value = options.fields[field]
  68. if (value === undefined) return
  69. if (Array.isArray(value)) {
  70. for (let i = 0; i < value.length; i++) {
  71. req.field(field + '[' + i + ']', value[i])
  72. }
  73. } else {
  74. req.field(field, value)
  75. }
  76. })
  77. Object.keys(options.attaches || {}).forEach(attach => {
  78. const value = options.attaches[attach]
  79. if (Array.isArray(value)) {
  80. req.attach(attach, buildAbsoluteFixturePath(value[0]), value[1])
  81. } else {
  82. req.attach(attach, buildAbsoluteFixturePath(value))
  83. }
  84. })
  85. return req.expect(options.statusCodeExpected)
  86. }
  87. function makePostBodyRequest (options: {
  88. url: string
  89. path: string
  90. token?: string
  91. fields?: { [ fieldName: string ]: any }
  92. statusCodeExpected?: HttpStatusCode
  93. }) {
  94. if (!options.fields) options.fields = {}
  95. if (!options.statusCodeExpected) options.statusCodeExpected = HttpStatusCode.BAD_REQUEST_400
  96. const req = request(options.url)
  97. .post(options.path)
  98. .set('Accept', 'application/json')
  99. if (options.token) req.set('Authorization', 'Bearer ' + options.token)
  100. return req.send(options.fields)
  101. .expect(options.statusCodeExpected)
  102. }
  103. function makePutBodyRequest (options: {
  104. url: string
  105. path: string
  106. token?: string
  107. fields: { [ fieldName: string ]: any }
  108. statusCodeExpected?: HttpStatusCode
  109. }) {
  110. if (!options.statusCodeExpected) options.statusCodeExpected = HttpStatusCode.BAD_REQUEST_400
  111. const req = request(options.url)
  112. .put(options.path)
  113. .set('Accept', 'application/json')
  114. if (options.token) req.set('Authorization', 'Bearer ' + options.token)
  115. return req.send(options.fields)
  116. .expect(options.statusCodeExpected)
  117. }
  118. function makeHTMLRequest (url: string, path: string) {
  119. return request(url)
  120. .get(path)
  121. .set('Accept', 'text/html')
  122. .expect(HttpStatusCode.OK_200)
  123. }
  124. function updateAvatarRequest (options: {
  125. url: string
  126. path: string
  127. accessToken: string
  128. fixture: string
  129. }) {
  130. let filePath = ''
  131. if (isAbsolute(options.fixture)) {
  132. filePath = options.fixture
  133. } else {
  134. filePath = join(root(), 'server', 'tests', 'fixtures', options.fixture)
  135. }
  136. return makeUploadRequest({
  137. url: options.url,
  138. path: options.path,
  139. token: options.accessToken,
  140. fields: {},
  141. attaches: { avatarfile: filePath },
  142. statusCodeExpected: HttpStatusCode.OK_200
  143. })
  144. }
  145. function decodeQueryString (path: string) {
  146. return decode(path.split('?')[1])
  147. }
  148. // ---------------------------------------------------------------------------
  149. export {
  150. get4KFileUrl,
  151. makeHTMLRequest,
  152. makeGetRequest,
  153. decodeQueryString,
  154. makeUploadRequest,
  155. makePostBodyRequest,
  156. makePutBodyRequest,
  157. makeDeleteRequest,
  158. makeRawRequest,
  159. updateAvatarRequest
  160. }