Skip to content

Commit

Permalink
#466@major: Adds support for Browser API.
Browse files Browse the repository at this point in the history
  • Loading branch information
capricorn86 committed Jan 10, 2024
1 parent 3a372de commit f245b8b
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 23 deletions.
1 change: 1 addition & 0 deletions packages/happy-dom/test/browser/BrowserFrame.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import DOMExceptionNameEnum from '../../src/exception/DOMExceptionNameEnum';
import BrowserNavigationCrossOriginPolicyEnum from '../../src/browser/enums/BrowserNavigationCrossOriginPolicyEnum';
import BrowserFrameFactory from '../../src/browser/utilities/BrowserFrameFactory';
import BrowserErrorCaptureEnum from '../../src/browser/enums/BrowserErrorCaptureEnum';
import Headers from '../../src/fetch/Headers';

describe('BrowserFrame', () => {
afterEach(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import DOMExceptionNameEnum from '../../../src/exception/DOMExceptionNameEnum';
import BrowserNavigationCrossOriginPolicyEnum from '../../../src/browser/enums/BrowserNavigationCrossOriginPolicyEnum';
import BrowserFrameFactory from '../../../src/browser/utilities/BrowserFrameFactory';
import BrowserErrorCaptureEnum from '../../../src/browser/enums/BrowserErrorCaptureEnum';
import Headers from '../../../src/fetch/Headers';

describe('DetachedBrowserFrame', () => {
afterEach(() => {
Expand Down
34 changes: 17 additions & 17 deletions packages/happy-dom/test/fetch/Fetch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3585,7 +3585,7 @@ describe('Fetch', () => {
expect(requestCount).toBe(1);
});

it('Revalidates cache with a "If-Modified-Since" request for a GET response with "Cache-Control" set to "max-age=0.002".', async () => {
it('Revalidates cache with a "If-Modified-Since" request for a GET response with "Cache-Control" set to "max-age=0.003".', async () => {
const window = new Window({ url: 'https://localhost:8080/' });
const url = 'https://localhost:8080/some/path';
const responseText = 'some text';
Expand All @@ -3612,7 +3612,7 @@ describe('Fetch', () => {
'last-modified',
'Mon, 11 Dec 2023 02:00:00 GMT',
'cache-control',
'max-age=0.002'
'max-age=0.003'
];

callback(response);
Expand All @@ -3632,7 +3632,7 @@ describe('Fetch', () => {
'content-length',
String(responseText.length),
'cache-control',
'max-age=0.002',
'max-age=0.003',
'last-modified',
'Mon, 11 Dec 2023 01:00:00 GMT'
];
Expand Down Expand Up @@ -3677,7 +3677,7 @@ describe('Fetch', () => {
expect(headers1).toEqual({
'content-type': 'text/html',
'content-length': String(responseText.length),
'cache-control': `max-age=0.002`,
'cache-control': `max-age=0.003`,
'last-modified': 'Mon, 11 Dec 2023 01:00:00 GMT'
});

Expand All @@ -3690,7 +3690,7 @@ describe('Fetch', () => {
expect(headers2).toEqual({
'content-type': 'text/html',
'content-length': String(responseText.length),
'Cache-Control': 'max-age=0.002',
'Cache-Control': 'max-age=0.003',
'Last-Modified': 'Mon, 11 Dec 2023 02:00:00 GMT'
});

Expand Down Expand Up @@ -3737,7 +3737,7 @@ describe('Fetch', () => {
]);
});

it('Updates cache after a failed revalidation with a "If-Modified-Since" request for a GET response with "Cache-Control" set to "max-age=0.002".', async () => {
it('Updates cache after a failed revalidation with a "If-Modified-Since" request for a GET response with "Cache-Control" set to "max-age=0.003".', async () => {
const window = new Window({ url: 'https://localhost:8080/' });
const url = '/some/path';
const responseText1 = 'some text';
Expand Down Expand Up @@ -3771,7 +3771,7 @@ describe('Fetch', () => {
'content-length',
String(responseText2.length),
'cache-control',
'max-age=0.002',
'max-age=0.003',
'last-modified',
'Mon, 11 Dec 2023 02:00:00 GMT'
];
Expand All @@ -3793,7 +3793,7 @@ describe('Fetch', () => {
'content-length',
String(responseText1.length),
'cache-control',
'max-age=0.002',
'max-age=0.004',
'last-modified',
'Mon, 11 Dec 2023 01:00:00 GMT'
];
Expand Down Expand Up @@ -3846,7 +3846,7 @@ describe('Fetch', () => {
expect(headers1).toEqual({
'content-type': 'text/html',
'content-length': String(responseText1.length),
'cache-control': `max-age=0.002`,
'cache-control': `max-age=0.004`,
'last-modified': 'Mon, 11 Dec 2023 01:00:00 GMT'
});

Expand All @@ -3859,7 +3859,7 @@ describe('Fetch', () => {
expect(headers2).toEqual({
'content-type': 'text/html',
'content-length': String(responseText2.length),
'cache-control': 'max-age=0.002',
'cache-control': 'max-age=0.003',
'last-modified': 'Mon, 11 Dec 2023 02:00:00 GMT'
});

Expand Down Expand Up @@ -3963,7 +3963,7 @@ describe('Fetch', () => {
'content-length',
String(responseText.length),
'cache-control',
'max-age=0.002',
'max-age=0.003',
'last-modified',
'Mon, 11 Dec 2023 01:00:00 GMT',
'etag',
Expand Down Expand Up @@ -4013,7 +4013,7 @@ describe('Fetch', () => {
expect(headers1).toEqual({
'content-type': 'text/html',
'content-length': String(responseText.length),
'cache-control': `max-age=0.002`,
'cache-control': `max-age=0.003`,
'last-modified': 'Mon, 11 Dec 2023 01:00:00 GMT',
etag: etag1
});
Expand All @@ -4027,7 +4027,7 @@ describe('Fetch', () => {
expect(headers2).toEqual({
'content-type': 'text/html',
'content-length': String(responseText.length),
'cache-control': `max-age=0.002`,
'cache-control': `max-age=0.003`,
'Last-Modified': 'Mon, 11 Dec 2023 02:00:00 GMT',
ETag: etag2
});
Expand Down Expand Up @@ -4111,7 +4111,7 @@ describe('Fetch', () => {
'content-length',
String(responseText2.length),
'cache-control',
'max-age=0.002',
'max-age=0.003',
'last-modified',
'Mon, 11 Dec 2023 02:00:00 GMT',
'etag',
Expand All @@ -4135,7 +4135,7 @@ describe('Fetch', () => {
'content-length',
String(responseText1.length),
'cache-control',
'max-age=0.002',
'max-age=0.003',
'last-modified',
'Mon, 11 Dec 2023 01:00:00 GMT',
'etag',
Expand Down Expand Up @@ -4182,7 +4182,7 @@ describe('Fetch', () => {
expect(headers1).toEqual({
'content-type': 'text/html',
'content-length': String(responseText1.length),
'cache-control': `max-age=0.002`,
'cache-control': `max-age=0.003`,
'last-modified': 'Mon, 11 Dec 2023 01:00:00 GMT',
etag: etag1
});
Expand All @@ -4196,7 +4196,7 @@ describe('Fetch', () => {
expect(headers2).toEqual({
'content-type': 'text/html',
'content-length': String(responseText2.length),
'cache-control': `max-age=0.002`,
'cache-control': `max-age=0.003`,
'last-modified': 'Mon, 11 Dec 2023 02:00:00 GMT',
etag: etag2
});
Expand Down
22 changes: 18 additions & 4 deletions packages/happy-dom/test/fetch/ResourceFetch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Browser from '../../src/browser/Browser.js';
import Fetch from '../../src/fetch/Fetch.js';
import SyncFetch from '../../src/fetch/SyncFetch.js';
import ISyncResponse from '../../src/fetch/types/ISyncResponse.js';
import DOMException from '../../src/exception/DOMException.js';

const URL = 'https://localhost:8080/base/';

Expand Down Expand Up @@ -52,7 +53,7 @@ describe('ResourceFetch', () => {
expect(test).toBe('test');
});

it('Handles error when resource is fetched asynchrounously.', () => {
it('Handles error when resource is fetched asynchrounously.', async () => {
vi.spyOn(Fetch.prototype, 'send').mockImplementation(async function () {
return <IResponse>{
ok: false,
Expand All @@ -61,7 +62,14 @@ describe('ResourceFetch', () => {
};
});

expect(resourceFetch.fetch('path/to/script/')).rejects.toEqual(
let error: Error | null = null;
try {
await resourceFetch.fetch('path/to/script/');
} catch (e) {
error = e;
}

expect(error).toEqual(
new DOMException(
`Failed to perform request to "${URL}path/to/script/". Status 404 Not Found.`
)
Expand Down Expand Up @@ -103,9 +111,15 @@ describe('ResourceFetch', () => {
};
});

expect(() => {
let error: Error | null = null;

try {
resourceFetch.fetchSync('path/to/script/');
}).toThrowError(
} catch (e) {
error = e;
}

expect(error).toEqual(
new DOMException(
`Failed to perform request to "${URL}path/to/script/". Status 404 Not Found.`
)
Expand Down
4 changes: 2 additions & 2 deletions packages/happy-dom/test/window/Window.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ describe('Window', () => {
expect(newWindow2.name).toBe('');
expect(newWindow2.location.href).toBe('https://localhost:8080/test/2/');

await new Promise((resolve) => setTimeout(resolve, 1));
await new Promise((resolve) => setTimeout(resolve, 2));

expect(newWindow2.document.body.innerHTML).toBe('Test');
});
Expand Down Expand Up @@ -343,7 +343,7 @@ describe('Window', () => {
expect(newWindow2.name).toBe('');
expect(newWindow2.location.href).toBe('https://localhost:8080/test/2/');

await new Promise((resolve) => setTimeout(resolve, 1));
await new Promise((resolve) => setTimeout(resolve, 2));

expect(newWindow2.document.body.innerHTML).toBe('Test');
});
Expand Down

0 comments on commit f245b8b

Please sign in to comment.