From dc7a19667befd5ece02dd31cf5d3a03bc011691e Mon Sep 17 00:00:00 2001 From: David Ortner Date: Mon, 20 Jan 2025 21:00:42 +0100 Subject: [PATCH] chore: [#1688] Adds unit tests --- packages/happy-dom/src/fetch/Fetch.ts | 6 +++++- packages/happy-dom/src/fetch/SyncFetch.ts | 10 ++++++---- .../src/fetch/utilities/VirtualServerUtility.ts | 6 +----- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/packages/happy-dom/src/fetch/Fetch.ts b/packages/happy-dom/src/fetch/Fetch.ts index 801d1fa6..13934060 100644 --- a/packages/happy-dom/src/fetch/Fetch.ts +++ b/packages/happy-dom/src/fetch/Fetch.ts @@ -9,6 +9,7 @@ import HTTPS from 'https'; import Zlib from 'zlib'; import URL from '../url/URL.js'; import FS from 'fs'; +import Path from 'path'; import { Socket } from 'net'; import Stream from 'stream'; import DataURIParser from './data-uri/DataURIParser.js'; @@ -305,7 +306,10 @@ export default class Fetch { let buffer: Buffer; try { - buffer = await FS.promises.readFile(filePath); + const stat = await FS.promises.stat(filePath); + buffer = await FS.promises.readFile( + stat.isDirectory() ? Path.join(filePath, 'index.html') : filePath + ); } catch (error) { this.#browserFrame?.page?.console.error( `${this.request.method} ${this.request.url} 404 (Not Found)` diff --git a/packages/happy-dom/src/fetch/SyncFetch.ts b/packages/happy-dom/src/fetch/SyncFetch.ts index c271e0ed..4f8016ad 100644 --- a/packages/happy-dom/src/fetch/SyncFetch.ts +++ b/packages/happy-dom/src/fetch/SyncFetch.ts @@ -4,6 +4,7 @@ import IRequestInfo from './types/IRequestInfo.js'; import DOMExceptionNameEnum from '../exception/DOMExceptionNameEnum.js'; import URL from '../url/URL.js'; import FS from 'fs'; +import Path from 'path'; import Request from './Request.js'; import IBrowserFrame from '../browser/types/IBrowserFrame.js'; import BrowserWindow from '../window/BrowserWindow.js'; @@ -103,7 +104,7 @@ export default class SyncFetch { ? this.interceptor.beforeSyncRequest({ request: this.request, window: this.#window - }) + }) : undefined; if (typeof beforeRequestResponse === 'object') { return beforeRequestResponse; @@ -140,7 +141,7 @@ export default class SyncFetch { window: this.#window, response, request: this.request - }) + }) : undefined; return typeof interceptedResponse === 'object' ? interceptedResponse : response; } @@ -287,7 +288,8 @@ export default class SyncFetch { let buffer: Buffer; try { - buffer = FS.readFileSync(filePath); + const stat = FS.statSync(filePath); + buffer = FS.readFileSync(stat.isDirectory() ? Path.join(filePath, 'index.html') : filePath); } catch { this.#browserFrame?.page?.console.error( `${this.request.method} ${this.request.url} 404 (Not Found)` @@ -491,7 +493,7 @@ export default class SyncFetch { window: this.#window, response: redirectedResponse, request: this.request - }) + }) : undefined; const returnResponse = typeof interceptedResponse === 'object' ? interceptedResponse : redirectedResponse; diff --git a/packages/happy-dom/src/fetch/utilities/VirtualServerUtility.ts b/packages/happy-dom/src/fetch/utilities/VirtualServerUtility.ts index 114f43a0..c01008b9 100644 --- a/packages/happy-dom/src/fetch/utilities/VirtualServerUtility.ts +++ b/packages/happy-dom/src/fetch/utilities/VirtualServerUtility.ts @@ -44,11 +44,7 @@ export default class VirtualServerUtility { } } if (baseURL) { - const basePath = requestURL.slice(baseURL.href.length).split('?')[0].split('#')[0]; - const parts = basePath.split('/'); - const isDirectory = !parts[parts.length - 1].includes('.'); - const path = isDirectory ? basePath + '/index.html' : basePath; - + const path = requestURL.slice(baseURL.href.length).split('?')[0].split('#')[0]; return Path.join(Path.resolve(virtualServer.directory), path); } }