|
1
|
|
|
from oe_utils import conditional_http_tween_factory |
|
2
|
|
|
import unittest |
|
3
|
|
|
import pretend |
|
4
|
|
|
|
|
5
|
|
|
""" |
|
6
|
|
|
tests based on https://github.com/pypa/warehouse/blob/master/tests/unit/cache/test_http.py |
|
7
|
|
|
""" |
|
8
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
class TestConditionalHTTPTween(unittest.TestCase): |
|
11
|
|
|
|
|
12
|
|
|
def setUp(self): |
|
13
|
|
|
pass |
|
14
|
|
|
|
|
15
|
|
|
def tearDown(self): |
|
16
|
|
|
pass |
|
17
|
|
|
|
|
18
|
|
View Code Duplication |
def test_has_last_modified(self): |
|
|
|
|
|
|
19
|
|
|
response = pretend.stub( |
|
20
|
|
|
last_modified=pretend.stub(), |
|
21
|
|
|
status_code=200, |
|
22
|
|
|
etag=None, |
|
23
|
|
|
conditional_response=False, |
|
24
|
|
|
app_iter=iter([b"foo"]), |
|
25
|
|
|
content_length=None, |
|
26
|
|
|
) |
|
27
|
|
|
handler = pretend.call_recorder(lambda request: response) |
|
28
|
|
|
request = pretend.stub(method="GET") |
|
29
|
|
|
|
|
30
|
|
|
tween = conditional_http_tween_factory(handler, pretend.stub()) |
|
31
|
|
|
|
|
32
|
|
|
self.assertEqual(tween(request), response) |
|
33
|
|
|
self.assertListEqual(handler.calls, [pretend.call(request)]) |
|
34
|
|
|
self.assertTrue(response.conditional_response) |
|
35
|
|
|
|
|
36
|
|
|
def test_explicit_etag(self): |
|
37
|
|
|
response = pretend.stub( |
|
38
|
|
|
last_modified=None, |
|
39
|
|
|
etag="foo", |
|
40
|
|
|
conditional_response=False, |
|
41
|
|
|
app_iter=iter([b"foo"]), |
|
42
|
|
|
) |
|
43
|
|
|
handler = pretend.call_recorder(lambda request: response) |
|
44
|
|
|
request = pretend.stub() |
|
45
|
|
|
|
|
46
|
|
|
tween = conditional_http_tween_factory(handler, pretend.stub()) |
|
47
|
|
|
|
|
48
|
|
|
self.assertEqual(tween(request), response) |
|
49
|
|
|
self.assertListEqual(handler.calls, [pretend.call(request)]) |
|
50
|
|
|
self.assertTrue(response.conditional_response) |
|
51
|
|
|
|
|
52
|
|
View Code Duplication |
def test_no_etag(self): |
|
|
|
|
|
|
53
|
|
|
response = pretend.stub( |
|
54
|
|
|
status_code=200, |
|
55
|
|
|
last_modified=None, |
|
56
|
|
|
etag=None, |
|
57
|
|
|
conditional_response=False, |
|
58
|
|
|
app_iter=iter([b"foo"]), |
|
59
|
|
|
content_length=None, |
|
60
|
|
|
) |
|
61
|
|
|
handler = pretend.call_recorder(lambda request: response) |
|
62
|
|
|
request = pretend.stub(method="GET") |
|
63
|
|
|
|
|
64
|
|
|
tween = conditional_http_tween_factory(handler, pretend.stub()) |
|
65
|
|
|
|
|
66
|
|
|
self.assertEqual(tween(request), response) |
|
67
|
|
|
self.assertListEqual(handler.calls, [pretend.call(request)]) |
|
68
|
|
|
self.assertFalse(response.conditional_response) |
|
69
|
|
|
|
|
70
|
|
|
def test_implicit_etag(self): |
|
71
|
|
|
response = pretend.stub( |
|
72
|
|
|
last_modified=None, |
|
73
|
|
|
etag=None, |
|
74
|
|
|
conditional_response=False, |
|
75
|
|
|
md5_etag=pretend.call_recorder(lambda: None), |
|
76
|
|
|
app_iter=[b"foo"], |
|
77
|
|
|
status_code=200, |
|
78
|
|
|
) |
|
79
|
|
|
handler = pretend.call_recorder(lambda request: response) |
|
80
|
|
|
request = pretend.stub(method='GET') |
|
81
|
|
|
|
|
82
|
|
|
tween = conditional_http_tween_factory(handler, pretend.stub()) |
|
83
|
|
|
|
|
84
|
|
|
self.assertEqual(tween(request), response) |
|
85
|
|
|
self.assertListEqual(handler.calls, [pretend.call(request)]) |
|
86
|
|
|
self.assertTrue(response.conditional_response) |
|
87
|
|
|
self.assertListEqual(response.md5_etag.calls, [pretend.call()]) |
|
88
|
|
|
|
|
89
|
|
View Code Duplication |
def test_implicit_etag_post(self): |
|
|
|
|
|
|
90
|
|
|
response = pretend.stub( |
|
91
|
|
|
last_modified=None, |
|
92
|
|
|
etag=None, |
|
93
|
|
|
conditional_response=False, |
|
94
|
|
|
md5_etag=pretend.call_recorder(lambda: None), |
|
95
|
|
|
app_iter=[b"foo"], |
|
96
|
|
|
status_code=201, |
|
97
|
|
|
) |
|
98
|
|
|
handler = pretend.call_recorder(lambda request: response) |
|
99
|
|
|
request = pretend.stub(method='POST') |
|
100
|
|
|
|
|
101
|
|
|
tween = conditional_http_tween_factory(handler, pretend.stub()) |
|
102
|
|
|
|
|
103
|
|
|
self.assertEqual(tween(request), response) |
|
104
|
|
|
self.assertListEqual(handler.calls, [pretend.call(request)]) |
|
105
|
|
|
self.assertFalse(response.conditional_response) |
|
106
|
|
|
|