def test_disabled_for_the_rest(self): """Test that we don't tag the request as API on "regular" pages.""" request = self.request_factory.get('/overtherainbow') APIRequestMiddleware().process_request(request) assert not request.is_api request = self.request_factory.get('/overtherainbow') APIRequestMiddleware().process_request(request) assert not request.is_api
def test_disabled_for_the_rest(self): """Test that we don't tag the request as API on "regular" pages.""" request = mock.Mock() request.path_info = '/' APIRequestMiddleware().process_request(request) assert not request.is_api request.path = '/en-US/firefox/' APIRequestMiddleware().process_request(request) assert not request.is_api
def test_vary_not_applied_outside_api(self): request = mock.Mock() request.is_api = False response = HttpResponse() APIRequestMiddleware().process_response(request, response) assert not response.has_header('Vary') response['Vary'] = 'Foo, Bar' APIRequestMiddleware().process_response(request, response) assert response['Vary'] == 'Foo, Bar'
def test_vary_applied(self): request = mock.Mock() request.is_api = True response = HttpResponse() APIRequestMiddleware().process_response(request, response) assert response['Vary'] == 'X-Country-Code' response['Vary'] = 'Foo, Bar' APIRequestMiddleware().process_response(request, response) assert response['Vary'] == 'Foo, Bar, X-Country-Code'
def test_vary_not_applied_outside_api(self): request = self.request_factory.get('/somewhere') request.is_api = False response = HttpResponse() APIRequestMiddleware().process_response(request, response) assert not response.has_header('Vary') response['Vary'] = 'Foo, Bar' APIRequestMiddleware().process_response(request, response) assert response['Vary'] == 'Foo, Bar'
def test_vary_applied(self): request = self.request_factory.get('/api/v5/foo') request.is_api = True response = HttpResponse() APIRequestMiddleware().process_response(request, response) assert response['Vary'] == 'X-Country-Code, Accept-Language' response['Vary'] = 'Foo, Bar' APIRequestMiddleware().process_response(request, response) assert response['Vary'] == 'Foo, Bar, X-Country-Code, Accept-Language'
def test_500_api(self): # Simulate an early API 500 not caught by DRF from olympia.api.middleware import APIRequestMiddleware request = RequestFactory().get('/api/v4/addons/addon/lol/') APIRequestMiddleware().process_exception(request, Exception()) response = handler500(request) assert response.status_code == 500 assert response['Content-Type'] == 'application/json' data = json.loads(response.content) assert data['detail'] == 'Internal Server Error'
def test_api_identified(self): request = mock.Mock() request.path_info = '/api/v3/lol/' APIRequestMiddleware().process_request(request) assert request.is_api
def test_api_identified(self): request = self.request_factory.get('/api/v3/lol/') APIRequestMiddleware().process_request(request) assert request.is_api