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 29, 2024
1 parent b6d8236 commit de037dc
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 13 deletions.
2 changes: 1 addition & 1 deletion packages/elements-core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@stoplight/elements-core",
"version": "8.3.3",
"version": "8.3.4",
"sideEffects": [
"web-components.min.js",
"src/web-components/**",
Expand Down
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
2 changes: 1 addition & 1 deletion packages/elements-dev-portal/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@stoplight/elements-dev-portal",
"version": "2.3.3",
"version": "2.3.4",
"description": "UI components for composing beautiful developer documentation.",
"keywords": [],
"sideEffects": [
Expand Down
2 changes: 1 addition & 1 deletion packages/elements/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@stoplight/elements",
"version": "8.3.3",
"version": "8.3.4",
"description": "UI components for composing beautiful developer documentation.",
"keywords": [],
"sideEffects": [
Expand Down

0 comments on commit de037dc

Please sign in to comment.