forked from kataras/iris
/
httptest.go
142 lines (124 loc) 路 3.68 KB
/
httptest.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
package httptest
import (
"crypto/tls"
"net/http"
"testing"
"github.com/iris-contrib/httpexpect"
"github.com/kataras/iris"
)
type (
// OptionSetter sets a configuration field to the configuration
OptionSetter interface {
// Set receives a pointer to the Configuration type and does the job of filling it
Set(c *Configuration)
}
// OptionSet implements the OptionSetter
OptionSet func(c *Configuration)
)
// Set is the func which makes the OptionSet an OptionSetter, this is used mostly
func (o OptionSet) Set(c *Configuration) {
o(c)
}
// Configuration httptest configuration
type Configuration struct {
// URL the base url.
// Defaults to empty string "".
URL string
// Debug if true then debug messages from the httpexpect will be shown when a test runs
// Defaults to false.
Debug bool
// LogLevel sets the application's log level.
// Defaults to "disable" when testing.
LogLevel string
}
// Set implements the OptionSetter for the Configuration itself
func (c Configuration) Set(main *Configuration) {
main.URL = c.URL
main.Debug = c.Debug
if c.LogLevel != "" {
main.LogLevel = c.LogLevel
}
}
var (
// URL if setted then it sets the httptest's BaseURL.
// Defaults to empty string "".
URL = func(schemeAndHost string) OptionSet {
return func(c *Configuration) {
c.URL = schemeAndHost
}
}
// Debug if true then debug messages from the httpexpect will be shown when a test runs
// Defaults to false.
Debug = func(val bool) OptionSet {
return func(c *Configuration) {
c.Debug = val
}
}
// LogLevel sets the application's log level "val".
// Defaults to disabled when testing.
LogLevel = func(val string) OptionSet {
return func(c *Configuration) {
c.LogLevel = val
}
}
)
// DefaultConfiguration returns the default configuration for the httptest.
func DefaultConfiguration() *Configuration {
return &Configuration{URL: "", Debug: false, LogLevel: "disable"}
}
// New Prepares and returns a new test framework based on the "app".
// You can find example on the https://github.com/kataras/iris/tree/master/_examples/testing/httptest
func New(t *testing.T, app *iris.Application, setters ...OptionSetter) *httpexpect.Expect {
conf := DefaultConfiguration()
for _, setter := range setters {
setter.Set(conf)
}
// set the logger or disable it (default) and disable the updater (for any case).
app.Configure(iris.WithoutVersionChecker)
app.Logger().SetLevel(conf.LogLevel)
if err := app.Build(); err != nil {
if conf.Debug && (conf.LogLevel == "disable" || conf.LogLevel == "disabled") {
app.Logger().Println(err.Error())
return nil
}
}
testConfiguration := httpexpect.Config{
BaseURL: conf.URL,
Client: &http.Client{
Transport: httpexpect.NewBinder(app),
Jar: httpexpect.NewJar(),
},
Reporter: httpexpect.NewAssertReporter(t),
}
if conf.Debug {
testConfiguration.Printers = []httpexpect.Printer{
httpexpect.NewDebugPrinter(t, true),
}
}
return httpexpect.WithConfig(testConfiguration)
}
// NewInsecure same as New but receives a single host instead of the whole framework.
// Useful for testing running TLS servers.
func NewInsecure(t *testing.T, setters ...OptionSetter) *httpexpect.Expect {
conf := DefaultConfiguration()
for _, setter := range setters {
setter.Set(conf)
}
transport := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
testConfiguration := httpexpect.Config{
BaseURL: conf.URL,
Client: &http.Client{
Transport: transport,
Jar: httpexpect.NewJar(),
},
Reporter: httpexpect.NewAssertReporter(t),
}
if conf.Debug {
testConfiguration.Printers = []httpexpect.Printer{
httpexpect.NewDebugPrinter(t, true),
}
}
return httpexpect.WithConfig(testConfiguration)
}