Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly parse response bodies as JSON where the Content-Type is application/scim+json #731

Merged
merged 5 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/fetch-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ async function getResponseData(response: Response): Promise<any> {

const mimetype = safeParse(contentType);

if (mimetype.type === "application/json") {
if (isJSONResponse(mimetype)) {
let text = "";
try {
text = await response.text();
Expand All @@ -173,6 +173,10 @@ async function getResponseData(response: Response): Promise<any> {
}
}

function isJSONResponse(mimetype: { type: string }) {
timrogers marked this conversation as resolved.
Show resolved Hide resolved
return mimetype.type === "application/json" || mimetype.type === "application/scim+json";
}

function toErrorMessage(data: string | ArrayBuffer | Record<string, unknown>) {
if (typeof data === "string") {
return data;
Expand Down
39 changes: 38 additions & 1 deletion test/request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -869,7 +869,7 @@ x//0u+zd/R/QRUzLOw4N72/Hu+UG6MNt5iDZFCtapRaKt6OvSBwy8w==
required_pull_request_reviews: {
required_approving_review_count: 1,
dismiss_stale_reviews: true,
require_code_owner_reviews: true,
require_code owner_reviews: true,
dismissal_restrictions: { users: [], teams: [] },
},
restrictions: { users: [], teams: [] },
Expand Down Expand Up @@ -1263,4 +1263,41 @@ x//0u+zd/R/QRUzLOw4N72/Hu+UG6MNt5iDZFCtapRaKt6OvSBwy8w==
}),
).rejects.toHaveProperty("message", "Unknown error: {}");
});

it("parses response bodies as JSON when Content-Type is application/scim+json", async () => {
expect.assertions(1);

const mock = fetchMock.createInstance();
mock.get("https://api.github.com/scim/v2/Users", {
status: 200,
body: {
totalResults: 1,
Resources: [
{
id: "123",
userName: "octocat",
},
],
},
headers: {
"Content-Type": "application/scim+json",
},
});

const response = await request("GET /scim/v2/Users", {
request: {
fetch: mock.fetchHandler,
},
});

expect(response.data).toEqual({
totalResults: 1,
Resources: [
{
id: "123",
userName: "octocat",
},
],
});
});
});
Loading