|
| 1 | +import assert from 'assert'; |
| 2 | +import { |
| 3 | + startHTTPServer, |
| 4 | + stopHTTPServer, |
| 5 | + LOCAL_SERVER_URL, |
| 6 | + setTimeoutAsync, |
| 7 | + makeReadableStream, |
| 8 | + generateReadable, |
| 9 | + makeEchoStream |
| 10 | +} from '../../helpers/server.js'; |
| 11 | +import axios from '../../../index.js'; |
| 12 | +import stream from "stream"; |
| 13 | +import {AbortController} from "abortcontroller-polyfill/dist/cjs-ponyfill.js"; |
| 14 | +import util from "util"; |
| 15 | + |
| 16 | +const pipelineAsync = util.promisify(stream.pipeline); |
| 17 | + |
| 18 | +const fetchAxios = axios.create({ |
| 19 | + baseURL: LOCAL_SERVER_URL, |
| 20 | + adapter: 'fetch' |
| 21 | +}); |
| 22 | + |
| 23 | +let server; |
| 24 | + |
| 25 | +describe('supports fetch with nodejs', function () { |
| 26 | + before(function () { |
| 27 | + if (typeof fetch !== 'function') { |
| 28 | + this.skip(); |
| 29 | + } |
| 30 | + }) |
| 31 | + |
| 32 | + afterEach(async function () { |
| 33 | + await stopHTTPServer(server); |
| 34 | + |
| 35 | + server = null; |
| 36 | + }); |
| 37 | + |
| 38 | + describe('responses', async () => { |
| 39 | + it(`should support text response type`, async () => { |
| 40 | + const originalData = 'my data'; |
| 41 | + |
| 42 | + server = await startHTTPServer((req, res) => res.end(originalData)); |
| 43 | + |
| 44 | + const {data} = await fetchAxios.get('/', { |
| 45 | + responseType: 'text' |
| 46 | + }); |
| 47 | + |
| 48 | + assert.deepStrictEqual(data, originalData); |
| 49 | + }); |
| 50 | + |
| 51 | + it(`should support arraybuffer response type`, async () => { |
| 52 | + const originalData = 'my data'; |
| 53 | + |
| 54 | + server = await startHTTPServer((req, res) => res.end(originalData)); |
| 55 | + |
| 56 | + const {data} = await fetchAxios.get('/', { |
| 57 | + responseType: 'arraybuffer' |
| 58 | + }); |
| 59 | + |
| 60 | + assert.deepStrictEqual(data, Uint8Array.from(await new TextEncoder().encode(originalData)).buffer); |
| 61 | + }); |
| 62 | + |
| 63 | + it(`should support blob response type`, async () => { |
| 64 | + const originalData = 'my data'; |
| 65 | + |
| 66 | + server = await startHTTPServer((req, res) => res.end(originalData)); |
| 67 | + |
| 68 | + const {data} = await fetchAxios.get('/', { |
| 69 | + responseType: 'blob' |
| 70 | + }); |
| 71 | + |
| 72 | + assert.deepStrictEqual(data, new Blob([originalData])); |
| 73 | + }); |
| 74 | + |
| 75 | + it(`should support stream response type`, async () => { |
| 76 | + const originalData = 'my data'; |
| 77 | + |
| 78 | + server = await startHTTPServer((req, res) => res.end(originalData)); |
| 79 | + |
| 80 | + const {data} = await fetchAxios.get('/', { |
| 81 | + responseType: 'stream' |
| 82 | + }); |
| 83 | + |
| 84 | + assert.ok(data instanceof ReadableStream, 'data is not instanceof ReadableStream'); |
| 85 | + |
| 86 | + let response = new Response(data); |
| 87 | + |
| 88 | + assert.deepStrictEqual(await response.text(), originalData); |
| 89 | + }); |
| 90 | + |
| 91 | + it(`should support formData response type`, async function () { |
| 92 | + this.timeout(5000); |
| 93 | + |
| 94 | + const originalData = new FormData(); |
| 95 | + |
| 96 | + originalData.append('x', '123'); |
| 97 | + |
| 98 | + server = await startHTTPServer(async (req, res) => { |
| 99 | + |
| 100 | + const response = await new Response(originalData); |
| 101 | + |
| 102 | + res.setHeader('Content-Type', response.headers.get('Content-Type')); |
| 103 | + |
| 104 | + res.end(await response.text()); |
| 105 | + }); |
| 106 | + |
| 107 | + const {data} = await fetchAxios.get('/', { |
| 108 | + responseType: 'formdata' |
| 109 | + }); |
| 110 | + |
| 111 | + assert.ok(data instanceof FormData, 'data is not instanceof FormData'); |
| 112 | + |
| 113 | + assert.deepStrictEqual(Object.fromEntries(data.entries()), Object.fromEntries(originalData.entries())); |
| 114 | + }); |
| 115 | + |
| 116 | + it(`should support json response type`, async () => { |
| 117 | + const originalData = {x: 'my data'}; |
| 118 | + |
| 119 | + server = await startHTTPServer((req, res) => res.end(JSON.stringify(originalData))); |
| 120 | + |
| 121 | + const {data} = await fetchAxios.get('/', { |
| 122 | + responseType: 'json' |
| 123 | + }); |
| 124 | + |
| 125 | + assert.deepStrictEqual(data, originalData); |
| 126 | + }); |
| 127 | + }); |
| 128 | + |
| 129 | + describe("progress", () => { |
| 130 | + describe('upload', function () { |
| 131 | + it('should support upload progress capturing', async function () { |
| 132 | + this.timeout(15000); |
| 133 | + |
| 134 | + server = await startHTTPServer({ |
| 135 | + rate: 100 * 1024 |
| 136 | + }); |
| 137 | + |
| 138 | + let content = ''; |
| 139 | + const count = 10; |
| 140 | + const chunk = "test"; |
| 141 | + const chunkLength = Buffer.byteLength(chunk); |
| 142 | + const contentLength = count * chunkLength; |
| 143 | + |
| 144 | + const readable = stream.Readable.from(async function* () { |
| 145 | + let i = count; |
| 146 | + |
| 147 | + while (i-- > 0) { |
| 148 | + await setTimeoutAsync(1100); |
| 149 | + content += chunk; |
| 150 | + yield chunk; |
| 151 | + } |
| 152 | + }()); |
| 153 | + |
| 154 | + const samples = []; |
| 155 | + |
| 156 | + const {data} = await fetchAxios.post('/', readable, { |
| 157 | + onUploadProgress: ({loaded, total, progress, bytes, upload}) => { |
| 158 | + console.log(`Upload Progress ${loaded} from ${total} bytes (${(progress * 100).toFixed(1)}%)`); |
| 159 | + |
| 160 | + samples.push({ |
| 161 | + loaded, |
| 162 | + total, |
| 163 | + progress, |
| 164 | + bytes, |
| 165 | + upload |
| 166 | + }); |
| 167 | + }, |
| 168 | + headers: { |
| 169 | + 'Content-Length': contentLength |
| 170 | + }, |
| 171 | + responseType: 'text' |
| 172 | + }); |
| 173 | + |
| 174 | + await setTimeoutAsync(500); |
| 175 | + |
| 176 | + assert.strictEqual(data, content); |
| 177 | + |
| 178 | + assert.deepStrictEqual(samples, Array.from(function* () { |
| 179 | + for (let i = 1; i <= 10; i++) { |
| 180 | + yield ({ |
| 181 | + loaded: chunkLength * i, |
| 182 | + total: contentLength, |
| 183 | + progress: (chunkLength * i) / contentLength, |
| 184 | + bytes: 4, |
| 185 | + upload: true |
| 186 | + }); |
| 187 | + } |
| 188 | + }())); |
| 189 | + }); |
| 190 | + |
| 191 | + it('should not fail with get method', async() => { |
| 192 | + server = await startHTTPServer((req, res) => res.end('OK')); |
| 193 | + |
| 194 | + const {data} = await fetchAxios.get('/', { |
| 195 | + onUploadProgress() { |
| 196 | + |
| 197 | + } |
| 198 | + }); |
| 199 | + |
| 200 | + assert.strictEqual(data, 'OK'); |
| 201 | + }); |
| 202 | + }); |
| 203 | + |
| 204 | + describe('download', function () { |
| 205 | + it('should support download progress capturing', async function () { |
| 206 | + this.timeout(15000); |
| 207 | + |
| 208 | + server = await startHTTPServer({ |
| 209 | + rate: 100 * 1024 |
| 210 | + }); |
| 211 | + |
| 212 | + let content = ''; |
| 213 | + const count = 10; |
| 214 | + const chunk = "test"; |
| 215 | + const chunkLength = Buffer.byteLength(chunk); |
| 216 | + const contentLength = count * chunkLength; |
| 217 | + |
| 218 | + const readable = stream.Readable.from(async function* () { |
| 219 | + let i = count; |
| 220 | + |
| 221 | + while (i-- > 0) { |
| 222 | + await setTimeoutAsync(1100); |
| 223 | + content += chunk; |
| 224 | + yield chunk; |
| 225 | + } |
| 226 | + }()); |
| 227 | + |
| 228 | + const samples = []; |
| 229 | + |
| 230 | + const {data} = await fetchAxios.post('/', readable, { |
| 231 | + onDownloadProgress: ({loaded, total, progress, bytes, download}) => { |
| 232 | + console.log(`Download Progress ${loaded} from ${total} bytes (${(progress * 100).toFixed(1)}%)`); |
| 233 | + |
| 234 | + samples.push({ |
| 235 | + loaded, |
| 236 | + total, |
| 237 | + progress, |
| 238 | + bytes, |
| 239 | + download |
| 240 | + }); |
| 241 | + }, |
| 242 | + headers: { |
| 243 | + 'Content-Length': contentLength |
| 244 | + }, |
| 245 | + responseType: 'text', |
| 246 | + maxRedirects: 0 |
| 247 | + }); |
| 248 | + |
| 249 | + await setTimeoutAsync(500); |
| 250 | + |
| 251 | + assert.strictEqual(data, content); |
| 252 | + |
| 253 | + assert.deepStrictEqual(samples, Array.from(function* () { |
| 254 | + for (let i = 1; i <= 10; i++) { |
| 255 | + yield ({ |
| 256 | + loaded: chunkLength * i, |
| 257 | + total: contentLength, |
| 258 | + progress: (chunkLength * i) / contentLength, |
| 259 | + bytes: 4, |
| 260 | + download: true |
| 261 | + }); |
| 262 | + } |
| 263 | + }())); |
| 264 | + }); |
| 265 | + }); |
| 266 | + }); |
| 267 | + |
| 268 | + it('should support basic auth', async () => { |
| 269 | + server = await startHTTPServer((req, res) => res.end(req.headers.authorization)); |
| 270 | + |
| 271 | + const user = 'foo'; |
| 272 | + const headers = {Authorization: 'Bearer 1234'}; |
| 273 | + const res = await axios.get('http://' + user + '@localhost:4444/', {headers: headers}); |
| 274 | + |
| 275 | + const base64 = Buffer.from(user + ':', 'utf8').toString('base64'); |
| 276 | + assert.equal(res.data, 'Basic ' + base64); |
| 277 | + }); |
| 278 | + |
| 279 | + it("should support stream.Readable as a payload", async () => { |
| 280 | + server = await startHTTPServer(); |
| 281 | + |
| 282 | + const {data} = await fetchAxios.post('/', stream.Readable.from('OK')); |
| 283 | + |
| 284 | + assert.strictEqual(data, 'OK'); |
| 285 | + }); |
| 286 | + |
| 287 | + describe('request aborting', function() { |
| 288 | + it('should be able to abort the request stream', async function () { |
| 289 | + server = await startHTTPServer({ |
| 290 | + rate: 100000, |
| 291 | + useBuffering: true |
| 292 | + }); |
| 293 | + |
| 294 | + const controller = new AbortController(); |
| 295 | + |
| 296 | + setTimeout(() => { |
| 297 | + controller.abort(); |
| 298 | + }, 500); |
| 299 | + |
| 300 | + await assert.rejects(async () => { |
| 301 | + await fetchAxios.post('/', makeReadableStream(), { |
| 302 | + responseType: 'stream', |
| 303 | + signal: controller.signal |
| 304 | + }); |
| 305 | + }, /CanceledError/); |
| 306 | + }); |
| 307 | + |
| 308 | + it('should be able to abort the response stream', async function () { |
| 309 | + server = await startHTTPServer((req, res) => { |
| 310 | + pipelineAsync(generateReadable(10000, 10), res); |
| 311 | + }); |
| 312 | + |
| 313 | + const controller = new AbortController(); |
| 314 | + |
| 315 | + setTimeout(() => { |
| 316 | + controller.abort(new Error('test')); |
| 317 | + }, 800); |
| 318 | + |
| 319 | + const {data} = await fetchAxios.get('/', { |
| 320 | + responseType: 'stream', |
| 321 | + signal: controller.signal |
| 322 | + }); |
| 323 | + |
| 324 | + await assert.rejects(async () => { |
| 325 | + await data.pipeTo(makeEchoStream()); |
| 326 | + }, /^(AbortError|CanceledError):/); |
| 327 | + }); |
| 328 | + }); |
| 329 | + |
| 330 | + it('should support a timeout', async () => { |
| 331 | + server = await startHTTPServer(async(req, res) => { |
| 332 | + await setTimeoutAsync(1000); |
| 333 | + res.end('OK'); |
| 334 | + }); |
| 335 | + |
| 336 | + const timeout = 500; |
| 337 | + |
| 338 | + const ts = Date.now(); |
| 339 | + |
| 340 | + await assert.rejects(async() => { |
| 341 | + await fetchAxios('/', { |
| 342 | + timeout |
| 343 | + }) |
| 344 | + }, /timeout/); |
| 345 | + |
| 346 | + const passed = Date.now() - ts; |
| 347 | + |
| 348 | + assert.ok(passed >= timeout - 5, `early cancellation detected (${passed} ms)`); |
| 349 | + }); |
| 350 | + |
| 351 | + |
| 352 | + it('should combine baseURL and url', async () => { |
| 353 | + server = await startHTTPServer(); |
| 354 | + |
| 355 | + const res = await fetchAxios('/foo'); |
| 356 | + |
| 357 | + assert.equal(res.config.baseURL, LOCAL_SERVER_URL); |
| 358 | + assert.equal(res.config.url, '/foo'); |
| 359 | + }); |
| 360 | + |
| 361 | + it('should support params', async() => { |
| 362 | + server = await startHTTPServer((req, res) => res.end(req.url)); |
| 363 | + |
| 364 | + const {data} = await fetchAxios.get('/?test=1', { |
| 365 | + params: { |
| 366 | + foo: 1, |
| 367 | + bar: 2 |
| 368 | + } |
| 369 | + }); |
| 370 | + |
| 371 | + assert.strictEqual(data, '/?test=1&foo=1&bar=2'); |
| 372 | + }); |
| 373 | +}); |
0 commit comments