2022-07-01 10:47:48 -05:00
|
|
|
import { expect } from 'chai';
|
|
|
|
import * as cheerio from 'cheerio';
|
|
|
|
import { loadFixture } from './test-utils.js';
|
|
|
|
import testAdapter from '../../../astro/test/test-adapter.js';
|
|
|
|
|
2022-09-01 09:58:57 -05:00
|
|
|
describe('SSR images - build', async function () {
|
2022-07-01 10:47:48 -05:00
|
|
|
let fixture;
|
|
|
|
|
|
|
|
before(async () => {
|
|
|
|
fixture = await loadFixture({
|
|
|
|
root: './fixtures/basic-image/',
|
2022-07-22 18:01:56 -05:00
|
|
|
adapter: testAdapter({ streaming: false }),
|
2022-08-30 16:12:45 -05:00
|
|
|
output: 'server',
|
2022-07-01 10:47:48 -05:00
|
|
|
});
|
|
|
|
await fixture.build();
|
|
|
|
});
|
2022-09-01 10:01:07 -05:00
|
|
|
|
2022-09-01 09:58:57 -05:00
|
|
|
[
|
|
|
|
{
|
|
|
|
title: 'Local images',
|
|
|
|
id: '#social-jpg',
|
|
|
|
url: '/_image',
|
2023-01-09 11:01:33 -05:00
|
|
|
query: { f: 'jpg', w: '506', h: '253', href: /^\/_astro\/social.\w{8}.jpg/ },
|
2022-09-01 09:58:57 -05:00
|
|
|
},
|
2022-09-01 15:31:53 -05:00
|
|
|
{
|
|
|
|
title: 'Filename with spaces',
|
|
|
|
id: '#spaces',
|
|
|
|
url: '/_image',
|
2023-01-09 11:01:33 -05:00
|
|
|
query: { f: 'webp', w: '768', h: '414', href: /^\/_astro\/introducing astro.\w{8}.jpg/ },
|
2022-09-01 15:31:53 -05:00
|
|
|
},
|
2023-02-15 03:21:41 -05:00
|
|
|
{
|
|
|
|
title: 'SVG image',
|
|
|
|
id: '#logo-svg',
|
|
|
|
url: '/_image',
|
|
|
|
query: { f: 'svg', w: '192', h: '256', href: /^\/_astro\/logo.\w{8}.svg/ },
|
|
|
|
},
|
2022-09-01 09:58:57 -05:00
|
|
|
{
|
|
|
|
title: 'Inline imports',
|
|
|
|
id: '#inline',
|
|
|
|
url: '/_image',
|
2023-01-09 11:01:33 -05:00
|
|
|
query: { f: 'jpg', w: '506', h: '253', href: /^\/_astro\/social.\w{8}.jpg/ },
|
2022-09-01 09:58:57 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Remote images',
|
|
|
|
id: '#google',
|
|
|
|
url: '/_image',
|
|
|
|
query: {
|
|
|
|
f: 'webp',
|
|
|
|
w: '544',
|
|
|
|
h: '184',
|
2022-09-01 10:01:07 -05:00
|
|
|
href: 'https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png',
|
|
|
|
},
|
2022-09-01 09:58:57 -05:00
|
|
|
},
|
2022-09-07 08:56:07 -05:00
|
|
|
{
|
|
|
|
title: 'Remote without file extension',
|
|
|
|
id: '#ipsum',
|
|
|
|
url: '/_image',
|
|
|
|
query: {
|
|
|
|
w: '200',
|
|
|
|
h: '300',
|
2022-09-22 14:48:14 -05:00
|
|
|
href: 'https://dummyimage.com/200x300',
|
2022-09-07 08:56:07 -05:00
|
|
|
},
|
|
|
|
},
|
2022-09-01 09:58:57 -05:00
|
|
|
{
|
|
|
|
title: 'Remote images with search',
|
|
|
|
id: '#query',
|
|
|
|
url: '/_image',
|
|
|
|
query: {
|
|
|
|
f: 'webp',
|
|
|
|
w: '544',
|
|
|
|
h: '184',
|
2022-09-01 10:01:07 -05:00
|
|
|
href: 'https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png?token=abc',
|
|
|
|
},
|
2022-09-01 09:58:57 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Public images',
|
|
|
|
id: '#hero',
|
|
|
|
url: '/_image',
|
2022-09-01 10:01:07 -05:00
|
|
|
query: { f: 'webp', w: '768', h: '414', href: '/hero.jpg' },
|
|
|
|
},
|
2022-09-07 12:22:11 -05:00
|
|
|
{
|
|
|
|
title: 'Background color',
|
|
|
|
id: '#bg-color',
|
|
|
|
url: '/_image',
|
|
|
|
query: {
|
|
|
|
f: 'jpeg',
|
|
|
|
w: '544',
|
|
|
|
h: '184',
|
|
|
|
bg: '#333333',
|
|
|
|
href: 'https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png',
|
|
|
|
},
|
|
|
|
},
|
2022-09-01 09:58:57 -05:00
|
|
|
].forEach(({ title, id, url, query }) => {
|
|
|
|
it(title, async () => {
|
2022-08-30 16:09:44 -05:00
|
|
|
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);
|
|
|
|
|
2022-09-01 09:58:57 -05:00
|
|
|
const image = $(id);
|
2022-08-30 16:09:44 -05:00
|
|
|
|
|
|
|
const src = image.attr('src');
|
|
|
|
const [route, params] = src.split('?');
|
|
|
|
|
2022-09-01 09:58:57 -05:00
|
|
|
expect(route).to.equal(url);
|
2022-08-30 16:09:44 -05:00
|
|
|
|
|
|
|
const searchParams = new URLSearchParams(params);
|
|
|
|
|
2022-09-01 09:58:57 -05:00
|
|
|
for (const [key, value] of Object.entries(query)) {
|
|
|
|
if (typeof value === 'string') {
|
|
|
|
expect(searchParams.get(key)).to.equal(value);
|
|
|
|
} else {
|
|
|
|
expect(searchParams.get(key)).to.match(value);
|
|
|
|
}
|
|
|
|
}
|
2022-08-30 16:09:44 -05:00
|
|
|
});
|
|
|
|
});
|
2022-07-01 10:47:48 -05:00
|
|
|
});
|
|
|
|
|
2022-08-30 16:09:44 -05:00
|
|
|
describe('SSR images with subpath - build', function () {
|
2022-07-01 10:47:48 -05:00
|
|
|
let fixture;
|
|
|
|
|
|
|
|
before(async () => {
|
|
|
|
fixture = await loadFixture({
|
|
|
|
root: './fixtures/basic-image/',
|
2022-08-30 16:09:44 -05:00
|
|
|
adapter: testAdapter({ streaming: false }),
|
2022-07-24 23:18:02 -05:00
|
|
|
output: 'server',
|
2022-08-30 16:12:45 -05:00
|
|
|
base: '/docs',
|
2022-07-01 10:47:48 -05:00
|
|
|
});
|
2022-08-30 16:09:44 -05:00
|
|
|
await fixture.build();
|
2022-07-01 10:47:48 -05:00
|
|
|
});
|
|
|
|
|
2022-09-01 09:58:57 -05:00
|
|
|
[
|
|
|
|
{
|
|
|
|
title: 'Local images',
|
|
|
|
id: '#social-jpg',
|
|
|
|
url: '/_image',
|
2023-01-09 11:01:33 -05:00
|
|
|
query: { f: 'jpg', w: '506', h: '253', href: /^\/docs\/_astro\/social.\w{8}.jpg/ },
|
2022-09-01 09:58:57 -05:00
|
|
|
},
|
2022-09-01 15:31:53 -05:00
|
|
|
{
|
|
|
|
title: 'Filename with spaces',
|
|
|
|
id: '#spaces',
|
|
|
|
url: '/_image',
|
2022-09-01 15:34:20 -05:00
|
|
|
query: {
|
|
|
|
f: 'webp',
|
|
|
|
w: '768',
|
|
|
|
h: '414',
|
2023-01-09 11:01:33 -05:00
|
|
|
href: /^\/docs\/_astro\/introducing astro.\w{8}.jpg/,
|
2022-09-01 15:34:20 -05:00
|
|
|
},
|
2022-09-01 15:31:53 -05:00
|
|
|
},
|
2023-02-15 03:21:41 -05:00
|
|
|
{
|
|
|
|
title: 'SVG image',
|
|
|
|
id: '#logo-svg',
|
|
|
|
url: '/_image',
|
|
|
|
query: { f: 'svg', w: '192', h: '256', href: /^\/docs\/_astro\/logo.\w{8}.svg/ },
|
|
|
|
},
|
2022-09-01 09:58:57 -05:00
|
|
|
{
|
|
|
|
title: 'Inline imports',
|
|
|
|
id: '#inline',
|
|
|
|
url: '/_image',
|
2023-01-09 11:01:33 -05:00
|
|
|
query: { f: 'jpg', w: '506', h: '253', href: /^\/docs\/_astro\/social.\w{8}.jpg/ },
|
2022-09-01 09:58:57 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Remote images',
|
|
|
|
id: '#google',
|
|
|
|
url: '/_image',
|
|
|
|
query: {
|
|
|
|
f: 'webp',
|
|
|
|
w: '544',
|
|
|
|
h: '184',
|
2022-09-01 10:01:07 -05:00
|
|
|
href: 'https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png',
|
|
|
|
},
|
2022-09-01 09:58:57 -05:00
|
|
|
},
|
2022-09-07 08:56:07 -05:00
|
|
|
{
|
|
|
|
title: 'Remote without file extension',
|
|
|
|
id: '#ipsum',
|
|
|
|
url: '/_image',
|
|
|
|
query: {
|
|
|
|
w: '200',
|
|
|
|
h: '300',
|
2022-09-22 14:48:14 -05:00
|
|
|
href: 'https://dummyimage.com/200x300',
|
2022-09-07 08:56:07 -05:00
|
|
|
},
|
|
|
|
},
|
2022-09-01 09:58:57 -05:00
|
|
|
{
|
|
|
|
title: 'Remote images with search',
|
|
|
|
id: '#query',
|
|
|
|
url: '/_image',
|
|
|
|
query: {
|
|
|
|
f: 'webp',
|
|
|
|
w: '544',
|
|
|
|
h: '184',
|
2022-09-01 10:01:07 -05:00
|
|
|
href: 'https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png?token=abc',
|
|
|
|
},
|
2022-09-01 09:58:57 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Public images',
|
|
|
|
id: '#hero',
|
|
|
|
url: '/_image',
|
2022-11-09 10:22:53 -05:00
|
|
|
query: { f: 'webp', w: '768', h: '414', href: '/docs/hero.jpg' },
|
2022-09-01 10:01:07 -05:00
|
|
|
},
|
2022-09-07 12:22:11 -05:00
|
|
|
{
|
|
|
|
title: 'Background color',
|
|
|
|
id: '#bg-color',
|
|
|
|
url: '/_image',
|
|
|
|
query: {
|
|
|
|
f: 'jpeg',
|
|
|
|
w: '544',
|
|
|
|
h: '184',
|
|
|
|
bg: '#333333',
|
|
|
|
href: 'https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png',
|
|
|
|
},
|
|
|
|
},
|
2022-09-01 09:58:57 -05:00
|
|
|
].forEach(({ title, id, url, query }) => {
|
|
|
|
it(title, async () => {
|
2022-08-30 16:09:44 -05:00
|
|
|
const app = await fixture.loadTestAdapterApp();
|
|
|
|
|
2022-11-08 08:31:05 -05:00
|
|
|
const request = new Request('http://example.com/docs/');
|
2022-08-30 16:09:44 -05:00
|
|
|
const response = await app.render(request);
|
|
|
|
const html = await response.text();
|
|
|
|
const $ = cheerio.load(html);
|
|
|
|
|
2022-09-01 09:58:57 -05:00
|
|
|
const image = $(id);
|
2022-08-30 16:09:44 -05:00
|
|
|
|
|
|
|
const src = image.attr('src');
|
|
|
|
const [route, params] = src.split('?');
|
|
|
|
|
2022-09-01 09:58:57 -05:00
|
|
|
expect(route).to.equal(url);
|
2022-08-30 16:09:44 -05:00
|
|
|
|
|
|
|
const searchParams = new URLSearchParams(params);
|
|
|
|
|
2022-09-01 09:58:57 -05:00
|
|
|
for (const [key, value] of Object.entries(query)) {
|
|
|
|
if (typeof value === 'string') {
|
|
|
|
expect(searchParams.get(key)).to.equal(value);
|
|
|
|
} else {
|
|
|
|
expect(searchParams.get(key)).to.match(value);
|
|
|
|
}
|
|
|
|
}
|
2022-08-30 16:09:44 -05:00
|
|
|
});
|
|
|
|
});
|
2022-07-01 10:47:48 -05:00
|
|
|
});
|