-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy_test.go
More file actions
87 lines (70 loc) · 2.18 KB
/
proxy_test.go
File metadata and controls
87 lines (70 loc) · 2.18 KB
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
package main
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
)
func TestProxy_HeaderStripping(t *testing.T) {
// Backend that echoes received headers as JSON.
backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
hdrs := make(map[string]string)
for _, name := range []string{"Cookie", "Authorization"} {
if v := r.Header.Get(name); v != "" {
hdrs[name] = v
}
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(hdrs)
}))
defer backend.Close()
cfg = &Config{GatewayURL: backend.URL}
proxy := newProxy()
req := httptest.NewRequest("GET", "/control/test", nil)
req.Header.Set("Cookie", "session=abc123")
req.Header.Set("Authorization", "Bearer secret")
rr := httptest.NewRecorder()
proxy.ServeHTTP(rr, req)
if rr.Code != http.StatusOK {
t.Fatalf("status = %d, want %d", rr.Code, http.StatusOK)
}
var received map[string]string
if err := json.NewDecoder(rr.Body).Decode(&received); err != nil {
t.Fatalf("decode response: %v", err)
}
if v, ok := received["Cookie"]; ok {
t.Errorf("backend received Cookie header: %q", v)
}
if v, ok := received["Authorization"]; ok {
t.Errorf("backend received Authorization header: %q", v)
}
}
func TestProxy_PathRewriting(t *testing.T) {
// Backend that echoes the request path.
backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(r.URL.Path))
}))
defer backend.Close()
cfg = &Config{GatewayURL: backend.URL}
proxy := newProxy()
req := httptest.NewRequest("GET", "/control/foo", nil)
rr := httptest.NewRecorder()
proxy.ServeHTTP(rr, req)
if rr.Code != http.StatusOK {
t.Fatalf("status = %d, want %d", rr.Code, http.StatusOK)
}
body := rr.Body.String()
if body != "/foo" {
t.Errorf("backend path = %q, want %q", body, "/foo")
}
}
func TestProxy_BadGateway(t *testing.T) {
cfg = &Config{GatewayURL: "http://127.0.0.1:1"}
proxy := newProxy()
req := httptest.NewRequest("GET", "/control/anything", nil)
rr := httptest.NewRecorder()
proxy.ServeHTTP(rr, req)
if rr.Code != http.StatusBadGateway {
t.Errorf("status = %d, want %d", rr.Code, http.StatusBadGateway)
}
}