Skip to content

Commit

Permalink
fix(elements-core): Do not omit request body for certain HTTP methods
Browse files Browse the repository at this point in the history
  • Loading branch information
provokateurin committed Jul 12, 2024
1 parent 663a0d1 commit ff22efd
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
24 changes: 18 additions & 6 deletions packages/elements-core/src/components/TryIt/TryIt.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ describe('TryIt', () => {
const requestInit = fetchMock.mock.calls[0][1]!;
expect(requestInit.method).toMatch(/^get$/i);
const headers = new Headers(requestInit.headers);
expect(headers.get('Content-Type')).toBe(null);
expect(headers.get('Content-Type')).toBe('application/json');
});

it('uses cors proxy url, if provided', async () => {
Expand Down Expand Up @@ -537,16 +537,14 @@ describe('TryIt', () => {
await waitFor(() => expect(fetchMock).toHaveBeenCalled());
expect(fetchMock.mock.calls[0]![1]!.body).toEqual(expect.stringMatching(/{.*}/s));
});
});

describe('is not attached', () => {
it('to operation with HEAD method', async () => {
render(<TryItWithPersistence httpOperation={headWithRequestBody} />);

clickSend();

await waitFor(() => expect(fetchMock).toHaveBeenCalled());
expect(typeof fetchMock.mock.calls[0]![1]!.body).not.toBe('string');
expect(fetchMock.mock.calls[0]![1]!.body).toEqual(expect.stringMatching(/{.*}/s));
});
});

Expand Down Expand Up @@ -726,14 +724,21 @@ describe('TryIt', () => {
method: 'GET',
headers: {
Prefer: 'code=200',
'Content-Type': 'application/json',
},
body: '',
credentials: 'omit',
}),
],
[
'https://todos.stoplight.io/todos',
expect.objectContaining({
method: 'GET',
headers: {},
headers: {
'Content-Type': 'application/json',
},
body: '',
credentials: 'omit',
}),
],
]);
Expand All @@ -760,7 +765,11 @@ describe('TryIt', () => {
'https://mock-todos.stoplight.io/todos',
expect.objectContaining({
method: 'GET',
headers: {},
headers: {
'Content-Type': 'application/json',
},
body: '',
credentials: 'omit',
}),
],
]);
Expand Down Expand Up @@ -821,8 +830,11 @@ describe('TryIt', () => {
expect.objectContaining({
method: 'GET',
headers: {
'Content-Type': 'application/json',
Prefer: 'code=200',
},
body: '',
credentials: 'omit',
}),
);
});
Expand Down
6 changes: 2 additions & 4 deletions packages/elements-core/src/components/TryIt/build-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,7 @@ export async function buildFetchRequest({
}: BuildRequestInput): Promise<Parameters<typeof fetch>> {
const serverUrl = getServerUrl({ httpOperation, mockData, chosenServer, corsProxy, serverVariableValues });

const shouldIncludeBody =
['PUT', 'POST', 'PATCH'].includes(httpOperation.method.toUpperCase()) && bodyInput !== undefined;
const shouldIncludeBody = bodyInput !== undefined;

const queryParams = getQueryParams({ httpOperation, parameterValues });

Expand Down Expand Up @@ -249,8 +248,7 @@ export async function buildHarRequest({
const serverUrl = getServerUrl({ httpOperation, mockData, chosenServer, corsProxy, serverVariableValues });

const mimeType = mediaTypeContent?.mediaType ?? 'application/json';
const shouldIncludeBody =
['PUT', 'POST', 'PATCH'].includes(httpOperation.method.toUpperCase()) && bodyInput !== undefined;
const shouldIncludeBody = bodyInput !== undefined;

const queryParams = getQueryParams({ httpOperation, parameterValues });

Expand Down

0 comments on commit ff22efd

Please sign in to comment.