forked from withastro/astro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient-address.test.js
135 lines (115 loc) · 3.39 KB
/
client-address.test.js
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
import assert from 'node:assert/strict';
import { after, before, describe, it } from 'node:test';
import * as cheerio from 'cheerio';
import testAdapter from './test-adapter.js';
import { loadFixture } from './test-utils.js';
describe('Astro.clientAddress', () => {
describe('SSR', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;
before(async () => {
fixture = await loadFixture({
root: './fixtures/client-address/',
output: 'server',
adapter: testAdapter(),
});
});
describe('Production', () => {
before(async () => {
await fixture.build();
});
it('Can get the address', async () => {
const app = await fixture.loadTestAdapterApp();
const request = new Request('http://example.com/');
const response = await app.render(request);
const html = await response.text();
const $ = cheerio.load(html);
assert.equal($('#address').text(), '0.0.0.0');
});
it('app.render can provide the address', async () => {
const app = await fixture.loadTestAdapterApp();
const request = new Request('http://example.com/');
const response = await app.render(request, { clientAddress: '1.1.1.1' });
const html = await response.text();
const $ = cheerio.load(html);
assert.equal($('#address').text(), '1.1.1.1');
});
});
describe('Development', () => {
/** @type {import('./test-utils').DevServer} */
let devServer;
before(async () => {
devServer = await fixture.startDevServer();
});
after(async () => {
await devServer.stop();
});
it('Gets the address', async () => {
let res = await fixture.fetch('/');
assert.equal(res.status, 200);
let html = await res.text();
let $ = cheerio.load(html);
let address = $('#address');
// Just checking that something is here. Not specifying address as it
// might differ per machine.
assert.equal(address.length > 0, true);
});
});
});
describe('SSR adapter not implemented', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;
before(async () => {
fixture = await loadFixture({
root: './fixtures/client-address/',
output: 'server',
adapter: testAdapter({ provideAddress: false }),
});
await fixture.build();
});
it('Gets an error message', async () => {
const app = await fixture.loadTestAdapterApp();
const request = new Request('http://example.com/');
const response = await app.render(request);
assert.equal(response.status, 500);
});
});
describe('SSG', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;
before(async () => {
fixture = await loadFixture({
root: './fixtures/client-address/',
output: 'static',
});
});
describe('Build', () => {
it('throws during generation', async () => {
try {
await fixture.build();
assert.equal(false, true, 'Build should not have completed');
} catch (err) {
assert.match(
err.message,
/Astro\.clientAddress/,
'Error message mentions Astro.clientAddress',
);
}
});
});
describe('Development', () => {
/** @type {import('./test-utils').DevServer} */
let devServer;
before(async () => {
devServer = await fixture.startDevServer();
});
after(async () => {
await devServer.stop();
});
it('is not accessible', async () => {
let res = await fixture.fetch('/');
assert.equal(res.status, 500);
});
});
});
});