Skip to content

Commit

Permalink
Updating test cases.
Browse files Browse the repository at this point in the history
  • Loading branch information
mike12345567 committed May 24, 2024
1 parent 223d301 commit 62c407d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
2 changes: 1 addition & 1 deletion packages/server/src/integrations/rest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ class RestIntegration implements IntegrationBase {
if (filename) {
return handleFileResponse(response, filename, this.startTimeMs)
} else {
responseTxt = hasContent ? await response.text() : ""
responseTxt = hasContent && response.text ? await response.text() : ""
if (response.status === 204) {
data = []
raw = ""
Expand Down
25 changes: 17 additions & 8 deletions packages/server/src/integrations/tests/rest.spec.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
jest.mock("node-fetch", () => {
const obj = {
my_next_cursor: 123,
}
const str = JSON.stringify(obj)
return jest.fn(() => ({
headers: {
raw: () => {
return { "content-type": ["application/json"] }
return {
"content-type": ["application/json"],
"content-length": str.length,
}
},
get: (name: string) => {
if (name.toLowerCase() === "content-type") {
const lcName = name.toLowerCase()
if (lcName === "content-type") {
return ["application/json"]
} else if (lcName === "content-length") {
return str.length
}
},
},
json: jest.fn(() => ({
my_next_cursor: 123,
})),
text: jest.fn(),
json: jest.fn(() => obj),
text: jest.fn(() => str),
}))
})

Expand Down Expand Up @@ -231,7 +239,8 @@ describe("REST Integration", () => {
}

it("should be able to parse JSON response", async () => {
const input = buildInput({ a: 1 }, null, "application/json")
const obj = { a: 1 }
const input = buildInput(obj, JSON.stringify(obj), "application/json")
const output = await config.integration.parseResponse(input)
expect(output.data).toEqual({ a: 1 })
expect(output.info.code).toEqual(200)
Expand Down Expand Up @@ -261,7 +270,7 @@ describe("REST Integration", () => {
test.each([...contentTypes, undefined])(
"should not throw an error on 204 no content",
async contentType => {
const input = buildInput(undefined, null, contentType, 204)
const input = buildInput(undefined, "", contentType, 204)
const output = await config.integration.parseResponse(input)
expect(output.data).toEqual([])
expect(output.extra.raw).toEqual("")
Expand Down

0 comments on commit 62c407d

Please sign in to comment.