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 1d0a61e commit dc7a196
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
6 changes: 5 additions & 1 deletion packages/happy-dom/src/fetch/Fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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)`
Expand Down
10 changes: 6 additions & 4 deletions packages/happy-dom/src/fetch/SyncFetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -103,7 +104,7 @@ export default class SyncFetch {
? this.interceptor.beforeSyncRequest({
request: this.request,
window: this.#window
})
})

Check warning on line 107 in packages/happy-dom/src/fetch/SyncFetch.ts

View workflow job for this annotation

GitHub Actions / build (18)

Replace `··` with `↹`

Check warning on line 107 in packages/happy-dom/src/fetch/SyncFetch.ts

View workflow job for this annotation

GitHub Actions / build (20)

Replace `··` with `↹`

Check warning on line 107 in packages/happy-dom/src/fetch/SyncFetch.ts

View workflow job for this annotation

GitHub Actions / build (22)

Replace `··` with `↹`
: undefined;
if (typeof beforeRequestResponse === 'object') {
return beforeRequestResponse;
Expand Down Expand Up @@ -140,7 +141,7 @@ export default class SyncFetch {
window: this.#window,
response,
request: this.request
})
})

Check warning on line 144 in packages/happy-dom/src/fetch/SyncFetch.ts

View workflow job for this annotation

GitHub Actions / build (18)

Replace `··` with `↹`

Check warning on line 144 in packages/happy-dom/src/fetch/SyncFetch.ts

View workflow job for this annotation

GitHub Actions / build (20)

Replace `··` with `↹`

Check warning on line 144 in packages/happy-dom/src/fetch/SyncFetch.ts

View workflow job for this annotation

GitHub Actions / build (22)

Replace `··` with `↹`
: undefined;
return typeof interceptedResponse === 'object' ? interceptedResponse : response;
}
Expand Down Expand Up @@ -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)`
Expand Down Expand Up @@ -491,7 +493,7 @@ export default class SyncFetch {
window: this.#window,
response: redirectedResponse,
request: this.request
})
})

Check warning on line 496 in packages/happy-dom/src/fetch/SyncFetch.ts

View workflow job for this annotation

GitHub Actions / build (18)

Replace `··` with `↹`

Check warning on line 496 in packages/happy-dom/src/fetch/SyncFetch.ts

View workflow job for this annotation

GitHub Actions / build (20)

Replace `··` with `↹`

Check warning on line 496 in packages/happy-dom/src/fetch/SyncFetch.ts

View workflow job for this annotation

GitHub Actions / build (22)

Replace `··` with `↹`
: undefined;
const returnResponse =
typeof interceptedResponse === 'object' ? interceptedResponse : redirectedResponse;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down

0 comments on commit dc7a196

Please sign in to comment.