2021-02-06 04:42:36 +00:00
---
id: 587d824f367417b2b2512c5a
2021-12-03 01:21:50 -08:00
title: Ejecutar pruebas funcionales en la respuesta de un API usando Chai-HTTP III - método PUT
2021-02-06 04:42:36 +00:00
challengeType: 2
forumTopicId: 301590
dashedName: run-functional-tests-on-an-api-response-using-chai-http-iii---put-method
---
# --description--
2021-12-05 19:57:54 -08:00
Como recordatorio, este proyecto está siendo construido con base en el siguiente proyecto inicial [Replit ](https://replit.com/github/freeCodeCamp/boilerplate-mochachai ), o clonado desde [GitHub ](https://github.com/freeCodeCamp/boilerplate-mochachai/ ).
2021-02-06 04:42:36 +00:00
2021-12-03 01:21:50 -08:00
Cuando se prueba una solicitud `PUT` , a menudo enviarás datos junto con ella. Los datos que incluye con su solicitud `PUT` se llama el body de la petición.
2021-02-06 04:42:36 +00:00
2021-12-03 01:21:50 -08:00
Para enviar una petición `PUT` y un objeto JSON al endpoint `'/travellers'` , puedes usar el complemento `chai-http` , con los métodos `put` y `send` :
```js
chai
.request(server)
.put('/travellers')
.send({
"surname": [last name of a traveller of the past]
})
...
2021-02-06 04:42:36 +00:00
```
2021-12-03 01:21:50 -08:00
Y la ruta responde con:
2021-02-06 04:42:36 +00:00
```json
{
2021-12-03 01:21:50 -08:00
"name": [first name],
"surname": [last name],
"dates": [birth - death years]
2021-02-06 04:42:36 +00:00
}
```
2021-12-03 01:21:50 -08:00
Vea el código del servidor para las diferentes respuestas del endpoint `'/travellers'` .
2021-02-06 04:42:36 +00:00
# --instructions--
2021-12-03 01:21:50 -08:00
Dentro de `tests/2_functional-tests.js` , arregla el test `'Send {surname: "Colombo"}'` (`// #3` ), use los métodos `put` y `send` para testear el endpoint `'/travellers'` .
2021-02-06 04:42:36 +00:00
2021-12-03 01:21:50 -08:00
Enviar el siguiente objeto JSON con su solicitud PUT:
2021-02-06 04:42:36 +00:00
```json
{
"surname": "Colombo"
}
```
2021-12-03 01:21:50 -08:00
Compruebe lo siguiente dentro del callback `request.end` :
2021-02-06 04:42:36 +00:00
2021-12-03 01:21:50 -08:00
1. El `status` debe ser `200`
2. El `type` debe ser `application/json`
3. El `body.name` debe ser `Cristoforo`
4. El `body.surname` debe ser `Colombo`
2021-02-06 04:42:36 +00:00
2021-12-03 01:21:50 -08:00
Sigue el orden de las aserciones de arriba - nos basamos en esto. También, asegúrese de eliminar `assert.fail()` una vez completado.
2021-02-06 04:42:36 +00:00
# --hints--
2021-12-03 01:21:50 -08:00
Todas las pruebas deben pasar.
2021-02-06 04:42:36 +00:00
```js
(getUserInput) =>
$.get(getUserInput('url') + '/_api/get-tests?type=functional& n=2').then(
(data) => {
assert.equal(data.state, 'passed');
},
(xhr) => {
throw new Error(xhr.responseText);
}
);
```
2021-12-03 01:21:50 -08:00
Debes comprobar que `res.status` sea 200.
2021-02-06 04:42:36 +00:00
```js
(getUserInput) =>
$.get(getUserInput('url') + '/_api/get-tests?type=functional& n=2').then(
(data) => {
assert.equal(data.assertions[0].method, 'equal');
assert.equal(data.assertions[0].args[0], 'res.status');
assert.equal(data.assertions[0].args[1], '200');
},
(xhr) => {
throw new Error(xhr.responseText);
}
);
```
2021-12-03 01:21:50 -08:00
Debes comprobar que `res.type` sea `'application/json'` .
2021-02-06 04:42:36 +00:00
```js
(getUserInput) =>
$.get(getUserInput('url') + '/_api/get-tests?type=functional& n=2').then(
(data) => {
assert.equal(data.assertions[1].method, 'equal');
assert.equal(data.assertions[1].args[0], 'res.type');
assert.match(data.assertions[1].args[1], /('|")application\/json\1/);
},
(xhr) => {
throw new Error(xhr.responseText);
}
);
```
2021-12-03 01:21:50 -08:00
Debes comprobar que `res.body.name` sea `'Cristoforo'` .
2021-02-06 04:42:36 +00:00
```js
(getUserInput) =>
$.get(getUserInput('url') + '/_api/get-tests?type=functional& n=2').then(
(data) => {
assert.equal(data.assertions[2].method, 'equal');
assert.equal(data.assertions[2].args[0], 'res.body.name');
assert.match(data.assertions[2].args[1], /('|")Cristoforo\1/);
},
(xhr) => {
throw new Error(xhr.responseText);
}
);
```
2021-12-03 01:21:50 -08:00
Debes comprobar que `res.body.surname` sea `'Colombo'` .
2021-02-06 04:42:36 +00:00
```js
(getUserInput) =>
$.get(getUserInput('url') + '/_api/get-tests?type=functional& n=2').then(
(data) => {
assert.equal(data.assertions[3].method, 'equal');
assert.equal(data.assertions[3].args[0], 'res.body.surname');
assert.match(data.assertions[3].args[1], /('|")Colombo\1/);
},
(xhr) => {
throw new Error(xhr.responseText);
}
);
```
# --solutions--
```js
/**
Backend challenges don't need solutions,
because they would need to be tested against a full working project.
Please check our contributing guidelines to learn more.
*/
```