fix(http): count redirects, not requests, against the chain limit
A chain of five redirects is six requests. The check failed it while net-guard had just followed it to the end, so the tool blamed the visitor for something it had already done. The budget now counts redirects, and an abandoned chain that ends on a 3xx counts the redirect it was in the middle of. 99 tests.
This commit is contained in:
1 parent
cb635893cc
commit
5a7e67d0b8
3 files changed
+75
-10
No files matched your search
@@ -0,0 +1,50 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { analyseHttp } from '../src/checks/http.mjs';
|
||||
|
||||
/** A chain with `redirects` 301s followed by one 200. */
|
||||
function chain(redirects) {
|
||||
const hops = [];
|
||||
for (let i = 0; i < redirects; i += 1) {
|
||||
hops.push({ url: `https://example.com/${i}`, status: 301, location: `https://example.com/${i + 1}`, totalMs: 40 });
|
||||
}
|
||||
const finalUrl = `https://example.com/${redirects}`;
|
||||
hops.push({ url: finalUrl, status: 200, location: null, totalMs: 40 });
|
||||
return {
|
||||
hops,
|
||||
final: { status: 200, url: finalUrl, headers: {} },
|
||||
headers: { 'strict-transport-security': 'max-age=31536000', 'x-content-type-options': 'nosniff' },
|
||||
body: '<html><head><title>Example</title></head><body>ok</body></html>',
|
||||
truncated: false
|
||||
};
|
||||
}
|
||||
|
||||
test('five redirects is the last acceptable chain, not a failure', () => {
|
||||
const out = analyseHttp(chain(5), { host: 'example.com' });
|
||||
const hopsFinding = out.findings.find((f) => f.code === 'http_too_many_hops');
|
||||
assert.equal(hopsFinding, undefined, 'six requests / five redirects must not be failed');
|
||||
assert.equal(out.state === 'fail', false);
|
||||
});
|
||||
|
||||
test('six redirects is a failure and names the redirect count', () => {
|
||||
const out = analyseHttp(chain(6), { host: 'example.com' });
|
||||
const hopsFinding = out.findings.find((f) => f.code === 'http_too_many_hops');
|
||||
assert.ok(hopsFinding, 'six redirects must produce http_too_many_hops');
|
||||
assert.equal(hopsFinding.level, 'fail');
|
||||
assert.match(hopsFinding.message, /6 redirects/);
|
||||
});
|
||||
|
||||
test('a single http to https redirect is a win, not a finding', () => {
|
||||
const out = analyseHttp({
|
||||
hops: [
|
||||
{ url: 'http://example.com/', status: 301, location: 'https://example.com/', totalMs: 30 },
|
||||
{ url: 'https://example.com/', status: 200, location: null, totalMs: 30 }
|
||||
],
|
||||
final: { status: 200, url: 'https://example.com/', headers: {} },
|
||||
headers: { 'strict-transport-security': 'max-age=31536000' },
|
||||
body: '<html><head><title>Example</title><meta property="og:image" content="https://example.com/a.png"></head></html>'
|
||||
}, { host: 'example.com' });
|
||||
assert.equal(out.findings.some((f) => f.level === 'fail'), false);
|
||||
assert.ok(out.preview, 'the preview object must reach the report');
|
||||
assert.equal(out.preview.image, 'https://example.com/a.png');
|
||||
});
|
||||
Reference in new issue
Block a user