2023-07-03 07:59:43 -05:00
|
|
|
/* Deno types consider DOM elements nullable */
|
|
|
|
/* eslint-disable @typescript-eslint/no-unnecessary-type-assertion */
|
2022-10-07 08:38:38 -05:00
|
|
|
import { DOMParser } from 'https://deno.land/x/deno_dom@v0.1.35-alpha/deno-dom-wasm.ts';
|
|
|
|
import { assert, assertEquals } from 'https://deno.land/std@0.158.0/testing/asserts.ts';
|
2023-06-05 02:05:16 -05:00
|
|
|
import { runBuildAndStartApp, defaultTestPermissions } from './helpers.ts';
|
2022-10-07 08:36:24 -05:00
|
|
|
|
|
|
|
// this needs to be here and not in the specific test case, because
|
|
|
|
// the variables are loaded in the global scope of the built server
|
|
|
|
// module, which is only executed once upon the first load
|
|
|
|
const varContent = 'this is a value stored in env variable';
|
|
|
|
Deno.env.set('SOME_VARIABLE', varContent);
|
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: 'Basics',
|
|
|
|
permissions: defaultTestPermissions,
|
2023-06-05 02:05:16 -05:00
|
|
|
sanitizeResources: false,
|
|
|
|
sanitizeOps: false,
|
|
|
|
async fn(t) {
|
|
|
|
const app = await runBuildAndStartApp('./fixtures/basics/');
|
|
|
|
|
|
|
|
await t.step('Works', async () => {
|
|
|
|
const resp = await fetch(app.url);
|
2022-10-07 08:36:24 -05:00
|
|
|
assertEquals(resp.status, 200);
|
|
|
|
|
|
|
|
const html = await resp.text();
|
|
|
|
assert(html);
|
|
|
|
|
|
|
|
const doc = new DOMParser().parseFromString(html, `text/html`);
|
|
|
|
const div = doc!.querySelector('#react');
|
|
|
|
|
|
|
|
assert(div, 'div exists');
|
|
|
|
});
|
|
|
|
|
2023-06-05 02:05:16 -05:00
|
|
|
await t.step('Custom 404', async () => {
|
|
|
|
const resp = await fetch(new URL('this-does-not-exist', app.url));
|
2022-10-07 08:36:24 -05:00
|
|
|
assertEquals(resp.status, 404);
|
|
|
|
|
|
|
|
const html = await resp.text();
|
|
|
|
assert(html);
|
|
|
|
|
|
|
|
const doc = new DOMParser().parseFromString(html, `text/html`);
|
|
|
|
const header = doc!.querySelector('#custom-404');
|
|
|
|
assert(header, 'displays custom 404');
|
|
|
|
});
|
|
|
|
|
2023-06-05 02:05:16 -05:00
|
|
|
await t.step('Loads style assets', async () => {
|
|
|
|
let resp = await fetch(app.url);
|
2022-10-07 08:36:24 -05:00
|
|
|
const html = await resp.text();
|
|
|
|
|
|
|
|
const doc = new DOMParser().parseFromString(html, `text/html`);
|
|
|
|
const link = doc!.querySelector('link');
|
|
|
|
const href = link!.getAttribute('href');
|
|
|
|
|
2023-06-05 02:05:16 -05:00
|
|
|
resp = await fetch(new URL(href!, app.url));
|
2022-10-07 08:36:24 -05:00
|
|
|
assertEquals(resp.status, 200);
|
|
|
|
const ct = resp.headers.get('content-type');
|
2023-02-17 13:19:19 -05:00
|
|
|
assertEquals(ct, 'text/css; charset=UTF-8');
|
2022-10-07 08:36:24 -05:00
|
|
|
await resp.body!.cancel();
|
|
|
|
});
|
|
|
|
|
2023-06-05 02:05:16 -05:00
|
|
|
await t.step('Correctly loads run-time env variables', async () => {
|
|
|
|
const resp = await fetch(app.url);
|
2022-10-07 08:36:24 -05:00
|
|
|
const html = await resp.text();
|
|
|
|
|
|
|
|
const doc = new DOMParser().parseFromString(html, `text/html`);
|
|
|
|
const p = doc!.querySelector('p#env-value');
|
|
|
|
assertEquals(p!.innerText, varContent);
|
|
|
|
});
|
|
|
|
|
2023-06-05 02:05:16 -05:00
|
|
|
await t.step('Works with Markdown', async () => {
|
|
|
|
const resp = await fetch(new URL('markdown', app.url));
|
2022-10-07 08:36:24 -05:00
|
|
|
const html = await resp.text();
|
|
|
|
|
|
|
|
const doc = new DOMParser().parseFromString(html, `text/html`);
|
|
|
|
const h1 = doc!.querySelector('h1');
|
|
|
|
assertEquals(h1!.innerText, 'Heading from Markdown');
|
|
|
|
});
|
|
|
|
|
2023-06-05 02:05:16 -05:00
|
|
|
await t.step('Works with MDX', async () => {
|
|
|
|
const resp = await fetch(new URL('mdx', app.url));
|
2022-10-07 08:36:24 -05:00
|
|
|
const html = await resp.text();
|
|
|
|
|
|
|
|
const doc = new DOMParser().parseFromString(html, `text/html`);
|
|
|
|
const h1 = doc!.querySelector('h1');
|
|
|
|
assertEquals(h1!.innerText, 'Heading from MDX');
|
|
|
|
});
|
2022-10-12 07:31:55 -05:00
|
|
|
|
2023-06-05 02:05:16 -05:00
|
|
|
await t.step('Astro.cookies', async () => {
|
|
|
|
const url = new URL('/admin', app.url);
|
2022-10-12 07:31:55 -05:00
|
|
|
const resp = await fetch(url, { redirect: 'manual' });
|
|
|
|
assertEquals(resp.status, 302);
|
|
|
|
|
|
|
|
const headers = resp.headers;
|
|
|
|
assertEquals(headers.get('set-cookie'), 'logged-in=false; Max-Age=77760000; Path=/');
|
|
|
|
});
|
2023-02-17 13:19:19 -05:00
|
|
|
|
2023-06-05 02:05:16 -05:00
|
|
|
await t.step('perendering', async () => {
|
|
|
|
const resp = await fetch(new URL('/prerender', app.url));
|
2023-02-17 13:19:19 -05:00
|
|
|
assertEquals(resp.status, 200);
|
|
|
|
|
|
|
|
const html = await resp.text();
|
|
|
|
assert(html);
|
|
|
|
|
|
|
|
const doc = new DOMParser().parseFromString(html, `text/html`);
|
|
|
|
const h1 = doc!.querySelector('h1');
|
|
|
|
assertEquals(h1!.innerText, 'test');
|
|
|
|
});
|
2023-06-05 02:05:16 -05:00
|
|
|
|
2023-06-15 09:20:35 -05:00
|
|
|
await t.step('node compatibility', async () => {
|
|
|
|
const resp = await fetch(new URL('/nodecompat', app.url));
|
|
|
|
assertEquals(resp.status, 200);
|
|
|
|
await resp.text();
|
|
|
|
});
|
|
|
|
|
2023-06-05 02:05:16 -05:00
|
|
|
app.stop();
|
2023-02-17 13:19:19 -05:00
|
|
|
},
|
|
|
|
});
|