Exemplo n.º 1
0
    def test_unicode_converted_to_utf8(self):
        """Any Unicode that sneaks into the URL, headers or body is
        converted to UTF-8.
        """
        class ResponseGenerator(object):
            def __init__(self):
                self.requests = []

            def response(self, *args, **kwargs):
                self.requests.append((args, kwargs))
                return MockRequestsResponse(200, content="Success!")

        generator = ResponseGenerator()
        url = "http://foo"
        response = HTTP._request_with_timeout(
            url,
            generator.response,
            url,
            "POST",
            headers={u"unicode header": u"unicode value"},
            data=u"unicode data")
        [(args, kwargs)] = generator.requests
        url, method = args
        headers = kwargs['headers']
        data = kwargs['data']

        # All the Unicode data was converted to bytes before being sent
        # "over the wire".
        for k, v in headers.items():
            assert isinstance(k, bytes)
            assert isinstance(v, bytes)
        assert isinstance(data, bytes)
Exemplo n.º 2
0
    def test_request_with_timeout_success(self):
        def fake_200_response(*args, **kwargs):
            return MockRequestsResponse(200, content="Success!")

        response = HTTP._request_with_timeout("the url", fake_200_response,
                                              "a", "b")
        eq_(200, response.status_code)
        eq_("Success!", response.content)