summaryrefslogtreecommitdiff
path: root/packages/integrations/netlify/test/functions/base64-response.test.js
blob: 6e59bd1922109b5d023a558d4ab31171b01369da (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
import { expect } from 'chai';
import { loadFixture, testIntegration } from './test-utils.js';
import netlifyAdapter from '../../dist/index.js';

describe('Base64 Responses', () => {
	/** @type {import('../../../astro/test/test-utils').Fixture} */
	let fixture;

	before(async () => {
		fixture = await loadFixture({
			root: new URL('./fixtures/base64-response/', import.meta.url).toString(),
			output: 'server',
			adapter: netlifyAdapter({
				dist: new URL('./fixtures/base64-response/dist/', import.meta.url),
				binaryMediaTypes: ['font/otf'],
			}),
			site: `http://example.com`,
			integrations: [testIntegration()],
		});
		await fixture.build();
	});

	it('Can return base64 encoded strings', async () => {
		const entryURL = new URL(
			'./fixtures/base64-response/.netlify/functions-internal/entry.mjs',
			import.meta.url
		);
		const { handler } = await import(entryURL);
		const resp = await handler({
			httpMethod: 'GET',
			headers: {},
			rawUrl: 'http://example.com/image',
			body: '{}',
			isBase64Encoded: false,
		});
		expect(resp.statusCode, 'successful response').to.equal(200);
		expect(resp.isBase64Encoded, 'includes isBase64Encoded flag').to.be.true;

		const buffer = Buffer.from(resp.body, 'base64');
		expect(buffer.toString(), 'decoded base64 string matches').to.equal('base64 test string');
	});

	it('Can define custom binaryMediaTypes', async () => {
		const entryURL = new URL(
			'./fixtures/base64-response/.netlify/functions-internal/entry.mjs',
			import.meta.url
		);
		const { handler } = await import(entryURL);
		const resp = await handler({
			httpMethod: 'GET',
			headers: {},
			rawUrl: 'http://example.com/font',
			body: '{}',
			isBase64Encoded: false,
		});
		expect(resp.statusCode, 'successful response').to.equal(200);
		expect(resp.isBase64Encoded, 'includes isBase64Encoded flag').to.be.true;

		const buffer = Buffer.from(resp.body, 'base64');
		expect(buffer.toString(), 'decoded base64 string matches').to.equal('base64 test font');
	});
});