-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
streaming.test.js
156 lines (133 loc) · 4.24 KB
/
streaming.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
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, streamAsyncIterator } from './test-utils.js';
describe('Streaming', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;
let decoder = new TextDecoder();
before(async () => {
fixture = await loadFixture({
root: './fixtures/streaming/',
adapter: testAdapter(),
output: 'server',
});
});
describe('Development', () => {
/** @type {import('./test-utils').DevServer} */
let devServer;
before(async () => {
devServer = await fixture.startDevServer();
});
after(async () => {
await devServer.stop();
});
it('Body is chunked', async () => {
let res = await fixture.fetch('/');
let chunks = [];
for await (const bytes of streamAsyncIterator(res.body)) {
let chunk = decoder.decode(bytes);
chunks.push(chunk);
}
assert.equal(chunks.length > 1, true);
});
it('Body of slots is chunked', async () => {
let res = await fixture.fetch('/slot');
let chunks = [];
for await (const bytes of streamAsyncIterator(res.body)) {
let chunk = decoder.decode(bytes);
chunks.push(chunk);
}
assert.equal(chunks.length, 3);
});
});
describe('Production', () => {
before(async () => {
await fixture.build();
});
it('Can get the full html body', 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($('header h1').length, 1);
assert.equal($('ul li').length, 10);
});
it('Body is chunked', async () => {
const app = await fixture.loadTestAdapterApp();
const request = new Request('http://example.com/');
const response = await app.render(request);
let chunks = [];
for await (const bytes of streamAsyncIterator(response.body)) {
let chunk = decoder.decode(bytes);
chunks.push(chunk);
}
assert.equal(chunks.length > 1, true);
});
// if the offshoot promise goes unhandled, this test will pass immediately but fail the test suite
it('Stays alive on failed component renders initiated by failed render templates', async () => {
const app = await fixture.loadTestAdapterApp();
const request = new Request('http://example.com/multiple-errors');
const response = await app.render(request);
assert.equal(response.status, 500);
const text = await response.text();
assert.equal(text, '');
});
});
});
describe('Streaming disabled', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;
before(async () => {
fixture = await loadFixture({
root: './fixtures/streaming/',
adapter: testAdapter(),
output: 'server',
server: {
streaming: false,
},
});
});
describe('Development', () => {
/** @type {import('./test-utils').DevServer} */
let devServer;
before(async () => {
devServer = await fixture.startDevServer();
});
after(async () => {
await devServer.stop();
});
it('Body is chunked', async () => {
let res = await fixture.fetch('/');
let chunks = [];
for await (const bytes of streamAsyncIterator(res.body)) {
let chunk = bytes.toString('utf-8');
chunks.push(chunk);
}
assert.equal(chunks.length > 1, true);
});
});
// TODO: find a different solution for the test-adapter,
// currently there's no way to resolve two different versions with one
// having streaming disabled
describe('Production', () => {
before(async () => {
await fixture.build();
});
it('Can get the full html body', async () => {
const app = await fixture.loadTestAdapterApp(false);
const request = new Request('http://example.com/');
const response = await app.render(request);
assert.equal(response.status, 200);
assert.equal(response.headers.get('content-type'), 'text/html');
assert.equal(response.headers.has('content-length'), true);
assert.equal(parseInt(response.headers.get('content-length')) > 0, true);
const html = await response.text();
const $ = cheerio.load(html);
assert.equal($('header h1').length, 1);
assert.equal($('ul li').length, 10);
});
});
});