Skip to content

Commit

Permalink
fix(hub-common): adjust discussions apiRequest response for text from… (
Browse files Browse the repository at this point in the history
  • Loading branch information
velveetachef authored Jan 29, 2025
1 parent 1914881 commit 69c0a9e
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/common/src/discussions/api/utils/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export function apiRequest<T>(
// this currently only applies to the search post route. we should rework things in the future such that we don't need
// to do this sort of evaluation in common logic.
const isCSV =
route === "/posts" &&
["/posts", "/posts/search"].includes(route) &&
(options.data?.f === SearchPostsFormat.CSV ||
headers.get("Accept") === "text/csv");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ describe("apiRequest", () => {
expect(calledOpts).toEqual(expectedOpts);
});

it(`cleans up baseUrl and enpoint`, async () => {
it(`cleans up baseUrl and endpoint`, async () => {
const options = { ...opts, hubApiUrl: `${hubApiUrl}/` };
const result = await utils.apiRequest(
`/${url}`,
Expand Down Expand Up @@ -224,7 +224,7 @@ describe("apiRequest", () => {
expect(calledOpts).toEqual(expectedOpts);
});

it("resolves plain-text when f=csv for search posts route", async () => {
it("resolves plain-text when f=csv for GET search posts route", async () => {
url = "/posts";
const options = {
data: {
Expand All @@ -243,7 +243,30 @@ describe("apiRequest", () => {
expect(calledOpts).toEqual(expectedOpts);
});

it('resolves plain-text when HTTP "Accept" header has a value of "text/csv" for search posts route', async () => {
it("resolves plain-text when f=csv for POST search posts route", async () => {
url = "/posts/search";
const options = {
data: {
f: SearchPostsFormat.CSV,
},
httpMethod: "POST",
} as IDiscussionsRequestOptions;
expectedOpts.method = "POST";
expectedOpts.body = JSON.stringify({
f: SearchPostsFormat.CSV,
});
const result = await utils.apiRequest(url, options);

expect(result).toEqual(JSON.stringify(response));

const [calledUrl, calledOpts] = fetchMock.calls()[0];
expect(calledUrl).toEqual(
"https://hub.arcgis.com/api/discussions/v1/posts/search"
);
expect(calledOpts).toEqual(expectedOpts);
});

it('resolves plain-text when HTTP "Accept" header has a value of "text/csv" for GET search posts route', async () => {
url = "/posts";
const options = {
headers: {
Expand All @@ -267,4 +290,30 @@ describe("apiRequest", () => {
);
expect(calledOpts).toEqual(expectedOpts);
});

it('resolves plain-text when HTTP "Accept" header has a value of "text/csv" for POST search posts route', async () => {
url = "/posts/search";
const options = {
headers: {
Accept: "text/csv",
},
httpMethod: "POST",
} as IDiscussionsRequestOptions;
expectedOpts.method = "POST";
const result = await utils.apiRequest(url, options);
const expectedHeaders = new Headers(expectedOpts.headers);
expectedHeaders.set("accept", "text/csv");
expectedOpts = {
...expectedOpts,
headers: expectedHeaders,
};

expect(result).toEqual(JSON.stringify(response));

const [calledUrl, calledOpts] = fetchMock.calls()[0];
expect(calledUrl).toEqual(
"https://hub.arcgis.com/api/discussions/v1/posts/search"
);
expect(calledOpts).toEqual(expectedOpts);
});
});

0 comments on commit 69c0a9e

Please sign in to comment.