Skip to content

Commit

Permalink
fix(xmlhttprequest): add line break for getAllResponseHeaders() (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
ykzts authored Jul 25, 2020
1 parent d60494d commit 41aca1c
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/xmlhttprequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ export default class XMLHttpRequest extends XMLHttpRequestEventTarget {
for (const name of headerNames) {
const value = this.#responseHeaders[name];

result += `${name}: ${value}`;
result += `${name}: ${value}\r\n`;
}

return result;
Expand Down
45 changes: 44 additions & 1 deletion test/integration/xmlhttprequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import * as http from 'http';
import getPort from 'get-port';
import { XMLHttpRequest } from '../..';

const referenceTime = new Date(Date.UTC(1999, 2, 3, 9, 1, 7, 8));

const launchMockServer = (port: number) =>
new Promise<http.Server>((resolve) => {
const server = http.createServer((req, res) => {
Expand All @@ -33,7 +35,9 @@ const launchMockServer = (port: number) =>
const body = url.searchParams.get('body') || '';

res.writeHead(status, {
'Content-Type': type
'Cache-Control': 'max-age=60',
'Content-Type': type,
Date: referenceTime.toUTCString()
});
res.write(body);
res.end();
Expand Down Expand Up @@ -106,6 +110,45 @@ describe('XMLHttpRequest', () => {
});
});

describe('getAllResponseHeaders()', () => {
it('returns all response headers', (done) => {
const client = new XMLHttpRequest();

client.addEventListener('load', () => {
expect(client.getAllResponseHeaders()).toBe(
[
'cache-control: max-age=60',
'connection: close',
'content-type: text/html',
'date: Wed, 03 Mar 1999 09:01:07 GMT',
'transfer-encoding: chunked',
''
].join('\r\n')
);

done();
});

client.open('GET', `${baseURL}/?type=text/html`);
client.send(null);
});
});

describe('getResponseHeader()', () => {
it('returns response header value', (done) => {
const client = new XMLHttpRequest();

client.addEventListener('load', () => {
expect(client.getResponseHeader('content-type')).toBe('image/png');

done();
});

client.open('GET', `${baseURL}/?type=image/png`);
client.send(null);
});
});

describe('responseText', () => {
it('returns object when given JSON', (done) => {
const client = new XMLHttpRequest();
Expand Down

0 comments on commit 41aca1c

Please sign in to comment.