Skip to content

Commit

Permalink
ci(tests): skip apollo server on nodejs 12.x & changed verification f…
Browse files Browse the repository at this point in the history
…or deepkit
  • Loading branch information
H4ad committed Nov 27, 2022
1 parent 4d6f35b commit 0e069ab
Show file tree
Hide file tree
Showing 4 changed files with 252 additions and 246 deletions.
4 changes: 0 additions & 4 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,6 @@ jobs:
if: matrix.node-version == '12.x'
run: echo "TEST_NODE_VERSION=12.x" >> $GITHUB_ENV

- name: Skip Deepkit Tests for 12.x
if: matrix.node-version == '12.x'
run: echo "SKIP_DEEPKIT=true" >> $GITHUB_ENV

- name: Run tests
run: npm test

Expand Down
247 changes: 6 additions & 241 deletions test/frameworks/apollo-server.framework.spec.ts
Original file line number Diff line number Diff line change
@@ -1,243 +1,8 @@
import { IncomingMessage, ServerResponse } from 'http';
import { OutgoingHttpHeaders } from 'http2';
import { ApolloServer, BaseContext, HeaderMap } from '@apollo/server';
import {
ServerlessRequest,
ServerlessResponse,
getEventBodyAsBuffer,
waitForStreamComplete,
} from '../../src';
import {
ApolloServerContextArguments,
ApolloServerFramework,
} from '../../src/frameworks/apollo-server';
import { JsonBodyParserFramework } from '../../src/frameworks/body-parser';
import { TestRouteBuilderMethods } from './utils';

export const frameworkTestOptions: [
method: TestRouteBuilderMethods,
path: string,
query: string,
statusCode: number,
expectedValue: string,
expectHeaderSet: boolean,
][] = [
['post', 'GetUser', 'message', 200, 'Joga10', true],
['post', 'ListUser', 'message', 200, 'Unkownn', true],
['post', 'UserCreated', 'message', 200, 'Created', true],
['post', 'WrongQuery', 'nonexist', 400, 'Cannot query field', false],
];

