-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
107 lines (93 loc) · 2.81 KB
/
server.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
package proxy_test_server
import (
"io"
"log"
"net/http"
"os"
"strconv"
"strings"
"time"
)
type RandomReader struct {
toRead int
written int
}
func RunUpstreamFakeServer(port string, fork bool) *http.Server {
log.Printf("Starting fake upstream server for functional tests on %s", port)
srv := &http.Server{Addr: port}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "ROOT-INDEX-OK")
})
http.HandleFunc("/slow/", func(w http.ResponseWriter, r *http.Request) {
params := strings.Split(strings.Replace(r.URL.Path, "/slow/", "", 1), "/")
if len(params) < 2 {
http.Error(w, "Bad usage", http.StatusBadRequest)
return
}
code, _ := strconv.Atoi(params[0])
delay, _ := strconv.Atoi(params[1])
message := http.StatusText(code)
if message == "" {
code = http.StatusBadRequest
message = http.StatusText(code)
}
time.Sleep(time.Duration(delay) * time.Millisecond)
http.Error(w, message, code)
})
http.HandleFunc("/slow/no-response/", func(w http.ResponseWriter, r *http.Request) {
delay, _ := strconv.Atoi(strings.Replace(r.URL.Path, "/slow/no-response/", "", 1))
time.Sleep(time.Duration(delay) * time.Millisecond)
// find a nicer way...?
panic("Believe I need to panic to create empty response")
})
http.HandleFunc("/code/", func(w http.ResponseWriter, r *http.Request) {
code, _ := strconv.Atoi(strings.Replace(r.URL.Path, "/code/", "", 1))
message := http.StatusText(code)
if message == "" {
code = http.StatusBadRequest
message = http.StatusText(code)
}
http.Error(w, message, code)
})
http.HandleFunc("/size/", func(w http.ResponseWriter, r *http.Request) {
size, _ := strconv.Atoi(strings.Replace(r.URL.Path, "/size/", "", 1))
w.Header().Set("Content-Length", strconv.Itoa(size))
io.Copy(w, io.LimitReader(NewRandomReaderWithSizeLimit(size), int64(size)))
})
http.HandleFunc("/shutdown", func(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "Fakeserver says ciao!")
go func() {
time.Sleep(250 * time.Millisecond)
os.Exit(0)
}()
})
// todo: return get args as json
// todo: echoBody / echoHeaders
// todo? route to dynamically set up fake response
if fork {
go func() {
if err := srv.ListenAndServe(); err != http.ErrServerClosed {
log.Fatalf("ListenAndServe(): %s", err)
}
}()
time.Sleep(250 * time.Millisecond)
} else {
err := srv.ListenAndServe()
log.Fatalf("ListenAndServe(): %s", err)
}
return srv
}
func NewRandomReaderWithSizeLimit(limit int) *RandomReader {
return &RandomReader{limit, 0}
}
func (r *RandomReader) Read(p []byte) (n int, err error) {
bufSize := len(p)
for i := 0; i < bufSize; i++ {
p[i] = 'X' // not supposed to be random. just fill bandwidth :)
}
r.written = r.written + bufSize
if r.written >= r.toRead {
return bufSize, io.EOF
}
return bufSize, nil
}