コード例 #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)
コード例 #2
0
    def token_post(self, url, payload, headers={}, **kwargs):
        """Mock the request for an OAuth token.
        """

        self.access_token_requests.append((url, payload, headers, kwargs))
        response = self.access_token_response
        return HTTP._process_response(url, response, **kwargs)
コード例 #3
0
 def _request_with_timeout(self, method, url, *args, **kwargs):
     """Simulate HTTP.request_with_timeout."""
     self.requests.append([method, url, args, kwargs])
     response = self.responses.pop()
     return HTTP._process_response(url, response,
                                   kwargs.get('allowed_response_codes'),
                                   kwargs.get('disallowed_response_codes'))
コード例 #4
0
ファイル: overdrive.py プロジェクト: rskm1/server_core
 def _make_request(self, url, *args, **kwargs):
     response = self.responses.pop()
     self.requests.append((url, args, kwargs))
     return HTTP._process_response(
         url, response, kwargs.get('allowed_response_codes'),
         kwargs.get('disallowed_response_codes')
     )
コード例 #5
0
ファイル: axis.py プロジェクト: datalogics/server_core
 def _make_request(self, url, method, headers, data=None, params=None, 
                   **kwargs):
     """Actually make an HTTP request."""
     return HTTP.request_with_timeout(
         method, url, headers=headers, data=data,
         params=params, **kwargs
     )
コード例 #6
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)
コード例 #7
0
    def _do_get(url, headers, **kwargs):
        # More time please
        if 'timeout' not in kwargs:
            kwargs['timeout'] = 60

        if 'allow_redirects' not in kwargs:
            kwargs['allow_redirects'] = True

        response = HTTP.get_with_timeout(url, headers=headers, **kwargs)
        return response.status_code, response.headers, response.content
コード例 #8
0
ファイル: overdrive.py プロジェクト: mdellabitta/server_core
    def token_post(self, url, payload, headers={}, **kwargs):
        """Mock the request for an OAuth token.

        We mock the method by looking at the access_token_response
        property, rather than inserting a mock response in the queue,
        because only the first MockOverdriveAPI instantiation in a
        given test actually makes this call. By mocking the response
        to this method separately we remove the need to figure out
        whether to queue a response in a given test.
        """
        self.access_token_requests.append((url, payload, headers, kwargs))
        response = self.access_token_response
        return HTTP._process_response(url, response, **kwargs)
コード例 #9
0
    def _get(self, url, headers):
        """Make the sort of HTTP request that's normal for an OPDS feed.

        Long timeout, raise error on anything but 2xx or 3xx.
        """
        headers = dict(headers or {})

        types = dict(
            opds_acquisition=OPDSFeed.ACQUISITION_FEED_TYPE,
            atom="application/atom+xml",
            xml="application/xml",
            anything="*/*",
        )
        headers['Accept'] = "%(opds_acquisition)s, %(atom)s;q=0.9, %(xml)s;q=0.8, %(anything)s;q=0.1" % types
        kwargs = dict(timeout=120, allowed_response_codes=['2xx', '3xx'])
        response = HTTP.get_with_timeout(url, headers=headers, **kwargs)
        return response.status_code, response.headers, response.content
コード例 #10
0
ファイル: overdrive.py プロジェクト: mdellabitta/server_core
 def _do_post(self, url, payload, headers, **kwargs):
     """This method is overridden in MockOverdriveAPI."""
     return HTTP.post_with_timeout(url, payload, headers=headers, **kwargs)
コード例 #11
0
 def _request_with_timeout(self, method, url, *args, **kwargs):
     """This will be overridden in MockThreeMAPI."""
     return HTTP.request_with_timeout(method, url, *args, **kwargs)
コード例 #12
0
    def _do_post(url, payload, headers, **kwargs):
        # More time please
        if 'timeout' not in kwargs:
            kwargs['timeout'] = 60

        return HTTP.post_with_timeout(url, payload, headers=headers, **kwargs)
コード例 #13
0
 def _get(self, url, *args, **kwargs):
     response = self.responses.pop()
     return HTTP._process_response(
         url, response, kwargs.get('allowed_response_codes'),
         kwargs.get('disallowed_response_codes')
     )
コード例 #14
0
 def _get(self, url, **kwargs):
     """Make an HTTP request. This method is overridden in the mock class."""
     return HTTP.get_with_timeout(url, **kwargs)