Skip to content

Commit 5e1a33c

Browse files
Add support to formParameters (#29)
1 parent 4d763fe commit 5e1a33c

File tree

4 files changed

+29
-0
lines changed

4 files changed

+29
-0
lines changed

client_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ func TestStubRule_ToJson(t *testing.T) {
3737
WithQueryParam("id", Contains("1").And(NotContains("2"))).
3838
WithBodyPattern(EqualToJson(`{"meta": "information"}`, IgnoreArrayOrder, IgnoreExtraElements)).
3939
WithBodyPattern(Contains("information")).
40+
WithFormParameter("form1", EqualTo("value1")).
41+
WithFormParameter("form2", Matching("value2")).
4042
WithMultipartPattern(
4143
NewMultipartPattern().
4244
WithName("info").

request.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ type Request struct {
1515
queryParams map[string]MatcherInterface
1616
cookies map[string]BasicParamMatcher
1717
bodyPatterns []BasicParamMatcher
18+
formParameters map[string]BasicParamMatcher
1819
multipartPatterns []MultipartPatternInterface
1920
basicAuthCredentials *struct {
2021
username string
@@ -66,6 +67,15 @@ func (r *Request) WithBodyPattern(matcher BasicParamMatcher) *Request {
6667
return r
6768
}
6869

70+
// WithFormParameter adds form parameter to list
71+
func (r *Request) WithFormParameter(name string, matcher BasicParamMatcher) *Request {
72+
if r.formParameters == nil {
73+
r.formParameters = make(map[string]BasicParamMatcher, 1)
74+
}
75+
r.formParameters[name] = matcher
76+
return r
77+
}
78+
6979
// WithMultipartPattern adds multipart pattern to list
7080
func (r *Request) WithMultipartPattern(pattern *MultipartPattern) *Request {
7181
r.multipartPatterns = append(r.multipartPatterns, pattern)
@@ -136,6 +146,9 @@ func (r *Request) MarshalJSON() ([]byte, error) {
136146
if len(r.bodyPatterns) > 0 {
137147
request["bodyPatterns"] = r.bodyPatterns
138148
}
149+
if len(r.formParameters) > 0 {
150+
request["formParameters"] = r.formParameters
151+
}
139152
if len(r.multipartPatterns) > 0 {
140153
request["multipartPatterns"] = r.multipartPatterns
141154
}

stub_rule.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ func (s *StubRule) WithBodyPattern(matcher BasicParamMatcher) *StubRule {
8181
return s
8282
}
8383

84+
// WithFormParameter adds form parameter and returns *StubRule
85+
func (s *StubRule) WithFormParameter(param string, matcher BasicParamMatcher) *StubRule {
86+
s.request.WithFormParameter(param, matcher)
87+
return s
88+
}
89+
8490
// WithMultipartPattern adds multipart body pattern and returns *StubRule
8591
func (s *StubRule) WithMultipartPattern(pattern *MultipartPattern) *StubRule {
8692
s.request.WithMultipartPattern(pattern)

testdata/expected-template-scenario.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@
2626
"contains": "information"
2727
}
2828
],
29+
"formParameters": {
30+
"form1": {
31+
"equalTo": "value1"
32+
},
33+
"form2": {
34+
"matches": "value2"
35+
}
36+
},
2937
"multipartPatterns": [
3038
{
3139
"matchingType": "ANY",

0 commit comments

Comments
 (0)