Setting headers

HTTP header fields supply the necessary information about the request or response. We can mock any HTTP response header by using HTTPretty. To achieve that, we will be adding them as keyword arguments. We should keep in mind that the keys of the keyword arguments are always lower case and have underscores (_) instead of dashes.

For example, if we want to mock the server, which returns Content-Type, we can use the argument content_type. Do notice that, in the following part we are using an inexistent URL to showcase the syntax:

import httpretty
import requests

from sure import expect

@httpretty.activate
def setting_header_example():
    httpretty.register_uri(httpretty.GET,
                           "http://api.example.com/some/path",
                           body='{"success": true}',
                           status=200,
                           content_type='text/json')

    response = requests.get("http://api.example.com/some/path")

    expect(response.json()).to.equal({'success': True})
    expect(response.status_code).to.equal(200)

Similarly, all the keyword arguments are taken by HTTPretty and changed into the RFC2616 equivalent name.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.14.245