-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathproxy_test.go
76 lines (61 loc) · 1.93 KB
/
proxy_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
package beyond
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/gorilla/websocket"
"github.com/stretchr/testify/assert"
)
func init() {
hostProxy.Store("test.com", nil)
}
func TestH2W(t *testing.T) {
req, err := http.NewRequest("GET", "/foo?key=bar", nil)
assert.NoError(t, err)
req.Host = "websocketserver:9443"
actual, err := http2ws(req)
assert.NoError(t, err)
assert.Equal(t, "wss://websocketserver:9443/foo?key=bar", actual.String())
req.Host = "websock etserver:9443"
actual, err = http2ws(req)
assert.Error(t, err)
assert.Nil(t, actual)
}
func TestReproxyParseError(t *testing.T) {
test := sites.m["test"]
sites.m["test"] = map[string]bool{":": true}
err := reproxy()
assert.Contains(t, err.Error(), "missing protocol scheme")
sites.m["test"] = test
}
func TestWebsocketEcho(t *testing.T) {
// echo.websocket.org offline as of 2019/01/29
t.SkipNow()
server := httptest.NewServer(http.HandlerFunc(nexthop))
defer server.Close()
h := http.Header{"Host": []string{"echo.websocket.org"}}
c, _, err := websocket.DefaultDialer.Dial("ws:"+server.URL[5:], h)
assert.NotNil(t, c)
assert.NoError(t, err)
assert.NoError(t, c.WriteJSON(map[string]string{"test": "123"}))
v := map[string]string{}
assert.NoError(t, c.ReadJSON(&v))
assert.Equal(t, "123", v["test"])
}
func TestWebsocketNew(t *testing.T) {
r, err := http.NewRequest("GET", "https://demos.kaazing.com/echo", nil)
assert.NoError(t, err)
assert.True(t, websocketproxyCheckOrigin(r))
p, err := websocketproxyNew(r)
assert.NoError(t, err)
assert.Equal(t, "wss:"+r.URL.String()[6:], p.Backend(r).String())
}
func TestWSPDirector(t *testing.T) {
incoming, err := http.NewRequest("GET", "https://localhost", nil)
assert.NoError(t, err)
incoming.Header.Set("User-Agent", "User-Agent")
out := http.Header{}
websocketproxyDirector(incoming, out)
assert.Equal(t, out.Get("User-Agent"), "User-Agent")
assert.Equal(t, out.Get("X-Forwarded-Proto"), "https")
}