From 48a65377c05237e91eba4beebeeaefdf49109e28 Mon Sep 17 00:00:00 2001 From: Thomas Boop <52323235+thboop@users.noreply.github.com> Date: Thu, 15 Aug 2024 16:53:06 -0400 Subject: [PATCH] Fix HTTP client tests (#1792) * fix tests and update dependencies --- package-lock.json | 9 +-- packages/http-client/__tests__/basics.test.ts | 63 ++++++++++--------- .../http-client/__tests__/headers.test.ts | 16 ++--- packages/http-client/__tests__/proxy.test.ts | 15 +++-- 4 files changed, 54 insertions(+), 49 deletions(-) diff --git a/package-lock.json b/package-lock.json index 05b29a24..4bc6fcf8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4925,12 +4925,13 @@ } }, "node_modules/axios": { - "version": "1.6.2", - "resolved": "https://registry.npmjs.org/axios/-/axios-1.6.2.tgz", - "integrity": "sha512-7i24Ri4pmDRfJTR7LDBhsOTtcm+9kjX5WiY1X3wIisx6G9So3pfMkEiU7emUBe46oceVImccTEM3k6C5dbVW8A==", + "version": "1.7.4", + "resolved": "https://registry.npmjs.org/axios/-/axios-1.7.4.tgz", + "integrity": "sha512-DukmaFRnY6AzAALSH4J2M3k6PkaC+MfaAGdEERRWcC9q3/TWQwLpHR8ZRLKTdQ3aBDL64EdluRDjJqKw+BPZEw==", "dev": true, + "license": "MIT", "dependencies": { - "follow-redirects": "^1.15.0", + "follow-redirects": "^1.15.6", "form-data": "^4.0.0", "proxy-from-env": "^1.1.0" } diff --git a/packages/http-client/__tests__/basics.test.ts b/packages/http-client/__tests__/basics.test.ts index 1e715ce9..8d93abb3 100644 --- a/packages/http-client/__tests__/basics.test.ts +++ b/packages/http-client/__tests__/basics.test.ts @@ -37,7 +37,7 @@ describe('basics', () => { // "user-agent": "typed-test-client-tests" // }, // "origin": "173.95.152.44", - // "url": "https://postman-echo.com/get" + // "url": "http://postman-echo.com/get" // } it('does basic http get request', async () => { @@ -63,16 +63,17 @@ describe('basics', () => { expect(obj.headers['user-agent']).toBeFalsy() }) + /* TODO write a mock rather then relying on a third party it('does basic https get request', async () => { const res: httpm.HttpClientResponse = await _http.get( - 'https://postman-echo.com/get' + 'http://postman-echo.com/get' ) expect(res.message.statusCode).toBe(200) const body: string = await res.readBody() const obj = JSON.parse(body) - expect(obj.url).toBe('https://postman-echo.com/get') + expect(obj.url).toBe('http://postman-echo.com/get') }) - +*/ it('does basic http get request with default headers', async () => { const http: httpm.HttpClient = new httpm.HttpClient( 'http-client-tests', @@ -125,12 +126,12 @@ describe('basics', () => { it('pipes a get request', async () => { return new Promise(async resolve => { const file = fs.createWriteStream(sampleFilePath) - ;(await _http.get('https://postman-echo.com/get')).message + ;(await _http.get('http://postman-echo.com/get')).message .pipe(file) .on('close', () => { const body: string = fs.readFileSync(sampleFilePath).toString() const obj = JSON.parse(body) - expect(obj.url).toBe('https://postman-echo.com/get') + expect(obj.url).toBe('http://postman-echo.com/get') resolve() }) }) @@ -138,32 +139,32 @@ describe('basics', () => { it('does basic get request with redirects', async () => { const res: httpm.HttpClientResponse = await _http.get( - `https://postman-echo.com/redirect-to?url=${encodeURIComponent( - 'https://postman-echo.com/get' + `http://postman-echo.com/redirect-to?url=${encodeURIComponent( + 'http://postman-echo.com/get' )}` ) expect(res.message.statusCode).toBe(200) const body: string = await res.readBody() const obj = JSON.parse(body) - expect(obj.url).toBe('https://postman-echo.com/get') + expect(obj.url).toBe('http://postman-echo.com/get') }) it('does basic get request with redirects (303)', async () => { const res: httpm.HttpClientResponse = await _http.get( - `https://postman-echo.com/redirect-to?url=${encodeURIComponent( - 'https://postman-echo.com/get' + `http://postman-echo.com/redirect-to?url=${encodeURIComponent( + 'http://postman-echo.com/get' )}&status_code=303` ) expect(res.message.statusCode).toBe(200) const body: string = await res.readBody() const obj = JSON.parse(body) - expect(obj.url).toBe('https://postman-echo.com/get') + expect(obj.url).toBe('http://postman-echo.com/get') }) it('returns 404 for not found get request on redirect', async () => { const res: httpm.HttpClientResponse = await _http.get( - `https://postman-echo.com/redirect-to?url=${encodeURIComponent( - 'https://postman-echo.com/status/404' + `http://postman-echo.com/redirect-to?url=${encodeURIComponent( + 'http://postman-echo.com/status/404' )}&status_code=303` ) expect(res.message.statusCode).toBe(404) @@ -177,8 +178,8 @@ describe('basics', () => { {allowRedirects: false} ) const res: httpm.HttpClientResponse = await http.get( - `https://postman-echo.com/redirect-to?url=${encodeURIComponent( - 'https://postman-echo.com/get' + `http://postman-echo.com/redirect-to?url=${encodeURIComponent( + 'http://postman-echo.com/get' )}` ) expect(res.message.statusCode).toBe(302) @@ -191,8 +192,8 @@ describe('basics', () => { authorization: 'shhh' } const res: httpm.HttpClientResponse = await _http.get( - `https://postman-echo.com/redirect-to?url=${encodeURIComponent( - 'https://www.postman-echo.com/get' + `http://postman-echo.com/redirect-to?url=${encodeURIComponent( + 'http://www.postman-echo.com/get' )}`, headers ) @@ -204,7 +205,7 @@ describe('basics', () => { expect(obj.headers[httpm.Headers.Accept]).toBe('application/json') expect(obj.headers['Authorization']).toBeUndefined() expect(obj.headers['authorization']).toBeUndefined() - expect(obj.url).toBe('https://www.postman-echo.com/get') + expect(obj.url).toBe('http://www.postman-echo.com/get') }) it('does not pass Auth with diff hostname redirects', async () => { @@ -213,8 +214,8 @@ describe('basics', () => { Authorization: 'shhh' } const res: httpm.HttpClientResponse = await _http.get( - `https://postman-echo.com/redirect-to?url=${encodeURIComponent( - 'https://www.postman-echo.com/get' + `http://postman-echo.com/redirect-to?url=${encodeURIComponent( + 'http://www.postman-echo.com/get' )}`, headers ) @@ -226,7 +227,7 @@ describe('basics', () => { expect(obj.headers[httpm.Headers.Accept]).toBe('application/json') expect(obj.headers['Authorization']).toBeUndefined() expect(obj.headers['authorization']).toBeUndefined() - expect(obj.url).toBe('https://www.postman-echo.com/get') + expect(obj.url).toBe('http://www.postman-echo.com/get') }) it('does basic head request', async () => { @@ -289,11 +290,11 @@ describe('basics', () => { it('gets a json object', async () => { const jsonObj = await _http.getJson( - 'https://postman-echo.com/get' + 'http://postman-echo.com/get' ) expect(jsonObj.statusCode).toBe(200) expect(jsonObj.result).toBeDefined() - expect(jsonObj.result?.url).toBe('https://postman-echo.com/get') + expect(jsonObj.result?.url).toBe('http://postman-echo.com/get') expect(jsonObj.result?.headers[httpm.Headers.Accept]).toBe( httpm.MediaTypes.ApplicationJson ) @@ -304,7 +305,7 @@ describe('basics', () => { it('getting a non existent json object returns null', async () => { const jsonObj = await _http.getJson( - 'https://postman-echo.com/status/404' + 'http://postman-echo.com/status/404' ) expect(jsonObj.statusCode).toBe(404) expect(jsonObj.result).toBeNull() @@ -313,12 +314,12 @@ describe('basics', () => { it('posts a json object', async () => { const res = {name: 'foo'} const restRes = await _http.postJson( - 'https://postman-echo.com/post', + 'http://postman-echo.com/post', res ) expect(restRes.statusCode).toBe(200) expect(restRes.result).toBeDefined() - expect(restRes.result?.url).toBe('https://postman-echo.com/post') + expect(restRes.result?.url).toBe('http://postman-echo.com/post') expect(restRes.result?.json.name).toBe('foo') expect(restRes.result?.headers[httpm.Headers.Accept]).toBe( httpm.MediaTypes.ApplicationJson @@ -334,12 +335,12 @@ describe('basics', () => { it('puts a json object', async () => { const res = {name: 'foo'} const restRes = await _http.putJson( - 'https://postman-echo.com/put', + 'http://postman-echo.com/put', res ) expect(restRes.statusCode).toBe(200) expect(restRes.result).toBeDefined() - expect(restRes.result?.url).toBe('https://postman-echo.com/put') + expect(restRes.result?.url).toBe('http://postman-echo.com/put') expect(restRes.result?.json.name).toBe('foo') expect(restRes.result?.headers[httpm.Headers.Accept]).toBe( @@ -356,12 +357,12 @@ describe('basics', () => { it('patch a json object', async () => { const res = {name: 'foo'} const restRes = await _http.patchJson( - 'https://postman-echo.com/patch', + 'http://postman-echo.com/patch', res ) expect(restRes.statusCode).toBe(200) expect(restRes.result).toBeDefined() - expect(restRes.result?.url).toBe('https://postman-echo.com/patch') + expect(restRes.result?.url).toBe('http://postman-echo.com/patch') expect(restRes.result?.json.name).toBe('foo') expect(restRes.result?.headers[httpm.Headers.Accept]).toBe( httpm.MediaTypes.ApplicationJson diff --git a/packages/http-client/__tests__/headers.test.ts b/packages/http-client/__tests__/headers.test.ts index c1ca0ec3..887d9333 100644 --- a/packages/http-client/__tests__/headers.test.ts +++ b/packages/http-client/__tests__/headers.test.ts @@ -12,7 +12,7 @@ describe('headers', () => { it('preserves existing headers on getJson', async () => { const additionalHeaders = {[httpm.Headers.Accept]: 'foo'} let jsonObj = await _http.getJson( - 'https://postman-echo.com/get', + 'http://postman-echo.com/get', additionalHeaders ) expect(jsonObj.result.headers[httpm.Headers.Accept]).toBe('foo') @@ -26,7 +26,7 @@ describe('headers', () => { [httpm.Headers.Accept]: 'baz' } } - jsonObj = await httpWithHeaders.getJson('https://postman-echo.com/get') + jsonObj = await httpWithHeaders.getJson('http://postman-echo.com/get') expect(jsonObj.result.headers[httpm.Headers.Accept]).toBe('baz') expect(jsonObj.headers[httpm.Headers.ContentType]).toContain( httpm.MediaTypes.ApplicationJson @@ -36,7 +36,7 @@ describe('headers', () => { it('preserves existing headers on postJson', async () => { const additionalHeaders = {[httpm.Headers.Accept]: 'foo'} let jsonObj = await _http.postJson( - 'https://postman-echo.com/post', + 'http://postman-echo.com/post', {}, additionalHeaders ) @@ -52,7 +52,7 @@ describe('headers', () => { } } jsonObj = await httpWithHeaders.postJson( - 'https://postman-echo.com/post', + 'http://postman-echo.com/post', {} ) expect(jsonObj.result.headers[httpm.Headers.Accept]).toBe('baz') @@ -64,7 +64,7 @@ describe('headers', () => { it('preserves existing headers on putJson', async () => { const additionalHeaders = {[httpm.Headers.Accept]: 'foo'} let jsonObj = await _http.putJson( - 'https://postman-echo.com/put', + 'http://postman-echo.com/put', {}, additionalHeaders ) @@ -80,7 +80,7 @@ describe('headers', () => { } } jsonObj = await httpWithHeaders.putJson( - 'https://postman-echo.com/put', + 'http://postman-echo.com/put', {} ) expect(jsonObj.result.headers[httpm.Headers.Accept]).toBe('baz') @@ -92,7 +92,7 @@ describe('headers', () => { it('preserves existing headers on patchJson', async () => { const additionalHeaders = {[httpm.Headers.Accept]: 'foo'} let jsonObj = await _http.patchJson( - 'https://postman-echo.com/patch', + 'http://postman-echo.com/patch', {}, additionalHeaders ) @@ -108,7 +108,7 @@ describe('headers', () => { } } jsonObj = await httpWithHeaders.patchJson( - 'https://postman-echo.com/patch', + 'http://postman-echo.com/patch', {} ) expect(jsonObj.result.headers[httpm.Headers.Accept]).toBe('baz') diff --git a/packages/http-client/__tests__/proxy.test.ts b/packages/http-client/__tests__/proxy.test.ts index c921b4bc..01888412 100644 --- a/packages/http-client/__tests__/proxy.test.ts +++ b/packages/http-client/__tests__/proxy.test.ts @@ -222,30 +222,33 @@ describe('proxy', () => { expect(_proxyConnects).toHaveLength(0) }) + // TODO mock this out so we don't rely on a third party + /* it('HttpClient does basic https get request through proxy', async () => { process.env['https_proxy'] = _proxyUrl const httpClient = new httpm.HttpClient() const res: httpm.HttpClientResponse = await httpClient.get( - 'https://postman-echo.com/get' + 'http://postman-echo.com/get' ) expect(res.message.statusCode).toBe(200) const body: string = await res.readBody() const obj = JSON.parse(body) - expect(obj.url).toBe('https://postman-echo.com/get') + expect(obj.url).toBe('http://postman-echo.com/get') expect(_proxyConnects).toEqual(['postman-echo.com:443']) }) + */ - it('HttpClient does basic https get request when bypass proxy', async () => { - process.env['https_proxy'] = _proxyUrl + it('HttpClient does basic http get request when bypass proxy', async () => { + process.env['http_proxy'] = _proxyUrl process.env['no_proxy'] = 'postman-echo.com' const httpClient = new httpm.HttpClient() const res: httpm.HttpClientResponse = await httpClient.get( - 'https://postman-echo.com/get' + 'http://postman-echo.com/get' ) expect(res.message.statusCode).toBe(200) const body: string = await res.readBody() const obj = JSON.parse(body) - expect(obj.url).toBe('https://postman-echo.com/get') + expect(obj.url).toBe('http://postman-echo.com/get') expect(_proxyConnects).toHaveLength(0) })