Skip to content

Commit

Permalink
Add routing to micro tests
Browse files Browse the repository at this point in the history
  • Loading branch information
megamaddu authored and helfer committed Jun 16, 2017
1 parent 9e57e11 commit 49dff93
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 13 deletions.
15 changes: 15 additions & 0 deletions packages/graphql-server-integration-testsuite/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -817,5 +817,20 @@ export default (createApp: CreateAppFunc, destroyApp?: DestroyAppFunc) => {
});
});
});

describe('server setup', () => {
it('throws error on 404 routes', () => {
app = createApp();

const query = {
query: '{ testString }',
};
const req = request(app)
.get(`/bogus-route?${querystring.stringify(query)}`);
return req.then((res) => {
expect(res.status).to.equal(404);
});
});
});
});
};
11 changes: 10 additions & 1 deletion packages/graphql-server-lambda/src/lambdaApollo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,28 @@ import 'mocha';
import * as url from 'url';

function createLambda(options: CreateAppOptions = {}) {
let handler,
let route,
handler,
callback,
event,
context;

options.graphqlOptions = options.graphqlOptions || { schema: Schema };
if (options.graphiqlOptions ) {
route = '/graphiql';
handler = graphiqlLambda( options.graphiqlOptions );
} else {
route = '/graphql';
handler = graphqlLambda( options.graphqlOptions );
}

return function(req, res) {
if (!req.url.startsWith(route)) {
res.statusCode = 404;
res.end();
return;
}

let body = '';
req.on('data', function (chunk) {
body += chunk;
Expand Down
7 changes: 4 additions & 3 deletions packages/graphql-server-micro/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ import micro, { send } from "micro";
import { get, post, router } from "microrouter";
import schema from "./schema";

const graphqlHandler: microGraphql({ schema });
const graphqlHandler = microGraphql({ schema });
const graphiqlHandler = microGraphiql({ endpointURL: "/graphql" });

const server = micro(
router(
get("/graphql", graphqlHandler),
post("/graphql", graphqlHandler),
get("/graphiql", microGraphiql({ endpointURL: "/graphql" })),
(req, res) => send(res, 404, "404 Not found."),
get("/graphiql", graphiqlHandler),
(req, res) => send(res, 404, "not found"),
),
);

Expand Down
3 changes: 2 additions & 1 deletion packages/graphql-server-micro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
},
"devDependencies": {
"graphql-server-integration-testsuite": "^0.8.4",
"micro": "^7.3.3"
"micro": "^7.3.3",
"microrouter": "^2.1.1"
},
"peerDependencies": {
"graphql": "^0.9.0 || ^0.10.1",
Expand Down
39 changes: 31 additions & 8 deletions packages/graphql-server-micro/src/microApollo.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,39 @@
import { microGraphql, microGraphiql } from './microApollo';
import 'mocha';

import micro from 'micro';
import testSuite, { schema as Schema, CreateAppOptions } from 'graphql-server-integration-testsuite';
import micro, { send } from 'micro';
import { router, get, post, put, patch, del, head, options as opts } from 'microrouter';
import testSuite, { schema, CreateAppOptions } from 'graphql-server-integration-testsuite';


function createApp(options: CreateAppOptions) {
if (options && options.graphiqlOptions ) {
return micro(microGraphiql(options.graphiqlOptions));
} else {
const graphqlOptions = (options && options.graphqlOptions) || { schema: Schema };
return micro(microGraphql(graphqlOptions));
}
const graphqlOptions = (options && options.graphqlOptions) || { schema };
const graphiqlOptions = (options && options.graphiqlOptions) || { endpointURL: '/graphql' };

const graphqlHandler = microGraphql(graphqlOptions);
const graphiqlHandler = microGraphiql(graphiqlOptions);

return micro(
router(
get('/graphql', graphqlHandler),
post('/graphql', graphqlHandler),
put('/graphql', graphqlHandler),
patch('/graphql', graphqlHandler),
del('/graphql', graphqlHandler),
head('/graphql', graphqlHandler),
opts('/graphql', graphqlHandler),

get('/graphiql', graphiqlHandler),
post('/graphiql', graphiqlHandler),
put('/graphiql', graphiqlHandler),
patch('/graphiql', graphiqlHandler),
del('/graphiql', graphiqlHandler),
head('/graphiql', graphiqlHandler),
opts('/graphiql', graphiqlHandler),

(req, res) => send(res, 404, 'not found'),
),
);
}

describe('integration:Micro', () => {
Expand Down

0 comments on commit 49dff93

Please sign in to comment.