def setUpMultipleResponse(self, responses):
        request = WebObRequest.blank('http://www.example.com/test')

        response = MultipleResponse(request, responses)

        # Clear out _response since that's what we'll be validating
        response._response = WebObResponse()

        return response
    def setUpMultipleResponse(self, responses):
        request = WebObRequest.blank('http://www.example.com/test')

        response = MultipleResponse(request, responses)

        # Clear out _response since that's what we'll be validating
        response._response = WebObResponse()

        return response
    def setUp(self):
        super(Test_Merge_Responses, self).setUp()

        request = Request.blank('http://www.example.com/test')

        self.headers = {'Content-Type': 'application/json'}
        self.response1 = self.setUpResponse(self.headers, '{"some": "json"}')
        self.response2 = self.setUpResponse(self.headers, '{"more": "json"}')
        responses = [self.response1, self.response2]

        self.multiple_response = MultipleResponse(request, responses)

        # Clear out _response since that's what we'll be validating
        self.multiple_response._response = WebObResponse()
예제 #4
0
    def test__merge_responses_request_accept(self):
        self.setUp__merge_responses()

        self.request.accept = 'application/json'

        MultipleResponse(self.request, self.responses)

        MultipleResponse._merge_responses.assert_called_with()
예제 #5
0
    def test__aggregate_responses_request_accept(self):
        self.setUp__merge_responses()

        self.request.accept = 'text/html'

        MultipleResponse(self.request, self.responses)

        MultipleResponse._aggregate_responses.assert_called_with()
예제 #6
0
    def test__merge_responses_request_accept_none(self):
        self.setUp__merge_responses()

        self.request.accept = None

        MultipleResponse(self.request, self.responses)

        MultipleResponse._merge_responses.assert_called_with()
예제 #7
0
    def test__merge_responses_request_method(self):
        self.setUp__merge_responses()

        self.request.method = 'GET'

        MultipleResponse(self.request, self.responses)

        MultipleResponse._merge_responses.assert_called_with()
    def test_body(self):
        json_boolean = True
        json_null = None
        json_number = -1.1
        json_object = {"some": "json"}
        json_string = "json"
        json_array = [
            json_boolean, json_null, json_number, json_object, json_string
        ]

        response1 = self.setUpResponse(None, json.dumps(json_boolean))
        response2 = self.setUpResponse(None, json.dumps(json_null))
        response3 = self.setUpResponse(None, json.dumps(json_number))
        response4 = self.setUpResponse(None, json.dumps(json_object))
        response5 = self.setUpResponse(None, json.dumps(json_string))
        response_array = self.setUpResponse(None, json.dumps(json_array))

        responses = [
            response1, response2, response3, response4, response5,
            response_array
        ]
        multiple_response = MultipleResponse(self.multiple_response._request,
                                             responses)

        # Clear out _response since that's what we'll be validating
        multiple_response._response = WebObResponse()

        multiple_response._merge_responses()

        json_response = json.loads(multiple_response.response.body)

        json_booleans = [x for x in json_response if x == json_boolean]
        self.assertEqual(2, len(json_booleans))

        json_nulls = [x for x in json_response if x == json_null]
        self.assertEqual(2, len(json_nulls))

        json_numbers = [x for x in json_response if x == json_number]
        self.assertEqual(2, len(json_numbers))

        json_objects = [x for x in json_response if x == json_object]
        self.assertEqual(2, len(json_objects))

        json_strings = [x for x in json_response if x == json_string]
        self.assertEqual(2, len(json_strings))
예제 #9
0
    def test__aggregate_responses_response_status_code(self):
        self.setUp__merge_responses()

        self.responses[0].status_code = 200
        self.responses[1].status_code = 201

        MultipleResponse(self.request, self.responses)

        MultipleResponse._aggregate_responses.assert_called_with()
예제 #10
0
    def test__fix_headers(self):
        old_call = ResponseBase._fix_headers
        ResponseBase._fix_headers = Mock()

        MultipleResponse(self.request, [self.response])

        ResponseBase._fix_headers.assert_called_with()

        ResponseBase._fix_headers = old_call
