Skip to content

Commit

Permalink
fix(xmlhttprequest): allow xhr.withCredentials set (#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
ykzts authored Jul 26, 2020
1 parent f19594b commit e3a4910
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/xmlhttprequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ export default class XMLHttpRequest extends XMLHttpRequestEventTarget {
#responseURL: string;
#status: number;
#statusText: string;
#timeout: number;
#withCredentials: boolean;

get readyState(): number {
return this.#readyState;
Expand Down Expand Up @@ -179,6 +181,30 @@ export default class XMLHttpRequest extends XMLHttpRequestEventTarget {
return this.#statusText;
}

get timeout(): number {
return this.#timeout;
}

set timeout(value: number) {
this.#timeout = value;
}

get withCredentials(): boolean {
return this.#withCredentials;
}

set withCredentials(value: boolean) {
if (
this.readyState !== XMLHttpRequest.UNSENT &&
this.readyState !== XMLHttpRequest.OPENED
) {
// TODO: Add human readable message.
throw new DOMException('', 'InvalidStateError');
}

this.#withCredentials = value;
}

constructor() {
super();

Expand All @@ -193,6 +219,8 @@ export default class XMLHttpRequest extends XMLHttpRequestEventTarget {
this.#responseURL = '';
this.#status = 0;
this.#statusText = '';
this.#timeout = 0;
this.#withCredentials = false;
}

/**
Expand Down
20 changes: 20 additions & 0 deletions test/integration/xmlhttprequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,4 +202,24 @@ describe('XMLHttpRequest', () => {
client.send(null);
});
});

describe('#withCredentials', () => {
it('throws InvalidStateError when readyState is DONE', (done) => {
const client = new XMLHttpRequest();

client.addEventListener('load', () => {
expect(() => {
client.withCredentials = false;
}).toThrow();

expect(client.withCredentials).toBe(true);

done();
});

client.open('GET', `${baseURL}/`);
client.withCredentials = true;
client.send(null);
});
});
});

0 comments on commit e3a4910

Please sign in to comment.