Skip to content

Commit

Permalink
test: add tests for http assertions
Browse files Browse the repository at this point in the history
Signed-off-by: Miroslav Bajtoš <oss@bajtos.net>
  • Loading branch information
bajtos committed Aug 7, 2024
1 parent 153132c commit e222db5
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
1 change: 1 addition & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import './test/http-assertions.test.js'
import './test/ipni-client.test.js'
import './test/miner-info.test.js'
import './test/multiaddr.test.js'
Expand Down
42 changes: 42 additions & 0 deletions test/http-assertions.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { test } from 'zinnia:test'
import { AssertionError, assertStringIncludes, assertRejects } from 'zinnia:assert'
import { assertRedirectResponse } from '../lib/http-assertions.js'

test('assertRedirectResponse - 302', async () => {
const responseMock = {
status: 302,
headers: new Headers({ location: '/new-location' }),
async text () {
throw new AssertionError('res.text() should not have been called')
}
}

await assertRedirectResponse(responseMock)
})

test('assertRedirectResponse - mission Location header', async () => {
const responseMock = {
status: 302,
headers: new Headers(),
async text () {
throw new AssertionError('res.text() should not have been called')
}
}

const err = await assertRejects(() => assertRedirectResponse(responseMock))
assertStringIncludes(err.message, 'Location')
})

test('assertRedirectResponse - not redirect', async () => {
const responseMock = {
status: 200,
headers: new Headers(),
async text () {
return 'RESPONSE BODY'
}
}

const err = await assertRejects(() => assertRedirectResponse(responseMock, 'NOT REDIRECT'))
assertStringIncludes(err.message, 'NOT REDIRECT')
assertStringIncludes(err.message, 'RESPONSE BODY')
})

0 comments on commit e222db5

Please sign in to comment.