Skip to content

Commit

Permalink
fix(client): Report close error even if Complete message followed
Browse files Browse the repository at this point in the history
  • Loading branch information
enisdenjo committed Oct 20, 2021
1 parent 53b6708 commit 27754b2
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
61 changes: 60 additions & 1 deletion src/__tests__/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
stringifyMessage,
SubscribePayload,
} from '../common';
import { startWSTServer as startTServer } from './utils';
import { startRawServer, startWSTServer as startTServer } from './utils';
import { ExecutionResult } from 'graphql';

// silence console.error calls for nicer tests overview
Expand Down Expand Up @@ -480,6 +480,65 @@ it('should close socket with error on malformed request', async (done) => {
);
});

it('should report close error even if complete message followed', async (done) => {
expect.assertions(4);

const { url, server } = await startRawServer();

server.on('connection', (socket) => {
socket.on('message', (data) => {
const msg = parseMessage(String(data));

// acknowledge conneciton
if (msg.type === MessageType.ConnectionInit)
socket.send(stringifyMessage({ type: MessageType.ConnectionAck }));

// respond with a malformed error message and a complete
if (msg.type === MessageType.Subscribe) {
socket.send(
JSON.stringify({
id: msg.id,
type: MessageType.Error,
payload: 'malformed',
}),
);
socket.send(
stringifyMessage({ id: msg.id, type: MessageType.Complete }),
);
}
});
});

const client = createClient({
url,
lazy: false,
retryAttempts: 0,
onNonLazyError: noop,
on: {
closed: (err) => {
expect((err as CloseEvent).code).toBe(CloseCode.BadRequest);
expect((err as CloseEvent).reason).toBe('Invalid message');
},
},
});

client.subscribe(
{
query: 'notaquery',
},
{
next: noop,
error: (err) => {
expect((err as CloseEvent).code).toBe(CloseCode.BadRequest);
expect((err as CloseEvent).reason).toBe('Invalid message');
client.dispose();
done();
},
complete: noop,
},
);
});

describe('ping/pong', () => {
it('should respond with a pong to a ping', async () => {
expect.assertions(1);
Expand Down
1 change: 1 addition & 0 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,7 @@ export function createClient(options: ClientOptions): Client {
),
]);
} catch (err) {
socket.onmessage = null; // stop reading messages as soon as reading breaks once
socket.close(
CloseCode.BadRequest,
err instanceof Error ? err.message : new Error(err).message,
Expand Down

0 comments on commit 27754b2

Please sign in to comment.