Skip to content

Commit 9b935b3

Browse files
committed
remove content-type header if there is no request body
Signed-off-by: Nik Nasr <[email protected]>
1 parent 29b074e commit 9b935b3

File tree

4 files changed

+54
-4
lines changed

4 files changed

+54
-4
lines changed

Diff for: packages/elements-core/src/components/TryIt/Body/useTextRequestBodyState.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ import { useGenerateExampleFromMediaTypeContent } from '../../../utils/exampleGe
1212
export const useTextRequestBodyState = (
1313
mediaTypeContent: IMediaTypeContent | undefined,
1414
skipReadOnly: boolean,
15-
): [string, React.Dispatch<React.SetStateAction<string>>] => {
15+
): [string | undefined, React.Dispatch<React.SetStateAction<string | undefined>>] => {
1616
const initialRequestBody = useGenerateExampleFromMediaTypeContent(mediaTypeContent, undefined, {
1717
skipReadOnly,
1818
});
1919

20-
const [textRequestBody, setTextRequestBody] = React.useState<string>(initialRequestBody);
20+
const [textRequestBody, setTextRequestBody] = React.useState<string | undefined>(initialRequestBody);
2121

2222
React.useEffect(() => {
2323
setTextRequestBody(initialRequestBody);

Diff for: packages/elements-core/src/components/TryIt/TryIt.spec.tsx

+50
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,56 @@ describe('TryIt', () => {
725725
});
726726
});
727727

728+
describe('No Request body', () => {
729+
it('with GET method', async () => {
730+
render(<TryItWithPersistence httpOperation={basicOperation} />);
731+
732+
clickSend();
733+
734+
await waitFor(() => expect(fetchMock).toHaveBeenCalled());
735+
const requestInit = fetchMock.mock.calls[0][1]!;
736+
expect(requestInit.method).toMatch(/^get$/i);
737+
const headers = new Headers(requestInit.headers);
738+
expect(headers.get('Content-Type')).toBe(null);
739+
});
740+
741+
it('with POST method', async () => {
742+
render(<TryItWithPersistence httpOperation={{ ...basicOperation, request: undefined, method: 'POST' }} />);
743+
744+
clickSend();
745+
746+
await waitFor(() => expect(fetchMock).toHaveBeenCalled());
747+
const requestInit = fetchMock.mock.calls[0][1]!;
748+
expect(requestInit.method).toMatch(/^post$/i);
749+
const headers = new Headers(requestInit.headers);
750+
expect(headers.get('Content-Type')).toBe(null);
751+
});
752+
753+
it('with PATCH method', async () => {
754+
render(<TryItWithPersistence httpOperation={{ ...basicOperation, request: undefined, method: 'PATCH' }} />);
755+
756+
clickSend();
757+
758+
await waitFor(() => expect(fetchMock).toHaveBeenCalled());
759+
const requestInit = fetchMock.mock.calls[0][1]!;
760+
expect(requestInit.method).toMatch(/^patch$/i);
761+
const headers = new Headers(requestInit.headers);
762+
expect(headers.get('Content-Type')).toBe(null);
763+
});
764+
765+
it('with PUT method', async () => {
766+
render(<TryItWithPersistence httpOperation={{ ...basicOperation, request: undefined, method: 'PUT' }} />);
767+
768+
clickSend();
769+
770+
await waitFor(() => expect(fetchMock).toHaveBeenCalled());
771+
const requestInit = fetchMock.mock.calls[0][1]!;
772+
expect(requestInit.method).toMatch(/^put$/i);
773+
const headers = new Headers(requestInit.headers);
774+
expect(headers.get('Content-Type')).toBe(null);
775+
});
776+
});
777+
728778
describe('Mocking', () => {
729779
it('Shows mock button', () => {
730780
render(<TryItWithPersistence httpOperation={basicOperation} mockUrl="https://mock-todos.stoplight.io" />);

Diff for: packages/elements-core/src/components/TryIt/TryIt.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ export const TryIt: React.FC<TryItProps> = ({
296296
values={bodyParameterValues}
297297
onChangeValues={setBodyParameterValues}
298298
/>
299-
) : mediaTypeContent ? (
299+
) : mediaTypeContent && textRequestBody !== undefined ? (
300300
<RequestBody
301301
examples={mediaTypeContent.examples ?? []}
302302
requestBody={textRequestBody}

Diff for: packages/elements-core/src/utils/exampleGeneration/exampleGeneration.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export const generateExampleFromMediaTypeContent = (
5757
console.warn(e);
5858
return `Example cannot be created for this schema\n${e}`;
5959
}
60-
return '';
60+
return undefined;
6161
};
6262

6363
export const generateExamplesFromJsonSchema = (schema: JSONSchema7 & { 'x-examples'?: unknown }): Example[] => {

0 commit comments

Comments
 (0)