Skip to content

Commit

Permalink
Fix to ignore non-permanent redirects
Browse files Browse the repository at this point in the history
Related-to: GH-54.
  • Loading branch information
wooorm committed Oct 8, 2024
1 parent 8655bbc commit 044382d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
6 changes: 5 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,11 @@ async function rule(tree, file, options) {
product.fatal = message.fatal
}

if (result.status === 'alive' && new URL(url).href !== result.url) {
if (
result.permanent &&
result.status === 'alive' &&
new URL(url).href !== result.url
) {
const message = file.message(
'Unexpected redirecting URL `' +
url +
Expand Down
28 changes: 27 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ No URLs in here.
assert.deepEqual(file.messages.map(String), [])
})

await t.test('should support redirects', async function () {
await t.test('should support permanent redirects', async function () {
const globalDispatcher = getGlobalDispatcher()
const mockAgent = new MockAgent()
mockAgent.enableNetConnect(/(?=a)b/)
Expand All @@ -354,4 +354,30 @@ No URLs in here.
'1:1-1:30: Unexpected redirecting URL `https://example.com/from`, expected final URL `https://example.com/to`'
])
})

await t.test('should support temporary redirects', async function () {
const globalDispatcher = getGlobalDispatcher()
const mockAgent = new MockAgent()
mockAgent.enableNetConnect(/(?=a)b/)
setGlobalDispatcher(mockAgent)
const site = mockAgent.get('https://example.com')

site.intercept({path: '/from'}).reply(302, '', {
headers: {Location: '/to'}
})

site.intercept({path: '/to'}).reply(200, 'ok', {
headers: {'Content-Type': 'text/html'}
})

const document = `[a](https://example.com/from)`
const file = await remark().use(remarkLintNoDeadUrls).process(document)

await mockAgent.close()
await setGlobalDispatcher(globalDispatcher)

file.messages.sort(compareMessage)

assert.deepEqual(file.messages.map(String), [])
})
})

0 comments on commit 044382d

Please sign in to comment.