Skip to content

Commit

Permalink
adds startsWith predicate support
Browse files Browse the repository at this point in the history
  • Loading branch information
durmaze committed Feb 6, 2016
1 parent 8c22cbb commit ea3131e
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 0 deletions.
64 changes: 64 additions & 0 deletions predicates/startsWith.go
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
}
47 changes: 47 additions & 0 deletions predicates/startsWith_test.go
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))
})

})

})

0 comments on commit ea3131e

Please sign in to comment.