Mock of httplib2.Http Mocks a sequence of calls to request returning different responses for each call. Create an instance initialized with the desired response headers and content and then use as if an httplib2.Http instance. http = HttpMockSequence([ ({'status': '401'},
| 1763 | |
| 1764 | |
| 1765 | class HttpMockSequence(object): |
| 1766 | """Mock of httplib2.Http |
| 1767 | |
| 1768 | Mocks a sequence of calls to request returning different responses for each |
| 1769 | call. Create an instance initialized with the desired response headers |
| 1770 | and content and then use as if an httplib2.Http instance. |
| 1771 | |
| 1772 | http = HttpMockSequence([ |
| 1773 | ({'status': '401'}, ''), |
| 1774 | ({'status': '200'}, '{"access_token":"1/3w","expires_in":3600}'), |
| 1775 | ({'status': '200'}, 'echo_request_headers'), |
| 1776 | ]) |
| 1777 | resp, content = http.request("http://examples.com") |
| 1778 | |
| 1779 | There are special values you can pass in for content to trigger |
| 1780 | behavours that are helpful in testing. |
| 1781 | |
| 1782 | 'echo_request_headers' means return the request headers in the response body |
| 1783 | 'echo_request_headers_as_json' means return the request headers in |
| 1784 | the response body |
| 1785 | 'echo_request_body' means return the request body in the response body |
| 1786 | 'echo_request_uri' means return the request uri in the response body |
| 1787 | """ |
| 1788 | |
| 1789 | def __init__(self, iterable): |
| 1790 | """ |
| 1791 | Args: |
| 1792 | iterable: iterable, a sequence of pairs of (headers, body) |
| 1793 | """ |
| 1794 | self._iterable = iterable |
| 1795 | self.follow_redirects = True |
| 1796 | self.request_sequence = list() |
| 1797 | |
| 1798 | def request( |
| 1799 | self, |
| 1800 | uri, |
| 1801 | method="GET", |
| 1802 | body=None, |
| 1803 | headers=None, |
| 1804 | redirections=1, |
| 1805 | connection_type=None, |
| 1806 | ): |
| 1807 | # Remember the request so after the fact this mock can be examined |
| 1808 | self.request_sequence.append((uri, method, body, headers)) |
| 1809 | resp, content = self._iterable.pop(0) |
| 1810 | if isinstance(content, str): |
| 1811 | content = content.encode("utf-8") |
| 1812 | |
| 1813 | if content == b"echo_request_headers": |
| 1814 | content = headers |
| 1815 | elif content == b"echo_request_headers_as_json": |
| 1816 | content = json.dumps(headers) |
| 1817 | elif content == b"echo_request_body": |
| 1818 | if hasattr(body, "read"): |
| 1819 | content = body.read() |
| 1820 | else: |
| 1821 | content = body |
| 1822 | elif content == b"echo_request_uri": |
no outgoing calls
searching dependent graphs…