11
11
LAST_MODIFIED_NEWER_STR = 'Mon, 18 Oct 2010 16:56:23 GMT'
12
12
LAST_MODIFIED_INVALID_STR = 'Mon, 32 Oct 2010 16:56:23 GMT'
13
13
EXPIRED_LAST_MODIFIED_STR = 'Sat, 20 Oct 2007 23:21:47 GMT'
14
- ETAG = 'b4246ffc4f62314ca13147c9d4f76974'
15
- EXPIRED_ETAG = '7fae4cd4b0f81e7d2914700043aa8ed6'
14
+ ETAG = '" b4246ffc4f62314ca13147c9d4f76974" '
15
+ EXPIRED_ETAG = '" 7fae4cd4b0f81e7d2914700043aa8ed6" '
16
16
17
17
18
18
@override_settings (ROOT_URLCONF = 'conditional_processing.urls' )
@@ -24,7 +24,7 @@ def assertFullResponse(self, response, check_last_modified=True, check_etag=True
24
24
if check_last_modified :
25
25
self .assertEqual (response ['Last-Modified' ], LAST_MODIFIED_STR )
26
26
if check_etag :
27
- self .assertEqual (response ['ETag' ], '"%s"' % ETAG )
27
+ self .assertEqual (response ['ETag' ], ETAG )
28
28
29
29
def assertNotModified (self , response ):
30
30
self .assertEqual (response .status_code , 304 )
@@ -63,66 +63,66 @@ def test_if_unmodified_since(self):
63
63
self .assertEqual (response .status_code , 412 )
64
64
65
65
def test_if_none_match (self ):
66
- self .client .defaults ['HTTP_IF_NONE_MATCH' ] = '"%s"' % ETAG
66
+ self .client .defaults ['HTTP_IF_NONE_MATCH' ] = ETAG
67
67
response = self .client .get ('/condition/' )
68
68
self .assertNotModified (response )
69
- self .client .defaults ['HTTP_IF_NONE_MATCH' ] = '"%s"' % EXPIRED_ETAG
69
+ self .client .defaults ['HTTP_IF_NONE_MATCH' ] = EXPIRED_ETAG
70
70
response = self .client .get ('/condition/' )
71
71
self .assertFullResponse (response )
72
72
73
73
# Several etags in If-None-Match is a bit exotic but why not?
74
- self .client .defaults ['HTTP_IF_NONE_MATCH' ] = '"%s", "%s" ' % (ETAG , EXPIRED_ETAG )
74
+ self .client .defaults ['HTTP_IF_NONE_MATCH' ] = '%s, %s ' % (ETAG , EXPIRED_ETAG )
75
75
response = self .client .get ('/condition/' )
76
76
self .assertNotModified (response )
77
77
78
78
def test_if_match (self ):
79
- self .client .defaults ['HTTP_IF_MATCH' ] = '"%s"' % ETAG
79
+ self .client .defaults ['HTTP_IF_MATCH' ] = ETAG
80
80
response = self .client .put ('/condition/etag/' )
81
81
self .assertEqual (response .status_code , 200 )
82
- self .client .defaults ['HTTP_IF_MATCH' ] = '"%s"' % EXPIRED_ETAG
82
+ self .client .defaults ['HTTP_IF_MATCH' ] = EXPIRED_ETAG
83
83
response = self .client .put ('/condition/etag/' )
84
84
self .assertEqual (response .status_code , 412 )
85
85
86
86
def test_both_headers (self ):
87
87
# see http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html#sec13.3.4
88
88
self .client .defaults ['HTTP_IF_MODIFIED_SINCE' ] = LAST_MODIFIED_STR
89
- self .client .defaults ['HTTP_IF_NONE_MATCH' ] = '"%s"' % ETAG
89
+ self .client .defaults ['HTTP_IF_NONE_MATCH' ] = ETAG
90
90
response = self .client .get ('/condition/' )
91
91
self .assertNotModified (response )
92
92
93
93
self .client .defaults ['HTTP_IF_MODIFIED_SINCE' ] = EXPIRED_LAST_MODIFIED_STR
94
- self .client .defaults ['HTTP_IF_NONE_MATCH' ] = '"%s"' % ETAG
94
+ self .client .defaults ['HTTP_IF_NONE_MATCH' ] = ETAG
95
95
response = self .client .get ('/condition/' )
96
96
self .assertFullResponse (response )
97
97
98
98
self .client .defaults ['HTTP_IF_MODIFIED_SINCE' ] = LAST_MODIFIED_STR
99
- self .client .defaults ['HTTP_IF_NONE_MATCH' ] = '"%s"' % EXPIRED_ETAG
99
+ self .client .defaults ['HTTP_IF_NONE_MATCH' ] = EXPIRED_ETAG
100
100
response = self .client .get ('/condition/' )
101
101
self .assertFullResponse (response )
102
102
103
103
self .client .defaults ['HTTP_IF_MODIFIED_SINCE' ] = EXPIRED_LAST_MODIFIED_STR
104
- self .client .defaults ['HTTP_IF_NONE_MATCH' ] = '"%s"' % EXPIRED_ETAG
104
+ self .client .defaults ['HTTP_IF_NONE_MATCH' ] = EXPIRED_ETAG
105
105
response = self .client .get ('/condition/' )
106
106
self .assertFullResponse (response )
107
107
108
108
def test_both_headers_2 (self ):
109
109
self .client .defaults ['HTTP_IF_UNMODIFIED_SINCE' ] = LAST_MODIFIED_STR
110
- self .client .defaults ['HTTP_IF_MATCH' ] = '"%s"' % ETAG
110
+ self .client .defaults ['HTTP_IF_MATCH' ] = ETAG
111
111
response = self .client .get ('/condition/' )
112
112
self .assertFullResponse (response )
113
113
114
114
self .client .defaults ['HTTP_IF_UNMODIFIED_SINCE' ] = EXPIRED_LAST_MODIFIED_STR
115
- self .client .defaults ['HTTP_IF_MATCH' ] = '"%s"' % EXPIRED_ETAG
115
+ self .client .defaults ['HTTP_IF_MATCH' ] = ETAG
116
116
response = self .client .get ('/condition/' )
117
117
self .assertEqual (response .status_code , 412 )
118
118
119
- self .client .defaults ['HTTP_IF_UNMODIFIED_SINCE' ] = LAST_MODIFIED_STR
120
- self .client .defaults ['HTTP_IF_MATCH' ] = '"%s"' % EXPIRED_ETAG
119
+ self .client .defaults ['HTTP_IF_UNMODIFIED_SINCE' ] = EXPIRED_LAST_MODIFIED_STR
120
+ self .client .defaults ['HTTP_IF_MATCH' ] = EXPIRED_ETAG
121
121
response = self .client .get ('/condition/' )
122
122
self .assertEqual (response .status_code , 412 )
123
123
124
- self .client .defaults ['HTTP_IF_UNMODIFIED_SINCE' ] = EXPIRED_LAST_MODIFIED_STR
125
- self .client .defaults ['HTTP_IF_MATCH' ] = '"%s"' % ETAG
124
+ self .client .defaults ['HTTP_IF_UNMODIFIED_SINCE' ] = LAST_MODIFIED_STR
125
+ self .client .defaults ['HTTP_IF_MATCH' ] = EXPIRED_ETAG
126
126
response = self .client .get ('/condition/' )
127
127
self .assertEqual (response .status_code , 412 )
128
128
@@ -134,7 +134,7 @@ def test_single_condition_1(self):
134
134
self .assertFullResponse (response , check_last_modified = False )
135
135
136
136
def test_single_condition_2 (self ):
137
- self .client .defaults ['HTTP_IF_NONE_MATCH' ] = '"%s"' % ETAG
137
+ self .client .defaults ['HTTP_IF_NONE_MATCH' ] = ETAG
138
138
response = self .client .get ('/condition/etag/' )
139
139
self .assertNotModified (response )
140
140
response = self .client .get ('/condition/last_modified/' )
@@ -146,7 +146,7 @@ def test_single_condition_3(self):
146
146
self .assertFullResponse (response , check_etag = False )
147
147
148
148
def test_single_condition_4 (self ):
149
- self .client .defaults ['HTTP_IF_NONE_MATCH' ] = '"%s"' % EXPIRED_ETAG
149
+ self .client .defaults ['HTTP_IF_NONE_MATCH' ] = EXPIRED_ETAG
150
150
response = self .client .get ('/condition/etag/' )
151
151
self .assertFullResponse (response , check_last_modified = False )
152
152
@@ -158,7 +158,7 @@ def test_single_condition_5(self):
158
158
self .assertFullResponse (response , check_last_modified = False )
159
159
160
160
def test_single_condition_6 (self ):
161
- self .client .defaults ['HTTP_IF_NONE_MATCH' ] = '"%s"' % ETAG
161
+ self .client .defaults ['HTTP_IF_NONE_MATCH' ] = ETAG
162
162
response = self .client .get ('/condition/etag2/' )
163
163
self .assertNotModified (response )
164
164
response = self .client .get ('/condition/last_modified2/' )
@@ -188,7 +188,34 @@ def test_single_condition_head(self):
188
188
response = self .client .head ('/condition/' )
189
189
self .assertNotModified (response )
190
190
191
+ def test_unquoted (self ):
192
+ """
193
+ The same quoted ETag should be set on the header regardless of whether
194
+ etag_func() in condition() returns a quoted or an unquoted ETag.
195
+ """
196
+ response_quoted = self .client .get ('/condition/etag/' )
197
+ response_unquoted = self .client .get ('/condition/unquoted_etag/' )
198
+ self .assertEqual (response_quoted ['ETag' ], response_unquoted ['ETag' ])
199
+
200
+ # It's possible that the matching algorithm could use the wrong value even
201
+ # if the ETag header is set correctly correctly (as tested by
202
+ # test_unquoted()), so check that the unquoted value is matched.
203
+ def test_unquoted_if_none_match (self ):
204
+ self .client .defaults ['HTTP_IF_NONE_MATCH' ] = ETAG
205
+ response = self .client .get ('/condition/unquoted_etag/' )
206
+ self .assertNotModified (response )
207
+ self .client .defaults ['HTTP_IF_NONE_MATCH' ] = EXPIRED_ETAG
208
+ response = self .client .get ('/condition/unquoted_etag/' )
209
+ self .assertFullResponse (response , check_last_modified = False )
210
+
211
+ def test_all_if_none_match (self ):
212
+ self .client .defaults ['HTTP_IF_NONE_MATCH' ] = '*'
213
+ response = self .client .get ('/condition/etag/' )
214
+ self .assertNotModified (response )
215
+ response = self .client .get ('/condition/no_etag/' )
216
+ self .assertFullResponse (response , check_last_modified = False , check_etag = False )
217
+
191
218
def test_invalid_etag (self ):
192
- self .client .defaults ['HTTP_IF_NONE_MATCH' ] = r'"\ "'
219
+ self .client .defaults ['HTTP_IF_NONE_MATCH' ] = '"" "'
193
220
response = self .client .get ('/condition/etag/' )
194
221
self .assertFullResponse (response , check_last_modified = False )
0 commit comments