示例#1
0
    def test_502_status_code(self):
        headers = {'Content-Type': 'text/html'}
        response_200 = RequestsResponseMock(status_code=200, headers=headers)
        response_500 = RequestsResponseMock(status_code=500, headers=headers)

        error_response = ErrorResponse(None, [response_200, response_500], [])

        self.assertEqual(502, error_response.response.status_code)
示例#2
0
    def test_502_aggregate_response_bodies(self):
        with patch.object(ErrorResponse, '_aggregate_response_bodies') as m:
            headers = {'Content-Type': 'text/html'}
            response_1 = RequestsResponseMock(status_code=200, headers=headers)
            response_2 = RequestsResponseMock(status_code=502, headers=headers)

            ErrorResponse(None, [response_1, response_2], [])

            m.assert_called_with()
示例#3
0
    def test_400_class_status_code(self):
        request = Request.blank('http://example.com/1')

        headers = {'Content-Type': 'text/html'}
        response_200 = RequestsResponseMock(status_code=200, headers=headers)
        response_401 = RequestsResponseMock(status_code=401, headers=headers)

        error_response = ErrorResponse(request, [response_200, response_401],
                                       [])

        self.assertEqual(401, error_response.response.status_code)
示例#4
0
    def test_priority_error_single(self):
        request = Request.blank('http://example.com/1')

        headers = {'Content-Type': 'text/html'}
        response_200 = RequestsResponseMock(status_code=200, headers=headers)
        response_500 = RequestsResponseMock(status_code=500, headers=headers)
        response_401 = RequestsResponseMock(status_code=401, headers=headers)
        responses = [response_200, response_500, response_401]

        error_response = ErrorResponse(request, responses, [401])

        self.assertEqual(401, error_response.response.status_code)
示例#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)
        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
示例#6
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