Skip to content

Commit 94f0707

Browse files
gbkwiattgbkwiatt
authored andcommitted
using local images for test instead of urls and fetch
1 parent e061fe9 commit 94f0707

File tree

4 files changed

+13
-33
lines changed

4 files changed

+13
-33
lines changed
Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
1+
import * as fs from 'fs';
2+
13
export async function GET({ params }) {
24
const slug = params.slug.split('/');
35
const extension = slug[0].split('.').pop();
4-
const examples = {
5-
png: 'https://repository-images.githubusercontent.com/354583933/72c58c80-9727-11eb-98b2-f352fded32b9',
6-
jpg: 'https://upload.wikimedia.org/wikipedia/commons/b/b2/JPEG_compression_Example.jpg'
7-
};
8-
const response = await fetch(examples[extension] || examples.png);
9-
const blob = await response.blob();
10-
const buffer = Buffer.from(await blob.arrayBuffer());
6+
7+
const file = fs.readFileSync(`./static/image.${extension}`);
8+
119
return {
1210
status: 200,
1311
headers: {
14-
'Content-Type': response.headers.get('content-type')
12+
'Content-Type': 'image/' + extension
1513
},
16-
body: buffer
14+
body: file
1715
};
1816
}
345 KB
Loading
5.32 KB
Loading

packages/kit/test/prerendering/basics/test/test.js

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import * as fs from 'fs';
22
import { fileURLToPath } from 'url';
33
import { test } from 'uvu';
44
import * as assert from 'uvu/assert';
5-
import fetch from 'node-fetch';
65

76
const build = fileURLToPath(new URL('../build', import.meta.url));
87

@@ -12,12 +11,6 @@ const read = (file) => fs.readFileSync(`${build}/${file}`, 'utf-8');
1211
/** @param {string} file */
1312
const readNoUtf = (file) => fs.readFileSync(`${build}/${file}`);
1413

15-
async function bufferFromUrl(url) {
16-
const response = await fetch(url);
17-
const blob = await response.blob();
18-
return Buffer.from(await blob.arrayBuffer());
19-
}
20-
2114
test('prerenders /', () => {
2215
const content = read('index.html');
2316
assert.ok(content.includes('<h1>hello</h1>'));
@@ -152,28 +145,17 @@ test('targets the data-sveltekit-hydrate parent node', () => {
152145
});
153146

154147
test('check binary data not corrupted - jpg', async () => {
155-
const url = 'https://upload.wikimedia.org/wikipedia/commons/b/b2/JPEG_compression_Example.jpg';
156-
157-
const originalBuffer = await bufferFromUrl(url);
158-
159-
const content = readNoUtf('fetch-image/image.jpg');
160-
const newBuffer = Buffer.from(content, 'binary');
161-
162-
const compare = Buffer.compare(originalBuffer, newBuffer);
148+
const newFile = readNoUtf('fetch-image/image.jpg');
149+
const orgFile = readNoUtf('image.jpg');
150+
const compare = Buffer.compare(newFile, orgFile);
163151

164152
assert.ok(compare === 0);
165153
});
166154

167155
test('check binary files not corrupted - png', async () => {
168-
const url =
169-
'https://repository-images.githubusercontent.com/354583933/72c58c80-9727-11eb-98b2-f352fded32b9';
170-
171-
const originalBuffer = await bufferFromUrl(url);
172-
173-
const content = readNoUtf('fetch-image/image.png');
174-
const newBuffer = Buffer.from(content, 'binary');
175-
176-
const compare = Buffer.compare(originalBuffer, newBuffer);
156+
const newFile = readNoUtf('fetch-image/image.png');
157+
const orgFile = readNoUtf('image.png');
158+
const compare = Buffer.compare(newFile, orgFile);
177159

178160
assert.ok(compare === 0);
179161
});

0 commit comments

Comments
 (0)