예제 #1
0
    def test_error_response_priority_errors(self):
        request = Request.blank('http://example.com/1,2/path')

        response1 = RequestsResponseMock(status_code=200)
        response2 = RequestsResponseMock(status_code=500)
        self.grequests_map.return_value = [response1, response2]

        route_patcher___init__ = patch.object(ErrorResponse,
                                              '__init__',
                                              return_value=None)
        error_response = route_patcher___init__.start()

        mock_response = time.time()
        route_patcher_response = patch.object(ErrorResponse,
                                              'response',
                                              new=mock_response)
        route_patcher_response.start()

        self.endpoints_route(request)

        args, kwargs = error_response.call_args
        self.assertEqual(self.endpoints_route._priority_errors, args[2])

        route_patcher___init__.stop()
        route_patcher_response.stop()
예제 #2
0
    def test_metadata_response(self):
        request_headers = {'Proxy-Aggregator-Body': 'respOnse-Metadata'}
        request = Request.blank('http://example.com/1,2/path',
                                headers=request_headers)

        headers = {'Content-Type': 'application/json'}
        response1 = RequestsResponseMock(status_code=200, headers=headers)
        response2 = RequestsResponseMock(status_code=500, headers=headers)
        self.grequests_map.return_value = [response1, response2]

        route_patcher___init__ = patch.object(MetadataResponse,
                                              '__init__',
                                              return_value=None)
        metadata_response = route_patcher___init__.start()

        mock_response = time.time()
        route_patcher_response = patch.object(MetadataResponse,
                                              'response',
                                              new=mock_response)
        route_patcher_response.start()

        self.endpoints_route(request)

        self.assertTrue(metadata_response.called)

        route_patcher___init__.stop()
        route_patcher_response.stop()
예제 #3
0
    def test_error_response_ignore_all_responses(self):
        request = Request.blank('http://example.com/1,2,3,4,5/path')
        headers = {'Content-Type': 'application/json'}

        response1 = RequestsResponseMock(status_code=400, headers=headers)
        response2 = RequestsResponseMock(status_code=500, headers=headers)
        response3 = RequestsResponseMock(status_code=500, headers=headers)
        response4 = RequestsResponseMock(status_code=400, headers=headers)
        response5 = RequestsResponseMock(status_code=400, headers=headers)
        self.grequests_map.return_value = [response1,
                                           response2,
                                           response3,
                                           response4,
                                           response5]

        route_patcher___init__ = patch.object(ErrorResponse,
                                              '__init__',
                                              return_value=None)
        error_response = route_patcher___init__.start()

        mock_response = time.time()
        route_patcher_response = patch.object(ErrorResponse,
                                              'response',
                                              new=mock_response)
        route_patcher_response.start()

        response = self.endpoints_route_with_ignore(request)

        args, kwargs = error_response.call_args
        self.assertEqual(response, mock_response)
        self.assertEqual(5, len(args[1]))
        self.assertEqual(True, 400 in [r.status_code for r in args[1]])

        route_patcher___init__.stop()
        route_patcher_response.stop()
예제 #4
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)
예제 #5
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()
예제 #6
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)
예제 #7
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)
예제 #8
0
    def test_destination_urls(self):
        request = Request.blank('http://example.com/1,2/path')

        headers = {'Content-Type': 'application/json'}
        response = RequestsResponseMock(status_code=200, headers=headers)
        self.grequests_map.return_value = [response]

        response___init__ = patch.object(SingleResponse,
                                         '__init__',
                                         return_value=None)
        response___init__.start()

        response_response = patch.object(SingleResponse, 'response')
        response_response.start()

        response = self.endpoints_route(request)

        """Since we're mocking grequests.map(), we need to manually iterate the
            generator to achieve 100% code coverage"""
        destination_urls = list(self.grequests_map.call_args[0][0])
        self.assertEqual(2, len(destination_urls))
        for grequest_request in destination_urls:
            self.assertTrue(grequest_request.url.endswith('/path'))

        response_response.stop()
        response___init__.stop()
