2022-07-08 21:37:55 +00:00
|
|
|
import { expect } from 'chai';
|
|
|
|
import * as cheerio from 'cheerio';
|
|
|
|
import { loadFixture } from './test-utils.js';
|
|
|
|
import testAdapter from '../../../astro/test/test-adapter.js';
|
|
|
|
|
|
|
|
describe('SSR pictures - build', function () {
|
|
|
|
let fixture;
|
|
|
|
|
|
|
|
before(async () => {
|
|
|
|
fixture = await loadFixture({
|
|
|
|
root: './fixtures/basic-picture/',
|
|
|
|
adapter: testAdapter(),
|
2022-07-25 04:18:02 +00:00
|
|
|
output: 'server',
|
2022-07-08 21:37:55 +00:00
|
|
|
});
|
|
|
|
await fixture.build();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Local images', () => {
|
|
|
|
it('includes sources', 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);
|
|
|
|
|
|
|
|
const sources = $('#social-jpg source');
|
2022-07-08 21:40:22 +00:00
|
|
|
|
2022-07-08 21:37:55 +00:00
|
|
|
expect(sources.length).to.equal(3);
|
|
|
|
|
|
|
|
// TODO: better coverage to verify source props
|
|
|
|
});
|
|
|
|
|
2022-07-19 19:21:58 +00:00
|
|
|
it('includes <img> attributes', async () => {
|
2022-07-08 21:37:55 +00: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);
|
|
|
|
|
|
|
|
const image = $('#social-jpg img');
|
|
|
|
|
|
|
|
const src = image.attr('src');
|
|
|
|
const [route, params] = src.split('?');
|
|
|
|
|
|
|
|
expect(route).to.equal('/_image');
|
|
|
|
|
|
|
|
const searchParams = new URLSearchParams(params);
|
|
|
|
|
|
|
|
expect(searchParams.get('f')).to.equal('jpg');
|
|
|
|
expect(searchParams.get('w')).to.equal('506');
|
|
|
|
expect(searchParams.get('h')).to.equal('253');
|
2022-08-30 21:09:44 +00:00
|
|
|
expect(searchParams.get('href')).to.equal('/assets/social.cece8c77.jpg');
|
2022-07-19 19:21:58 +00:00
|
|
|
expect(image.attr('alt')).to.equal('Social image');
|
2022-07-08 21:37:55 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Inline imports', () => {
|
|
|
|
it('includes sources', 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);
|
|
|
|
|
|
|
|
const sources = $('#inline source');
|
2022-07-08 21:40:22 +00:00
|
|
|
|
2022-07-08 21:37:55 +00:00
|
|
|
expect(sources.length).to.equal(3);
|
|
|
|
|
|
|
|
// TODO: better coverage to verify source props
|
|
|
|
});
|
|
|
|
|
2022-07-19 19:21:58 +00:00
|
|
|
it('includes <img> attributes', async () => {
|
2022-07-08 21:37:55 +00: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);
|
|
|
|
|
|
|
|
const image = $('#inline img');
|
|
|
|
|
|
|
|
const src = image.attr('src');
|
|
|
|
const [route, params] = src.split('?');
|
|
|
|
|
|
|
|
expect(route).to.equal('/_image');
|
|
|
|
|
|
|
|
const searchParams = new URLSearchParams(params);
|
|
|
|
|
|
|
|
expect(searchParams.get('f')).to.equal('jpg');
|
|
|
|
expect(searchParams.get('w')).to.equal('506');
|
|
|
|
expect(searchParams.get('h')).to.equal('253');
|
|
|
|
// TODO: possible to avoid encoding the full image path?
|
2022-08-30 21:09:44 +00:00
|
|
|
expect(searchParams.get('href')).to.equal('/assets/social.cece8c77.jpg');
|
2022-07-19 19:21:58 +00:00
|
|
|
expect(image.attr('alt')).to.equal('Inline social image');
|
2022-07-08 21:37:55 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Remote images', () => {
|
|
|
|
it('includes sources', 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);
|
|
|
|
|
|
|
|
const sources = $('#google source');
|
2022-07-08 21:40:22 +00:00
|
|
|
|
2022-07-08 21:37:55 +00:00
|
|
|
expect(sources.length).to.equal(3);
|
|
|
|
|
|
|
|
// TODO: better coverage to verify source props
|
|
|
|
});
|
|
|
|
|
2022-07-19 19:21:58 +00:00
|
|
|
it('includes <img> attributes', async () => {
|
2022-07-08 21:37:55 +00: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);
|
|
|
|
|
|
|
|
const image = $('#google img');
|
|
|
|
|
|
|
|
const src = image.attr('src');
|
|
|
|
const [route, params] = src.split('?');
|
|
|
|
|
|
|
|
expect(route).to.equal('/_image');
|
|
|
|
|
|
|
|
const searchParams = new URLSearchParams(params);
|
|
|
|
|
|
|
|
expect(searchParams.get('f')).to.equal('png');
|
|
|
|
expect(searchParams.get('w')).to.equal('544');
|
|
|
|
expect(searchParams.get('h')).to.equal('184');
|
|
|
|
// TODO: possible to avoid encoding the full image path?
|
2022-08-30 21:09:44 +00:00
|
|
|
expect(searchParams.get('href')).to.equal(
|
|
|
|
'https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png'
|
|
|
|
);
|
2022-07-19 19:21:58 +00:00
|
|
|
expect(image.attr('alt')).to.equal('Google logo');
|
2022-07-08 21:37:55 +00:00
|
|
|
});
|
|
|
|
});
|
2022-08-30 21:09:44 +00:00
|
|
|
|
|
|
|
describe('/public images', () => {
|
|
|
|
it('includes sources', 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);
|
|
|
|
|
|
|
|
const sources = $('#hero source');
|
|
|
|
|
|
|
|
expect(sources.length).to.equal(3);
|
|
|
|
|
|
|
|
// TODO: better coverage to verify source props
|
|
|
|
});
|
|
|
|
|
|
|
|
it('includes <img> attributes', 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);
|
|
|
|
|
|
|
|
const image = $('#hero img');
|
|
|
|
|
|
|
|
const src = image.attr('src');
|
|
|
|
const [route, params] = src.split('?');
|
|
|
|
|
|
|
|
expect(route).to.equal('/_image');
|
|
|
|
|
|
|
|
const searchParams = new URLSearchParams(params);
|
|
|
|
|
|
|
|
expect(searchParams.get('f')).to.equal('jpg');
|
|
|
|
expect(searchParams.get('w')).to.equal('768');
|
|
|
|
expect(searchParams.get('h')).to.equal('414');
|
|
|
|
expect(searchParams.get('href')).to.equal('/hero.jpg');
|
|
|
|
expect(image.attr('alt')).to.equal('Hero image');
|
|
|
|
});
|
|
|
|
});
|
2022-07-08 21:37:55 +00:00
|
|
|
});
|
|
|
|
|
2022-08-30 21:09:44 +00:00
|
|
|
describe('SSR pictures with subpath - build', function () {
|
2022-07-08 21:37:55 +00:00
|
|
|
let fixture;
|
|
|
|
|
|
|
|
before(async () => {
|
|
|
|
fixture = await loadFixture({
|
|
|
|
root: './fixtures/basic-picture/',
|
|
|
|
adapter: testAdapter(),
|
2022-07-25 04:18:02 +00:00
|
|
|
output: 'server',
|
2022-08-30 21:12:45 +00:00
|
|
|
base: '/docs',
|
2022-07-08 21:37:55 +00:00
|
|
|
});
|
2022-08-30 21:09:44 +00:00
|
|
|
await fixture.build();
|
2022-07-08 21:37:55 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('Local images', () => {
|
2022-08-30 21:09:44 +00:00
|
|
|
it('includes sources', 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);
|
|
|
|
|
2022-07-08 21:37:55 +00:00
|
|
|
const sources = $('#social-jpg source');
|
2022-07-08 21:40:22 +00:00
|
|
|
|
2022-07-08 21:37:55 +00:00
|
|
|
expect(sources.length).to.equal(3);
|
|
|
|
|
|
|
|
// TODO: better coverage to verify source props
|
|
|
|
});
|
|
|
|
|
2022-08-30 21:09:44 +00:00
|
|
|
it('includes <img> attributes', 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);
|
|
|
|
|
2022-07-08 21:37:55 +00:00
|
|
|
const image = $('#social-jpg img');
|
|
|
|
|
|
|
|
const src = image.attr('src');
|
|
|
|
const [route, params] = src.split('?');
|
|
|
|
|
|
|
|
expect(route).to.equal('/_image');
|
|
|
|
|
|
|
|
const searchParams = new URLSearchParams(params);
|
|
|
|
|
|
|
|
expect(searchParams.get('f')).to.equal('jpg');
|
|
|
|
expect(searchParams.get('w')).to.equal('506');
|
|
|
|
expect(searchParams.get('h')).to.equal('253');
|
2022-08-30 21:09:44 +00:00
|
|
|
expect(searchParams.get('href')).to.equal('/docs/assets/social.cece8c77.jpg');
|
2022-07-19 19:21:58 +00:00
|
|
|
expect(image.attr('alt')).to.equal('Social image');
|
2022-07-08 21:37:55 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Inline imports', () => {
|
2022-08-30 21:09:44 +00:00
|
|
|
it('includes sources', 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);
|
|
|
|
|
2022-07-08 21:37:55 +00:00
|
|
|
const sources = $('#inline source');
|
2022-07-08 21:40:22 +00:00
|
|
|
|
2022-07-08 21:37:55 +00:00
|
|
|
expect(sources.length).to.equal(3);
|
|
|
|
|
|
|
|
// TODO: better coverage to verify source props
|
|
|
|
});
|
|
|
|
|
2022-08-30 21:09:44 +00:00
|
|
|
it('includes <img> attributes', 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);
|
|
|
|
|
2022-07-08 21:37:55 +00:00
|
|
|
const image = $('#inline img');
|
|
|
|
|
|
|
|
const src = image.attr('src');
|
|
|
|
const [route, params] = src.split('?');
|
|
|
|
|
|
|
|
expect(route).to.equal('/_image');
|
|
|
|
|
|
|
|
const searchParams = new URLSearchParams(params);
|
|
|
|
|
|
|
|
expect(searchParams.get('f')).to.equal('jpg');
|
|
|
|
expect(searchParams.get('w')).to.equal('506');
|
|
|
|
expect(searchParams.get('h')).to.equal('253');
|
|
|
|
// TODO: possible to avoid encoding the full image path?
|
2022-08-30 21:09:44 +00:00
|
|
|
expect(searchParams.get('href')).to.equal('/docs/assets/social.cece8c77.jpg');
|
2022-07-19 19:21:58 +00:00
|
|
|
expect(image.attr('alt')).to.equal('Inline social image');
|
2022-07-08 21:37:55 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Remote images', () => {
|
2022-08-30 21:09:44 +00:00
|
|
|
it('includes sources', 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);
|
|
|
|
|
2022-07-08 21:37:55 +00:00
|
|
|
const sources = $('#google source');
|
2022-07-08 21:40:22 +00:00
|
|
|
|
2022-07-08 21:37:55 +00:00
|
|
|
expect(sources.length).to.equal(3);
|
|
|
|
|
|
|
|
// TODO: better coverage to verify source props
|
|
|
|
});
|
|
|
|
|
2022-08-30 21:09:44 +00:00
|
|
|
it('includes <img> attributes', 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);
|
|
|
|
|
2022-07-08 21:37:55 +00:00
|
|
|
const image = $('#google img');
|
|
|
|
|
|
|
|
const src = image.attr('src');
|
|
|
|
const [route, params] = src.split('?');
|
|
|
|
|
|
|
|
expect(route).to.equal('/_image');
|
|
|
|
|
|
|
|
const searchParams = new URLSearchParams(params);
|
|
|
|
|
|
|
|
expect(searchParams.get('f')).to.equal('png');
|
|
|
|
expect(searchParams.get('w')).to.equal('544');
|
|
|
|
expect(searchParams.get('h')).to.equal('184');
|
2022-08-30 21:09:44 +00:00
|
|
|
// TODO: possible to avoid encoding the full image path?
|
2022-07-08 21:37:55 +00:00
|
|
|
expect(searchParams.get('href')).to.equal(
|
|
|
|
'https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png'
|
|
|
|
);
|
2022-07-19 19:21:58 +00:00
|
|
|
expect(image.attr('alt')).to.equal('Google logo');
|
2022-07-08 21:37:55 +00:00
|
|
|
});
|
|
|
|
});
|
2022-08-30 21:09:44 +00:00
|
|
|
|
|
|
|
describe('/public images', () => {
|
|
|
|
it('includes sources', 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);
|
|
|
|
|
|
|
|
const sources = $('#hero source');
|
|
|
|
|
|
|
|
expect(sources.length).to.equal(3);
|
|
|
|
|
|
|
|
// TODO: better coverage to verify source props
|
|
|
|
});
|
|
|
|
|
|
|
|
it('includes <img> attributes', 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);
|
|
|
|
|
|
|
|
const image = $('#hero img');
|
|
|
|
|
|
|
|
const src = image.attr('src');
|
|
|
|
const [route, params] = src.split('?');
|
|
|
|
|
|
|
|
expect(route).to.equal('/_image');
|
|
|
|
|
|
|
|
const searchParams = new URLSearchParams(params);
|
|
|
|
|
|
|
|
expect(searchParams.get('f')).to.equal('jpg');
|
|
|
|
expect(searchParams.get('w')).to.equal('768');
|
|
|
|
expect(searchParams.get('h')).to.equal('414');
|
|
|
|
expect(searchParams.get('href')).to.equal('/hero.jpg');
|
|
|
|
expect(image.attr('alt')).to.equal('Hero image');
|
|
|
|
});
|
|
|
|
});
|
2022-07-08 21:37:55 +00:00
|
|
|
});
|