]> kaliko git repositories - mpd-sima.git/blob - tests/test_http.py
Fixed test_config
[mpd-sima.git] / tests / test_http.py
1 # -*- coding: utf-8 -*-
2
3 import unittest
4 import time
5
6 from unittest.mock import Mock
7
8 from sima.lib.cache import DictCache
9 from sima.lib.http import CacheController
10
11 TIME_FMT = "%a, %d %b %Y %H:%M:%S GMT"
12
13
14 class TestCacheControlRequest(unittest.TestCase):
15     url = 'http://foo.com/bar'
16
17     def setUp(self):
18         self.c = CacheController(
19             DictCache(),
20         )
21
22     def req(self, headers):
23         return self.c.cached_request(Mock(url=self.url, headers=headers))
24
25     def test_cache_request_no_cache(self):
26         resp = self.req({'cache-control': 'no-cache'})
27         assert not resp
28
29     def test_cache_request_pragma_no_cache(self):
30         resp = self.req({'pragma': 'no-cache'})
31         assert not resp
32
33     def test_cache_request_no_store(self):
34         resp = self.req({'cache-control': 'no-store'})
35         assert not resp
36
37     def test_cache_request_max_age_0(self):
38         resp = self.req({'cache-control': 'max-age=0'})
39         assert not resp
40
41     def test_cache_request_not_in_cache(self):
42         resp = self.req({})
43         assert not resp
44
45     def test_cache_request_fresh_max_age(self):
46         now = time.strftime(TIME_FMT, time.gmtime())
47         resp = Mock(headers={'cache-control': 'max-age=3600',
48                              'date': now})
49
50         cache = DictCache({self.url: resp})
51         self.c.cache = cache
52         r = self.req({})
53         assert r == resp
54
55     def test_cache_request_unfresh_max_age(self):
56         earlier = time.time() - 3700  # epoch - 1h01m40s
57         now = time.strftime(TIME_FMT, time.gmtime(earlier))
58         resp = Mock(headers={'cache-control': 'max-age=3600',
59                              'date': now})
60         self.c.cache = DictCache({self.url: resp})
61         r = self.req({})
62         assert not r
63
64     def test_cache_request_fresh_expires(self):
65         later = time.time() + 86400  # GMT + 1 day
66         expires = time.strftime(TIME_FMT, time.gmtime(later))
67         now = time.strftime(TIME_FMT, time.gmtime())
68         resp = Mock(headers={'expires': expires,
69                              'date': now})
70         cache = DictCache({self.url: resp})
71         self.c.cache = cache
72         r = self.req({})
73         assert r == resp
74
75     def test_cache_request_unfresh_expires(self):
76         sooner = time.time() - 86400  # GMT - 1 day
77         expires = time.strftime(TIME_FMT, time.gmtime(sooner))
78         now = time.strftime(TIME_FMT, time.gmtime())
79         resp = Mock(headers={'expires': expires,
80                              'date': now})
81         cache = DictCache({self.url: resp})
82         self.c.cache = cache
83         r = self.req({})
84         assert not r
85
86 # VIM MODLINE
87 # vim: ai ts=4 sw=4 sts=4 expandtab
88