/
backend_helpers.go
67 lines (57 loc) · 1.48 KB
/
backend_helpers.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
package integration
import (
"net/http"
"net/http/httptest"
"strconv"
"time"
// revive:disable:dot-imports
. "github.com/onsi/gomega"
// revive:enable:dot-imports
"github.com/onsi/gomega/ghttp"
)
func startSimpleBackend(identifier string) *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, err := w.Write([]byte(identifier))
Expect(err).NotTo(HaveOccurred())
}))
}
func startTarpitBackend(delays ...time.Duration) *httptest.Server {
responseDelay := 2 * time.Second
if len(delays) > 0 {
responseDelay = delays[0]
}
bodyDelay := 0 * time.Second
if len(delays) > 1 {
bodyDelay = delays[1]
}
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
body := "Tarpit\n"
if responseDelay > 0 {
time.Sleep(responseDelay)
}
w.Header().Add("Content-Length", strconv.Itoa(len(body)))
w.WriteHeader(http.StatusOK)
w.(http.Flusher).Flush()
if bodyDelay > 0 {
time.Sleep(bodyDelay)
}
_, err := w.Write([]byte(body))
Expect(err).NotTo(HaveOccurred())
}))
}
func startRecordingBackend() *ghttp.Server {
return startRecordingServer(false)
}
func startRecordingTLSBackend() *ghttp.Server {
return startRecordingServer(true)
}
func startRecordingServer(tls bool) (server *ghttp.Server) {
if tls {
server = ghttp.NewTLSServer()
} else {
server = ghttp.NewServer()
}
server.AllowUnhandledRequests = true
server.UnhandledRequestStatusCode = http.StatusOK
return server
}