2018-10-12 15:37:13 -04:00
---
title: Run Functional Tests on an API Response using Chai-HTTP IV - PUT method
---
2019-07-24 00:59:27 -07:00
# Run Functional Tests on an API Response using Chai-HTTP IV - PUT method
2018-10-12 15:37:13 -04:00
2019-07-24 00:59:27 -07:00
---
## Problem Explanation
2019-07-02 07:00:18 +10:00
To begin, open the file "tests/2_functional_tests.js" and locate the test 'send {surname: "da Verrazzano"}'.
2018-10-12 15:37:13 -04:00
2019-07-24 00:59:27 -07:00
---
## Hints
### Hint 1
2018-10-12 15:37:13 -04:00
2019-07-02 07:00:18 +10:00
Using the example above, look how the request is being sent, and how the assertions are making comparisons between the expected and actual values of the response.
2019-07-24 00:59:27 -07:00
### Hint 2
2019-07-02 07:00:18 +10:00
Make sure you are placing a request through `chai.request(server)` .
2019-07-24 00:59:27 -07:00
### Hint 3
2019-07-02 07:00:18 +10:00
You need to use make a .put() request to `/travellers` and use .send() to attach the payload `{surname: 'da Verrazzano'}` to the request.
2019-07-24 00:59:27 -07:00
### Hint 4
2019-07-02 07:00:18 +10:00
Replace the assert.fail() statement with your own tests checking for status, type, body.name, and body.surname in that order. Remember, all of these values are contained in the response `(res` ), and you should expect the response to be of type `'application/json'` .
2019-07-24 00:59:27 -07:00
### Hint 5
2019-07-02 07:00:18 +10:00
Check the tests on the challenge page to determine the expected values for `body.name` and `body.surname` .
2019-07-24 00:59:27 -07:00
### Hint 6
2019-07-02 07:00:18 +10:00
Ensure your call to `done()` is inside your callback function for the tests.
2019-07-24 00:59:27 -07:00
---
## Solutions
<details><summary>Solution 1 (Click to Show/Hide)</summary>
2019-07-02 07:00:18 +10:00
```js
test('send {surname: "da Verrazzano"}', function(done) {
/** place the chai-http request code here... * * /
2019-07-24 00:59:27 -07:00
chai
.request(server)
.put('/travellers')
.send({ surname: 'da Verrazzano' })
/** place your tests inside the callback * * /
.end(function(err, res) {
assert.equal(res.status, 200, 'response status should be 200');
assert.equal(res.type, 'application/json', 'Response should be json');
assert.equal(res.body.name, 'Giovanni');
assert.equal(res.body.surname, 'da Verrazzano');
done();
});
2019-07-02 07:00:18 +10:00
});
2019-07-24 00:59:27 -07:00
```
</details>