Skip to content

Commit

Permalink
support array in expectHeader and less any-s
Browse files Browse the repository at this point in the history
  • Loading branch information
v1rtl committed Apr 30, 2023
1 parent 9db9bd1 commit 23935e7
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 20 deletions.
17 changes: 5 additions & 12 deletions deno.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
{
"fmt": {
"options": {
"useTabs": false,
"lineWidth": 80,
"indentWidth": 2,
"singleQuote": true,
"semiColons": false
}
"useTabs": false,
"lineWidth": 80,
"indentWidth": 2,
"singleQuote": true,
"semiColons": false
},
"tasks": {
"test": "deno test --allow-net --allow-read --allow-write --coverage=coverage"
},
"test": {
"files":{
"exclude": ["./tests/util.ts"]
}
}
}
24 changes: 16 additions & 8 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const makeFetchPromise = (handlerOrListener: HandlerOrListener) => {
// listener
if ('rid' in handlerOrListener && 'addr' in handlerOrListener) {
return async (url: URL | string = '', params?: RequestInit) => {
const p = new Promise<{ res: Response; data?: any }>((resolve) => {
const p = new Promise<{ res: Response; data?: unknown }>((resolve) => {
setTimeout(async () => {
const res = await fetch(
`http://localhost:${port}${url}`,
Expand Down Expand Up @@ -41,7 +41,7 @@ const makeFetchPromise = (handlerOrListener: HandlerOrListener) => {
}

return async (url: URL | string = '', params?: RequestInit) => {
const p = new Promise<{ res: Response; data?: any }>((resolve) => {
const p = new Promise<{ res: Response; data?: unknown }>((resolve) => {
setTimeout(async () => {
const res = await fetch(
`http://localhost:${port}${url}`,
Expand Down Expand Up @@ -85,17 +85,25 @@ export const makeFetch = (h: HandlerOrListener) => {
expectBody,
}
}
const expectHeader = (a: string, b: string | RegExp | null) => {
const expectHeader = (a: string, b: string | RegExp | null | string[]) => {
const header = res.headers.get(a)
if (header === null) {
throw new Error(`expected header ${header} to not be empty`)
}
if (b instanceof RegExp) {
if (header === null) {
throw new Error(`expected header ${header} to not be empty`)
}
assertMatch(
header,
b,
`expected header ${a} to match regexp ${b}, got ${header}`,
)
} else if (
Array.isArray(b)
) {
assertEquals(
header,
b.join(','),
`expected header ${a} to match regexp ${b}, got ${header}`,
)
} else {
assertEquals(
header,
Expand All @@ -110,11 +118,11 @@ export const makeFetch = (h: HandlerOrListener) => {
expectBody,
}
}
const expectBody = (a: any) => {
const expectBody = (a: unknown) => {
assertEquals(data, a, `Expected to have body ${a}, got ${data}`)
}

const expectAll = (a: any, b?: any) => {
const expectAll = (a: unknown, b?: any) => {
if (typeof a === 'number') {
expectStatus(a, b)
} else if (typeof a === 'string' && typeof b !== 'undefined') {
Expand Down
7 changes: 7 additions & 0 deletions mod_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,13 @@ describe('expectHeader', () => {
)
}
})
it('can expect array of header values', async () => {
const handler: Handler = () =>
new Response('Hello World', { headers: { 'A': '1,2,3' } })
const fetch = makeFetch(handler)
const res = await fetch('/')
res.expectHeader('A', ['1', '2', '3'])
})
})

describe('expectBody', () => {
Expand Down

0 comments on commit 23935e7

Please sign in to comment.