예제 #9
0
    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):
        url_1 = 'http://1.example.com'
        status_code_1 = 200
        reason_1 = 'OK'
        headers_1 = {'Content-Type': 'text/html'}
        body_1 = '<html><body><h1>Hello World</h1></body></html>'
        response_html = RequestsResponseMock(url_1, status_code_1, reason_1,
                                             headers_1, body_1)

        url_2 = 'http://2.example.com'
        status_code_2 = 404
        reason_2 = 'Custom Not Found'
        headers_2 = {'Content-Type': 'application/json'}
        body_2 = '{"some": "json"}'
        response_json = RequestsResponseMock(url_2, status_code_2, reason_2,
                                             headers_2, body_2)

        response_base = ResponseBase(None)
        response_base._responses = [response_html, response_json, None]

        response_base._aggregate_response_bodies()

        response = response_base.response.json
        self.assertEqual(
            'application/json',
            response_base.response.content_type,
        )
        self.assertEqual(3, len(response))

        self.assertEqual(url_1, response[0]['url'])
        self.assertEqual('{0} {1}'.format(status_code_1, reason_1),
                         response[0]['status'])
        self.assertEqual(headers_1, response[0]['headers'])
        self.assertEqual(body_1, response[0]['body'])

        self.assertEqual(url_2, response[1]['url'])
        self.assertEqual('{0} {1}'.format(status_code_2, reason_2),
                         response[1]['status'])
        self.assertEqual(headers_2, response[1]['headers'])
        self.assertEqual(body_2, response[1]['body'])

        self.assertIsNone(response[2])
예제 #11
0
    def test_timeout_status_code(self):
        request = Request.blank('http://example.com/1,2/path')
        request.environ[ENVIRON_REQUEST_UUID_KEY] = uuid.uuid4()

        headers = {'Content-Type': 'application/json'}
        response1 = None
        response2 = RequestsResponseMock(status_code=200, headers=headers)
        self.grequests_map.return_value = [response1, response2]

        response = self.endpoints_route(request)

        self.assertEqual(504, response.status_code)
예제 #12
0
    def setUpResponse(self, headers, body, status_code=200):
        if not headers:
            headers = {'Content-Type': 'application/json'}

        def decode_content():
            pass

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

        return response
예제 #13
0
    def test_timeout_logged(self):
        request = Request.blank('http://example.com/1,2/path')
        request.environ[ENVIRON_REQUEST_UUID_KEY] = uuid.uuid4()

        headers = {'Content-Type': 'application/json'}
        response1 = None
        response2 = RequestsResponseMock(status_code=200, headers=headers)
        self.grequests_map.return_value = [response1, response2]

        with patch('logging.error') as le:
            self.endpoints_route(request)

            self.assertTrue(le.called)
예제 #14
0
    def test_timeout_body(self):
        request = Request.blank('http://example.com/1,2/path')
        request.environ[ENVIRON_REQUEST_UUID_KEY] = uuid.uuid4()

        headers = {'Content-Type': 'application/json'}
        response1 = None
        response2 = RequestsResponseMock(status_code=200,
                                         reason='OK',
                                         headers=headers)
        self.grequests_map.return_value = [response1, response2]

        response = self.endpoints_route(request)

        response_object = json.loads(response.body)
        self.assertTrue(None in response_object)
        response_200s = [i for i in response_object
                         if i is not None and i['status'] == '200 OK']
        self.assertTrue(len(response_200s) == 1)
예제 #15
0
    def test_single_endpoint(self):
        request = Request.blank('http://example.com/1/path')

        headers = {'Content-Type': 'application/json'}
        response = RequestsResponseMock(status_code=200, headers=headers)
        self.grequests_map.return_value = [response]

        response___init__ = patch.object(SingleResponse,
                                         '__init__',
                                         return_value=None)
        response___init__.start()

        mock_response = time.time()
        response_response = patch.object(SingleResponse,
                                         'response',
                                         new=mock_response)
        response_response.start()

        response = self.endpoints_route(request)

        self.assertEqual(mock_response, response)

        response_response.stop()
        response___init__.stop()
예제 #16
0
    def test___call__(self):
        route = ForwardingRoute(['http://example.com'], 'http://1.example.com')

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

        requests_response = RequestsResponseMock(headers={})
        requests_patcher = patch('requests.request',
                                 return_value=requests_response)
        requests_patcher.start()

        response___init__ = patch.object(SingleResponse,
                                         '__init__',
                                         return_value=None)
        sr_mock = response___init__.start()

        response_response = patch.object(SingleResponse, 'response')
        response_response.start()

        route(request)

        sr_mock.assert_called_with(request, requests_response)

        response_response.stop()
        response___init__.stop()