예제 #11
0
    def test_body(self):
        json_boolean = True
        json_null = None
        json_number = -1.1
        json_object = {"some": "json"}
        json_string = "json"
        json_array = [json_boolean, json_null, json_number, json_object,
                      json_string]

        response1 = self.setUpResponse(None, json.dumps(json_boolean))
        response2 = self.setUpResponse(None, json.dumps(json_null))
        response3 = self.setUpResponse(None, json.dumps(json_number))
        response4 = self.setUpResponse(None, json.dumps(json_object))
        response5 = self.setUpResponse(None, json.dumps(json_string))
        response_array = self.setUpResponse(None, json.dumps(json_array))

        responses = [response1, response2, response3, response4, response5,
                     response_array]
        multiple_response = MultipleResponse(self.multiple_response._request,
                                             responses)

        # Clear out _response since that's what we'll be validating
        multiple_response._response = WebObResponse()

        multiple_response._merge_responses()

        json_response = json.loads(multiple_response.response.body)

        json_booleans = filter(lambda x: x == json_boolean, json_response)
        self.assertEquals(2, len(json_booleans))

        json_nulls = filter(lambda x: x == json_null, json_response)
        self.assertEquals(2, len(json_nulls))

        json_numbers = filter(lambda x: x == json_number, json_response)
        self.assertEquals(2, len(json_numbers))

        json_objects = filter(lambda x: x == json_object, json_response)
        self.assertEquals(2, len(json_objects))

        json_strings = filter(lambda x: x == json_string, json_response)
        self.assertEquals(2, len(json_strings))
예제 #12
0
    def test__aggregate_responses_response_content_type_none(self):
        self.setUp__merge_responses()

        # requests library handles case insensitivity of header field names so
        # we don't need to test it here
        self.responses[0].headers['Content-Type'] = 'application/json'
        del self.responses[1].headers['Content-Type']

        MultipleResponse(self.request, self.responses)

        MultipleResponse._aggregate_responses.assert_called_with()
예제 #13
0
    def setUp(self):
        super(Test_Merge_Responses, self).setUp()

        request = Request.blank('http://www.example.com/test')

        self.headers = {'Content-Type': 'application/json'}
        self.response1 = self.setUpResponse(self.headers, '{"some": "json"}')
        self.response2 = self.setUpResponse(self.headers, '{"more": "json"}')
        responses = [self.response1, self.response2]

        self.multiple_response = MultipleResponse(request, responses)

        # Clear out _response since that's what we'll be validating
        self.multiple_response._response = WebObResponse()
예제 #14
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
예제 #15
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
class Test_Merge_Responses(TestCase):
    def setUp(self):
        super(Test_Merge_Responses, self).setUp()

        request = Request.blank('http://www.example.com/test')

        self.headers = {'Content-Type': 'application/json'}
        self.response1 = self.setUpResponse(self.headers, '{"some": "json"}')
        self.response2 = self.setUpResponse(self.headers, '{"more": "json"}')
        responses = [self.response1, self.response2]

        self.multiple_response = MultipleResponse(request, responses)

        # Clear out _response since that's what we'll be validating
        self.multiple_response._response = WebObResponse()

    def setUpResponse(self, headers, body):
        if not headers:
            headers = {'Content-Type': 'application/json'}

        response = RequestsResponseMock(status_code=200,
                                        headers=headers,
                                        body=body)

        return response

    def test_body(self):
        json_boolean = True
        json_null = None
        json_number = -1.1
        json_object = {"some": "json"}
        json_string = "json"
        json_array = [
            json_boolean, json_null, json_number, json_object, json_string
        ]

        response1 = self.setUpResponse(None, json.dumps(json_boolean))
        response2 = self.setUpResponse(None, json.dumps(json_null))
        response3 = self.setUpResponse(None, json.dumps(json_number))
        response4 = self.setUpResponse(None, json.dumps(json_object))
        response5 = self.setUpResponse(None, json.dumps(json_string))
        response_array = self.setUpResponse(None, json.dumps(json_array))

        responses = [
            response1, response2, response3, response4, response5,
            response_array
        ]
        multiple_response = MultipleResponse(self.multiple_response._request,
                                             responses)

        # Clear out _response since that's what we'll be validating
        multiple_response._response = WebObResponse()

        multiple_response._merge_responses()

        json_response = json.loads(multiple_response.response.body)

        json_booleans = [x for x in json_response if x == json_boolean]
        self.assertEqual(2, len(json_booleans))

        json_nulls = [x for x in json_response if x == json_null]
        self.assertEqual(2, len(json_nulls))

        json_numbers = [x for x in json_response if x == json_number]
        self.assertEqual(2, len(json_numbers))

        json_objects = [x for x in json_response if x == json_object]
        self.assertEqual(2, len(json_objects))

        json_strings = [x for x in json_response if x == json_string]
        self.assertEqual(2, len(json_strings))

    def test_header_content_encoding(self):
        self.multiple_response._merge_responses()

        self.assertEqual(None,
                         self.multiple_response.response.content_encoding)

    def test_header_content_type(self):
        self.multiple_response._merge_responses()

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

    def test_status(self):
        self.multiple_response._merge_responses()

        self.assertEqual(200, self.multiple_response.response.status_code)
