Skip to content

Commit 003002f

Browse files
committed
Fix tests.
1 parent 7ce0c17 commit 003002f

File tree

4 files changed

+53
-17
lines changed

4 files changed

+53
-17
lines changed

spec/common/providers/https.spec.ts

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -803,6 +803,33 @@ describe("onCallHandler", () => {
803803
expect(resp.body).to.equal([...data, ""].join("\n"));
804804
});
805805

806+
it("stops processing when client disconnects", async () => {
807+
const mockReq = mockRequest(
808+
{ message: "test abort" },
809+
"application/json",
810+
{},
811+
{ accept: "text/event-stream" }
812+
) as any;
813+
814+
815+
const fn = https.onCallHandler(
816+
{
817+
cors: { origin: true, methods: "POST" },
818+
},
819+
async (req, resp) => {
820+
resp.write("initial message");
821+
mockReq.emit('close');
822+
resp.write("should not be sent");
823+
return "done";
824+
},
825+
"gcfv2"
826+
);
827+
828+
const resp = await runHandler(fn, mockReq);
829+
830+
expect(resp.body).to.equal(`data: {"message":"initial message"}\n`);
831+
});
832+
806833
describe("Heartbeats", () => {
807834
let clock: sinon.SinonFakeTimers;
808835

@@ -836,7 +863,7 @@ describe("onCallHandler", () => {
836863
);
837864

838865
const handlerPromise = runHandler(fn, mockReq as any);
839-
clock.tick(11_000);
866+
await clock.tickAsync(11_000)
840867
const resp = await handlerPromise;
841868
expect(resp.body).to.include(': ping\n: ping\ndata: {"result":"done"}');
842869
});
@@ -862,7 +889,7 @@ describe("onCallHandler", () => {
862889
);
863890

864891
const handlerPromise = runHandler(fn, mockReq as any);
865-
clock.tick(31_000);
892+
await clock.tickAsync(31_000);
866893
const resp = await handlerPromise;
867894
expect(resp.body).to.include('data: {"result":"done"}');
868895
});

spec/fixtures/mockrequest.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
1+
import { EventEmitter } from 'node:stream';
2+
13
import * as jwt from 'jsonwebtoken';
24
import * as jwkToPem from 'jwk-to-pem';
35
import * as nock from 'nock';
46
import * as mockJWK from '../fixtures/credential/jwk.json';
57
import * as mockKey from '../fixtures/credential/key.json';
68

79
// MockRequest mocks an https.Request.
8-
export class MockRequest {
10+
export class MockRequest extends EventEmitter {
911
public method: 'POST' | 'GET' | 'OPTIONS' = 'POST';
1012

1113
constructor(
1214
readonly body: any,
1315
readonly headers: { [name: string]: string }
1416
) {
15-
// This block intentionally left blank.
17+
super()
1618
}
1719

1820
public header(name: string): string {

spec/helper.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ export function runHandler(
5252
private headers: { [name: string]: string } = {};
5353
private callback: () => void;
5454

55+
constructor() {
56+
request.on("close", () => this.end())
57+
}
58+
5559
public status(code: number) {
5660
this.statusCode = code;
5761
return this;

src/common/providers/https.ts

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -871,29 +871,32 @@ function wrapOnCallHandler<Req = any, Res = any>(
871871

872872
// If there was some result, encode it in the body.
873873
const responseBody: HttpResponseBody = { result };
874+
874875
if (acceptsStreaming) {
875876
res.write(encodeSSE(responseBody));
876877
res.end();
877878
} else {
878879
res.status(200).send(responseBody);
879880
}
880881
} catch (err) {
881-
let httpErr = err;
882-
if (!(err instanceof HttpsError)) {
883-
// This doesn't count as an 'explicit' error.
884-
logger.error("Unhandled error", err);
885-
httpErr = new HttpsError("internal", "INTERNAL");
886-
}
882+
if (!abortController.signal.aborted) {
883+
let httpErr = err;
884+
if (!(err instanceof HttpsError)) {
885+
// This doesn't count as an 'explicit' error.
886+
logger.error("Unhandled error", err);
887+
httpErr = new HttpsError("internal", "INTERNAL");
888+
}
887889

888-
const { status } = httpErr.httpErrorCode;
889-
const body = { error: httpErr.toJSON() };
890-
if (req.header("accept") === "text/event-stream") {
891-
res.send(encodeSSE(body));
892-
} else {
893-
res.status(status).send(body);
890+
const { status } = httpErr.httpErrorCode;
891+
const body = { error: httpErr.toJSON() };
892+
if (req.header("accept") === "text/event-stream") {
893+
res.send(encodeSSE(body));
894+
} else {
895+
res.status(status).send(body);
896+
}
894897
}
895898
} finally {
896-
cleanup()
899+
cleanup();
897900
}
898901
};
899902
}

0 commit comments

Comments
 (0)