Skip to content

Commit

Permalink
fix not properly parsing content type, usez JSR for deps and add e2e …
Browse files Browse the repository at this point in the history
…tests
  • Loading branch information
talentlessguy committed Jul 17, 2024
1 parent 8d9874d commit 60ff17a
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 108 deletions.
164 changes: 67 additions & 97 deletions deno.lock

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

6 changes: 3 additions & 3 deletions deps.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export { expect } from 'https://deno.land/std@0.210.0/expect/mod.ts'
export { assertEquals } from 'https://deno.land/std@0.210.0/assert/assert_equals.ts'
export { assertMatch } from 'https://deno.land/std@0.210.0/assert/assert_match.ts'
export { assertEquals } from 'jsr:@std/assert@1.0.0/equals'
export { assertMatch } from 'jsr:@std/assert@1.0.0/match'
export { parseMediaType } from 'jsr:@std/media-types@1.0.1/parse-media-type'
29 changes: 29 additions & 0 deletions e2e_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { describe, it } from 'https://deno.land/std@0.224.0/testing/bdd.ts'
import { makeFetch } from './mod.ts'
import { GraphQLHTTP } from 'https://deno.land/x/gql@2.0.2/mod.ts'
import { buildSchema } from 'npm:graphql@16.8.1'

describe('e2e', () => {
it('should work with GraphQL', async () => {
const schema = buildSchema(`
type Query {
hello: String
}
`)

const handler = GraphQLHTTP({
schema,
rootValue: { hello: () => 'Hello World!' },
})

const fetch = makeFetch(handler)

const res = await fetch('/', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ query: '{hello}' }),
})

res.expectBody({ data: { hello: 'Hello World!' } })
})
})
10 changes: 6 additions & 4 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assertEquals, assertMatch } from './deps.ts'
import { assertEquals, assertMatch, parseMediaType } from './deps.ts'
import { Handler, HandlerOrListener } from './types.ts'

// credit - 'https://deno.land/x/free_port@v1.2.0/mod.ts'
Expand Down Expand Up @@ -39,9 +39,11 @@ const fetchEndpoint = async (
const res = await fetch(`http://localhost:${port}${url}`, params)
let data: unknown
const ct = res.headers.get('Content-Type')
if (ct === 'application/json') data = await res.json()
else if (ct?.includes('text')) data = await res.text()
else if (ct === null) data = await res.text()
if (ct === null) return { data: await res.text(), res }
const [mediaType] = parseMediaType(ct)

if (mediaType === 'application/json') data = await res.json()
else if (mediaType.includes('text')) data = await res.text()
else data = await res.arrayBuffer()
return { res, data }
}
Expand Down
20 changes: 16 additions & 4 deletions mod_test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { describe, it } from 'https://deno.land/std@0.210.0/testing/bdd.ts'
import { expect } from './deps.ts'
import { describe, it } from 'https://deno.land/std@0.224.0/testing/bdd.ts'
import { expect } from 'jsr:@std/expect@0.224.5/expect'
import { makeFetch } from './mod.ts'
import { Handler } from './types.ts'
import { AssertionError } from 'https://deno.land/std@0.210.0/assert/assertion_error.ts'
import { AssertionError } from 'jsr:@std/assert@1.0.0/assertion-error'

// this simulates the listener
class PseudoListener {
Expand All @@ -23,7 +23,7 @@ class PseudoListener {
this.rid = this.#listener.rid
}

fetchRandomPort = () => {
fetchRandomPort() {
return Math.round(Math.random() * (9000 - 2000)) + 2000
}

Expand Down Expand Up @@ -53,6 +53,18 @@ describe('makeFetch', () => {

res.expect('Hello World')
})
it('should not crash with text/plain', async () => {
const handler: Handler = () =>
new Response('Hello World', {
headers: {
'Content-Type': 'text/plain;charset=UTF-8',
},
})
const fetch = makeFetch(handler)
const res = await fetch('/')

res.expect('Hello World')
})
it('should parse JSON if response is JSON', async () => {
const handler: Handler = () =>
new Response(JSON.stringify({ hello: 'world' }), {
Expand Down

0 comments on commit 60ff17a

Please sign in to comment.