test case
This commit is contained in:
1 parent
30ce3f6bf4
commit
29b68b8c87
1 file changed
+81
-31
@@ -78,13 +78,15 @@ const invokeBuildStatusResponse = <T>(
|
|||||||
response: Response<T> | null,
|
response: Response<T> | null,
|
||||||
error: any | null
|
error: any | null
|
||||||
) =>
|
) =>
|
||||||
(service as unknown as {
|
(
|
||||||
buildStatusResponse: (
|
service as unknown as {
|
||||||
monitor: IMonitor,
|
buildStatusResponse: (
|
||||||
response: Response<T> | null,
|
monitor: IMonitor,
|
||||||
error: any | null
|
response: Response<T> | null,
|
||||||
) => StatusResponse<T>;
|
error: any | null
|
||||||
}).buildStatusResponse(monitor, response, error);
|
) => StatusResponse<T>;
|
||||||
|
}
|
||||||
|
).buildStatusResponse(monitor, response, error);
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
const gotModule = getGotModule();
|
const gotModule = getGotModule();
|
||||||
@@ -141,6 +143,22 @@ describe("NetworkService.buildStatusResponse", () => {
|
|||||||
expect(result.timings).toEqual({ phases: {} });
|
expect(result.timings).toEqual({ phases: {} });
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("returns default error payload for generic failures without message", () => {
|
||||||
|
const { service } = createService();
|
||||||
|
const monitor = buildMonitor();
|
||||||
|
const error = new Error("timeout");
|
||||||
|
error.message = "";
|
||||||
|
|
||||||
|
const result = invokeBuildStatusResponse(service, monitor, null, error);
|
||||||
|
|
||||||
|
expect(result.monitorId).toBe(monitor._id.toString());
|
||||||
|
expect(result.status).toBe("down");
|
||||||
|
expect(result.code).toBe(5000);
|
||||||
|
expect(result.message).toBe("Network error");
|
||||||
|
expect(result.responseTime).toBe(0);
|
||||||
|
expect(result.timings).toEqual({ phases: {} });
|
||||||
|
});
|
||||||
|
|
||||||
it("honors HTTPError metadata", () => {
|
it("honors HTTPError metadata", () => {
|
||||||
const { service, gotModule } = createService();
|
const { service, gotModule } = createService();
|
||||||
const { HTTPError } = gotModule;
|
const { HTTPError } = gotModule;
|
||||||
@@ -190,7 +208,9 @@ describe("NetworkService.requestHttp", () => {
|
|||||||
const { service, extendMock } = createService(requestMock);
|
const { service, extendMock } = createService(requestMock);
|
||||||
const monitor = buildMonitor({ url: undefined as unknown as string });
|
const monitor = buildMonitor({ url: undefined as unknown as string });
|
||||||
|
|
||||||
await expect(service.requestHttp(monitor)).rejects.toThrow("No URL provided");
|
await expect(service.requestHttp(monitor)).rejects.toThrow(
|
||||||
|
"No URL provided"
|
||||||
|
);
|
||||||
expect(extendMock).toHaveBeenCalledTimes(1);
|
expect(extendMock).toHaveBeenCalledTimes(1);
|
||||||
expect(requestMock).not.toHaveBeenCalled();
|
expect(requestMock).not.toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
@@ -250,9 +270,14 @@ describe("NetworkService.requestInfrastructure", () => {
|
|||||||
it("throws when monitor is missing a URL", async () => {
|
it("throws when monitor is missing a URL", async () => {
|
||||||
const requestMock = jest.fn();
|
const requestMock = jest.fn();
|
||||||
const { service, extendMock } = createService(requestMock);
|
const { service, extendMock } = createService(requestMock);
|
||||||
const monitor = buildMonitor({ type: "infrastructure" as any, url: undefined as any });
|
const monitor = buildMonitor({
|
||||||
|
type: "infrastructure" as any,
|
||||||
|
url: undefined as any,
|
||||||
|
});
|
||||||
|
|
||||||
await expect(service.requestInfrastructure(monitor)).rejects.toThrow("No URL provided");
|
await expect(service.requestInfrastructure(monitor)).rejects.toThrow(
|
||||||
|
"No URL provided"
|
||||||
|
);
|
||||||
expect(extendMock).toHaveBeenCalledTimes(1);
|
expect(extendMock).toHaveBeenCalledTimes(1);
|
||||||
expect(requestMock).not.toHaveBeenCalled();
|
expect(requestMock).not.toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
@@ -260,7 +285,10 @@ describe("NetworkService.requestInfrastructure", () => {
|
|||||||
it("throws when monitor is missing a secret", async () => {
|
it("throws when monitor is missing a secret", async () => {
|
||||||
const requestMock = jest.fn();
|
const requestMock = jest.fn();
|
||||||
const { service, extendMock } = createService(requestMock);
|
const { service, extendMock } = createService(requestMock);
|
||||||
const monitor = buildMonitor({ type: "infrastructure" as any, secret: undefined as any });
|
const monitor = buildMonitor({
|
||||||
|
type: "infrastructure" as any,
|
||||||
|
secret: undefined as any,
|
||||||
|
});
|
||||||
|
|
||||||
await expect(service.requestInfrastructure(monitor)).rejects.toThrow(
|
await expect(service.requestInfrastructure(monitor)).rejects.toThrow(
|
||||||
"No secret provided for infrastructure monitor"
|
"No secret provided for infrastructure monitor"
|
||||||
@@ -293,7 +321,9 @@ describe("NetworkService.requestInfrastructure", () => {
|
|||||||
});
|
});
|
||||||
expect(result.status).toBe("down");
|
expect(result.status).toBe("down");
|
||||||
expect(result.code).toBe(5000);
|
expect(result.code).toBe(5000);
|
||||||
expect(result.message).toBe("No payload received from infrastructure monitor");
|
expect(result.message).toBe(
|
||||||
|
"No payload received from infrastructure monitor"
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("returns fallback response when infrastructure request rejects", async () => {
|
it("returns fallback response when infrastructure request rejects", async () => {
|
||||||
@@ -368,9 +398,14 @@ describe("NetworkService.requestPagespeed", () => {
|
|||||||
it("throws when monitor lacks URL", async () => {
|
it("throws when monitor lacks URL", async () => {
|
||||||
const requestMock = jest.fn();
|
const requestMock = jest.fn();
|
||||||
const { service, extendMock } = createService(requestMock);
|
const { service, extendMock } = createService(requestMock);
|
||||||
const monitor = buildMonitor({ type: "pagespeed" as any, url: undefined as any });
|
const monitor = buildMonitor({
|
||||||
|
type: "pagespeed" as any,
|
||||||
|
url: undefined as any,
|
||||||
|
});
|
||||||
|
|
||||||
await expect(service.requestPagespeed(monitor)).rejects.toThrow("No URL provided");
|
await expect(service.requestPagespeed(monitor)).rejects.toThrow(
|
||||||
|
"No URL provided"
|
||||||
|
);
|
||||||
expect(extendMock).toHaveBeenCalledTimes(1);
|
expect(extendMock).toHaveBeenCalledTimes(1);
|
||||||
expect(requestMock).not.toHaveBeenCalled();
|
expect(requestMock).not.toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
@@ -383,7 +418,10 @@ describe("NetworkService.requestPagespeed", () => {
|
|||||||
.mockRejectedValueOnce(error)
|
.mockRejectedValueOnce(error)
|
||||||
.mockResolvedValueOnce({ body: payload });
|
.mockResolvedValueOnce({ body: payload });
|
||||||
const { service, extendMock } = createService(requestMock);
|
const { service, extendMock } = createService(requestMock);
|
||||||
const monitor = buildMonitor({ type: "pagespeed" as any, url: "https://s" });
|
const monitor = buildMonitor({
|
||||||
|
type: "pagespeed" as any,
|
||||||
|
url: "https://s",
|
||||||
|
});
|
||||||
|
|
||||||
const result = await service.requestPagespeed(monitor);
|
const result = await service.requestPagespeed(monitor);
|
||||||
|
|
||||||
@@ -406,7 +444,10 @@ describe("NetworkService.requestPagespeed", () => {
|
|||||||
.mockResolvedValueOnce(primaryResponse)
|
.mockResolvedValueOnce(primaryResponse)
|
||||||
.mockResolvedValueOnce({ body: undefined });
|
.mockResolvedValueOnce({ body: undefined });
|
||||||
const { service, extendMock } = createService(requestMock);
|
const { service, extendMock } = createService(requestMock);
|
||||||
const monitor = buildMonitor({ type: "pagespeed" as any, url: "https://site.example.com" });
|
const monitor = buildMonitor({
|
||||||
|
type: "pagespeed" as any,
|
||||||
|
url: "https://site.example.com",
|
||||||
|
});
|
||||||
|
|
||||||
await expect(service.requestPagespeed(monitor)).rejects.toThrow(
|
await expect(service.requestPagespeed(monitor)).rejects.toThrow(
|
||||||
"No payload received from pagespeed monitor"
|
"No payload received from pagespeed monitor"
|
||||||
@@ -420,7 +461,10 @@ describe("NetworkService.requestPagespeed", () => {
|
|||||||
|
|
||||||
describe("NetworkService.requestPing", () => {
|
describe("NetworkService.requestPing", () => {
|
||||||
it("maps successful ping with string time", async () => {
|
it("maps successful ping with string time", async () => {
|
||||||
(ping.promise.probe as jest.Mock).mockResolvedValue({ alive: true, time: "12.5" });
|
(ping.promise.probe as jest.Mock).mockResolvedValue({
|
||||||
|
alive: true,
|
||||||
|
time: "12.5",
|
||||||
|
});
|
||||||
const { service } = createService();
|
const { service } = createService();
|
||||||
const monitor = buildMonitor({ type: "ping" as any, url: "8.8.8.8" });
|
const monitor = buildMonitor({ type: "ping" as any, url: "8.8.8.8" });
|
||||||
|
|
||||||
@@ -433,7 +477,10 @@ describe("NetworkService.requestPing", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("maps failure ping with invalid time to zero", async () => {
|
it("maps failure ping with invalid time to zero", async () => {
|
||||||
(ping.promise.probe as jest.Mock).mockResolvedValue({ alive: false, time: "NaN" });
|
(ping.promise.probe as jest.Mock).mockResolvedValue({
|
||||||
|
alive: false,
|
||||||
|
time: "NaN",
|
||||||
|
});
|
||||||
const { service } = createService();
|
const { service } = createService();
|
||||||
const monitor = buildMonitor({ type: "ping" as any, url: "1.1.1.1" });
|
const monitor = buildMonitor({ type: "ping" as any, url: "1.1.1.1" });
|
||||||
|
|
||||||
@@ -445,7 +492,10 @@ describe("NetworkService.requestPing", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("handles numeric ping times", async () => {
|
it("handles numeric ping times", async () => {
|
||||||
(ping.promise.probe as jest.Mock).mockResolvedValue({ alive: true, time: 18 });
|
(ping.promise.probe as jest.Mock).mockResolvedValue({
|
||||||
|
alive: true,
|
||||||
|
time: 18,
|
||||||
|
});
|
||||||
const { service } = createService();
|
const { service } = createService();
|
||||||
const monitor = buildMonitor({ type: "ping" as any, url: "9.9.9.9" });
|
const monitor = buildMonitor({ type: "ping" as any, url: "9.9.9.9" });
|
||||||
|
|
||||||
@@ -536,17 +586,15 @@ describe("NetworkService.requestStatus", () => {
|
|||||||
|
|
||||||
it("routes ping monitors through requestPing", async () => {
|
it("routes ping monitors through requestPing", async () => {
|
||||||
const { service } = createService();
|
const { service } = createService();
|
||||||
const pingSpy = jest
|
const pingSpy = jest.spyOn(service, "requestPing").mockResolvedValueOnce({
|
||||||
.spyOn(service, "requestPing")
|
monitorId: "5",
|
||||||
.mockResolvedValueOnce({
|
teamId: "t",
|
||||||
monitorId: "5",
|
type: "ping" as any,
|
||||||
teamId: "t",
|
status: "down",
|
||||||
type: "ping" as any,
|
message: "Timeout",
|
||||||
status: "down",
|
responseTime: 0,
|
||||||
message: "Timeout",
|
timings: { phases: {} } as any,
|
||||||
responseTime: 0,
|
});
|
||||||
timings: { phases: {} } as any,
|
|
||||||
});
|
|
||||||
const monitor = buildMonitor({ type: "ping" as any });
|
const monitor = buildMonitor({ type: "ping" as any });
|
||||||
|
|
||||||
await service.requestStatus(monitor);
|
await service.requestStatus(monitor);
|
||||||
@@ -558,6 +606,8 @@ describe("NetworkService.requestStatus", () => {
|
|||||||
const { service } = createService();
|
const { service } = createService();
|
||||||
const monitor = buildMonitor({ type: "websocket" as any });
|
const monitor = buildMonitor({ type: "websocket" as any });
|
||||||
|
|
||||||
await expect(service.requestStatus(monitor)).rejects.toThrow("Not implemented");
|
await expect(service.requestStatus(monitor)).rejects.toThrow(
|
||||||
|
"Not implemented"
|
||||||
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
Reference in new issue
Block a user