/
testserver.go
58 lines (50 loc) · 1.2 KB
/
testserver.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
package quotetesting
import (
"fmt"
"net/http"
"net/http/httptest"
"strconv"
"time"
)
// NewTestServer create a new httptest server that returns a response
// build on the request parameters.
// The response in an html page with a table that prints
// kew/value pairs of query parameters.
// Special parameters:
// delay: number of msec to sleep before returning the response
// code: returned http status
func NewTestServer() *httptest.Server {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query()
// code
code, _ := strconv.Atoi(query.Get("code"))
if code == 0 {
code = http.StatusOK
}
// delay
delaymsec, _ := strconv.Atoi(query.Get("delay"))
if delaymsec > 0 {
time.Sleep(time.Duration(delaymsec) * time.Millisecond)
}
if code != http.StatusOK {
// set the status code
http.Error(w, http.StatusText(code), code)
return
}
fmt.Fprint(w, `<html>
<head>
<title>Test Server Result</title>
</head>
<body>
<h1>Test Server Result</h1>
<table>
`)
for k, v := range query {
fmt.Fprintf(w, "<tr><th>%s</th><td>%s</td></tr>\n", k, v[0])
}
fmt.Fprint(w, `</table>
</body>
</html>`)
}))
return server
}