This repository has been archived by the owner on Apr 4, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
cdn_misc_test.go
124 lines (106 loc) · 3 KB
/
cdn_misc_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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package main
import (
"io/ioutil"
"net/http"
"testing"
)
// Should redirect from HTTP to HTTPS without hitting origin, whilst
// preserving path and query params. Fragments are not preserved because the
// client should reapply them:
// - http://tools.ietf.org/html/rfc7231#section-7.1.2
func TestMiscProtocolRedirect(t *testing.T) {
ResetBackends(backendsByPriority)
const reqPath = "/one/two"
const reqProto = "http"
const expectedProto = "https"
const expectedStatus = http.StatusMovedPermanently
const headerName = "Location"
var expectedURL string
originServer.SwitchHandler(func(w http.ResponseWriter, r *http.Request) {
t.Error("Request should not have made it to origin")
})
req := NewUniqueEdgeGET(t)
req.URL.Path = reqPath
req.URL.Scheme = reqProto
if len(req.URL.RawQuery) == 0 {
t.Fatal("Request must have query params to test preservation")
}
if req.URL.Fragment != "" {
t.Fatal("Request must not have fragment because preservation is not supported")
}
resp := RoundTripCheckError(t, req)
defer resp.Body.Close()
req.URL.Scheme = expectedProto
expectedURL = req.URL.String()
if resp.StatusCode != expectedStatus {
t.Errorf(
"Received incorrect status code. Expected %d, got %d",
expectedStatus,
resp.StatusCode,
)
}
if dest := resp.Header.Get(headerName); dest != expectedURL {
t.Errorf(
"Received incorrect %q header. Expected %q, got %q",
headerName,
expectedURL,
dest,
)
}
}
// Should return 403 and not invalidate the edge's cache for PURGE requests
// that come from IPs not in the whitelist. We assume that this is not
// running from a whitelisted address.
func TestMiscRestrictPurgeRequests(t *testing.T) {
ResetBackends(backendsByPriority)
var expectedBody string
var expectedStatus int
req := NewUniqueEdgeGET(t)
for requestCount := 1; requestCount < 4; requestCount++ {
switch requestCount {
case 1:
req.Method = "GET"
expectedBody = "this should not be purged"
expectedStatus = 200
originServer.SwitchHandler(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(expectedBody))
})
case 2:
req.Method = "PURGE"
expectedBody = ""
expectedStatus = 403
originServer.SwitchHandler(func(w http.ResponseWriter, r *http.Request) {
t.Error("Request should not have made it to origin")
w.Write([]byte(originServer.Name))
})
case 3:
req.Method = "GET"
expectedBody = "this should not be purged"
expectedStatus = 200
}
resp := RoundTripCheckError(t, req)
defer resp.Body.Close()
if resp.StatusCode != expectedStatus {
t.Errorf(
"Request %d received incorrect status code. Expected %d, got %d",
requestCount,
expectedStatus,
resp.StatusCode,
)
}
if expectedBody != "" {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
if bodyStr := string(body); bodyStr != expectedBody {
t.Errorf(
"Request %d received incorrect response body. Expected %q, got %q",
requestCount,
expectedBody,
bodyStr,
)
}
}
}
}