forked from alphagov/cdn-acceptance-tests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cdn_nocache_test.go
107 lines (81 loc) · 2.64 KB
/
cdn_nocache_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package main
import (
"fmt"
"net/http"
"testing"
)
// Should send request to origin by default
func TestNoCacheNewRequestOrigin(t *testing.T) {
ResetBackends(backendsByPriority)
uuid := NewUUID()
originServer.SwitchHandler(func(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" && r.URL.Path == fmt.Sprintf("/%s", uuid) {
w.Header().Set("EnsureOriginServed", uuid)
}
})
sourceURL := fmt.Sprintf("https://%s/%s", *edgeHost, uuid)
req, _ := http.NewRequest("GET", sourceURL, nil)
resp := RoundTripCheckError(t, req)
defer resp.Body.Close()
if resp.StatusCode != 200 {
t.Errorf("Status code expected 200, got %d", resp.StatusCode)
}
if d := resp.Header.Get("EnsureOriginServed"); d != uuid {
t.Errorf("EnsureOriginServed header has not come from Origin: expected %q, got %q", uuid, d)
}
}
// Should not cache the response to a POST request.
func TestNoCachePOST(t *testing.T) {
ResetBackends(backendsByPriority)
req := NewUniqueEdgeGET(t)
req.Method = "POST"
testThreeRequestsNotCached(t, req, nil)
}
// Should not cache responses with a `Cache-Control: no-cache` header.
// Varnish doesn't respect this by default.
func TestNoCacheCacheControlNoCache(t *testing.T) {
ResetBackends(backendsByPriority)
handler := func(h http.Header) {
h.Set("Cache-Control", "no-cache")
}
req := NewUniqueEdgeGET(t)
testThreeRequestsNotCached(t, req, handler)
}
// Should not cache responses with a `Cache-Control: no-store` header.
// Varnish doesn't respect this by default.
func TestNoCacheCacheControlNoStore(t *testing.T) {
ResetBackends(backendsByPriority)
handler := func(h http.Header) {
h.Set("Cache-Control", "no-store")
}
req := NewUniqueEdgeGET(t)
testThreeRequestsNotCached(t, req, handler)
}
// Should not cache a response with a `Cache-Control: private` header.
func TestNoCacheHeaderCacheControlPrivate(t *testing.T) {
ResetBackends(backendsByPriority)
handler := func(h http.Header) {
h.Set("Cache-Control", "private")
}
req := NewUniqueEdgeGET(t)
testThreeRequestsNotCached(t, req, handler)
}
// Should not cache a response with a `Cache-Control: max-age=0` header.
func TestNoCacheHeaderCacheControlMaxAge0(t *testing.T) {
ResetBackends(backendsByPriority)
handler := func(h http.Header) {
h.Set("Cache-Control", "max-age=0")
}
req := NewUniqueEdgeGET(t)
testThreeRequestsNotCached(t, req, handler)
}
// Should not cache a response with a `Vary: *` header.
func TestNoCacheHeaderVaryAsterisk(t *testing.T) {
t.Skip("Not widely supported")
ResetBackends(backendsByPriority)
handler := func(h http.Header) {
h.Set("Vary", "*")
}
req := NewUniqueEdgeGET(t)
testThreeRequestsNotCached(t, req, handler)
}