-
Notifications
You must be signed in to change notification settings - Fork 1
/
handler_test.go
74 lines (67 loc) · 1.43 KB
/
handler_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
package s3proxy
import (
"net/http"
"net/http/httptest"
"os"
"testing"
)
func okHandler(w http.ResponseWriter, req *http.Request) {
w.Write([]byte(`ok`))
}
func newRequest(method, url string) *http.Request {
req, err := http.NewRequest(method, url, nil)
if err != nil {
panic(err)
}
req.Host = "example.com"
return req
}
var testBucket = "go-s3proxy"
func init() {
if bucket := os.Getenv("S3PROXY_TESTBUCKET"); bucket != "" {
testBucket = bucket
}
}
func testHandler() http.Handler {
mux := http.NewServeMux()
mux.HandleFunc("/", okHandler)
mux.Handle("/.well-known/acme-challenge/", Proxy(testBucket))
mux.Handle("/.well-known/acme-challenge2/", Proxy("invalid"))
return mux
}
func TestHandler(t *testing.T) {
tests := []struct {
Method string
Path string
Code int
}{
{
Method: "GET",
Path: "/",
Code: 200,
},
{
Method: "GET",
Path: "/.well-known/acme-challenge/404",
Code: 404,
},
{
Method: "GET",
Path: "/.well-known/acme-challenge/200",
Code: 200,
},
{
Method: "GET",
Path: "/.well-known/acme-challenge2/403",
Code: 403,
},
}
for _, test := range tests {
t.Logf("Testing %s %s", test.Method, test.Path)
writer := httptest.NewRecorder()
testHandler().ServeHTTP(writer, newRequest(test.Method, test.Path))
if writer.Code != test.Code {
t.Errorf("expected %d but got %d, res.headers: %v", test.Code, writer.Code, writer.HeaderMap)
}
}
}