-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfixtures.go
64 lines (54 loc) · 1.38 KB
/
fixtures.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
// Use and distribution licensed under the Apache license version 2.
//
// See the COPYING file in the root project directory for full text.
package http
import (
nethttp "net/http"
"net/http/httptest"
"strings"
gdttypes "github.com/gdt-dev/gdt/types"
)
const (
StateKeyBaseURL = "http.base_url"
StateKeyClient = "http.client"
)
type httpServerFixture struct {
handler nethttp.Handler
server *httptest.Server
useTLS bool
}
func (f *httpServerFixture) Start() {
if !f.useTLS {
f.server = httptest.NewServer(f.handler)
} else {
f.server = httptest.NewTLSServer(f.handler)
}
}
func (f *httpServerFixture) Stop() {
f.server.Close()
}
func (f *httpServerFixture) HasState(key string) bool {
lkey := strings.ToLower(key)
switch lkey {
case StateKeyBaseURL, StateKeyClient:
return true
}
return false
}
func (f *httpServerFixture) State(key string) interface{} {
key = strings.ToLower(key)
switch key {
case StateKeyBaseURL:
return f.server.URL
case StateKeyClient:
return f.server.Client()
}
return ""
}
// NewServerFixture returns a fixture that will start and stop a supplied
// http.Handler. The returned fixture exposes an "http.base_url" state key that
// test cases of type "http" examine to determine the base URL the tests should
// hit
func NewServerFixture(h nethttp.Handler, useTLS bool) gdttypes.Fixture {
return &httpServerFixture{handler: h, useTLS: useTLS}
}