mirror of
https://github.com/vale981/apollo-server
synced 2025-03-06 10:11:40 -05:00
Use supertest query() method instead of manually constructing query string
This commit is contained in:
parent
1b70453939
commit
5db613facd
2 changed files with 39 additions and 49 deletions
|
@ -22,7 +22,6 @@ import { graphqlExpress } from './expressApollo';
|
|||
*/
|
||||
|
||||
import { expect } from 'chai';
|
||||
import { stringify } from 'querystring';
|
||||
import * as zlib from 'zlib';
|
||||
import * as multer from 'multer';
|
||||
import * as bodyParser from 'body-parser';
|
||||
|
@ -93,14 +92,6 @@ const TestSchema = new GraphQLSchema({
|
|||
})
|
||||
});
|
||||
|
||||
function urlString(urlParams?: any): string {
|
||||
let str = '/graphql';
|
||||
if (urlParams) {
|
||||
str += ('?' + stringify(urlParams));
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
function catchError(p) {
|
||||
return p.then(
|
||||
(res) => {
|
||||
|
@ -175,8 +166,8 @@ describe(`GraphQL-HTTP (apolloServer) tests for ${version} express`, () => {
|
|||
it('allows gzipped POST bodies', async () => {
|
||||
const app = express();
|
||||
|
||||
app.use(urlString(), bodyParser.json());
|
||||
app.use(urlString(), graphqlExpress(() => ({
|
||||
app.use('/graphql', bodyParser.json());
|
||||
app.use('/graphql', graphqlExpress(() => ({
|
||||
schema: TestSchema
|
||||
})));
|
||||
|
||||
|
@ -186,7 +177,7 @@ describe(`GraphQL-HTTP (apolloServer) tests for ${version} express`, () => {
|
|||
const gzippedJson = await promiseTo(cb => zlib.gzip(json as any as Buffer, cb));
|
||||
|
||||
const req = request(app)
|
||||
.post(urlString())
|
||||
.post('/graphql')
|
||||
.set('Content-Type', 'application/json')
|
||||
.set('Content-Encoding', 'gzip');
|
||||
req.write(gzippedJson);
|
||||
|
@ -202,8 +193,8 @@ describe(`GraphQL-HTTP (apolloServer) tests for ${version} express`, () => {
|
|||
it('allows deflated POST bodies', async () => {
|
||||
const app = express();
|
||||
|
||||
app.use(urlString(), bodyParser.json());
|
||||
app.use(urlString(), graphqlExpress(() => ({
|
||||
app.use('/graphql', bodyParser.json());
|
||||
app.use('/graphql', graphqlExpress(() => ({
|
||||
schema: TestSchema
|
||||
})));
|
||||
|
||||
|
@ -213,7 +204,7 @@ describe(`GraphQL-HTTP (apolloServer) tests for ${version} express`, () => {
|
|||
const deflatedJson = await promiseTo(cb => zlib.deflate(json as any as Buffer, cb));
|
||||
|
||||
const req = request(app)
|
||||
.post(urlString())
|
||||
.post('/graphql')
|
||||
.set('Content-Type', 'application/json')
|
||||
.set('Content-Encoding', 'deflate');
|
||||
req.write(deflatedJson);
|
||||
|
@ -267,11 +258,11 @@ describe(`GraphQL-HTTP (apolloServer) tests for ${version} express`, () => {
|
|||
|
||||
// Multer provides multipart form data parsing.
|
||||
const storage = multer.memoryStorage();
|
||||
app.use(urlString(), multer({ storage }).single('file'));
|
||||
app.use('/graphql', multer({ storage }).single('file'));
|
||||
|
||||
// Providing the request as part of `rootValue` allows it to
|
||||
// be accessible from within Schema resolve functions.
|
||||
app.use(urlString(), graphqlExpress(req => {
|
||||
app.use('/graphql', graphqlExpress(req => {
|
||||
return {
|
||||
schema: TestMutationSchema,
|
||||
rootValue: { request: req }
|
||||
|
@ -279,7 +270,7 @@ describe(`GraphQL-HTTP (apolloServer) tests for ${version} express`, () => {
|
|||
}));
|
||||
|
||||
const req = request(app)
|
||||
.post(urlString())
|
||||
.post('/graphql')
|
||||
.field('query', `mutation TestMutation {
|
||||
uploadFile { originalname, mimetype }
|
||||
}`)
|
||||
|
@ -302,13 +293,13 @@ describe(`GraphQL-HTTP (apolloServer) tests for ${version} express`, () => {
|
|||
it('handles field errors caught by GraphQL', async () => {
|
||||
const app = express();
|
||||
|
||||
app.use(urlString(), bodyParser.json());
|
||||
app.use(urlString(), graphqlExpress({
|
||||
app.use('/graphql', bodyParser.json());
|
||||
app.use('/graphql', graphqlExpress({
|
||||
schema: TestSchema
|
||||
}));
|
||||
|
||||
const response = await request(app)
|
||||
.post(urlString())
|
||||
.post('/graphql')
|
||||
.send({
|
||||
query: '{thrower}',
|
||||
});
|
||||
|
@ -328,13 +319,13 @@ describe(`GraphQL-HTTP (apolloServer) tests for ${version} express`, () => {
|
|||
it('handles type validation', async () => {
|
||||
const app = express();
|
||||
|
||||
app.use(urlString(), bodyParser.json());
|
||||
app.use(urlString(), graphqlExpress({
|
||||
app.use('/graphql', bodyParser.json());
|
||||
app.use('/graphql', graphqlExpress({
|
||||
schema: TestSchema
|
||||
}));
|
||||
|
||||
const response = await request(app)
|
||||
.post(urlString())
|
||||
.post('/graphql')
|
||||
.send({
|
||||
query: '{notExists}',
|
||||
});
|
||||
|
@ -351,12 +342,12 @@ describe(`GraphQL-HTTP (apolloServer) tests for ${version} express`, () => {
|
|||
it('handles type validation (GET)', async () => {
|
||||
const app = express();
|
||||
|
||||
app.use(urlString(), graphqlExpress({
|
||||
app.use('/graphql', graphqlExpress({
|
||||
schema: TestSchema
|
||||
}));
|
||||
|
||||
const response = await request(app)
|
||||
.get(urlString({ query: '{notExists}' }))
|
||||
.get('/graphql').query({ query: '{notExists}' });
|
||||
|
||||
expect(response.status).to.equal(400);
|
||||
expect(JSON.parse(response.text)).to.deep.equal({
|
||||
|
@ -370,13 +361,13 @@ describe(`GraphQL-HTTP (apolloServer) tests for ${version} express`, () => {
|
|||
it('handles errors thrown during custom graphql type handling', async () => {
|
||||
const app = express();
|
||||
|
||||
app.use(urlString(), bodyParser.json());
|
||||
app.use(urlString(), graphqlExpress({
|
||||
app.use('/graphql', bodyParser.json());
|
||||
app.use('/graphql', graphqlExpress({
|
||||
schema: TestSchema
|
||||
}));
|
||||
|
||||
const response = await request(app)
|
||||
.post(urlString())
|
||||
.post('/graphql')
|
||||
.send({
|
||||
query: '{custom(foo: 123)}',
|
||||
});
|
||||
|
@ -387,8 +378,8 @@ describe(`GraphQL-HTTP (apolloServer) tests for ${version} express`, () => {
|
|||
it('allows for custom error formatting to sanitize', async () => {
|
||||
const app = express();
|
||||
|
||||
app.use(urlString(), bodyParser.json());
|
||||
app.use(urlString(), graphqlExpress({
|
||||
app.use('/graphql', bodyParser.json());
|
||||
app.use('/graphql', graphqlExpress({
|
||||
schema: TestSchema,
|
||||
formatError(error) {
|
||||
return { message: 'Custom error format: ' + error.message };
|
||||
|
@ -396,7 +387,7 @@ describe(`GraphQL-HTTP (apolloServer) tests for ${version} express`, () => {
|
|||
}));
|
||||
|
||||
const response = await request(app)
|
||||
.post(urlString())
|
||||
.post('/graphql')
|
||||
.send({
|
||||
query: '{thrower}',
|
||||
});
|
||||
|
@ -413,8 +404,8 @@ describe(`GraphQL-HTTP (apolloServer) tests for ${version} express`, () => {
|
|||
it('allows for custom error formatting to elaborate', async () => {
|
||||
const app = express();
|
||||
|
||||
app.use(urlString(), bodyParser.json());
|
||||
app.use(urlString(), graphqlExpress({
|
||||
app.use('/graphql', bodyParser.json());
|
||||
app.use('/graphql', graphqlExpress({
|
||||
schema: TestSchema,
|
||||
formatError(error) {
|
||||
return {
|
||||
|
@ -426,7 +417,7 @@ describe(`GraphQL-HTTP (apolloServer) tests for ${version} express`, () => {
|
|||
}));
|
||||
|
||||
const response = await request(app)
|
||||
.post(urlString())
|
||||
.post('/graphql')
|
||||
.send({
|
||||
query: '{thrower}',
|
||||
});
|
||||
|
@ -445,11 +436,11 @@ describe(`GraphQL-HTTP (apolloServer) tests for ${version} express`, () => {
|
|||
it('handles unsupported HTTP methods', async () => {
|
||||
const app = express();
|
||||
|
||||
app.use(urlString(), bodyParser.json());
|
||||
app.use(urlString(), graphqlExpress({ schema: TestSchema }));
|
||||
app.use('/graphql', bodyParser.json());
|
||||
app.use('/graphql', graphqlExpress({ schema: TestSchema }));
|
||||
|
||||
const response = await request(app)
|
||||
.put(urlString({ query: '{test}' }));
|
||||
.put('/graphql').query({ query: '{test}' });
|
||||
|
||||
expect(response.status).to.equal(405);
|
||||
expect(response.headers.allow).to.equal('GET, POST');
|
||||
|
@ -472,14 +463,14 @@ describe(`GraphQL-HTTP (apolloServer) tests for ${version} express`, () => {
|
|||
it('Do not execute a query if it do not pass the custom validation.', async() => {
|
||||
const app = express();
|
||||
|
||||
app.use(urlString(), bodyParser.json());
|
||||
app.use(urlString(), graphqlExpress({
|
||||
app.use('/graphql', bodyParser.json());
|
||||
app.use('/graphql', graphqlExpress({
|
||||
schema: TestSchema,
|
||||
validationRules: [ AlwaysInvalidRule ],
|
||||
}));
|
||||
|
||||
const response = await request(app)
|
||||
.post(urlString())
|
||||
.post('/graphql')
|
||||
.send({
|
||||
query: '{thrower}',
|
||||
})
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
import { expect } from 'chai';
|
||||
import { stub } from 'sinon';
|
||||
import 'mocha';
|
||||
import * as querystring from 'querystring';
|
||||
|
||||
import {
|
||||
GraphQLSchema,
|
||||
|
@ -239,7 +238,7 @@ export default (createApp: CreateAppFunc, destroyApp?: DestroyAppFunc) => {
|
|||
query: 'query test{ testString }',
|
||||
};
|
||||
const req = request(app)
|
||||
.get(`/graphql?${querystring.stringify(query)}`);
|
||||
.get('/graphql').query(query);
|
||||
return req.then((res) => {
|
||||
expect(res.status).to.equal(200);
|
||||
return expect(res.body.data).to.deep.equal(expected);
|
||||
|
@ -255,7 +254,7 @@ export default (createApp: CreateAppFunc, destroyApp?: DestroyAppFunc) => {
|
|||
query: '{ testString }',
|
||||
};
|
||||
const req = request(app)
|
||||
.get(`/graphql?${querystring.stringify(query)}`);
|
||||
.get('/graphql').query(query);
|
||||
return req.then((res) => {
|
||||
expect(res.status).to.equal(200);
|
||||
return expect(res.body.data).to.deep.equal(expected);
|
||||
|
@ -268,7 +267,7 @@ export default (createApp: CreateAppFunc, destroyApp?: DestroyAppFunc) => {
|
|||
query: 'mutation test{ testMutation(echo: "ping") }',
|
||||
};
|
||||
const req = request(app)
|
||||
.get(`/graphql?${querystring.stringify(query)}`);
|
||||
.get('/graphql').query(query);
|
||||
return req.then((res) => {
|
||||
expect(res.status).to.equal(405);
|
||||
expect(res.headers['allow']).to.equal('POST');
|
||||
|
@ -290,7 +289,7 @@ export default (createApp: CreateAppFunc, destroyApp?: DestroyAppFunc) => {
|
|||
}`,
|
||||
};
|
||||
const req = request(app)
|
||||
.get(`/graphql?${querystring.stringify(query)}`);
|
||||
.get('/graphql').query(query);
|
||||
return req.then((res) => {
|
||||
expect(res.status).to.equal(405);
|
||||
expect(res.headers['allow']).to.equal('POST');
|
||||
|
@ -308,7 +307,7 @@ export default (createApp: CreateAppFunc, destroyApp?: DestroyAppFunc) => {
|
|||
testArgument: 'hello world',
|
||||
};
|
||||
const req = request(app)
|
||||
.get(`/graphql?${querystring.stringify(query)}`);
|
||||
.get('/graphql').query(query);
|
||||
return req.then((res) => {
|
||||
expect(res.status).to.equal(200);
|
||||
return expect(res.body.data).to.deep.equal(expected);
|
||||
|
@ -716,7 +715,7 @@ export default (createApp: CreateAppFunc, destroyApp?: DestroyAppFunc) => {
|
|||
}});
|
||||
|
||||
const req = request(app)
|
||||
.get('/graphiql?query={test}')
|
||||
.get('/graphiql').query('query={test}')
|
||||
.set('Accept', 'text/html');
|
||||
return req.then((response) => {
|
||||
expect(response.status).to.equal(200);
|
||||
|
@ -852,7 +851,7 @@ export default (createApp: CreateAppFunc, destroyApp?: DestroyAppFunc) => {
|
|||
query: '{ testString }',
|
||||
};
|
||||
const req = request(app)
|
||||
.get(`/bogus-route?${querystring.stringify(query)}`);
|
||||
.get('/bogus-route').query(query);
|
||||
return req.then((res) => {
|
||||
expect(res.status).to.equal(404);
|
||||
});
|
||||
|
|
Loading…
Add table
Reference in a new issue