예제 #1
0
    def test_last_modified_as_cache_key(self, timestamp1, timestamp2, cache_stale_expected):
        client = Client()

        mocked_get = mock.MagicMock(return_value=None)
        with mock.patch.multiple(CacheJson, timestamp=timestamp1, get=mocked_get):
            client.get(
                reverse('last_modified'),
            )
            self.assertTrue(mocked_get.called)

        mocked_get = mock.MagicMock(return_value=None)
        with mock.patch.multiple(CacheJson, timestamp=timestamp1, get=mocked_get):
            client.get(
                reverse('last_modified'),
            )
            self.assertFalse(mocked_get.called)

        mocked_get = mock.MagicMock(return_value=None)
        with mock.patch.multiple(CacheJson, timestamp=timestamp2, get=mocked_get):
            client.get(
                reverse('last_modified'),
            )
            self.assertEqual(cache_stale_expected, mocked_get.called)

        mocked_get = mock.MagicMock(return_value=None)
        with mock.patch.multiple(CacheJson, timestamp=timestamp2, get=mocked_get):
            client.get(
                reverse('last_modified'),
            )
            self.assertFalse(mocked_get.called)
예제 #2
0
    def test_last_modified_as_cache_key(self, timestamp1, timestamp2,
                                        cache_stale_expected):
        client = Client()

        mocked_get = mock.MagicMock(return_value=None)
        with mock.patch.multiple(CacheJson,
                                 timestamp=timestamp1,
                                 get=mocked_get):
            client.get(reverse('last_modified'), )
            self.assertTrue(mocked_get.called)

        mocked_get = mock.MagicMock(return_value=None)
        with mock.patch.multiple(CacheJson,
                                 timestamp=timestamp1,
                                 get=mocked_get):
            client.get(reverse('last_modified'), )
            self.assertFalse(mocked_get.called)

        mocked_get = mock.MagicMock(return_value=None)
        with mock.patch.multiple(CacheJson,
                                 timestamp=timestamp2,
                                 get=mocked_get):
            client.get(reverse('last_modified'), )
            self.assertEqual(cache_stale_expected, mocked_get.called)

        mocked_get = mock.MagicMock(return_value=None)
        with mock.patch.multiple(CacheJson,
                                 timestamp=timestamp2,
                                 get=mocked_get):
            client.get(reverse('last_modified'), )
            self.assertFalse(mocked_get.called)
예제 #3
0
    def test_validate_request(self):
        client = Client()
        path = reverse('validate')

        # GET validation
        response = client.get(path,
                              data=dict(
                                  get_required=123,
                                  get_not_required=123,
                                  unknown=123,
                              ))
        self.assertEqual(
            200,
            response.status_code,
        )

        # POST validation
        response = client.post(
            path,
            data=json.dumps(
                dict(
                    post_required=123,
                    post_not_required=123,
                    unknown=123,
                )),
            content_type='application/json',
        )
        self.assertEqual(
            200,
            response.status_code,
        )

        # GET and POST validation
        response = client.patch(
            '{path}?{query}'.format(
                path=path,
                query=urllib.parse.urlencode(dict(get_required=123)),
            ),
            data=json.dumps(dict(post_required=123)),
            content_type='application/json',
        )
        self.assertEqual(
            200,
            response.status_code,
        )
예제 #4
0
    def test_validate_request(self):
        client = Client()
        path = reverse('validate')

        # GET validation
        response = client.get(path, data=dict(
            get_required=123,
            get_not_required=123,
            unknown=123,
        ))
        self.assertEqual(
            200,
            response.status_code,
        )

        # POST validation
        response = client.post(
            path,
            data=json.dumps(dict(
                post_required=123,
                post_not_required=123,
                unknown=123,
            )),
            content_type='application/json',
        )
        self.assertEqual(
            200,
            response.status_code,
        )

        # GET and POST validation
        response = client.patch(
            '{path}?{query}'.format(
                path=path,
                query=urllib.parse.urlencode(dict(get_required=123)),
            ),
            data=json.dumps(dict(post_required=123)),
            content_type='application/json',
        )
        self.assertEqual(
            200,
            response.status_code,
        )
