Skip to content

Commit

Permalink
updated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
imolorhe committed Jun 26, 2024
1 parent bc8027e commit 908b3c0
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,17 @@ exports[`response-builder concatenate strategy should concatenate the responses
]
`;

exports[`response-builder concatenate strategy should concatenate the responses and format the data if concatenated data is valid JSON 1`] = `
[
{
"content": "{
"hello": "world"
}",
"timestamp": 1718252802585,
},
]
`;

exports[`response-builder patch strategy should gather all errors from all responses 1`] = `
[
{
Expand Down
51 changes: 51 additions & 0 deletions packages/altair-core/src/request/handlers/http.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,57 @@ describe('HTTP handler', () => {
]);
});


it('should properly handle normal unsuccessful HTTP GET requests', async () => {
const mockHandler = new MswMockRequestHandler(
'http://localhost:3000/graphql',
async () => {
return new Response('my data is not found', {
status: 404,
});
}
);
server.use(mockHandler);

const request: GraphQLRequestOptions = {
url: 'http://localhost:3000/graphql',
method: 'GET',
additionalParams: {
testData: [
{
hello: 'world',
},
],
},
headers: [],
query: 'query { hello }',
variables: {},
selectedOperation: 'hello',
};

const httpHandler: GraphQLRequestHandler = new HttpRequestHandler();
const res = await testObserver(httpHandler.handle(request));

const receivedRequest = mockHandler.receivedRequest();
expect(receivedRequest?.url).toEqual(
'http://localhost:3000/graphql?query=query+%7B+hello+%7D&variables=%7B%7D&operationName=hello'
);
expect(receivedRequest?.body).toBeNull();

expect(res).toEqual([
expect.objectContaining({
ok: false,
data: 'my data is not found',
headers: expect.any(Object),
status: 404,
url: 'http://localhost:3000/graphql?query=query+%7B+hello+%7D&variables=%7B%7D&operationName=hello',
requestStartTimestamp: expect.any(Number),
requestEndTimestamp: expect.any(Number),
resopnseTimeMs: expect.any(Number),
}),
]);
});

it('should properly handle failed HTTP requests', async () => {
const mockHandler = new MswMockRequestHandler(
'http://localhost:3000/error',
Expand Down
3 changes: 2 additions & 1 deletion packages/altair-core/src/request/handlers/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@ export class HttpRequestHandler implements GraphQLRequestHandler {

if (!merosResponse.ok || !merosResponse.body) {
// don't handle streaming
const buffer = await merosResponse.arrayBuffer()
return this.emitChunk(
merosResponse,
new Uint8Array(),
new Uint8Array(buffer),
true,
observer,
requestStartTime,
Expand Down
14 changes: 14 additions & 0 deletions packages/altair-core/src/request/response-builder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,20 @@ describe('response-builder', () => {
expect(res).toMatchSnapshot();
});

it('should concatenate the responses and format the data if concatenated data is valid JSON', () => {
const res = buildResponse([
{
content: '{"hello":',
timestamp: 1718252802585
},
{
content: '"world"}',
timestamp: 1718252802585,
}
])
expect(res).toMatchSnapshot();
});

it('should return timestamp as 0 if no responses are provided', () => {
const res = buildResponse([], MultiResponseStrategy.CONCATENATE);
expect(res).toEqual([
Expand Down
4 changes: 3 additions & 1 deletion packages/altair-core/src/request/response-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,11 @@ export const buildResponse = (
};

const buildResponse__concatenate = (responses: QueryResponse[]): QueryResponse[] => {
const content = responses.map((r) => r.content).join('');
const parsedContent = parseJson(content, {defaultValue: null});
return [
{
content: responses.map((r) => r.content).join(''),
content: parsedContent ? JSON.stringify(parsedContent, null, 2) : content,
timestamp: responses.at(-1)?.timestamp ?? 0,
},
];
Expand Down

0 comments on commit 908b3c0

Please sign in to comment.