5
5
"net/http"
6
6
)
7
7
8
+ const ScenarioStateStarted = "Started"
9
+
8
10
// ParamMatcherInterface is pair ParamMatchingStrategy and string matched value
9
11
type ParamMatcherInterface interface {
10
12
Strategy () ParamMatchingStrategy
@@ -34,17 +36,31 @@ type response struct {
34
36
35
37
// StubRule is struct of http request body to WireMock
36
38
type StubRule struct {
37
- request request
38
- response response
39
+ request request
40
+ response response
41
+ priority * int64
42
+ scenarioName * string
43
+ requiredScenarioState * string
44
+ newScenarioState * string
45
+ }
46
+
47
+ // NewStubRule returns a new *StubRule.
48
+ func NewStubRule (method string , urlMatcher URLMatcher ) * StubRule {
49
+ return & StubRule {
50
+ request : request {
51
+ urlMatcher : urlMatcher ,
52
+ method : method ,
53
+ },
54
+ response : response {
55
+ status : http .StatusOK ,
56
+ },
57
+ }
39
58
}
40
59
41
60
// WithQueryParam adds query param and returns *StubRule
42
61
func (s * StubRule ) WithQueryParam (param string , matcher ParamMatcherInterface ) * StubRule {
43
62
if s .request .queryParams == nil {
44
- s .request .queryParams = map [string ]ParamMatcherInterface {
45
- param : matcher ,
46
- }
47
- return s
63
+ s .request .queryParams = map [string ]ParamMatcherInterface {}
48
64
}
49
65
50
66
s .request .queryParams [param ] = matcher
@@ -54,10 +70,7 @@ func (s *StubRule) WithQueryParam(param string, matcher ParamMatcherInterface) *
54
70
// WithHeader adds header to Headers and returns *StubRule
55
71
func (s * StubRule ) WithHeader (header string , matcher ParamMatcherInterface ) * StubRule {
56
72
if s .request .headers == nil {
57
- s .request .headers = map [string ]ParamMatcherInterface {
58
- header : matcher ,
59
- }
60
- return s
73
+ s .request .headers = map [string ]ParamMatcherInterface {}
61
74
}
62
75
63
76
s .request .headers [header ] = matcher
@@ -67,10 +80,7 @@ func (s *StubRule) WithHeader(header string, matcher ParamMatcherInterface) *Stu
67
80
// WithCookie adds cookie and returns *StubRule
68
81
func (s * StubRule ) WithCookie (cookie string , matcher ParamMatcherInterface ) * StubRule {
69
82
if s .request .cookies == nil {
70
- s .request .cookies = map [string ]ParamMatcherInterface {
71
- cookie : matcher ,
72
- }
73
- return s
83
+ s .request .cookies = map [string ]ParamMatcherInterface {}
74
84
}
75
85
76
86
s .request .cookies [cookie ] = matcher
@@ -91,17 +101,28 @@ func (s *StubRule) WillReturn(body string, headers map[string]string, status int
91
101
return s
92
102
}
93
103
94
- // NewStubRule returns a new *StubRule.
95
- func NewStubRule (method string , urlMatcher URLMatcher ) * StubRule {
96
- return & StubRule {
97
- request : request {
98
- urlMatcher : urlMatcher ,
99
- method : method ,
100
- },
101
- response : response {
102
- status : http .StatusOK ,
103
- },
104
- }
104
+ // AtPriority sets priority and returns *StubRule
105
+ func (s * StubRule ) AtPriority (priority int64 ) * StubRule {
106
+ s .priority = & priority
107
+ return s
108
+ }
109
+
110
+ // InScenario sets scenarioName and returns *StubRule
111
+ func (s * StubRule ) InScenario (scenarioName string ) * StubRule {
112
+ s .scenarioName = & scenarioName
113
+ return s
114
+ }
115
+
116
+ // WhenScenarioStateIs sets requiredScenarioState and returns *StubRule
117
+ func (s * StubRule ) WhenScenarioStateIs (scenarioState string ) * StubRule {
118
+ s .requiredScenarioState = & scenarioState
119
+ return s
120
+ }
121
+
122
+ // WillSetStateTo sets newScenarioState and returns *StubRule
123
+ func (s * StubRule ) WillSetStateTo (scenarioState string ) * StubRule {
124
+ s .newScenarioState = & scenarioState
125
+ return s
105
126
}
106
127
107
128
// Post returns *StubRule for POST method.
@@ -124,15 +145,24 @@ func Put(urlMatchingPair URLMatcher) *StubRule {
124
145
return NewStubRule (http .MethodPut , urlMatchingPair )
125
146
}
126
147
148
+ //MarshalJSON makes json body for http request
127
149
func (s * StubRule ) MarshalJSON () ([]byte , error ) {
128
150
jsonStubRule := struct {
129
- Request map [string ]interface {} `json:"request"`
130
- Response struct {
151
+ Priority * int64 `json:"priority,omitempty"`
152
+ ScenarioName * string `json:"scenarioName,omitempty"`
153
+ RequiredScenarioScenarioState * string `json:"requiredScenarioState,omitempty"`
154
+ NewScenarioState * string `json:"newScenarioState,omitempty"`
155
+ Request map [string ]interface {} `json:"request"`
156
+ Response struct {
131
157
Body string `json:"body,omitempty"`
132
158
Headers map [string ]string `json:"headers,omitempty"`
133
159
Status int64 `json:"status,omitempty"`
134
160
} `json:"response"`
135
161
}{}
162
+ jsonStubRule .Priority = s .priority
163
+ jsonStubRule .ScenarioName = s .scenarioName
164
+ jsonStubRule .RequiredScenarioScenarioState = s .requiredScenarioState
165
+ jsonStubRule .NewScenarioState = s .newScenarioState
136
166
jsonStubRule .Response .Body = s .response .body
137
167
jsonStubRule .Response .Headers = s .response .headers
138
168
jsonStubRule .Response .Status = s .response .status
0 commit comments