forked from durmaze/gobank
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package predicates | ||
|
||
import "encoding/json" | ||
|
||
type startsWith struct { | ||
req Request | ||
} | ||
|
||
func (p startsWith) Type() string { | ||
return "startsWith" | ||
} | ||
|
||
func (p startsWith) MarshalJSON() ([]byte, error) { | ||
requestBytes, _ := json.Marshal(p.req) | ||
|
||
requestJson := string(requestBytes) | ||
startsWithJson := " { \"startsWith\" : " + requestJson + "}" | ||
|
||
return []byte(startsWithJson), nil | ||
} | ||
|
||
type StartsWithBuilder struct { | ||
startsWith startsWith | ||
} | ||
|
||
func StartsWith() *StartsWithBuilder { | ||
return &StartsWithBuilder{startsWith: startsWith{req: Request{}}} | ||
} | ||
|
||
func (builder *StartsWithBuilder) Path(path string) *StartsWithBuilder { | ||
builder.startsWith.req.Path = path | ||
return builder | ||
} | ||
|
||
func (builder *StartsWithBuilder) Method(method string) *StartsWithBuilder { | ||
builder.startsWith.req.Method = method | ||
return builder | ||
} | ||
|
||
func (builder *StartsWithBuilder) Header(header string, value string) *StartsWithBuilder { | ||
if builder.startsWith.req.Headers == nil { | ||
builder.startsWith.req.Headers = map[string]string{} | ||
} | ||
builder.startsWith.req.Headers[header] = value | ||
return builder | ||
} | ||
|
||
func (builder *StartsWithBuilder) Query(param string, value string) *StartsWithBuilder { | ||
if builder.startsWith.req.QueryParams == nil { | ||
builder.startsWith.req.QueryParams = map[string]string{} | ||
} | ||
|
||
builder.startsWith.req.QueryParams[param] = value | ||
return builder | ||
} | ||
|
||
func (builder *StartsWithBuilder) Body(body string) *StartsWithBuilder { | ||
builder.startsWith.req.Body = body | ||
return builder | ||
} | ||
|
||
func (builder *StartsWithBuilder) Build() startsWith { | ||
return builder.startsWith | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package predicates_test | ||
|
||
import ( | ||
"encoding/json" | ||
"sync" | ||
|
||
"github.com/durmaze/gobank/predicates" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("StartsWith Predicate Builder Tests", func() { | ||
|
||
Describe("When building a Predicate of type \"StartsWith\"", func() { | ||
|
||
var ( | ||
actualPredicateAsMap map[string]interface{} | ||
|
||
expectedPath = "/testing/" | ||
|
||
once sync.Once | ||
) | ||
|
||
BeforeEach(func() { | ||
once.Do(func() { | ||
actualPredicate := predicates.StartsWith().Path(expectedPath).Build() | ||
|
||
jsonBytes, _ := json.Marshal(actualPredicate) | ||
actualPredicateAsMap = map[string]interface{}{} | ||
json.Unmarshal(jsonBytes, &actualPredicateAsMap) | ||
}) | ||
}) | ||
|
||
It("should create a \"StartsWith\" Predicate", func() { | ||
Expect(actualPredicateAsMap).To(HaveKey("startsWith")) | ||
}) | ||
|
||
It("should create a Predicate with the correct Path", func() { | ||
startsWithPredicate := actualPredicateAsMap["startsWith"] | ||
|
||
Expect(startsWithPredicate).To(HaveKeyWithValue("path", expectedPath)) | ||
}) | ||
|
||
}) | ||
|
||
}) |