Skip to content

Commit

Permalink
Add logical not
Browse files Browse the repository at this point in the history
  • Loading branch information
walkerus committed Oct 20, 2024
1 parent 21343b1 commit 04be624
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 16 deletions.
27 changes: 11 additions & 16 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,7 @@ func TestStubRule_ToJson(t *testing.T) {
WithScheme("http").
WithPort(8080).
WithBearerToken(StartsWith("token")).
WillReturnResponse(
NewResponse().
WithStatus(http.StatusOK),
),
WillReturnResponse(OK()),
ExpectedFileName: "expected-template-bearer-auth-startsWith.json",
},
{
Expand All @@ -96,10 +93,7 @@ func TestStubRule_ToJson(t *testing.T) {
WithScheme("http").
WithPort(8080).
WithBearerToken(EqualTo("token")).
WillReturnResponse(
NewResponse().
WithStatus(http.StatusOK),
),
WillReturnResponse(OK()),
ExpectedFileName: "expected-template-bearer-auth-equalTo.json",
},
{
Expand All @@ -109,10 +103,7 @@ func TestStubRule_ToJson(t *testing.T) {
WithScheme("http").
WithPort(8080).
WithBearerToken(Contains("token")).
WillReturnResponse(
NewResponse().
WithStatus(http.StatusOK),
),
WillReturnResponse(OK()),
ExpectedFileName: "expected-template-bearer-auth-contains.json",
},
{
Expand All @@ -122,12 +113,16 @@ func TestStubRule_ToJson(t *testing.T) {
WithScheme("http").
WithPort(8080).
WithBearerToken(EqualTo("token123").And(StartsWith("token"))).
WillReturnResponse(
NewResponse().
WithStatus(http.StatusOK),
),
WillReturnResponse(OK()),
ExpectedFileName: "expected-template-bearer-auth-logicalMatcher.json",
},
{
Name: "NotLogicalMatcher",
StubRule: Post(URLPathEqualTo("/example")).
WithQueryParam("firstName", Not(EqualTo("John").Or(EqualTo("Jack")))).
WillReturnResponse(OK()),
ExpectedFileName: "not-logical-expression.json",
},
}

for _, tc := range testCases {
Expand Down
14 changes: 14 additions & 0 deletions logical_matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ func (m LogicalMatcher) MarshalJSON() ([]byte, error) {

// ParseMatcher returns the map representation of the structure.
func (m LogicalMatcher) ParseMatcher() map[string]interface{} {
if m.operator == "not" {
return map[string]interface{}{
m.operator: m.operands[0],
}
}

return map[string]interface{}{
m.operator: m.operands,
}
Expand Down Expand Up @@ -56,3 +62,11 @@ func And(matchers ...BasicParamMatcher) LogicalMatcher {
operands: matchers,
}
}

// Not returns a logical NOT of the given matcher. Required wiremock version >= 3.0.0
func Not(matcher BasicParamMatcher) LogicalMatcher {
return LogicalMatcher{
operator: "not",
operands: []BasicParamMatcher{matcher},
}
}
4 changes: 4 additions & 0 deletions response.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ func NewResponse() Response {
}
}

func OK() Response {
return NewResponse().WithStatus(http.StatusOK)
}

// WithLogNormalRandomDelay sets log normal random delay for response
func (r Response) WithLogNormalRandomDelay(median time.Duration, sigma float64) Response {
r.delayDistribution = NewLogNormalRandomDelay(median, sigma)
Expand Down
25 changes: 25 additions & 0 deletions testdata/not-logical-expression.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"uuid": "%s",
"id": "%s",
"request": {
"method": "POST",
"urlPath": "/example",
"queryParameters": {
"firstName": {
"not": {
"or": [
{
"equalTo": "John"
},
{
"equalTo": "Jack"
}
]
}
}
}
},
"response": {
"status": 200
}
}

0 comments on commit 04be624

Please sign in to comment.