예제 #17
0
class Test_Merge_Responses(TestCase):
    def setUp(self):
        super(Test_Merge_Responses, self).setUp()

        request = Request.blank('http://www.example.com/test')

        self.headers = {'Content-Type': 'application/json'}
        self.response1 = self.setUpResponse(self.headers, '{"some": "json"}')
        self.response2 = self.setUpResponse(self.headers, '{"more": "json"}')
        responses = [self.response1, self.response2]

        self.multiple_response = MultipleResponse(request, responses)

        # Clear out _response since that's what we'll be validating
        self.multiple_response._response = WebObResponse()

    def setUpResponse(self, headers, body):
        if not headers:
            headers = {'Content-Type': 'application/json'}

        response = RequestsResponseMock(status_code=200, headers=headers,
                                        body=body)

        return response

    def test_body(self):
        json_boolean = True
        json_null = None
        json_number = -1.1
        json_object = {"some": "json"}
        json_string = "json"
        json_array = [json_boolean, json_null, json_number, json_object,
                      json_string]

        response1 = self.setUpResponse(None, json.dumps(json_boolean))
        response2 = self.setUpResponse(None, json.dumps(json_null))
        response3 = self.setUpResponse(None, json.dumps(json_number))
        response4 = self.setUpResponse(None, json.dumps(json_object))
        response5 = self.setUpResponse(None, json.dumps(json_string))
        response_array = self.setUpResponse(None, json.dumps(json_array))

        responses = [response1, response2, response3, response4, response5,
                     response_array]
        multiple_response = MultipleResponse(self.multiple_response._request,
                                             responses)

        # Clear out _response since that's what we'll be validating
        multiple_response._response = WebObResponse()

        multiple_response._merge_responses()

        json_response = json.loads(multiple_response.response.body)

        json_booleans = filter(lambda x: x == json_boolean, json_response)
        self.assertEquals(2, len(json_booleans))

        json_nulls = filter(lambda x: x == json_null, json_response)
        self.assertEquals(2, len(json_nulls))

        json_numbers = filter(lambda x: x == json_number, json_response)
        self.assertEquals(2, len(json_numbers))

        json_objects = filter(lambda x: x == json_object, json_response)
        self.assertEquals(2, len(json_objects))

        json_strings = filter(lambda x: x == json_string, json_response)
        self.assertEquals(2, len(json_strings))

    def test_header_content_encoding(self):
        self.multiple_response._merge_responses()

        self.assertEquals(None,
                          self.multiple_response.response.content_encoding)

    def test_header_content_type(self):
        self.multiple_response._merge_responses()

        self.assertEquals(self.headers['Content-Type'],
                          self.multiple_response.response.content_type)

    def test_status(self):
        self.multiple_response._merge_responses()

        self.assertEquals(200, self.multiple_response.response.status_code)