Skip to content

Commit

Permalink
Add support to formParameters (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
jamillosantos authored Mar 26, 2024
1 parent 4d763fe commit 5e1a33c
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 0 deletions.
2 changes: 2 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ func TestStubRule_ToJson(t *testing.T) {
WithQueryParam("id", Contains("1").And(NotContains("2"))).
WithBodyPattern(EqualToJson(`{"meta": "information"}`, IgnoreArrayOrder, IgnoreExtraElements)).
WithBodyPattern(Contains("information")).
WithFormParameter("form1", EqualTo("value1")).
WithFormParameter("form2", Matching("value2")).
WithMultipartPattern(
NewMultipartPattern().
WithName("info").
Expand Down
13 changes: 13 additions & 0 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type Request struct {
queryParams map[string]MatcherInterface
cookies map[string]BasicParamMatcher
bodyPatterns []BasicParamMatcher
formParameters map[string]BasicParamMatcher
multipartPatterns []MultipartPatternInterface
basicAuthCredentials *struct {
username string
Expand Down Expand Up @@ -66,6 +67,15 @@ func (r *Request) WithBodyPattern(matcher BasicParamMatcher) *Request {
return r
}

// WithFormParameter adds form parameter to list
func (r *Request) WithFormParameter(name string, matcher BasicParamMatcher) *Request {
if r.formParameters == nil {
r.formParameters = make(map[string]BasicParamMatcher, 1)
}
r.formParameters[name] = matcher
return r
}

// WithMultipartPattern adds multipart pattern to list
func (r *Request) WithMultipartPattern(pattern *MultipartPattern) *Request {
r.multipartPatterns = append(r.multipartPatterns, pattern)
Expand Down Expand Up @@ -136,6 +146,9 @@ func (r *Request) MarshalJSON() ([]byte, error) {
if len(r.bodyPatterns) > 0 {
request["bodyPatterns"] = r.bodyPatterns
}
if len(r.formParameters) > 0 {
request["formParameters"] = r.formParameters
}
if len(r.multipartPatterns) > 0 {
request["multipartPatterns"] = r.multipartPatterns
}
Expand Down
6 changes: 6 additions & 0 deletions stub_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ func (s *StubRule) WithBodyPattern(matcher BasicParamMatcher) *StubRule {
return s
}

// WithFormParameter adds form parameter and returns *StubRule
func (s *StubRule) WithFormParameter(param string, matcher BasicParamMatcher) *StubRule {
s.request.WithFormParameter(param, matcher)
return s
}

// WithMultipartPattern adds multipart body pattern and returns *StubRule
func (s *StubRule) WithMultipartPattern(pattern *MultipartPattern) *StubRule {
s.request.WithMultipartPattern(pattern)
Expand Down
8 changes: 8 additions & 0 deletions testdata/expected-template-scenario.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@
"contains": "information"
}
],
"formParameters": {
"form1": {
"equalTo": "value1"
},
"form2": {
"matches": "value2"
}
},
"multipartPatterns": [
{
"matchingType": "ANY",
Expand Down

0 comments on commit 5e1a33c

Please sign in to comment.