예제 #1
0
    def test_not_modified(self):
        """Test when pages is not modified"""
        with self.app.app_context():
            rv = self.client.get('/abs/0704.0600')
            self.assertEqual(rv.status_code, 200)

            mod_dt = parser.parse(rv.headers['Last-Modified'])

            rv = self.client.get(
                '/abs/0704.0600',
                headers={'If-Modified-Since': mime_header_date(mod_dt)})
            self.assertEqual(rv.status_code, 304)

            rv = self.client.get('/abs/0704.0600',
                                 headers={
                                     'If-Modified-Since':
                                     mime_header_date(mod_dt +
                                                      timedelta(seconds=-1))
                                 })
            self.assertEqual(rv.status_code, 200)

            rv = self.client.get(
                '/abs/0704.0600',
                headers={'If-None-Match': '"should-never-match"'})
            self.assertEqual(rv.status_code, 200)
예제 #2
0
    def test_mime_header_date(self) -> None:
        """Test MIME header date string is correct."""

        config = get_application_config()
        tz = gettz('US/Eastern')

        dt = datetime(year=2018, month=9, day=14,
                      hour=19, minute=1, tzinfo=tzutc())
        self.assertEqual(mime_header_date(dt), 'Fri, 14 Sep 2018 19:01:00 GMT')

        dt = datetime(year=2018, month=9, day=14,
                      hour=20, minute=1, tzinfo=tz)
        self.assertEqual(mime_header_date(dt), 'Sat, 15 Sep 2018 00:01:00 GMT')
예제 #3
0
def _check_request_headers(docmeta: DocMetadata, response_data: Dict[str, Any],
                           headers: Dict[str, Any]) -> bool:
    """Check the request headers, update the response headers accordingly."""
    last_mod_dt: datetime = docmeta.modified

    # Latest trackback ping time depends on the database
    if 'trackback_ping_latest' in response_data \
       and isinstance(response_data['trackback_ping_latest'], datetime) \
       and response_data['trackback_ping_latest'] > last_mod_dt:
        # If there is a more recent trackback ping, use that datetime
        last_mod_dt = response_data['trackback_ping_latest']

    # Check for request headers If-Modified-Since and If-None-Match and compare
    # them to the last modified time to determine whether we will return a
    # "not modified" response
    mod_since_dt = _time_header_parse(headers, 'If-Modified-Since')
    none_match_dt = _time_header_parse(headers, 'If-None-Match')
    not_modified = _not_modified(last_mod_dt, mod_since_dt, none_match_dt)

    last_mod_mime = mime_header_date(last_mod_dt)
    headers['Last-Modified'] = last_mod_mime
    headers['ETag'] = last_mod_mime
    headers['Expires'] = abs_expires_header()[1]

    return not_modified
예제 #4
0
def _check_request_headers(docmeta: DocMetadata, response_data: Dict[str, Any],
                           resp_headers: Dict[str, Any]) -> bool:
    """Check the request headers, update the response headers accordingly."""
    last_mod_dt: datetime = docmeta.modified

    # Latest trackback ping time depends on the database
    if 'trackback_ping_latest' in response_data \
       and isinstance(response_data['trackback_ping_latest'], datetime) \
       and response_data['trackback_ping_latest'] > last_mod_dt:
        # If there is a more recent trackback ping, use that datetime
        last_mod_dt = response_data['trackback_ping_latest']

    last_mod_mime = mime_header_date(last_mod_dt)
    etag = f'"{last_mod_mime}"'

    resp_headers['Last-Modified'] = last_mod_mime
    resp_headers['ETag'] = etag
    resp_headers['Expires'] = abs_expires_header()[1]

    not_modified = _not_modified(last_mod_dt,
                                 _time_header_parse('If-Modified-Since'),
                                 _get_req_header('if-none-match'), etag)

    return not_modified