Skip to content

Commit

Permalink
Test conversion to/from widget error payloads
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewFerr committed Nov 8, 2024
1 parent 2cecdd1 commit b9591c1
Showing 1 changed file with 93 additions and 2 deletions.
95 changes: 93 additions & 2 deletions spec/unit/http-api/errors.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ describe("MatrixError", () => {
headers = new Headers({ "Content-Type": "application/json" });
});

function makeMatrixError(httpStatus: number, data: IErrorJson): MatrixError {
return new MatrixError(data, httpStatus, undefined, undefined, headers);
function makeMatrixError(httpStatus: number, data: IErrorJson, url?: string): MatrixError {
return new MatrixError(data, httpStatus, url, undefined, headers);
}

it("should accept absent retry time from rate-limit error", () => {
Expand Down Expand Up @@ -95,4 +95,95 @@ describe("MatrixError", () => {
const err = makeMatrixError(429, { errcode: "M_LIMIT_EXCEEDED" });
expect(() => err.getRetryAfterMs()).toThrow("integer value is too large");
});

describe("can be converted to data compatible with the widget api", () => {
it("from default values", () => {
const matrixError = new MatrixError();

const widgetApiErrorData = {
http_status: 400,
http_headers: {},
url: "",
response: {
errcode: "M_UNKNOWN",
error: "Unknown message",
},
};

expect(matrixError.asWidgetApiErrorData()).toEqual(widgetApiErrorData);
});

it("from non-default values", () => {
headers.set("Retry-After", "120");
const statusCode = 429;
const data = {
errcode: "M_LIMIT_EXCEEDED",
error: "Request is rate-limited.",
retry_after_ms: 120000,
};
const url = "http://example.net";

const matrixError = makeMatrixError(statusCode, data, url);

const widgetApiErrorData = {
http_status: statusCode,
http_headers: {
"content-type": "application/json",
"retry-after": "120",
},
url,
response: data,
};

expect(matrixError.asWidgetApiErrorData()).toEqual(widgetApiErrorData);
});
});

describe("can be created from data received from the widget api", () => {
it("from minimal data", () => {
const statusCode = 400;
const data = {
errcode: "M_UNKNOWN",
error: "Something went wrong.",
};
const url = "";

const widgetApiErrorData = {
http_status: statusCode,
http_headers: {},
url,
response: data,
};

headers.delete("Content-Type");
const matrixError = makeMatrixError(statusCode, data, url);

expect(MatrixError.fromWidgetApiErrorData(widgetApiErrorData)).toEqual(matrixError);
});

it("from more data", () => {
const statusCode = 429;
const data = {
errcode: "M_LIMIT_EXCEEDED",
error: "Request is rate-limited.",
retry_after_ms: 120000,
};
const url = "http://example.net";

const widgetApiErrorData = {
http_status: statusCode,
http_headers: {
"content-type": "application/json",
"retry-after": "120",
},
url,
response: data,
};

headers.set("Retry-After", "120");
const matrixError = makeMatrixError(statusCode, data, url);

expect(MatrixError.fromWidgetApiErrorData(widgetApiErrorData)).toEqual(matrixError);
});
});
});

0 comments on commit b9591c1

Please sign in to comment.