예제 #5
0
    def test_response_headers(self):
        client = Client()

        # GET request of not cached content with If-Modified-Since provided
        response = client.get(
            reverse('cache'),
            HTTP_IF_MODIFIED_SINCE='Thu, 01 Jan 2015 00:00:00 GMT',
        )
        self.assertIn('Content-Length', response)
        self.assertIn('Last-Modified', response)
        self.assertIn('Expires', response)
        self.assertIn('Cache-Control', response)
        self.assertIn('max-age=3600', response['Cache-Control'])
        self.assertEqual('Thu, 01 Jan 2015 00:00:00 GMT', response['Last-Modified'])
        self.assertEqual('0', response['Content-Length'])
        self.assertEqual(304, response.status_code)

        # GET request of cachable content
        response = client.get(reverse('cache'))
        self.assertEqual(Json.http_code, response.status_code)
        self.assertIn('Content-Length', response)
        self.assertIn('Content-Type', response)
        self.assertIn('Cache-Control', response)
        self.assertIn('Expires', response)
        self.assertIn('Last-Modified', response)
        self.assertEqual('14', response['Content-Length'])
        self.assertIn('max-age=3600', response['Cache-Control'])
        self.assertEqual('Thu, 01 Jan 2015 00:00:00 GMT', response['Last-Modified'])
        self.assertEqual('application/json', response['Content-Type'])

        # POST request of cachable content
        response = client.post(reverse('cache'))
        self.assertEqual(Json.http_code, response.status_code)
        self.assertIn('Content-Length', response)
        self.assertIn('Content-Type', response)
        self.assertNotIn('Expires', response)
        self.assertNotIn('Cache-Control', response)
        self.assertNotIn('Last-Modified', response)
        self.assertEqual('14', response['Content-Length'])
        self.assertEqual('application/json', response['Content-Type'])

        # GET request of cached content with If-Modified-Since provided
        response = client.get(
            reverse('cache'),
            HTTP_IF_MODIFIED_SINCE='Thu, 01 Jan 2015 00:00:00 GMT',
        )
        self.assertIn('Content-Length', response)
        self.assertIn('Last-Modified', response)
        self.assertIn('Expires', response)
        self.assertIn('Cache-Control', response)
        self.assertIn('max-age=3600', response['Cache-Control'])
        self.assertEqual('Thu, 01 Jan 2015 00:00:00 GMT', response['Last-Modified'])
        self.assertEqual('0', response['Content-Length'])
        self.assertEqual(304, response.status_code)

        # GET request of cached content without If-Modified-Since provided
        response = client.get(reverse('cache'))
        self.assertEqual(Json.http_code, response.status_code)
        self.assertIn('Content-Length', response)
        self.assertIn('Content-Type', response)
        self.assertIn('Cache-Control', response)
        self.assertIn('Expires', response)
        self.assertIn('Last-Modified', response)
        self.assertEqual('14', response['Content-Length'])
        self.assertIn('max-age=3600', response['Cache-Control'])
        self.assertEqual('Thu, 01 Jan 2015 00:00:00 GMT', response['Last-Modified'])
        self.assertEqual('application/json', response['Content-Type'])

        # POST request of cachable content with If-Modified-Since provided
        response = client.post(
            reverse('cache'),
            HTTP_IF_MODIFIED_SINCE='Thu, 01 Jan 2015 00:00:00 GMT',
        )
        self.assertEqual(Json.http_code, response.status_code)
        self.assertIn('Content-Length', response)
        self.assertNotIn('Expires', response)
        self.assertNotIn('Cache-Control', response)
        self.assertNotIn('Last-Modified', response)
        self.assertEqual('14', response['Content-Length'])
        self.assertEqual('application/json', response['Content-Type'])

        # GET request of non-cachable content
        response = client.get(reverse('ok'))
        self.assertIn('Cache-Control', response)
        self.assertIn('Content-Length', response)
        self.assertIn('Content-Type', response)
        self.assertIn('max-age=0', response['Cache-Control'])
        self.assertEqual('14', response['Content-Length'])
        self.assertEqual('application/json', response['Content-Type'])
예제 #6
0
 def test_status_code(self, url_name, expected_status_code):
     client = Client()
     response = client.get(reverse(url_name))
     self.assertEqual(expected_status_code, response.status_code)
