Latest
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267import { superdeno } from 'https://deno.land/x/superdeno@4.9.0/mod.ts?target=deno'import { describe, it } from 'https://deno.land/std@0.221.0/testing/bdd.ts'import { buildSchema, GraphQLObjectType, GraphQLSchema, GraphQLString,} from 'npm:graphql@16.8.1'import { GraphQLHTTP } from './mod.ts'
const schema = buildSchema(` type Query { hello: String }`)
const rootValue = { hello: () => 'Hello World!',}
const app = GraphQLHTTP({ schema, rootValue })
describe('GraphQLHTTP({ schema, rootValue })', () => { it('should send 400 on malformed request query', async () => { const request = superdeno(app)
await request.get('/').expect(400, { 'errors': [{ 'message': 'Missing query' }], }) }) it('should send unsupported media type on empty request body', async () => { const request = superdeno(app)
await request.post('/').expect(415) }) it('should send resolved POST GraphQL query', async () => { const request = superdeno(app)
await request .post('/') .send({ 'query': '\n{\n hello\n}', }) .expect(200, { data: { hello: 'Hello World!' } }) }) it('should send resolved GET GraphQL query', async () => { const request = superdeno(app)
await request.get('/?query={hello}').expect( 200, { data: { hello: 'Hello World!' } }, ) }) it('should send resolved GET GraphQL query when Accept is application/json', async () => { const request = superdeno(app)
await request .get('/?query={hello}') .set('Accept', 'application/json') .expect(200, { data: { hello: 'Hello World!' } }) .expect('Content-Type', 'application/json; charset=utf-8') }) it('should send resolved GET GraphQL query when Accept is */*', async () => { const request = superdeno(app)
await request .get('/?query={hello}') .set('Accept', '*/*') .expect(200, { data: { hello: 'Hello World!' } }) .expect('Content-Type', 'application/json; charset=utf-8') }) it('should send 406 not acceptable when Accept is other (text/html)', async () => { const request = superdeno(app)
await request.get('/?query={hello}').set('Accept', 'text/html').expect( 406, 'Not Acceptable', ) }) it('should send 406 not acceptable when Accept is other (text/css)', async () => { const request = superdeno(app)
await request.get('/?query={hello}').set('Accept', 'text/css').expect( 406, 'Not Acceptable', ) }) it('should pass req obj to server context', async () => { type Context = { request: Request }
const app = GraphQLHTTP<Request, Context>({ schema: new GraphQLSchema({ query: new GraphQLObjectType({ name: 'Query', fields: { 'hello': { type: GraphQLString, resolve: (_root, _args, { request: { url } }: Context) => `Request from ${url.slice(url.lastIndexOf('/'))}`, }, }, }), }), context: (req) => ({ request: req.raw }), })
const request = superdeno(app)
await request .post('/') .send({ 'query': '\n{\n hello\n}', 'variables': {}, 'operationName': null, }) .expect(200, { data: { hello: 'Request from /' } }) })
describe('graphiql', () => { it('should allow query GET requests when set to false', async () => { const app = GraphQLHTTP({ graphiql: false, schema, rootValue })
const request = superdeno(app)
await request.get('/?query={hello}').expect( 200, { data: { hello: 'Hello World!' } }, ) }) it('should allow query GET requests when set to true', async () => { const app = GraphQLHTTP({ graphiql: true, schema, rootValue })
const request = superdeno(app)
await request.get('/?query={hello}').expect( 200, { data: { hello: 'Hello World!' } }, ) }) it('should send 406 when Accept is only text/html when set to false', async () => { const app = GraphQLHTTP({ graphiql: false, schema, rootValue })
const request = superdeno(app)
await request.get('/').set('Accept', 'text/html').expect( 406, 'Not Acceptable', ) }) it('should render a playground when Accept does include text/html when set to true', async () => { const app = GraphQLHTTP({ graphiql: true, schema, rootValue })
const request = superdeno(app)
await request .get('/?query={hello}') .set('Accept', 'text/html;*/*') .expect(200) .expect('Content-Type', 'text/html') }) it('should render a playground if graphiql is set to true', async () => { const app = GraphQLHTTP({ graphiql: true, schema, rootValue })
const request = superdeno(app)
await request.get('/').set('Accept', 'text/html').expect(200).expect( 'Content-Type', 'text/html', ) }) describe('playgroundOptions', () => { it('supports custom favicon', async () => { const app = GraphQLHTTP({ graphiql: true, schema, rootValue, playgroundOptions: { faviconUrl: 'https://github.com/favicon.ico', }, })
const request = superdeno(app)
await request .get('/') .set('Accept', 'text/html') .expect(200) .expect('Content-Type', 'text/html') .expect( new RegExp( '<link rel="shortcut icon" href="https://github.com/favicon.ico" />', ), ) }) it('supports custom title', async () => { const app = GraphQLHTTP({ graphiql: true, schema, rootValue, playgroundOptions: { title: 'Hello gql!', }, })
const request = superdeno(app)
await request .get('/') .set('Accept', 'text/html') .expect(200) .expect('Content-Type', 'text/html') .expect(new RegExp('<title>Hello gql!</title>')) }) it('adds React CDN links if env is React', async () => { const app = GraphQLHTTP({ graphiql: true, schema, rootValue, playgroundOptions: { cdnUrl: 'https://unpkg.com', }, })
const request = superdeno(app)
await request .get('/') .set('Accept', 'text/html') .expect(200) .expect('Content-Type', 'text/html') .expect(new RegExp('https://unpkg.com')) }) }) }) describe('headers', () => { it('should pass custom headers to response', async () => { const app = GraphQLHTTP({ schema, rootValue, headers: { Key: 'Value' } })
const request = superdeno(app)
await request .post('/') .send({ 'query': '\n{\n hello\n}', 'variables': {}, 'operationName': null, }) .expect(200, { data: { hello: 'Hello World!' } }) .expect('Key', 'Value') }) it('does not error with empty header object', async () => { const app = GraphQLHTTP({ schema, rootValue, headers: {} })
const request = superdeno(app)
await request .post('/') .send({ 'query': '\n{\n hello\n}', 'variables': {}, 'operationName': null, }) .expect(200, { data: { hello: 'Hello World!' } }) }) })})