|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import { http } from 'msw'; |
| 3 | +import { server } from '../../mocks/server.js'; |
| 4 | +import { NOT_FOUND } from '../api/haveibeenpwned/responses.js'; |
| 5 | +import { stealerLogsByWebsiteDomain } from '../stealer-logs-by-website-domain.js'; |
| 6 | + |
| 7 | +describe('stealerLogsByWebsiteDomain', () => { |
| 8 | + const RESULTS = ['andy@gmail.com', 'jane@gmail.com']; |
| 9 | + |
| 10 | + describe('found', () => { |
| 11 | + it('resolves with data from the remote API', () => { |
| 12 | + server.use( |
| 13 | + http.get('*', () => { |
| 14 | + return new Response(JSON.stringify(RESULTS)); |
| 15 | + }), |
| 16 | + ); |
| 17 | + |
| 18 | + return expect(stealerLogsByWebsiteDomain('example.com', { apiKey: 'k' })).resolves.toEqual( |
| 19 | + RESULTS, |
| 20 | + ); |
| 21 | + }); |
| 22 | + }); |
| 23 | + |
| 24 | + describe('not found', () => { |
| 25 | + it('resolves with null', () => { |
| 26 | + server.use( |
| 27 | + http.get('*', () => { |
| 28 | + return new Response(null, { status: NOT_FOUND.status }); |
| 29 | + }), |
| 30 | + ); |
| 31 | + |
| 32 | + return expect(stealerLogsByWebsiteDomain('example.com')).resolves.toBeNull(); |
| 33 | + }); |
| 34 | + }); |
| 35 | + |
| 36 | + describe('apiKey option', () => { |
| 37 | + it('sets the hibp-api-key header', async () => { |
| 38 | + expect.assertions(1); |
| 39 | + const apiKey = 'my-api-key'; |
| 40 | + server.use( |
| 41 | + http.get('*', ({ request }) => { |
| 42 | + expect(request.headers.get('hibp-api-key')).toBe(apiKey); |
| 43 | + return new Response(JSON.stringify(RESULTS)); |
| 44 | + }), |
| 45 | + ); |
| 46 | + |
| 47 | + return stealerLogsByWebsiteDomain('example.com', { apiKey }); |
| 48 | + }); |
| 49 | + }); |
| 50 | + |
| 51 | + describe('baseUrl option', () => { |
| 52 | + it('is the beginning of the final URL', () => { |
| 53 | + const baseUrl = 'https://my-hibp-proxy:8080'; |
| 54 | + server.use( |
| 55 | + http.get(new RegExp(`^${baseUrl}`), () => { |
| 56 | + return new Response(JSON.stringify(RESULTS)); |
| 57 | + }), |
| 58 | + ); |
| 59 | + |
| 60 | + return expect(stealerLogsByWebsiteDomain('example.com', { baseUrl })).resolves.toEqual( |
| 61 | + RESULTS, |
| 62 | + ); |
| 63 | + }); |
| 64 | + }); |
| 65 | + |
| 66 | + describe('timeoutMs option', () => { |
| 67 | + it('aborts the request after the given value', () => { |
| 68 | + expect.assertions(1); |
| 69 | + const timeoutMs = 1; |
| 70 | + server.use( |
| 71 | + http.get('*', async () => { |
| 72 | + await new Promise((resolve) => { |
| 73 | + setTimeout(resolve, timeoutMs + 1); |
| 74 | + }); |
| 75 | + return new Response(JSON.stringify(RESULTS)); |
| 76 | + }), |
| 77 | + ); |
| 78 | + |
| 79 | + return expect( |
| 80 | + stealerLogsByWebsiteDomain('example.com', { timeoutMs }), |
| 81 | + ).rejects.toMatchInlineSnapshot(`[TimeoutError: The operation was aborted due to timeout]`); |
| 82 | + }); |
| 83 | + }); |
| 84 | + |
| 85 | + describe('userAgent option', () => { |
| 86 | + it('is passed on as a request header', () => { |
| 87 | + expect.assertions(1); |
| 88 | + const userAgent = 'Custom UA'; |
| 89 | + server.use( |
| 90 | + http.get('*', ({ request }) => { |
| 91 | + expect(request.headers.get('User-Agent')).toBe(userAgent); |
| 92 | + return new Response(JSON.stringify(RESULTS)); |
| 93 | + }), |
| 94 | + ); |
| 95 | + |
| 96 | + return stealerLogsByWebsiteDomain('example.com', { userAgent }); |
| 97 | + }); |
| 98 | + }); |
| 99 | +}); |
0 commit comments