Skip to content

Commit

Permalink
chore: [#1688] Adds unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
capricorn86 committed Jan 20, 2025
1 parent 25f98e0 commit 1d0a61e
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions packages/happy-dom/src/fetch/Fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,16 @@ export default class Fetch {
public async send(): Promise<Response> {
FetchRequestReferrerUtility.prepareRequest(new URL(this.#window.location.href), this.request);

const beforeRequestResponse = this.interceptor?.beforeAsyncRequest
? await this.interceptor.beforeAsyncRequest({
request: this.request,
window: this.#window
})
: undefined;
if (beforeRequestResponse instanceof Response) {
return beforeRequestResponse;
if (this.interceptor?.beforeAsyncRequest) {
const taskID = this.#browserFrame[PropertySymbol.asyncTaskManager].startTask();
const response = await this.interceptor.beforeAsyncRequest({
request: this.request,
window: this.#window
});
this.#browserFrame[PropertySymbol.asyncTaskManager].endTask(taskID);
if (response instanceof Response) {
return response;
}
}

FetchRequestValidationUtility.validateSchema(this.request);
Expand Down Expand Up @@ -149,7 +151,7 @@ export default class Fetch {
window: this.#window,
response: this.response,
request: this.request
})
})

Check warning on line 154 in packages/happy-dom/src/fetch/Fetch.ts

View workflow job for this annotation

GitHub Actions / build (20)

Replace `··` with `↹`

Check warning on line 154 in packages/happy-dom/src/fetch/Fetch.ts

View workflow job for this annotation

GitHub Actions / build (22)

Replace `··` with `↹`
: undefined;
return interceptedResponse instanceof Response ? interceptedResponse : this.response;
}
Expand Down Expand Up @@ -299,15 +301,23 @@ export default class Fetch {
return VirtualServerUtility.getNotFoundResponse(this.#window);
}

const taskID = this.#browserFrame[PropertySymbol.asyncTaskManager].startTask();
let buffer: Buffer;

try {
buffer = await FS.promises.readFile(filePath);
} catch (error) {
this.#browserFrame?.page?.console.error(
`${this.request.method} ${this.request.url} 404 (Not Found)`
);

this.#browserFrame[PropertySymbol.asyncTaskManager].endTask(taskID);

return VirtualServerUtility.getNotFoundResponse(this.#window);
}

this.#browserFrame[PropertySymbol.asyncTaskManager].endTask(taskID);

const body = new this.#window.ReadableStream({
start(controller) {
setTimeout(() => {
Expand Down Expand Up @@ -462,7 +472,7 @@ export default class Fetch {
window: this.#window,
response: await response,
request: this.request
})
})

Check warning on line 475 in packages/happy-dom/src/fetch/Fetch.ts

View workflow job for this annotation

GitHub Actions / build (20)

Replace `··` with `↹`

Check warning on line 475 in packages/happy-dom/src/fetch/Fetch.ts

View workflow job for this annotation

GitHub Actions / build (22)

Replace `··` with `↹`
: undefined;
this.#browserFrame[PropertySymbol.asyncTaskManager].endTask(taskID);
const returnResponse =
Expand Down

0 comments on commit 1d0a61e

Please sign in to comment.