Example #1
0
    def test_response_status(self):
        single_response = SingleResponse(self.request, self.response)

        expected_status = '{0} {1}'.format(self.response.status_code,
                                           self.response.reason)

        self.assertEqual(expected_status, single_response.response.status)
Example #2
0
    def test__fix_headers(self):
        old_call = ResponseBase._fix_headers
        ResponseBase._fix_headers = Mock()

        SingleResponse(self.request, self.response)

        ResponseBase._fix_headers.assert_called_with()

        ResponseBase._fix_headers = old_call
    def __call__(self, request):
        requests_request = request.copy()

        destination_url = self._create_forwarded_url(requests_request.url)
        # Take advantage of gzip even if the client doesn't support it
        requests_request.headers['Accept-Encoding'] = 'gzip,identity'
        requests_request.headers['Host'] = urlparse(destination_url).netloc

        requests_response = requests.request(requests_request.method,
                                             destination_url,
                                             data=requests_request.body,
                                             headers=requests_request.headers,
                                             allow_redirects=False,
                                             verify=True,
                                             stream=True)

        single_response = SingleResponse(request, requests_response)
        return single_response.response
Example #4
0
    def __call__(self, request):
        requests_request = request.copy()

        destination_urls = self._create_forwarded_urls(requests_request.url)

        # Use gzip even if the original requestor didn't support it
        requests_request.headers['Accept-Encoding'] = 'gzip,identity'
        # Host header is automatically added for each request by grequests
        del requests_request.headers['Host']

        requests = (grequests.request(requests_request.method,
                                      destination_url,
                                      data=requests_request.body,
                                      headers=requests_request.headers,
                                      allow_redirects=False,
                                      verify=True)
                    for destination_url in destination_urls)
        requests_responses = grequests.map(requests, stream=True)

        self._log_responses(request, requests_responses)
        requests_responses = self._filter_responses(requests_responses)

        response = None
        if None in requests_responses:
            response = MetadataResponse(request, requests_responses)
            response.response.status = 504
            request_uuid = request.environ[ENVIRON_REQUEST_UUID_KEY]
            logging.error('Unable to connect to one or more backend '
                          'endpoints: {0}'.format(', '.join(destination_urls)),
                          extra={'request_uuid': request_uuid})
        elif ('Proxy-Aggregator-Body' in request.headers
                and request.headers['Proxy-Aggregator-Body'].lower()
                == 'response-metadata'):
            response = MetadataResponse(request, requests_responses)
        elif len(requests_responses) == 1:
            response = SingleResponse(request, requests_responses[0])
        elif any(r.status_code >= 400 for r in requests_responses):
            response = ErrorResponse(request,
                                     requests_responses,
                                     self._priority_errors)
        else:
            response = MultipleResponse(request, requests_responses)

        return response.response
Example #5
0
    def __call__(self, request):
        requests_request = request.copy()

        destination_urls = self._create_forwarded_urls(requests_request.url)

        # Use gzip even if the original requestor didn't support it
        requests_request.headers['Accept-Encoding'] = 'gzip,identity'
        # Host header is automatically added for each request by grequests
        del requests_request.headers['Host']

        requests = (grequests.request(requests_request.method,
                                      destination_url,
                                      data=requests_request.body,
                                      headers=requests_request.headers,
                                      allow_redirects=False,
                                      verify=True)
                    for destination_url in destination_urls)

        exhandler = partial(helpers.log_failures, environ=request.environ)
        requests_responses = grequests.map(requests,
                                           stream=True,
                                           exception_handler=exhandler)

        self._log_responses(request, requests_responses)
        requests_responses = self._filter_responses(requests_responses)

        response = None
        if None in requests_responses:
            response = MetadataResponse(request, requests_responses)
            response.response.status = 504
        elif ('Proxy-Aggregator-Body' in request.headers
              and request.headers['Proxy-Aggregator-Body'].lower()
              == 'response-metadata'):
            response = MetadataResponse(request, requests_responses)
        elif len(requests_responses) == 1:
            response = SingleResponse(request, requests_responses[0])
        elif any(r.status_code >= 400 for r in requests_responses):
            response = ErrorResponse(request, requests_responses,
                                     self._priority_errors)
        else:
            response = MultipleResponse(request, requests_responses)

        return response.response
Example #6
0
    def test_request(self):
        single_response = SingleResponse(self.request, self.response)

        self.assertEqual(self.request, single_response._request)
Example #7
0
    def test_response_body(self):
        single_response = SingleResponse(self.request, self.response)

        self.assertEqual(self.response_body, single_response.response.body)
Example #8
0
    def test_response_headers(self):
        single_response = SingleResponse(self.request, self.response)

        self.assertEqual(self.response.headers['Content-Type'],
                         single_response.response.headers['Content-Type'])