Skip to content

Commit

Permalink
test: fix broken test
Browse files Browse the repository at this point in the history
Signed-off-by: Andres Correa Casablanca <[email protected]>
  • Loading branch information
castarco committed Mar 19, 2024
1 parent 0abf851 commit 87615c9
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 58 deletions.
4 changes: 3 additions & 1 deletion packages/integrations/node/src/serve-static.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import type { NodeApp } from 'astro/app/node';
import send from 'send';
import type { Options } from './types.js';

const isSubresourceRegex = /.+\.[a-z]+$/i

/**
* Creates a Node.js http listener for static files and prerendered pages.
* In standalone mode, the static handler is queried first for the static files.
Expand Down Expand Up @@ -49,7 +51,7 @@ export function createStaticHandler(app: NodeApp, options: Options) {
break;
case 'always':
// trailing slash is not added to "subresources"
if (!hasSlash && !urlPath.match(/.+\.[a-z]+$/i)) {
if (!hasSlash && !urlPath.match(isSubresourceRegex)) {
pathname = urlPath + '/' + (urlQuery ? '?' + urlQuery : '');
res.statusCode = 301;
res.setHeader('Location', pathname);
Expand Down
115 changes: 58 additions & 57 deletions packages/integrations/node/test/trailing-slash.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { after, before, describe, it, expect } from 'node:test';
import { after, before, describe, it } from 'node:test';
import * as assert from 'node:assert/strict';
import * as cheerio from 'cheerio';
import nodejs from '../dist/index.js';
import { loadFixture } from './test-utils.js';
Expand Down Expand Up @@ -48,41 +49,41 @@ describe('Trailing slash', () => {
const html = await res.text();
const $ = cheerio.load(html);

expect(res.status).to.equal(200);
expect($('h1').text()).to.equal('Index');
assert.equal(res.status, 200);
assert.equal($('h1').text(), 'Index');
});

it('Can render prerendered route with redirect', async () => {
const res = await fetch(`http://${server.host}:${server.port}/some-base/one`, {
redirect: 'manual',
});
expect(res.status).to.equal(301);
expect(res.headers.get('location')).to.equal('/some-base/one/');
assert.equal(res.status, 301);
assert.equal(res.headers.get('location'), '/some-base/one/');
});

it('Can render prerendered route with redirect and query params', async () => {
const res = await fetch(`http://${server.host}:${server.port}/some-base/one?foo=bar`, {
redirect: 'manual',
});
expect(res.status).to.equal(301);
expect(res.headers.get('location')).to.equal('/some-base/one/?foo=bar');
assert.equal(res.status, 301);
assert.equal(res.headers.get('location'), '/some-base/one/?foo=bar');
});

it('Can render prerendered route with query params', async () => {
const res = await fetch(`http://${server.host}:${server.port}/some-base/one/?foo=bar`);
const html = await res.text();
const $ = cheerio.load(html);

expect(res.status).to.equal(200);
expect($('h1').text()).to.equal('One');
assert.equal(res.status, 200);
assert.equal($('h1').text(), 'One');
});

it('Does not add trailing slash to subresource urls', async () => {
const res = await fetch(`http://${server.host}:${server.port}/some-base/one.css`);
const css = await res.text();

expect(res.status).to.equal(200);
expect(css).to.equal('h1 { color: red; }\n');
assert.equal(res.status, 200);
assert.equal(css, 'h1 { color: red; }\n');
})
});
describe('Without base', async () => {
Expand Down Expand Up @@ -113,41 +114,41 @@ describe('Trailing slash', () => {
const html = await res.text();
const $ = cheerio.load(html);

expect(res.status).to.equal(200);
expect($('h1').text()).to.equal('Index');
assert.equal(res.status, 200);
assert.equal($('h1').text(), 'Index');
});

it('Can render prerendered route with redirect', async () => {
const res = await fetch(`http://${server.host}:${server.port}/one`, {
redirect: 'manual',
});
expect(res.status).to.equal(301);
expect(res.headers.get('location')).to.equal('/one/');
assert.equal(res.status, 301);
assert.equal(res.headers.get('location'), '/one/');
});

it('Can render prerendered route with redirect and query params', async () => {
const res = await fetch(`http://${server.host}:${server.port}/one?foo=bar`, {
redirect: 'manual',
});
expect(res.status).to.equal(301);
expect(res.headers.get('location')).to.equal('/one/?foo=bar');
assert.equal(res.status, 301);
assert.equal(res.headers.get('location'), '/one/?foo=bar');
});

it('Can render prerendered route with query params', async () => {
const res = await fetch(`http://${server.host}:${server.port}/one/?foo=bar`);
const html = await res.text();
const $ = cheerio.load(html);

expect(res.status).to.equal(200);
expect($('h1').text()).to.equal('One');
assert.equal(res.status, 200);
assert.equal($('h1').text(), 'One');
});

it('Does not add trailing slash to subresource urls', async () => {
const res = await fetch(`http://${server.host}:${server.port}/one.css`);
const css = await res.text();

expect(res.status).to.equal(200);
expect(css).to.equal('h1 { color: red; }\n');
assert.equal(res.status, 200);
assert.equal(css, 'h1 { color: red; }\n');
})
});
});
Expand Down Expand Up @@ -181,34 +182,34 @@ describe('Trailing slash', () => {
const html = await res.text();
const $ = cheerio.load(html);

expect(res.status).to.equal(200);
expect($('h1').text()).to.equal('Index');
assert.equal(res.status, 200);
assert.equal($('h1').text(), 'Index');
});

it('Can render prerendered route with redirect', async () => {
const res = await fetch(`http://${server.host}:${server.port}/some-base/one/`, {
redirect: 'manual',
});
expect(res.status).to.equal(301);
expect(res.headers.get('location')).to.equal('/some-base/one');
assert.equal(res.status, 301);
assert.equal(res.headers.get('location'), '/some-base/one');
});

it('Can render prerendered route with redirect and query params', async () => {
const res = await fetch(`http://${server.host}:${server.port}/some-base/one/?foo=bar`, {
redirect: 'manual',
});

expect(res.status).to.equal(301);
expect(res.headers.get('location')).to.equal('/some-base/one?foo=bar');
assert.equal(res.status, 301);
assert.equal(res.headers.get('location'), '/some-base/one?foo=bar');
});

it('Can render prerendered route with query params', async () => {
const res = await fetch(`http://${server.host}:${server.port}/some-base/one?foo=bar`);
const html = await res.text();
const $ = cheerio.load(html);

expect(res.status).to.equal(200);
expect($('h1').text()).to.equal('One');
assert.equal(res.status, 200);
assert.equal($('h1').text(), 'One');
});
});
describe('Without base', async () => {
Expand Down Expand Up @@ -239,34 +240,34 @@ describe('Trailing slash', () => {
const html = await res.text();
const $ = cheerio.load(html);

expect(res.status).to.equal(200);
expect($('h1').text()).to.equal('Index');
assert.equal(res.status, 200);
assert.equal($('h1').text(), 'Index');
});

it('Can render prerendered route with redirect', async () => {
const res = await fetch(`http://${server.host}:${server.port}/one/`, {
redirect: 'manual',
});
expect(res.status).to.equal(301);
expect(res.headers.get('location')).to.equal('/one');
assert.equal(res.status, 301);
assert.equal(res.headers.get('location'), '/one');
});

it('Can render prerendered route with redirect and query params', async () => {
const res = await fetch(`http://${server.host}:${server.port}/one/?foo=bar`, {
redirect: 'manual',
});

expect(res.status).to.equal(301);
expect(res.headers.get('location')).to.equal('/one?foo=bar');
assert.equal(res.status, 301);
assert.equal(res.headers.get('location'), '/one?foo=bar');
});

it('Can render prerendered route and query params', async () => {
const res = await fetch(`http://${server.host}:${server.port}/one?foo=bar`);
const html = await res.text();
const $ = cheerio.load(html);

expect(res.status).to.equal(200);
expect($('h1').text()).to.equal('One');
assert.equal(res.status, 200);
assert.equal($('h1').text(), 'One');
});
});
});
Expand Down Expand Up @@ -300,8 +301,8 @@ describe('Trailing slash', () => {
const html = await res.text();
const $ = cheerio.load(html);

expect(res.status).to.equal(200);
expect($('h1').text()).to.equal('Index');
assert.equal(res.status, 200);
assert.equal($('h1').text(), 'Index');
});

it('Can render prerendered route with slash', async () => {
Expand All @@ -311,8 +312,8 @@ describe('Trailing slash', () => {
const html = await res.text();
const $ = cheerio.load(html);

expect(res.status).to.equal(200);
expect($('h1').text()).to.equal('One');
assert.equal(res.status, 200);
assert.equal($('h1').text(), 'One');
});

it('Can render prerendered route without slash', async () => {
Expand All @@ -322,8 +323,8 @@ describe('Trailing slash', () => {
const html = await res.text();
const $ = cheerio.load(html);

expect(res.status).to.equal(200);
expect($('h1').text()).to.equal('One');
assert.equal(res.status, 200);
assert.equal($('h1').text(), 'One');
});

it('Can render prerendered route with slash and query params', async () => {
Expand All @@ -333,8 +334,8 @@ describe('Trailing slash', () => {
const html = await res.text();
const $ = cheerio.load(html);

expect(res.status).to.equal(200);
expect($('h1').text()).to.equal('One');
assert.equal(res.status, 200);
assert.equal($('h1').text(), 'One');
});

it('Can render prerendered route without slash and with query params', async () => {
Expand All @@ -344,8 +345,8 @@ describe('Trailing slash', () => {
const html = await res.text();
const $ = cheerio.load(html);

expect(res.status).to.equal(200);
expect($('h1').text()).to.equal('One');
assert.equal(res.status, 200);
assert.equal($('h1').text(), 'One');
});
});
describe('Without base', async () => {
Expand Down Expand Up @@ -376,26 +377,26 @@ describe('Trailing slash', () => {
const html = await res.text();
const $ = cheerio.load(html);

expect(res.status).to.equal(200);
expect($('h1').text()).to.equal('Index');
assert.equal(res.status, 200);
assert.equal($('h1').text(), 'Index');
});

it('Can render prerendered route with slash', async () => {
const res = await fetch(`http://${server.host}:${server.port}/one/`);
const html = await res.text();
const $ = cheerio.load(html);

expect(res.status).to.equal(200);
expect($('h1').text()).to.equal('One');
assert.equal(res.status, 200);
assert.equal($('h1').text(), 'One');
});

it('Can render prerendered route without slash', async () => {
const res = await fetch(`http://${server.host}:${server.port}/one`);
const html = await res.text();
const $ = cheerio.load(html);

expect(res.status).to.equal(200);
expect($('h1').text()).to.equal('One');
assert.equal(res.status, 200);
assert.equal($('h1').text(), 'One');
});

it('Can render prerendered route with slash and query params', async () => {
Expand All @@ -405,17 +406,17 @@ describe('Trailing slash', () => {
const html = await res.text();
const $ = cheerio.load(html);

expect(res.status).to.equal(200);
expect($('h1').text()).to.equal('One');
assert.equal(res.status, 200);
assert.equal($('h1').text(), 'One');
});

it('Can render prerendered route without slash and with query params', async () => {
const res = await fetch(`http://${server.host}:${server.port}/one?foo=bar`);
const html = await res.text();
const $ = cheerio.load(html);

expect(res.status).to.equal(200);
expect($('h1').text()).to.equal('One');
assert.equal(res.status, 200);
assert.equal($('h1').text(), 'One');
});
});
});
Expand Down

0 comments on commit 87615c9

Please sign in to comment.