From 606a39a7e167246c5d22e3585db8218ce74e0ac6 Mon Sep 17 00:00:00 2001 From: Yamagishi Kazutoshi Date: Fri, 31 Jul 2020 09:23:49 +0900 Subject: [PATCH] fix(xmlhttprequest): add support xhr.responseURL (#126) --- src/xmlhttprequest.ts | 4 ++++ test/integration/xmlhttprequest.ts | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/src/xmlhttprequest.ts b/src/xmlhttprequest.ts index 68f6bc0..a8ce204 100644 --- a/src/xmlhttprequest.ts +++ b/src/xmlhttprequest.ts @@ -373,6 +373,8 @@ export default class XMLHttpRequest extends XMLHttpRequestEventTarget { throw new DOMException('', 'InvalidAccessError'); } + this.#responseURL = ''; + let parsedURL: URL; try { @@ -427,6 +429,8 @@ export default class XMLHttpRequest extends XMLHttpRequestEventTarget { this.#statusText = response.statusMessage ?? ''; this.#responseHeaders = response.headers; + this.#responseURL = `${protocol}//${parsedURL.host}${path}`; + response.addListener('data', (chunk: Buffer) => { if (this.#responseBuffer.length === 0) { this.dispatchEvent(new ProgressEvent('loadstart')); diff --git a/test/integration/xmlhttprequest.ts b/test/integration/xmlhttprequest.ts index d99784d..27cc3de 100644 --- a/test/integration/xmlhttprequest.ts +++ b/test/integration/xmlhttprequest.ts @@ -302,6 +302,25 @@ describe('XMLHttpRequest', () => { }); }); + describe('.responseURL', () => { + it('', (done) => { + const client = new XMLHttpRequest(); + + client.addEventListener('loadstart', () => { + expect(client.responseURL).toBe(''); + }); + + client.addEventListener('load', () => { + expect(client.responseURL).toMatch(/http:\/\/localhost:\d+\/path\/to/); + + done(); + }); + + client.open('GET', `${baseURL}/path/to`); + client.send(null); + }); + }); + describe('.withCredentials', () => { it('throws InvalidStateError when readyState is DONE', (done) => { const client = new XMLHttpRequest();