Skip to content
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

#4480-added new method for sending payload in delete request #4493

Open
wants to merge 1 commit into
base: 3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ REST helper can send GET/POST/PATCH/etc requests to REST API endpoint:
* [`I.sendPutRequest()`](/helpers/REST#sendPutRequest)
* [`I.sendPatchRequest()`](/helpers/REST#sendPatchRequest)
* [`I.sendDeleteRequest()`](/helpers/REST#sendDeleteRequest)
* [`I.sendDeleteRequestWithPayload()`](/helpers/REST#sendDeleteRequestWithPayload)
* ...

Authentication headers can be set in [helper's config](https://codecept.io/helpers/REST/#configuration) or per test with headers or special methods like `I.amBearerAuthenticated`.
Expand Down
1 change: 1 addition & 0 deletions docs/data.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ I.sendPostRequest()
I.sendPutRequest()
I.sendPatchRequest()
I.sendDeleteRequest()
I.sendDeleteRequestWithPayload()
```

As well as a method for setting headers: `haveRequestHeaders`.
Expand Down
16 changes: 16 additions & 0 deletions docs/helpers/REST.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,22 @@ I.sendDeleteRequest('/api/users/1');

Returns **[Promise][2]<any>** response

### sendDeleteRequestWithPayload

Sends DELETE request to API with payload.

```js
I.sendDeleteRequestWithPayload('/api/users/1', { author: 'john' });
```

#### Parameters

- `url` **any**
- `payload` **any** the payload to be sent. By default it is sent as an empty object
- `headers` **[object][4]** the headers object to be sent. By default, it is sent as an empty object

Returns **[Promise][2]<any>** response

### sendGetRequest

Send GET request to REST API
Expand Down
24 changes: 24 additions & 0 deletions lib/helper/REST.js
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,30 @@ class REST extends Helper {

return this._executeRequest(request)
}

/**
* Sends DELETE request to API with payload.
*
* ```js
* I.sendDeleteRequestWithPayload('/api/users/1', { author: 'john' });
* ```
*
* @param {*} url
* @param {*} [payload={}] - the payload to be sent. By default it is sent as an empty object
* @param {object} [headers={}] - the headers object to be sent. By default, it is sent as an empty object
*
* @returns {Promise<*>} response
*/
async sendDeleteRequestWithPayload(url, payload = {}, headers = {}) {
const request = {
baseURL: this._url(url),
method: 'DELETE',
data: payload,
headers,
}

return this._executeRequest(request)
}
}

module.exports = REST
Expand Down
7 changes: 7 additions & 0 deletions test/rest/REST_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,13 @@ describe('REST', () => {
getResponse.data.should.be.empty
})

it('should send DELETE requests with payload', async () => {
await I.sendDeleteRequestWithPayload('/posts/1', { author: 'john' })
const getResponse = await I.sendGetRequest('/posts')

getResponse.data.should.be.empty
})

it('should update request with onRequest', async () => {
I.config.onRequest = (request) => (request.data = { name: 'Vasya' })

Expand Down
1 change: 1 addition & 0 deletions translations/de-DE.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ module.exports = {
sendGetRequest: 'mache_einen_get_request',
sendPutRequest: 'mache_einen_put_request',
sendDeleteRequest: 'mache_einen_delete_request',
sendDeleteRequestWithPayload: 'mache_einen_delete_request_mit_payload',
sendPostRequest: 'mache_einen_post_request',
switchTo: 'wechlse_in_iframe',
},
Expand Down
2 changes: 1 addition & 1 deletion translations/fr-FR.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ module.exports = {
scrollTo: 'défileVers',
sendGetRequest: 'envoieLaRequêteGet',
sendPutRequest: 'envoieLaRequêtePut',
sendDeleteRequest: 'envoieLaRequêteDelete',
sendDeleteRequest: 'envoieLaRequêteDeleteAvecPayload',
sendPostRequest: 'envoieLaRequêtePost',
},
}