-
Notifications
You must be signed in to change notification settings - Fork 1
/
router_test.go
80 lines (64 loc) · 2.2 KB
/
router_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
package pxy
import (
"testing"
"io"
"net"
"net/http"
"net/http/httptest"
)
func StubHttpServer(url, response string) *httptest.Server{
server := httptest.NewUnstartedServer(http.HandlerFunc(func(responseWrite http.ResponseWriter, reqeust *http.Request) {
io.WriteString(responseWrite, response)
}))
listener, _ := net.Listen("tcp", url)
server.Listener.Close()
server.Listener = listener
return server
}
func TestConfig(t *testing.T) {
config := Config{}
config.load("/root/pxy/config_example.json")
expectingTTL := 60
if config.TTL != int64(expectingTTL) {
t.Errorf("expecting TTL is %d, but get %d", expectingTTL, config.TTL)
}
expectingTTL = 10
if config.Resources[1].TTL != int64(expectingTTL) {
t.Errorf("expecting TTL is %d, but get %d", expectingTTL, config.Resources[1].TTL)
}
}
func TestCacheTransport(t *testing.T) {
router := Initialize("/root/pxy/config_example.json")
proxy := router.Proxy
host := "127.0.0.1:6666"
key := "/posts/5"
url := "http://" + host + key
server := StubHttpServer(host, "{\"userId\":2, \"id\":5, \"title\": \"sint suscipit perspiciatis velit dolorum rerum ipsa laboriosam odio\"}")
server.Start()
defer server.Close()
postRequest, _ := http.NewRequest("GET", url, nil)
response := httptest.NewRecorder()
router.ServeHTTP(response, postRequest)
_, ok := router.Proxy.Transport.(*CacheTransport).Cache.Get(key)
if !ok {
t.Errorf("didn't get the expectiing key: %s", key)
}
key = "/posts/6"
url = "http://" + host + key
flushRequest, _ := http.NewRequest("FLUSH", url, nil)
flushRequest.SetBasicAuth("flush", "abc")
_ = proxy
response = httptest.NewRecorder()
router.ServeHTTP(response, flushRequest)
_, ok = proxy.Transport.(*CacheTransport).Cache.Get(key)
if ok {
t.Errorf("should't get the unexpectiing key: %s", key)
}
router.Logger.Printf("flush result: %s", response.Body.String())
key = "/posts/7?item=%3Fa%3D3"
url = "http://" + host + key
paramsRequest, _ := http.NewRequest("GET", url, nil)
_ = proxy
response = httptest.NewRecorder()
router.ServeHTTP(response, paramsRequest)
}