-
Notifications
You must be signed in to change notification settings - Fork 13.8k
fix: send body in api client delete method #41349
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Rohit3523
wants to merge
5
commits into
develop
Choose a base branch
from
fix/api-client-delete-body
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+105
−4
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ebdd9aa
fix: send body in RestClient.delete so DELETE endpoints like push.tok…
Rohit3523 b121c41
chore: add changeset for RestClient.delete body fix
Rohit3523 5bfa63e
Merge branch 'develop' into fix/api-client-delete-body
Rohit3523 adfd9be
some changes
Rohit3523 2716824
added test
Rohit3523 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@rocket.chat/api-client': patch | ||
| --- | ||
|
|
||
| Fixes `RestClient.delete` to send `params` as a request body, matching `post`/`put`. Previously the body was silently dropped, breaking DELETE endpoints that expect a body. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| import fetchMock from 'jest-fetch-mock'; | ||
|
|
||
| import { RestClient } from '../src/index'; | ||
|
|
||
| beforeAll(() => { | ||
| fetchMock.enableMocks(); | ||
| }); | ||
|
|
||
| afterAll(() => { | ||
| fetchMock.disableMocks(); | ||
| }); | ||
|
|
||
| beforeEach(() => { | ||
| fetchMock.resetMocks(); | ||
| fetchMock.doMock(); | ||
| fetchMock.mockResponse(JSON.stringify({ status: 'success' })); | ||
| }); | ||
|
|
||
| const lastCall = () => { | ||
| const { calls } = fetchMock.mock; | ||
| expect(calls.length).toBeGreaterThan(0); | ||
| return calls[calls.length - 1]; | ||
| }; | ||
|
|
||
| const methodOf = (call: any) => (call[1] as RequestInit).method; | ||
| const bodyOf = (call: any) => (call[1] as RequestInit).body; | ||
| const headerOf = (call: any, name: string) => (call[1] as RequestInit).headers?.[name as keyof HeadersInit]; | ||
|
|
||
| test('DELETE with object params serializes JSON body and sets Content-Type', async () => { | ||
| const client = new RestClient({ baseUrl: 'https://example.com' }); | ||
|
|
||
| const result = await client.delete('/v1/rooms.cleanHistory', { roomId: 'abc', latest: '2020-01-01' }); | ||
|
|
||
| expect(result).toMatchObject({ status: 'success' }); | ||
| const call = lastCall(); | ||
| expect(methodOf(call)).toBe('DELETE'); | ||
| expect(JSON.parse(bodyOf(call) as string)).toEqual({ roomId: 'abc', latest: '2020-01-01' }); | ||
| expect(headerOf(call, 'Content-Type')).toBe('application/json'); | ||
| }); | ||
|
|
||
| test('DELETE without params sends no body and no Content-Type', async () => { | ||
| const client = new RestClient({ baseUrl: 'https://example.com' }); | ||
|
|
||
| await client.delete('/v1/rooms.cleanHistory'); | ||
|
|
||
| const call = lastCall(); | ||
| expect(methodOf(call)).toBe('DELETE'); | ||
| expect(bodyOf(call)).toBeUndefined(); | ||
| expect(headerOf(call, 'Content-Type')).toBeUndefined(); | ||
| }); | ||
|
|
||
| test('DELETE with a File forwards a multipart body including the file and fields', async () => { | ||
| const client = new RestClient({ baseUrl: 'https://example.com' }); | ||
| const file = new File(['content'], 'note.txt', { type: 'text/plain' }); | ||
|
|
||
| await client.delete('/v1/rooms.cleanHistory', { roomId: 'abc', file }); | ||
|
|
||
| const call = lastCall(); | ||
| expect(methodOf(call)).toBe('DELETE'); | ||
| const body = bodyOf(call); | ||
| expect(body).toBeInstanceOf(FormData); | ||
| const entries = Array.from((body as FormData).entries()); | ||
| expect(entries).toEqual( | ||
| expect.arrayContaining([ | ||
| ['roomId', 'abc'], | ||
| ['file', file], | ||
| ]), | ||
| ); | ||
| }); | ||
|
|
||
| test('DELETE returning 204 resolves to an empty object', async () => { | ||
| fetchMock.resetMocks(); | ||
| fetchMock.doMock(); | ||
| fetchMock.mockResponse('', { status: 204 }); | ||
| const client = new RestClient({ baseUrl: 'https://example.com' }); | ||
|
|
||
| const result = await client.delete('/v1/rooms.cleanHistory'); | ||
|
|
||
| expect(result).toEqual({}); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.