This repository has been archived by the owner on Mar 16, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathhttp_test.go
206 lines (162 loc) · 4.03 KB
/
http_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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package abutil
import (
"bytes"
"crypto/tls"
"fmt"
"net"
"net/http"
"net/http/httptest"
"testing"
"time"
)
func mockRequestContext(t *testing.T, fn func(*http.Request)) {
b := bytes.NewReader([]byte(""))
r, err := http.NewRequest("GET", "http://some.url", b)
if err != nil {
t.Error(err)
}
fn(r)
}
func TestRemoteIP(t *testing.T) {
ip := "123.456.7.8"
mockRequestContext(t, func(r *http.Request) {
r.Header = http.Header{
"X-Real-Ip": []string{ip},
}
out := RemoteIP(r)
if out != ip {
t.Errorf("Expected %s, but got %s", ip, out)
}
})
mockRequestContext(t, func(r *http.Request) {
r.Header = http.Header{
"X-Forwarded-For": []string{ip},
}
out := RemoteIP(r)
if out != ip {
t.Errorf("Expected %s, but got %s", ip, out)
}
})
mockRequestContext(t, func(r *http.Request) {
r.RemoteAddr = ip
out := RemoteIP(r)
if out != ip {
t.Errorf("Expected %s, but got %s", ip, out)
}
})
mockRequestContext(t, func(r *http.Request) {
ip := "127.0.0.1"
r.RemoteAddr = "["
out := RemoteIP(r)
if out != ip {
t.Errorf("Expected %s, but got %s", ip, out)
}
})
}
func remoteIPMockServe(h http.HandlerFunc) {
mockRequestContext(nil, func(r *http.Request) {
r.RemoteAddr = "123.456.7.8"
w := httptest.NewRecorder()
h(w, r)
})
}
func ExampleRemoteIP() {
someHandler := func(w http.ResponseWriter, r *http.Request) {
fmt.Printf("New request from %s\n", RemoteIP(r))
}
// Get's called with RemoteAddr = 123.456.7.8
remoteIPMockServe(someHandler)
// Output: New request from 123.456.7.8
}
func gracefulServerContext(t *testing.T, fn func(*GracefulServer)) {
p := 1337
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Foobar"))
})
fn(NewGracefulServer(p, h))
}
func TestGracefulServer(t *testing.T) {
gracefulServerContext(t, func(s *GracefulServer) {
if s.Server.NoSignalHandling != true {
t.Error("NoSignalHandling should be true")
}
if s.Server.Addr != ":1337" {
t.Error("Didn't set the port correctly")
}
})
gracefulServerContext(t, func(s *GracefulServer) {
if !s.Stopped() {
t.Error("Stopped returned false, but shouldn't")
}
s.setStopped(false)
if s.Stopped() {
t.Error("Stopped returned true, but shouldn't")
}
})
gracefulServerContext(t, func(s *GracefulServer) {
time.AfterFunc(20*time.Millisecond, func() {
s.Stop(0)
})
s.ListenAndServe()
if !s.Stopped() {
t.Error("Stopped returned false after Stop()")
}
})
gracefulServerContext(t, func(s *GracefulServer) {
time.AfterFunc(20*time.Millisecond, func() {
if s.Stopped() {
t.Error("Server should not be stopped when running")
}
s.Stop(0)
})
s.ListenAndServe()
})
gracefulServerContext(t, func(s *GracefulServer) {
time.AfterFunc(20*time.Millisecond, func() {
if s.Stopped() {
t.Error("Server should not be stopped when running")
}
s.Stop(0)
})
s.ListenAndServeTLS("foo", "bar")
})
gracefulServerContext(t, func(s *GracefulServer) {
time.AfterFunc(20*time.Millisecond, func() {
if s.Stopped() {
t.Error("Server should not be stopped when running")
}
s.Stop(0)
})
s.ListenAndServeTLSConfig(&tls.Config{})
})
gracefulServerContext(t, func(s *GracefulServer) {
time.AfterFunc(20*time.Millisecond, func() {
if s.Stopped() {
t.Error("Server should not be stopped when running")
}
s.Stop(0)
})
s.Serve(&net.TCPListener{})
})
}
func ExampleGracefulServer() {
s := NewGracefulServer(1337,
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Foo bar"))
}))
// This channel blocks until all connections are closed or the time is up
sc := s.StopChan()
// Some go func that stops the server after 2 seconds for no reason
time.AfterFunc(1*time.Second, func() {
fmt.Print("Stopping server..")
s.Stop(10 * time.Second)
})
if err := s.ListenAndServe(); err != nil && !s.Stopped() {
// We didn't stop the server, so something must be wrong
panic(err)
}
// Wait for the server to finish
<-sc
fmt.Print("bye!")
// Output: Stopping server..bye!
}