-
Notifications
You must be signed in to change notification settings - Fork 18
/
builder.go
147 lines (118 loc) · 3.35 KB
/
builder.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
143
144
145
146
147
package cute
import (
"net/http"
"time"
)
const defaultHTTPTimeout = 30
var (
errorAssertIsNil = "assert must be not nil"
)
// HTTPTestMaker is a creator tests
type HTTPTestMaker struct {
httpClient *http.Client
middleware *Middleware
jsonMarshaler JSONMarshaler
}
// NewHTTPTestMaker is function for set options for all cute.
// For example, you can set timeout for all requests or set custom http client
// Options:
// - WithCustomHTTPTimeout - set timeout for all requests
// - WithHTTPClient - set custom http client
// - WithCustomHTTPRoundTripper - set custom http round tripper
// - WithJSONMarshaler - set custom json marshaler
// - WithMiddlewareAfter - set function which will run AFTER test execution
// - WithMiddlewareAfterT - set function which will run AFTER test execution with TB
// - WithMiddlewareBefore - set function which will run BEFORE test execution
// - WithMiddlewareBeforeT - set function which will run BEFORE test execution with TB
func NewHTTPTestMaker(opts ...Option) *HTTPTestMaker {
var (
o = &options{
middleware: new(Middleware),
}
timeout = defaultHTTPTimeout * time.Second
roundTripper = http.DefaultTransport
jsMarshaler JSONMarshaler = &jsonMarshaler{}
)
for _, opt := range opts {
opt(o)
}
if o.httpTimeout != 0 {
timeout = o.httpTimeout
}
if o.httpRoundTripper != nil { //nolint
roundTripper = o.httpRoundTripper
}
httpClient := &http.Client{
Transport: roundTripper,
Timeout: timeout,
}
if o.httpClient != nil {
httpClient = o.httpClient
}
if o.jsonMarshaler != nil {
jsMarshaler = o.jsonMarshaler
}
m := &HTTPTestMaker{
httpClient: httpClient,
jsonMarshaler: jsMarshaler,
middleware: o.middleware,
}
return m
}
// NewTestBuilder is a function for initialization foundation for cute
func (m *HTTPTestMaker) NewTestBuilder() AllureBuilder {
tests := createDefaultTests(m)
return &cute{
baseProps: m,
countTests: 0,
tests: tests,
allureInfo: new(allureInformation),
allureLinks: new(allureLinks),
allureLabels: new(allureLabels),
parallel: false,
}
}
func createDefaultTests(m *HTTPTestMaker) []*Test {
tests := make([]*Test, 1)
tests[0] = createDefaultTest(m)
return tests
}
func createDefaultTest(m *HTTPTestMaker) *Test {
return &Test{
httpClient: m.httpClient,
jsonMarshaler: m.jsonMarshaler,
Middleware: createMiddlewareFromTemplate(m.middleware),
AllureStep: new(AllureStep),
Request: &Request{
Retry: new(RequestRetryPolitic),
},
Expect: &Expect{JSONSchema: new(ExpectJSONSchema)},
}
}
func createMiddlewareFromTemplate(m *Middleware) *Middleware {
after := make([]AfterExecute, 0, len(m.After))
after = append(after, m.After...)
afterT := make([]AfterExecuteT, 0, len(m.AfterT))
afterT = append(afterT, m.AfterT...)
before := make([]BeforeExecute, 0, len(m.Before))
before = append(before, m.Before...)
beforeT := make([]BeforeExecuteT, 0, len(m.BeforeT))
beforeT = append(beforeT, m.BeforeT...)
middleware := &Middleware{
After: after,
AfterT: afterT,
Before: before,
BeforeT: beforeT,
}
return middleware
}
func (qt *cute) Create() MiddlewareRequest {
return qt
}
func (qt *cute) CreateStep(name string) MiddlewareRequest {
qt.tests[qt.countTests].AllureStep.Name = name
return qt
}
func (qt *cute) CreateRequest() RequestHTTPBuilder {
return qt
}