From 0f37a733d5f16e7906177c0ed231e5c2d2b16836 Mon Sep 17 00:00:00 2001 From: Artem Zakharchenko Date: Wed, 24 Apr 2024 18:44:56 +0200 Subject: [PATCH] test(axios): add "auth" encoding test --- test/third-party/axios.test.ts | 86 +++++++++++++++++++++++++--------- 1 file changed, 65 insertions(+), 21 deletions(-) diff --git a/test/third-party/axios.test.ts b/test/third-party/axios.test.ts index 14ea917a..c4c1e0d5 100644 --- a/test/third-party/axios.test.ts +++ b/test/third-party/axios.test.ts @@ -1,9 +1,25 @@ // @vitest-environment jsdom -import { it, expect, beforeAll, afterAll } from 'vitest' +import { it, expect, beforeAll, afterAll, afterEach } from 'vitest' import axios from 'axios' import { HttpServer } from '@open-draft/test-server/http' import { ClientRequestInterceptor } from '../../src/interceptors/ClientRequest' import { useCors } from '../helpers' +import { DeferredPromise } from '@open-draft/deferred-promise' + +function createMockResponse() { + return new Response( + JSON.stringify({ + mocked: true, + }), + { + status: 200, + headers: { + 'content-type': 'application/json', + 'x-header': 'yes', + }, + } + ) +} const httpServer = new HttpServer((app) => { app.use(useCors) @@ -22,38 +38,26 @@ const httpServer = new HttpServer((app) => { }) const interceptor = new ClientRequestInterceptor() -interceptor.on('request', ({ request }) => { - const url = new URL(request.url) - - if (url.pathname === '/user') { - request.respondWith( - new Response( - JSON.stringify({ - mocked: true, - }), - { - status: 200, - headers: { - 'content-type': 'application/json', - 'x-header': 'yes', - }, - } - ) - ) - } -}) beforeAll(async () => { await httpServer.listen() interceptor.apply() }) +afterEach(() => { + interceptor.removeAllListeners() +}) + afterAll(async () => { interceptor.dispose() await httpServer.close() }) it('responds with a mocked response to an "axios()" request', async () => { + interceptor.on('request', ({ request }) => { + request.respondWith(createMockResponse()) + }) + const res = await axios('/user') expect(res.status).toEqual(200) @@ -62,6 +66,10 @@ it('responds with a mocked response to an "axios()" request', async () => { }) it('responds with a mocked response to an "axios.get()" request', async () => { + interceptor.on('request', ({ request }) => { + request.respondWith(createMockResponse()) + }) + const res = await axios.get('/user') expect(res.status).toEqual(200) @@ -70,6 +78,10 @@ it('responds with a mocked response to an "axios.get()" request', async () => { }) it('responds with a mocked response to an "axios.post()" request', async () => { + interceptor.on('request', ({ request }) => { + request.respondWith(createMockResponse()) + }) + const res = await axios.post('/user') expect(res.status).toEqual(200) @@ -78,6 +90,10 @@ it('responds with a mocked response to an "axios.post()" request', async () => { }) it('bypass the interceptor and return the original response', async () => { + interceptor.on('request', () => { + // Intentionally do nothing. + }) + const res = await axios.get(httpServer.http.url('/books')) expect(res.status).toEqual(200) @@ -92,3 +108,31 @@ it('bypass the interceptor and return the original response', async () => { }, ]) }) + +/** + * @see https://github.com/mswjs/interceptors/issues/564 + */ +it('preserves "auth" (Authorization)', async () => { + const getRequestPromise = new DeferredPromise() + + interceptor.on('request', ({ request }) => { + if (request.method === 'GET') { + getRequestPromise.resolve(request) + } + }) + + // Construct an Axios request with "auth". + const response = await axios.get(httpServer.http.url('/books'), { + auth: { + // Use an email address as the username. + // This must NOT be encoded. + username: 'foo@bar.com', + password: 'secret123', + }, + }) + + const request = await getRequestPromise + expect(request.headers.get('Authorization')).toBe( + `Basic ${btoa('foo@bar.com:secret123')}` + ) +})