diff options
| author | Gabriel Falcão <gabriel@nacaolivre.org> | 2014-05-13 16:32:18 -0400 |
|---|---|---|
| committer | Gabriel Falcão <gabriel@nacaolivre.org> | 2014-05-13 16:32:18 -0400 |
| commit | 849682dee16bcf5c14e4e8cef5798072d83fd2b4 (patch) | |
| tree | 285b5fdde9729cb8481c723ada4e6f7c519a0bd7 | |
| parent | 236f205e2c8c1a0b81758ff3b876b4398bd5b7ba (diff) | |
| parent | 7fd69118995c189a755fef716aed2c0cdbcf3543 (diff) | |
| download | httpretty-849682dee16bcf5c14e4e8cef5798072d83fd2b4.tar.gz | |
Merge pull request #163 from aljosa/patch-1
Update README.md
| -rw-r--r-- | README.md | 26 |
1 files changed, 26 insertions, 0 deletions
@@ -305,6 +305,32 @@ def test_response_callbacks(): expect(response.text).to.equal('The GET response from https://api.yahoo.com/test') ``` +Dynamic responses can also be used when you have to work with badly designed APIs where, for example, the same uri and method are used to handle different requests based on request body which contains xml. + +```python +import requests +import httpretty + +@httpretty.activate +def test_response_callbacks(): + + def request_callback(request, uri, headers): + # parse_xml() extracts important data from request + data = parse_xml(request.body) + # response based on that data + if data.something_important: + return (200, headers, "relevant data") + else: + return (400, headers, "panic mode!") + + httpretty.register_uri( + httpretty.GET, "https://api.brilliant-api.com/", + body=request_callback) + + response = requests.get('https://api.brilliant-api.com/') + ... +``` + ## matching regular expressions You can register a |