describe(ApolloServerFramework.name, () => {
describe('test requests', () => {
for (const [
method,
queryName,
query,
statusCode,
expectedValue,
expectHeaderSet,
] of frameworkTestOptions) {
it(`${method}${queryName}: should forward request and receive response correctly`, async () => {
interface ApolloCustomContext extends BaseContext {
request: IncomingMessage;
response: ServerResponse;
}

const app = new ApolloServer<ApolloCustomContext>({
typeDefs: 'type Query { message: String }',
resolvers: {
Query: {
message: (_, __, context: ApolloServerContextArguments) => {
context.response.setHeader('response-header', 'true');

return expectedValue;
},
},
},
});

app.startInBackgroundHandlingStartupErrorsByLoggingAndFailingAllRequests();

const stringBody = JSON.stringify({
query: `
query Query {
${query}
}
`,
});
const [bufferBody, bodyLength] = stringBody
? getEventBodyAsBuffer(stringBody, false)
: [undefined, 0];

const framework = new JsonBodyParserFramework(
new ApolloServerFramework<ApolloCustomContext>(),
);

const request = new ServerlessRequest({
method: method.toUpperCase(),
url: '/',
headers: {
'content-length': String(bodyLength),
'request-header': 'true',
'content-type': 'application/json',
},
body: bufferBody,
});

const response = new ServerlessResponse({
method: method.toUpperCase(),
});

framework.sendRequest(app, request, response);

await waitForStreamComplete(response);

const resultBody = ServerlessResponse.body(response);

expect(resultBody.toString('utf-8')).toContain(
expectedValue !== undefined ? expectedValue : expectedValue,
);
if (expectHeaderSet) {
expect(ServerlessResponse.headers(response)).toHaveProperty(
'response-header',
'true',
);
} else {
expect(ServerlessResponse.headers(response)).not.toHaveProperty(
'response-header',
'true',
);
}
expect(response.statusCode).toBe(statusCode);
});
}
});

describe('async iterator', () => {
it('should handle well async iterator', async () => {
const app = new ApolloServer<BaseContext>({
typeDefs: 'type Query { message: String }',
resolvers: {
Query: {
message: (_, __, context: ApolloServerContextArguments) => {
context.response.setHeader('response-header', 'true');

return 'ok';
},
},
},
});

const asyncContent = ['hello', 'world', '!'];

// eslint-disable-next-line @typescript-eslint/require-await
async function* iterator(values) {
for (let i = 0; i < values.length; i++) yield values[i];
}

jest.spyOn(app, 'executeHTTPGraphQLRequest').mockImplementation(() =>
Promise.resolve({
status: 200,
headers: new HeaderMap(),
body: {
kind: 'chunked',
asyncIterator: iterator(asyncContent),
},
}),
);

app.startInBackgroundHandlingStartupErrorsByLoggingAndFailingAllRequests();

const stringBody = JSON.stringify({
query: `
query Query {
message
}
`,
});

const [bufferBody, bodyLength] = stringBody
? getEventBodyAsBuffer(stringBody, false)
: [undefined, 0];

const framework = new JsonBodyParserFramework(
new ApolloServerFramework(),
);

const request = new ServerlessRequest({
method: 'POST',
url: '/',
headers: {
'content-length': String(bodyLength),
'request-header': 'true',
'content-type': 'application/json',
},
body: bufferBody,
});

const response: ServerlessResponse & { flush?: () => void } =
new ServerlessResponse({
method: 'POST',
});

response.flush = jest.fn(() => void 0);

framework.sendRequest(app, request, response);

await waitForStreamComplete(response);

const resultBody = ServerlessResponse.body(response);

expect(resultBody.toString()).toEqual(asyncContent.join(''));
expect(response.flush).toHaveBeenNthCalledWith(asyncContent.length);
describe('ApolloServerFramework', () => {
// very ugly, i know, but i didn't find any other way, sorry
if (process.env.TEST_NODE_VERSION === '12.x') {
it('when nodejs is 12.x', () => {
expect(true).toEqual(true);
});
});

describe('possible headers', () => {
it('should handle all types of OutgoingHttpHeaders', () => {
const headers: OutgoingHttpHeaders = {
test: 'test',
foo: ['bar', 'boe'],
bar: undefined,
doe: 10,
};

const app = new ApolloServer<BaseContext>({
typeDefs: 'type Query { doe: String }',
resolvers: {},
});

jest
.spyOn(app, 'executeHTTPGraphQLRequest')
.mockImplementation(({ httpGraphQLRequest: { headers } }) => {
const objHeaders = Object.fromEntries(headers.entries());

expect(objHeaders).toHaveProperty('test', 'test');
expect(objHeaders).toHaveProperty('foo', 'bar, boe');
expect(objHeaders).toHaveProperty('doe', '10');
expect(objHeaders).not.toHaveProperty('bar');

return Promise.resolve({
status: 200,
body: { kind: 'complete', string: 'ok' },
headers: new HeaderMap(),
});
});

const request = new ServerlessRequest({
method: 'POST',
url: '/',
headers: headers as any,
});

const response: ServerlessResponse = new ServerlessResponse({
method: 'POST',
});

const framework = new ApolloServerFramework();

framework.sendRequest(app, request, response);
});
});
} else require('./utils-apollo-server').runApolloServerTests();
});
2 changes: 1 addition & 1 deletion test/frameworks/http-deepkit.framework.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
describe('HttpDeepkitFramework', () => {
// very ugly, i know, but i didn't find any other way, sorry
if (process.env.SKIP_DEEPKIT === 'true') {
if (process.env.TEST_NODE_VERSION === '12.x') {
it('when nodejs is 12.x', () => {
expect(true).toEqual(true);
});
Expand Down
Loading

0 comments on commit 0e069ab

Please sign in to comment.