This repository was archived by the owner on May 29, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathproxy_test.go
More file actions
53 lines (47 loc) · 1.6 KB
/
proxy_test.go
File metadata and controls
53 lines (47 loc) · 1.6 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
package apiproxy
import (
"bytes"
"net/http"
"net/http/httptest"
"net/url"
"testing"
)
func TestNewCachingSingleHostReverseProxy(t *testing.T) {
targetRequestCount := 0
targetResponseBody := []byte("qux")
// Start the target server.
targetMux := http.NewServeMux()
targetMux.HandleFunc("/foo", func(w http.ResponseWriter, r *http.Request) {
targetRequestCount++
w.Header().Add("Cache-Control", "max-age=60")
w.Write(targetResponseBody)
})
target := httptest.NewServer(targetMux)
defer target.Close()
targetURL := mustParseURL(t, target.URL)
// Start the reverse proxy.
proxyMux := http.NewServeMux()
proxyMux.Handle("/", NewCachingSingleHostReverseProxy(targetURL, nil))
proxy := httptest.NewServer(proxyMux)
defer proxy.Close()
proxyURL := mustParseURL(t, proxy.URL)
proxiedFooURL := proxyURL.ResolveReference(&url.URL{Path: "/foo"})
// First request will hit target because the response has not been cached yet.
res := httpGet(t, proxiedFooURL)
resBody := readAll(t, res.Body)
if want := 1; targetRequestCount != want {
t.Errorf("want targetRequestCount == %d, got %d", want, targetRequestCount)
}
if !bytes.Equal(targetResponseBody, resBody) {
t.Errorf("want response body == %q, got %q", targetResponseBody, resBody)
}
// Subsequent requests (within max-age) will hit cache.
res = httpGet(t, proxiedFooURL)
resBody = readAll(t, res.Body)
if want := 1; targetRequestCount != want {
t.Errorf("want targetRequestCount == %d, got %d", want, targetRequestCount)
}
if !bytes.Equal(targetResponseBody, resBody) {
t.Errorf("want response body == %q, got %q", targetResponseBody, resBody)
}
}