|
| 1 | +import { expect } from "chai"; |
| 2 | +import * as sinon from "sinon"; |
| 3 | +import * as https from "../../../src/v1/providers/https"; |
| 4 | +import * as logger from "../../../src/logger"; |
| 5 | +import { MockRequest } from "../../fixtures/mockrequest"; |
| 6 | +import { runHandler } from "../../helper"; |
| 7 | + |
| 8 | +describe("CloudHttpsBuilder async onRequest", () => { |
| 9 | + let loggerSpy: sinon.SinonSpy; |
| 10 | + |
| 11 | + beforeEach(() => { |
| 12 | + loggerSpy = sinon.spy(logger, "error"); |
| 13 | + }); |
| 14 | + |
| 15 | + afterEach(() => { |
| 16 | + loggerSpy.restore(); |
| 17 | + }); |
| 18 | + |
| 19 | + it("should catch and log unhandled rejections in async onRequest handlers", async () => { |
| 20 | + const err = new Error("boom"); |
| 21 | + const fn = https.onRequest(async (_req, _res) => { |
| 22 | + await Promise.resolve(); |
| 23 | + throw err; |
| 24 | + }); |
| 25 | + |
| 26 | + const req = new MockRequest({}, {}); |
| 27 | + req.method = "GET"; |
| 28 | + |
| 29 | + const result = await runHandler(fn, req as any); |
| 30 | + |
| 31 | + expect(loggerSpy.calledWith("Unhandled error", err)).to.be.true; |
| 32 | + expect(result.status).to.equal(500); |
| 33 | + expect(result.body).to.equal("Internal Server Error"); |
| 34 | + }); |
| 35 | + |
| 36 | + it("should not log if handler completes successfully", async () => { |
| 37 | + const fn = https.onRequest(async (_req, res) => { |
| 38 | + await Promise.resolve(); |
| 39 | + res.send(200); |
| 40 | + }); |
| 41 | + |
| 42 | + const req = new MockRequest({}, {}); |
| 43 | + req.method = "GET"; |
| 44 | + |
| 45 | + await runHandler(fn, req as any); |
| 46 | + |
| 47 | + expect(loggerSpy.called).to.be.false; |
| 48 | + }); |
| 49 | +}); |
0 commit comments