Skip to content

Commit

Permalink
Fix FakeXMLHttpRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
falsandtru committed Sep 8, 2023
1 parent 6ab9ffa commit 152a88e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 22 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 3.40.3

- Fix FakeXMLHttpRequest.

## 3.40.2

- Fix URL processing.
Expand Down
49 changes: 27 additions & 22 deletions src/lib/xhr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export class FakeXMLHttpRequest extends XMLHttpRequest {
AtomicPromise.resolve(response)
.then(
response => {
if (xhr.readyState === 4) return;
Object.defineProperties(xhr, {
responseURL: {
value: url,
Expand All @@ -17,10 +18,11 @@ export class FakeXMLHttpRequest extends XMLHttpRequest {
xhr.send();
},
reason => {
if (xhr.readyState === 4) return;
const response = reason instanceof Response
? reason
: new Response(null, xhr);
Object.defineProperties(this, {
Object.defineProperties(xhr, {
responseURL: {
value: url,
},
Expand All @@ -43,48 +45,51 @@ export class FakeXMLHttpRequest extends XMLHttpRequest {
this.responseType = 'document';
}
public override send(_?: Document | XMLHttpRequestBodyInit | null | undefined): void {
let state = 3;
if (this.readyState === 4) return;
Object.defineProperties(this, {
readyState: {
get: () => state,
configurable: true,
value: 3,
},
});
this.dispatchEvent(new ProgressEvent('loadstart'));
setTimeout(() => {
if (this.readyState === 4) return;
Object.defineProperties(this, {
status: {
value: 200,
},
statusText: {
value: 'OK',
},
response: {
get: () =>
this.responseType === 'document'
? this.responseXML
: this.responseText,
},
});
this.dispatchEvent(new ProgressEvent('loadstart'));
state = 4;
this.dispatchEvent(new ProgressEvent('loadend'));
this.dispatchEvent(new ProgressEvent('load'));
});
}
public override abort(): void {
setTimeout(() => {
Object.defineProperties(this, {
readyState: {
value: 4,
},
status: {
value: 400,
value: 200,
},
statusText: {
value: 'Bad Request',
value: 'OK',
},
});
this.dispatchEvent(new ProgressEvent('abort'));
this.dispatchEvent(new ProgressEvent('loadend'));
this.dispatchEvent(new ProgressEvent('load'));
});
}
public override abort(): void {
if (this.readyState === 4) return;
Object.defineProperties(this, {
readyState: {
value: 4,
},
status: {
value: 400,
},
statusText: {
value: 'Bad Request',
},
});
this.dispatchEvent(new ProgressEvent('abort'));
}
public override getResponseHeader(name: string): string | null {
switch (name.toLowerCase()) {
Expand Down

0 comments on commit 152a88e

Please sign in to comment.