From 736c436835878afaff4e14f027813a83cf15a2b5 Mon Sep 17 00:00:00 2001 From: Alberto Villa Date: Fri, 28 Jul 2023 06:20:06 +0000 Subject: [PATCH] test(http): add test for raw Axios HTTP client --- packages/http-raw-axios/src/AxiosHttp.test.ts | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 packages/http-raw-axios/src/AxiosHttp.test.ts diff --git a/packages/http-raw-axios/src/AxiosHttp.test.ts b/packages/http-raw-axios/src/AxiosHttp.test.ts new file mode 100644 index 0000000..655fba6 --- /dev/null +++ b/packages/http-raw-axios/src/AxiosHttp.test.ts @@ -0,0 +1,35 @@ +import { DateClock } from '@imho/clock-raw' +import { HttpError, HttpResponseError } from '@imho/http' +import { VoidLog } from '@imho/log-raw' +import axios from 'axios' +import { AxiosHttp } from './AxiosHttp' + +describe('AxiosHttp', () => { + const http = new AxiosHttp(axios, new DateClock(), new VoidLog()) + + describe('get', () => { + test('throwing `HttpError` on invalid request', async () => { + const response = http.get(`foo://bar`) + await expect(response).rejects.toThrow(HttpError) + await expect(response).rejects.not.toThrow(HttpResponseError) + }) + test('throwing `HttpResponseError` on HTTP error', async () => { + await expect(http.get(`${process.env.NGINX_URL}/404`)).rejects.toThrow( + HttpResponseError, + ) + }) + test.each([ + ['JSON', 'json', 'application/json', { foo: 'bar' }], + ['text', 'text', 'text/plain', 'foobar'], + ['XML', 'xml', 'application/xml', 'bar'], + ])('forwarding %s response', async (_type, path, mimeType, body) => { + await expect( + http.get(`${process.env.NGINX_URL}/${path}`), + ).resolves.toMatchObject({ + status: 200, + headers: { 'content-type': mimeType }, + body, + }) + }) + }) +})