Skip to content

Commit

Permalink
support regex in body
Browse files Browse the repository at this point in the history
  • Loading branch information
talentlessguy committed Jul 17, 2024
1 parent 458f0be commit eba8ed8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
6 changes: 4 additions & 2 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export const makeFetch = (h: HandlerOrListener): FetchFunction => {
assertEquals(
header,
b.join(','),
`expected header ${a} to match regexp ${b}, got ${header}`,
`expected header ${a} to match ${b.join(', ')}, got ${header}`,
)
} else {
assertEquals(
Expand All @@ -152,7 +152,13 @@ export const makeFetch = (h: HandlerOrListener): FetchFunction => {
}
}
const expectBody = (a: unknown) => {
assertEquals(data, a, `Expected to have body ${a}, got ${data}`)
if (a instanceof RegExp) {
assertMatch(
data as string,
a,
`Expected body to match regexp ${a}, got ${data}`,
)
} else assertEquals(data, a, `Expected to have body ${a}, got ${data}`)
}

// deno-lint-ignore no-explicit-any
Expand Down
12 changes: 8 additions & 4 deletions mod_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,8 @@ describe('expectHeader', () => {
try {
res.expectHeader('Content-Type', /image/)
} catch (e) {
expect((e as Error).message).toMatch(
new RegExp(
'Expected actual: "text/html" to match: "/image/": expected header Content-Type to match regexp /image/, got text/html',
),
expect((e as Error).message).toEqual(
'Expected actual: "text/html" to match: "/image/": expected header Content-Type to match regexp /image/, got text/html',
)
}
})
Expand Down Expand Up @@ -258,6 +256,12 @@ describe('expectBody', () => {
expect(e).toBeDefined()
}
})
it('supports regex', async () => {
const handler: Handler = () => new Response('Hello World')
const fetch = makeFetch(handler)
const res = await fetch('/')
res.expectBody(new RegExp('Hello'))
})
})

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

0 comments on commit eba8ed8

Please sign in to comment.