def testReturnsTrueIfHeaderIsNone(self):
     'APIGateway.wasModifiedSince() should return True if the If-Modified-Since header is None'
     event = {}
     entity = {'lastModified': datetime.datetime.now()}
     sut = APIGateway(mockLoggerFactory, event)
     wasModifiedSince = sut.wasModifiedSince(entity)
     self.assertTrue(wasModifiedSince)
 def testReturnsFalseIfEqual(self):
     '''
     APIGateway.wasModifiedSince() should return False if the If-Modified-Since header's timestamp is equal to
     entity.lastModified's timestamp truncated to the second
     '''
     entity = {'lastModified': datetime.datetime.now()}
     event = {'headers': {'If-Modified-Since': formatDateRFC2822(entity['lastModified'])}}
     sut = APIGateway(mockLoggerFactory, event)
     wasModifiedSince = sut.wasModifiedSince(entity)
     self.assertFalse(wasModifiedSince)
 def testRaises(self):
     'APIGateway.wasModifiedSince() should raise if the If-Modified-Since header is not a valid RFC 2822 date'
     event = {'headers': {'If-Modified-Since': 'hello'}}
     entity = {'lastModified': datetime.datetime.now()}
     sut = APIGateway(mockLoggerFactory, event)
     with self.assertRaises(HttpError) as cm:
         wasModifiedSince = sut.wasModifiedSince(entity)
     self.assertEqual(cm.exception.statusCode, 400)
     self.assertEqual(cm.exception.message, 'Invalid If-Modified-Since header: "hello"')
     self.assertEqual(cm.exception.causes, ['ValueError(\'Unknown string format\',)'])
 def testReturnsTrueIfLess(self):
     '''
     APIGateway.wasModifiedSince() should return True if the If-Modified-Since header's timestamp is less than
     entity.lastModified's timestamp truncated to the second
     '''
     entity = {'lastModified': datetime.datetime.now()}
     event = {
         'headers': {
             'If-Modified-Since': formatDateRFC2822(entity['lastModified'] - datetime.timedelta(hours=1))
         }
     }
     sut = APIGateway(mockLoggerFactory, event)
     wasModifiedSince = sut.wasModifiedSince(entity)
     self.assertTrue(wasModifiedSince)