예제 #7
0
    def test_response_headers(self):
        client = Client()

        # GET request of not cached content with If-Modified-Since provided
        response = client.get(
            reverse('cache'),
            HTTP_IF_MODIFIED_SINCE='Thu, 01 Jan 2015 00:00:00 GMT',
        )
        self.assertIn('Content-Length', response)
        self.assertIn('Last-Modified', response)
        self.assertIn('Expires', response)
        self.assertIn('Cache-Control', response)
        self.assertIn('max-age=3600', response['Cache-Control'])
        self.assertEqual('Thu, 01 Jan 2015 00:00:00 GMT',
                         response['Last-Modified'])
        self.assertEqual('0', response['Content-Length'])
        self.assertEqual(304, response.status_code)

        # GET request of cachable content
        response = client.get(reverse('cache'))
        self.assertEqual(Json.http_code, response.status_code)
        self.assertIn('Content-Length', response)
        self.assertIn('Content-Type', response)
        self.assertIn('Cache-Control', response)
        self.assertIn('Expires', response)
        self.assertIn('Last-Modified', response)
        self.assertEqual('14', response['Content-Length'])
        self.assertIn('max-age=3600', response['Cache-Control'])
        self.assertEqual('Thu, 01 Jan 2015 00:00:00 GMT',
                         response['Last-Modified'])
        self.assertEqual('application/json', response['Content-Type'])

        # POST request of cachable content
        response = client.post(reverse('cache'))
        self.assertEqual(Json.http_code, response.status_code)
        self.assertIn('Content-Length', response)
        self.assertIn('Content-Type', response)
        self.assertNotIn('Expires', response)
        self.assertNotIn('Cache-Control', response)
        self.assertNotIn('Last-Modified', response)
        self.assertEqual('14', response['Content-Length'])
        self.assertEqual('application/json', response['Content-Type'])

        # GET request of cached content with If-Modified-Since provided
        response = client.get(
            reverse('cache'),
            HTTP_IF_MODIFIED_SINCE='Thu, 01 Jan 2015 00:00:00 GMT',
        )
        self.assertIn('Content-Length', response)
        self.assertIn('Last-Modified', response)
        self.assertIn('Expires', response)
        self.assertIn('Cache-Control', response)
        self.assertIn('max-age=3600', response['Cache-Control'])
        self.assertEqual('Thu, 01 Jan 2015 00:00:00 GMT',
                         response['Last-Modified'])
        self.assertEqual('0', response['Content-Length'])
        self.assertEqual(304, response.status_code)

        # GET request of cached content without If-Modified-Since provided
        response = client.get(reverse('cache'))
        self.assertEqual(Json.http_code, response.status_code)
        self.assertIn('Content-Length', response)
        self.assertIn('Content-Type', response)
        self.assertIn('Cache-Control', response)
        self.assertIn('Expires', response)
        self.assertIn('Last-Modified', response)
        self.assertEqual('14', response['Content-Length'])
        self.assertIn('max-age=3600', response['Cache-Control'])
        self.assertEqual('Thu, 01 Jan 2015 00:00:00 GMT',
                         response['Last-Modified'])
        self.assertEqual('application/json', response['Content-Type'])

        # POST request of cachable content with If-Modified-Since provided
        response = client.post(
            reverse('cache'),
            HTTP_IF_MODIFIED_SINCE='Thu, 01 Jan 2015 00:00:00 GMT',
        )
        self.assertEqual(Json.http_code, response.status_code)
        self.assertIn('Content-Length', response)
        self.assertNotIn('Expires', response)
        self.assertNotIn('Cache-Control', response)
        self.assertNotIn('Last-Modified', response)
        self.assertEqual('14', response['Content-Length'])
        self.assertEqual('application/json', response['Content-Type'])

        # GET request of non-cachable content
        response = client.get(reverse('ok'))
        self.assertIn('Cache-Control', response)
        self.assertIn('Content-Length', response)
        self.assertIn('Content-Type', response)
        self.assertIn('max-age=0', response['Cache-Control'])
        self.assertEqual('14', response['Content-Length'])
        self.assertEqual('application/json', response['Content-Type'])
예제 #8
0
 def test_status_code(self, url_name, expected_status_code):
     client = Client()
     response = client.get(reverse(url_name))
     self.assertEqual(expected_status_code, response.status_code)