Skip to content

Commit

Permalink
test(axios): add "auth" encoding test
Browse files Browse the repository at this point in the history
  • Loading branch information
kettanaito committed Apr 24, 2024
1 parent f27ef30 commit 0f37a73
Showing 1 changed file with 65 additions and 21 deletions.
86 changes: 65 additions & 21 deletions test/third-party/axios.test.ts
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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<Request>()

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')}`
)
})

0 comments on commit 0f37a73

Please sign in to comment.