summaryrefslogtreecommitdiff
path: root/packages/integrations/deno/test/basics.test.ts
blob: 48a35d7856862ebde2bed6fd50193273638f2cae (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import { StartServerCallback, runBuildAndStartApp, defaultTestPermissions } from './helpers.ts';
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';

async function startApp(cb: StartServerCallback) {
	await runBuildAndStartApp('./fixtures/basics/', cb);
}

// 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,
	async fn() {
		await startApp(async (baseUrl: URL) => {
			const resp = await fetch(baseUrl);
			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');
		});
	},
	sanitizeResources: false,
	sanitizeOps: false,
});

Deno.test({
	name: 'Custom 404',
	permissions: defaultTestPermissions,
	async fn() {
		await startApp(async (baseUrl: URL) => {
			const resp = await fetch(new URL('this-does-not-exist', baseUrl));
			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');
		});
	},
	sanitizeResources: false,
	sanitizeOps: false,
});

Deno.test({
	name: 'Loads style assets',
	permissions: defaultTestPermissions,
	async fn() {
		await startApp(async (baseUrl: URL) => {
			let resp = await fetch(baseUrl);
			const html = await resp.text();

			const doc = new DOMParser().parseFromString(html, `text/html`);
			const link = doc!.querySelector('link');
			const href = link!.getAttribute('href');

			resp = await fetch(new URL(href!, baseUrl));
			assertEquals(resp.status, 200);
			const ct = resp.headers.get('content-type');
			assertEquals(ct, 'text/css; charset=UTF-8');
			await resp.body!.cancel();
		});
	},
	sanitizeResources: false,
	sanitizeOps: false,
});

Deno.test({
	name: 'Correctly loads run-time env variables',
	permissions: defaultTestPermissions,
	async fn() {
		await startApp(async (baseUrl: URL) => {
			const resp = await fetch(baseUrl);
			const html = await resp.text();

			const doc = new DOMParser().parseFromString(html, `text/html`);
			const p = doc!.querySelector('p#env-value');
			assertEquals(p!.innerText, varContent);
		});
	},
	sanitizeResources: false,
	sanitizeOps: false,
});

Deno.test({
	name: 'Works with Markdown',
	permissions: defaultTestPermissions,
	async fn() {
		await startApp(async (baseUrl: URL) => {
			const resp = await fetch(new URL('markdown', baseUrl));
			const html = await resp.text();

			const doc = new DOMParser().parseFromString(html, `text/html`);
			const h1 = doc!.querySelector('h1');
			assertEquals(h1!.innerText, 'Heading from Markdown');
		});
	},
	sanitizeResources: false,
	sanitizeOps: false,
});

Deno.test({
	name: 'Works with MDX',
	permissions: defaultTestPermissions,
	async fn() {
		await startApp(async (baseUrl: URL) => {
			const resp = await fetch(new URL('mdx', baseUrl));
			const html = await resp.text();

			const doc = new DOMParser().parseFromString(html, `text/html`);
			const h1 = doc!.querySelector('h1');
			assertEquals(h1!.innerText, 'Heading from MDX');
		});
	},
	sanitizeResources: false,
	sanitizeOps: false,
});

Deno.test({
	name: 'Astro.cookies',
	permissions: defaultTestPermissions,
	async fn() {
		await startApp(async (baseUrl: URL) => {
			const url = new URL('/admin', baseUrl);
			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=/');
		});
	},
	sanitizeResources: false,
	sanitizeOps: false,
});

Deno.test({
	name: 'perendering',
	permissions: defaultTestPermissions,
	async fn() {
		await startApp(async (baseUrl: URL) => {
			const resp = await fetch(new URL('/prerender', baseUrl));
			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');
		});
	},
	sanitizeResources: false,
	sanitizeOps: false,
});