Skip to content

Commit

Permalink
Add tests for playgroundPath
Browse files Browse the repository at this point in the history
  • Loading branch information
mondaychen committed Nov 15, 2018
1 parent 0f434ba commit d5f49b3
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 17 deletions.
41 changes: 41 additions & 0 deletions packages/apollo-server-express/src/__tests__/ApolloServer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,47 @@ describe('apollo-server-express', () => {
});
});

it('can allow custom path for playground and GraphQL API', async () => {
const { url: uri, playgroundUrl } = await createServer(
{
typeDefs,
resolvers,
},
{
path: '/gq',
playgroundPath: '/playground',
},
);
expect(uri).not.toEqual(playgroundUrl);

const apolloFetch = createApolloFetch({ uri });
const result = await apolloFetch({ query: '{hello}' });

expect(result.data).toEqual({ hello: 'hi' });

return new Promise<http.Server>((resolve, reject) => {
request(
{
url: playgroundUrl,
method: 'GET',
headers: {
accept:
'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
},
},
(error, response, body) => {
if (error) {
reject(error);
} else {
expect(body).toMatch('GraphQLPlayground');
expect(response.statusCode).toEqual(200);
resolve();
}
},
);
});
});

const playgroundPartialOptionsTest = async () => {
const defaultQuery = 'query { foo { bar } }';
const endpoint = '/fumanchupacabra';
Expand Down
76 changes: 59 additions & 17 deletions packages/apollo-server-hapi/src/__tests__/ApolloServer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ const port = 5555;
typeDefs,
resolvers,
});
app = new Server({ port });
app = new Server({ port, host: 'localhost' });

await server.applyMiddleware({ app });
await app.start();
Expand Down Expand Up @@ -108,7 +108,7 @@ const port = 5555;
resolvers,
introspection: false,
});
app = new Server({ port });
app = new Server({ port, host: 'localhost' });

await server.applyMiddleware({ app });
await app.start();
Expand Down Expand Up @@ -156,7 +156,7 @@ const port = 5555;
typeDefs,
resolvers,
});
app = new Server({ port });
app = new Server({ port, host: 'localhost' });

await server.applyMiddleware({ app });
await app.start();
Expand Down Expand Up @@ -188,14 +188,58 @@ const port = 5555;
});
});

it('accepts cors configuration', async () => {
it('can allow custom path for playground and GraphQL API', async () => {
server = new ApolloServer({
typeDefs,
resolvers,
});
app = new Server({
port,
app = new Server({ port, host: 'localhost' });

await server.applyMiddleware({
app,
path: '/gq',
playgroundPath: '/playground',
});
await app.start();

httpServer = app.listener;
const uri = app.info.uri + '/gq';
const playgroundUrl = app.info.uri + '/playground';

const apolloFetch = createApolloFetch({ uri });
const result = await apolloFetch({ query: '{hello}' });

expect(result.data).toEqual({ hello: 'hi' });

return new Promise<http.Server>((resolve, reject) => {
request(
{
url: playgroundUrl,
method: 'GET',
headers: {
accept:
'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
},
},
(error, response, body) => {
if (error) {
reject(error);
} else {
expect(body).toMatch('GraphQLPlayground');
expect(response.statusCode).toEqual(200);
resolve();
}
},
);
});
});

it('accepts cors configuration', async () => {
server = new ApolloServer({
typeDefs,
resolvers,
});
app = new Server({ port, host: 'localhost' });

await server.applyMiddleware({
app,
Expand Down Expand Up @@ -233,9 +277,7 @@ const port = 5555;
typeDefs,
resolvers,
});
app = new Server({
port,
});
app = new Server({ port, host: 'localhost' });

await server.applyMiddleware({
app,
Expand Down Expand Up @@ -284,7 +326,7 @@ const port = 5555;
resolvers,
context,
});
app = new Server({ port });
app = new Server({ port, host: 'localhost' });

await server.applyMiddleware({ app });
await app.start();
Expand All @@ -309,7 +351,7 @@ const port = 5555;
typeDefs,
resolvers,
});
app = new Server({ port });
app = new Server({ port, host: 'localhost' });

await server.applyMiddleware({ app });
await app.start();
Expand Down Expand Up @@ -341,7 +383,7 @@ const port = 5555;
typeDefs,
resolvers,
});
app = new Server({ port });
app = new Server({ port, host: 'localhost' });

await server.applyMiddleware({
app,
Expand Down Expand Up @@ -379,7 +421,7 @@ const port = 5555;
resolvers,
});

app = new Server({ port });
app = new Server({ port, host: 'localhost' });

await server.applyMiddleware({
app,
Expand Down Expand Up @@ -519,7 +561,7 @@ const port = 5555;
},
});

app = new Server({ port });
app = new Server({ port, host: 'localhost' });

await server.applyMiddleware({
app,
Expand Down Expand Up @@ -564,7 +606,7 @@ const port = 5555;
},
});

app = new Server({ port });
app = new Server({ port, host: 'localhost' });

await server.applyMiddleware({
app,
Expand Down Expand Up @@ -611,7 +653,7 @@ const port = 5555;
},
});

app = new Server({ port });
app = new Server({ port, host: 'localhost' });

await server.applyMiddleware({
app,
Expand Down Expand Up @@ -655,7 +697,7 @@ const port = 5555;
},
});

app = new Server({ port });
app = new Server({ port, host: 'localhost' });

await server.applyMiddleware({
app,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ export function createServerInfo<AS extends ApolloServerBase>(
pathname: server.graphqlPath,
});

serverInfo.playgroundUrl = require('url').format({
protocol: 'http',
hostname: hostForUrl,
port: serverInfo.port,
pathname: server.playgroundPath,
});

return serverInfo;
}

Expand Down Expand Up @@ -104,6 +111,7 @@ export interface ServerInfo<AS extends ApolloServerBase> {
address: string;
family: string;
url: string;
playgroundUrl: string;
port: number | string;
server: AS;
httpServer: http.Server;
Expand Down
42 changes: 42 additions & 0 deletions packages/apollo-server-koa/src/__tests__/ApolloServer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,48 @@ describe('apollo-server-koa', () => {
});
});

it('can allow custom path for playground and GraphQL API', async () => {
const { url: uri, playgroundUrl } = await createServer(
{
typeDefs,
resolvers,
},
{
path: '/gq',
playgroundPath: '/playground',
},
);
expect(uri).not.toEqual(playgroundUrl);

const apolloFetch = createApolloFetch({ uri });
const result = await apolloFetch({ query: '{hello}' });

expect(result.data).toEqual({ hello: 'hi' });
expect(result.errors).toBeUndefined();

return new Promise<http.Server>((resolve, reject) => {
request(
{
url: playgroundUrl,
method: 'GET',
headers: {
accept:
'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
},
},
(error, response, body) => {
if (error) {
reject(error);
} else {
expect(body).toMatch('GraphQLPlayground');
expect(response.statusCode).toEqual(200);
resolve();
}
},
);
});
});

it('accepts cors configuration', async () => {
const { url: uri } = await createServer(
{
Expand Down

0 comments on commit d5f49b3

Please sign in to comment.