|
4 | 4 | "fmt" |
5 | 5 | "net/http" |
6 | 6 | "net/http/httptest" |
| 7 | + "strconv" |
7 | 8 | "testing" |
8 | 9 | "time" |
9 | 10 |
|
@@ -89,6 +90,18 @@ func (s *S) SetUpSuite(c *C) { |
89 | 90 | w.Header().Set("Vary", "X-Madeup-Header") |
90 | 91 | w.Write([]byte("Some text content")) |
91 | 92 | })) |
| 93 | + |
| 94 | + updateFieldsCounter := 0 |
| 95 | + mux.HandleFunc("/updatefields", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 96 | + w.Header().Set("X-Counter", strconv.Itoa(updateFieldsCounter)) |
| 97 | + w.Header().Set("Etag", `"e"`) |
| 98 | + updateFieldsCounter++ |
| 99 | + if r.Header.Get("if-none-match") != "" { |
| 100 | + w.WriteHeader(http.StatusNotModified) |
| 101 | + } else { |
| 102 | + w.Write([]byte("Some text content")) |
| 103 | + } |
| 104 | + })) |
92 | 105 | } |
93 | 106 |
|
94 | 107 | func (s *S) TearDownSuite(c *C) { |
@@ -302,6 +315,22 @@ func (s *S) TestGetVaryUnused(c *C) { |
302 | 315 | c.Assert(resp2.Header.Get(XFromCache), Equals, "1") |
303 | 316 | } |
304 | 317 |
|
| 318 | +func (s *S) TestUpdateFields(c *C) { |
| 319 | + req, err := http.NewRequest("GET", s.server.URL+"/updatefields", nil) |
| 320 | + resp, err := s.client.Do(req) |
| 321 | + defer resp.Body.Close() |
| 322 | + c.Assert(err, IsNil) |
| 323 | + counter := resp.Header.Get("x-counter") |
| 324 | + |
| 325 | + resp2, err2 := s.client.Do(req) |
| 326 | + defer resp2.Body.Close() |
| 327 | + c.Assert(err2, IsNil) |
| 328 | + c.Assert(resp2.Header.Get(XFromCache), Equals, "1") |
| 329 | + counter2 := resp2.Header.Get("x-counter") |
| 330 | + |
| 331 | + c.Assert(counter, Not(Equals), counter2) |
| 332 | +} |
| 333 | + |
305 | 334 | func (s *S) TestParseCacheControl(c *C) { |
306 | 335 | h := http.Header{} |
307 | 336 | for _ = range parseCacheControl(h) { |
@@ -459,3 +488,54 @@ func (s *S) TestMaxStaleValue(c *C) { |
459 | 488 |
|
460 | 489 | c.Assert(getFreshness(respHeaders, reqHeaders), Equals, stale) |
461 | 490 | } |
| 491 | + |
| 492 | +type containsHeaderChecker struct { |
| 493 | + *CheckerInfo |
| 494 | +} |
| 495 | + |
| 496 | +func (c *containsHeaderChecker) Check(params []interface{}, names []string) (bool, string) { |
| 497 | + items, ok := params[0].([]string) |
| 498 | + if !ok { |
| 499 | + return false, "Expected first param to be []string" |
| 500 | + } |
| 501 | + value, ok := params[1].(string) |
| 502 | + if !ok { |
| 503 | + return false, "Expected 2nd param to be string" |
| 504 | + } |
| 505 | + return containsHeader(items, value), "" |
| 506 | +} |
| 507 | + |
| 508 | +var ContainsHeader Checker = &containsHeaderChecker{&CheckerInfo{Name: "Contains", Params: []string{"Container", "expected to contain"}}} |
| 509 | + |
| 510 | +func (s *S) TestGetEndToEndHeaders(c *C) { |
| 511 | + var ( |
| 512 | + headers http.Header |
| 513 | + end2end []string |
| 514 | + ) |
| 515 | + |
| 516 | + headers = http.Header{} |
| 517 | + headers.Set("content-type", "text/html") |
| 518 | + headers.Set("te", "deflate") |
| 519 | + |
| 520 | + end2end = getEndToEndHeaders(headers) |
| 521 | + c.Check(end2end, ContainsHeader, "content-type") |
| 522 | + c.Check(end2end, Not(ContainsHeader), "te") |
| 523 | + |
| 524 | + headers = http.Header{} |
| 525 | + headers.Set("connection", "content-type") |
| 526 | + headers.Set("content-type", "text/csv") |
| 527 | + headers.Set("te", "deflate") |
| 528 | + end2end = getEndToEndHeaders(headers) |
| 529 | + c.Check(end2end, Not(ContainsHeader), "connection") |
| 530 | + c.Check(end2end, Not(ContainsHeader), "content-type") |
| 531 | + c.Check(end2end, Not(ContainsHeader), "te") |
| 532 | + |
| 533 | + headers = http.Header{} |
| 534 | + end2end = getEndToEndHeaders(headers) |
| 535 | + c.Check(end2end, HasLen, 0) |
| 536 | + |
| 537 | + headers = http.Header{} |
| 538 | + headers.Set("connection", "content-type") |
| 539 | + end2end = getEndToEndHeaders(headers) |
| 540 | + c.Check(end2end, HasLen, 0) |
| 541 | +} |
0 commit comments