Skip to content

Commit

Permalink
remove content-type header if there is no request body
Browse files Browse the repository at this point in the history
Signed-off-by: Nik Nasr <[email protected]>
  • Loading branch information
nikrooz committed Dec 12, 2024
1 parent be11c9a commit 9786f33
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ import { useGenerateExampleFromMediaTypeContent } from '../../../utils/exampleGe
export const useTextRequestBodyState = (
mediaTypeContent: IMediaTypeContent | undefined,
skipReadOnly: boolean,
): [string, React.Dispatch<React.SetStateAction<string>>] => {
): [string | undefined, React.Dispatch<React.SetStateAction<string | undefined>>] => {
const initialRequestBody = useGenerateExampleFromMediaTypeContent(mediaTypeContent, undefined, {
skipReadOnly,
});

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

React.useEffect(() => {
setTextRequestBody(initialRequestBody);
Expand Down
50 changes: 50 additions & 0 deletions packages/elements-core/src/components/TryIt/TryIt.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,56 @@ describe('TryIt', () => {
});
});

describe('No Request body', () => {
it('with GET method', async () => {
render(<TryItWithPersistence httpOperation={basicOperation} />);

clickSend();

await waitFor(() => expect(fetchMock).toHaveBeenCalled());
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);
});

it('with POST method', async () => {
render(<TryItWithPersistence httpOperation={{ ...basicOperation, request: undefined, method: 'POST' }} />);

clickSend();

await waitFor(() => expect(fetchMock).toHaveBeenCalled());
const requestInit = fetchMock.mock.calls[0][1]!;
expect(requestInit.method).toMatch(/^post$/i);
const headers = new Headers(requestInit.headers);
expect(headers.get('Content-Type')).toBe(null);
});

it('with PATCH method', async () => {
render(<TryItWithPersistence httpOperation={{ ...basicOperation, request: undefined, method: 'PATCH' }} />);

clickSend();

await waitFor(() => expect(fetchMock).toHaveBeenCalled());
const requestInit = fetchMock.mock.calls[0][1]!;
expect(requestInit.method).toMatch(/^patch$/i);
const headers = new Headers(requestInit.headers);
expect(headers.get('Content-Type')).toBe(null);
});

it('with PUT method', async () => {
render(<TryItWithPersistence httpOperation={{ ...basicOperation, request: undefined, method: 'PUT' }} />);

clickSend();

await waitFor(() => expect(fetchMock).toHaveBeenCalled());
const requestInit = fetchMock.mock.calls[0][1]!;
expect(requestInit.method).toMatch(/^put$/i);
const headers = new Headers(requestInit.headers);
expect(headers.get('Content-Type')).toBe(null);
});
});

describe('Mocking', () => {
it('Shows mock button', () => {
render(<TryItWithPersistence httpOperation={basicOperation} mockUrl="https://mock-todos.stoplight.io" />);
Expand Down
2 changes: 1 addition & 1 deletion packages/elements-core/src/components/TryIt/TryIt.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ export const TryIt: React.FC<TryItProps> = ({
values={bodyParameterValues}
onChangeValues={setBodyParameterValues}
/>
) : mediaTypeContent ? (
) : mediaTypeContent && textRequestBody !== undefined ? (
<RequestBody
examples={mediaTypeContent.examples ?? []}
requestBody={textRequestBody}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export const generateExampleFromMediaTypeContent = (
console.warn(e);
return `Example cannot be created for this schema\n${e}`;
}
return '';
return undefined;
};

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

0 comments on commit 9786f33

Please sign in to comment.