diff options
author | 2022-06-15 19:49:09 +0000 | |
---|---|---|
committer | 2022-06-15 19:49:09 +0000 | |
commit | 0ddcef2043e3c2f65aaeec7a969c374c053e22f3 (patch) | |
tree | 627b580dd88d3ae1ad55602c7a57081c4f34a56a /packages/integrations/netlify/test | |
parent | 8ed924d2ed21a6e2e6df9345b7315d05da866b54 (diff) | |
download | astro-0ddcef2043e3c2f65aaeec7a969c374c053e22f3.tar.gz astro-0ddcef2043e3c2f65aaeec7a969c374c053e22f3.tar.zst astro-0ddcef2043e3c2f65aaeec7a969c374c053e22f3.zip |
Adds support base64 encoding in Netlify Functions (#3592)
* Adding support for base64 encoded responses in Netlify Functions
* chore: add changeset
* removing the regex check for a more simple header-based check
* nit: cleaning up the readme a bit
Diffstat (limited to 'packages/integrations/netlify/test')
3 files changed, 86 insertions, 0 deletions
diff --git a/packages/integrations/netlify/test/functions/base64-response.test.js b/packages/integrations/netlify/test/functions/base64-response.test.js new file mode 100644 index 000000000..10d43b046 --- /dev/null +++ b/packages/integrations/netlify/test/functions/base64-response.test.js @@ -0,0 +1,64 @@ +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(), + experimental: { + ssr: true, + }, + 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'); + }); +}); diff --git a/packages/integrations/netlify/test/functions/fixtures/base64-response/src/pages/font.js b/packages/integrations/netlify/test/functions/fixtures/base64-response/src/pages/font.js new file mode 100644 index 000000000..3ec4c8364 --- /dev/null +++ b/packages/integrations/netlify/test/functions/fixtures/base64-response/src/pages/font.js @@ -0,0 +1,11 @@ + +export function get() { + const buffer = Buffer.from('base64 test font', 'utf-8') + + return new Response(buffer, { + status: 200, + headers: { + 'Content-Type': 'font/otf' + } + }); +} diff --git a/packages/integrations/netlify/test/functions/fixtures/base64-response/src/pages/image.js b/packages/integrations/netlify/test/functions/fixtures/base64-response/src/pages/image.js new file mode 100644 index 000000000..ca3b4d9d3 --- /dev/null +++ b/packages/integrations/netlify/test/functions/fixtures/base64-response/src/pages/image.js @@ -0,0 +1,11 @@ + +export function get() { + const buffer = Buffer.from('base64 test string', 'utf-8') + + return new Response(buffer, { + status: 200, + headers: { + 'content-type': 'image/jpeg;foo=foo' + } + }); +} |