From ef653c6d64d2af002e1f08dc15a52f111308b698 Mon Sep 17 00:00:00 2001 From: Artem Zakharchenko Date: Sun, 27 Feb 2022 02:05:52 +0100 Subject: [PATCH] fix(xhr): support original responses without any headers (#216) --- package.json | 2 +- .../xhr-no-response-headers.test.ts | 53 +++++++++++++++++++ yarn.lock | 11 ++-- 3 files changed, 62 insertions(+), 4 deletions(-) create mode 100644 test/modules/XMLHttpRequest/compliance/xhr-no-response-headers.test.ts diff --git a/package.json b/package.json index d72b1140..bba6582b 100644 --- a/package.json +++ b/package.json @@ -65,7 +65,7 @@ "@open-draft/until": "^1.0.3", "@xmldom/xmldom": "^0.7.5", "debug": "^4.3.3", - "headers-polyfill": "^3.0.3", + "headers-polyfill": "^3.0.4", "outvariant": "^1.2.1", "strict-event-emitter": "^0.2.0" }, diff --git a/test/modules/XMLHttpRequest/compliance/xhr-no-response-headers.test.ts b/test/modules/XMLHttpRequest/compliance/xhr-no-response-headers.test.ts new file mode 100644 index 00000000..5340bd1b --- /dev/null +++ b/test/modules/XMLHttpRequest/compliance/xhr-no-response-headers.test.ts @@ -0,0 +1,53 @@ +/** + * @jest-environment jsdom + */ +import { ServerApi, createServer } from '@open-draft/test-server' +import { createInterceptor } from '../../../../src' +import { interceptXMLHttpRequest } from '../../../../src/interceptors/XMLHttpRequest' +import { createXMLHttpRequest } from '../../../helpers' + +let httpServer: ServerApi + +const interceptor = createInterceptor({ + modules: [interceptXMLHttpRequest], + resolver() {}, +}) + +beforeAll(async () => { + httpServer = await createServer((app) => { + app.get('/user', (_req, res) => { + // Respond with a message that has no headers. + res.socket?.end(`\ +HTTP/1.1 200 OK + +hello world`) + }) + }) + + interceptor.apply() + + /** + * @note Stub the internal JSDOM property to prevent the following error: + * Error: Cross origin http://127.0.0.1:XXXXX/ forbidden + */ + const { protocol, host, port } = httpServer.http.getAddress() + // @ts-expect-error + window._origin = `${protocol}//${host}:${port}` +}) + +afterAll(async () => { + interceptor.restore() + await httpServer.close() +}) + +test('handles an original response without any headers', async () => { + const req = await createXMLHttpRequest((req) => { + req.open('GET', httpServer.http.makeUrl('/user')) + req.send() + }) + + expect(req.status).toEqual(200) + expect(req.statusText).toEqual('OK') + expect(req.responseText).toEqual('hello world') + expect(req.getAllResponseHeaders()).toEqual('') +}) diff --git a/yarn.lock b/yarn.lock index 3e03cf9e..33be98c9 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2544,9 +2544,9 @@ findup-sync@^4.0.0: resolve-dir "^1.0.1" follow-redirects@^1.14.4: - version "1.14.8" - resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.14.8.tgz#016996fb9a11a100566398b1c6839337d7bfa8fc" - integrity sha512-1x0S9UVJHsQprFcEC/qnNzBLcIxsjAV905f/UkQxbclCsoTWlacCNOpQa/anodLl2uaEKFhfWOvM2Qg77+15zA== + version "1.14.9" + resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.14.9.tgz#dd4ea157de7bfaf9ea9b3fbd85aa16951f78d8d7" + integrity sha512-MQDfihBQYMcyy5dhRDJUHcw7lb2Pv/TuE6xP1vyraLukNDHKbDxDNaOE3NbCAdKQApno+GPRyo1YAp89yCjK4w== form-data@^3.0.0: version "3.0.1" @@ -2777,6 +2777,11 @@ headers-polyfill@^3.0.3: resolved "https://registry.yarnpkg.com/headers-polyfill/-/headers-polyfill-3.0.3.tgz#222bd9a6b5a1f610ad563473efb0d34a13eb7ebc" integrity sha512-Ak5m549Y+5vBtWNnIUcyARJjF0tQpINuy3+vOqG8tK/qjF0itYhAPgrzJc0zsZJh7AX8PalzICMPuWIqHpZlEA== +headers-polyfill@^3.0.4: + version "3.0.4" + resolved "https://registry.yarnpkg.com/headers-polyfill/-/headers-polyfill-3.0.4.tgz#cd70c815a441dd882372fcd6eda212ce997c9b18" + integrity sha512-I1DOM1EdWYntdrnCvqQtcKwSSuiTzoqOExy4v1mdcFixFZABlWP4IPHdmoLtPda0abMHqDOY4H9svhQ10DFR4w== + homedir-polyfill@^1.0.1: version "1.0.3" resolved "https://registry.yarnpkg.com/homedir-polyfill/-/homedir-polyfill-1.0.3.tgz#743298cef4e5af3e194161fbadcc2151d3a058e8"