-
Notifications
You must be signed in to change notification settings - Fork 3
/
mina_test.go
176 lines (156 loc) · 3.86 KB
/
mina_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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
"net/url"
"os"
"testing"
)
func checkResponse(body string, wantBody string, header string, wantHeader string) error {
if body != wantBody {
return fmt.Errorf("body got %q; want %q", body, wantBody)
}
if header != wantHeader {
return fmt.Errorf("header got %q; want %q", header, wantHeader)
}
return nil
}
func TestMina(t *testing.T) {
want := []byte("tweet")
backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write(want)
}))
defer backend.Close()
cacheDir := os.TempDir()
url, err := url.Parse(backend.URL)
if err != nil {
t.Fatal(err)
}
m := &Mina{
Target: url,
CacheDir: cacheDir,
Headers: map[string]string{},
}
frontend := httptest.NewServer(m)
defer frontend.Close()
// first time
gotBody, gotHeader := get(frontend.URL)
err = checkResponse(gotBody, string(want), gotHeader, XHeaderValueMiss)
if err != nil {
t.Fatal(err)
}
// second time
gotBody, gotHeader = get(frontend.URL)
err = checkResponse(gotBody, string(want), gotHeader, XHeaderValueHit)
if err != nil {
t.Fatal(err)
}
}
func TestIgnoreHeader(t *testing.T) {
want := []byte("tweet")
backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write(want)
}))
defer backend.Close()
cacheDir := os.TempDir()
url, err := url.Parse(backend.URL)
if err != nil {
t.Fatal(err)
}
m := &Mina{
Target: url,
CacheDir: cacheDir,
Headers: map[string]string{},
}
frontend := httptest.NewServer(m)
defer frontend.Close()
req, err := http.NewRequest("GET", frontend.URL, nil)
if err != nil {
log.Fatal(err)
}
req.Header.Add(RequestOptionsHeaderName, XHeaderValueIgnore)
client := http.Client{}
firstResponse, err := client.Do(req) // first time to call
if err != nil {
log.Fatal(err)
}
defer firstResponse.Body.Close()
buf, err := ioutil.ReadAll(firstResponse.Body)
if err != nil {
log.Fatal(err)
}
gotBody, gotHeader := string(buf), firstResponse.Header.Get(XHeaderName)
err = checkResponse(gotBody, string(want), gotHeader, XHeaderValueMiss)
if err != nil {
t.Fatal(err)
}
secondResponse, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
defer secondResponse.Body.Close()
buf, err = ioutil.ReadAll(secondResponse.Body)
if err != nil {
log.Fatal(err)
}
gotBody, gotHeader = string(buf), secondResponse.Header.Get(XHeaderName)
err = checkResponse(gotBody, string(want), gotHeader, XHeaderValueMiss)
if err != nil {
t.Fatal(err)
}
}
func TestNotModifiedStatusCode(t *testing.T) {
firstTime := true
want := []byte("tweet")
backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if firstTime {
// for example
w.Header().Set("If-Modified-Since", "Sun, 22 Nov 2015 07:10:45 GMT")
w.Header().Set("If-None-Match", "W/\"6a6248d0dfd45c6aac94b6ad02c856bb")
firstTime = false
} else {
w.WriteHeader(http.StatusNotModified)
}
w.Write(want)
}))
defer backend.Close()
cacheDir := os.TempDir()
url, err := url.Parse(backend.URL)
if err != nil {
t.Fatal(err)
}
m := &Mina{
Target: url,
CacheDir: cacheDir,
Headers: map[string]string{},
}
frontend := httptest.NewServer(m)
defer frontend.Close()
// first time with If-Modified-Since header
gotBody, gotHeader := get(frontend.URL)
err = checkResponse(gotBody, string(want), gotHeader, XHeaderValueMiss)
if err != nil {
t.Fatal(err)
}
// second time
gotBody, gotHeader = get(frontend.URL)
err = checkResponse(gotBody, string(want), gotHeader, XHeaderValueHit)
if err != nil {
t.Fatal(err)
}
}
func get(url string) (body string, xHeaderValue string) {
res, err := http.Get(url)
if err != nil {
log.Fatal(err)
}
defer res.Body.Close()
buf, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Fatal(err)
}
return string(buf), res.Header.Get(XHeaderName)
}