Skip to content

Commit

Permalink
Add find Request by path and method
Browse files Browse the repository at this point in the history
  • Loading branch information
regiluze committed Sep 1, 2018
1 parent be28cc6 commit 9261f95
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 13 deletions.
12 changes: 11 additions & 1 deletion service.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ func NewRequest(path string, method string, data map[string]string) Request {
return request
}

type compationFunc func(Request) bool

type Request struct {
Path string
Method string
Expand All @@ -33,9 +35,17 @@ func (requestHub *RequestHub) Reset() {
}

func (requestHub RequestHub) Find_by_path(path string) []Request {
return requestHub.find_by(func(request Request) bool { return request.Path == path })
}

func (requestHub RequestHub) Find_by_path_and_method(path string, method string) []Request {
return requestHub.find_by(func(request Request) bool { return request.Path == path && request.Method == method })
}

func (requestHub RequestHub) find_by(compFunc compationFunc) []Request {
matchRequests := requestHub.Requests[:0]
for _, request := range requestHub.Requests {
if request.Path == path {
if compFunc(request) {
matchRequests = append(matchRequests, request)
}
}
Expand Down
45 changes: 33 additions & 12 deletions service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,41 @@ var _ = Describe("Third party API double service specs", func() {
Expect(requestHub.Requests).To(BeEmpty())
})
})
Context("when finding the request by path", func() {
It("returns a slice of match requests", func() {
data := map[string]string{
"data": "foo",
}
request1 := NewRequest("path1", "method", data)
request2 := NewRequest("path2", "method", data)
requestHub.Requests = append(requestHub.Requests, request1)
requestHub.Requests = append(requestHub.Requests, request2)
Context("when finding the request", func() {
Context("by path", func() {
It("returns a slice of requests with specific path", func() {
data := map[string]string{
"data": "foo",
}
request1 := NewRequest("path1", "method", data)
request2 := NewRequest("path2", "method", data)
requestHub.Requests = append(requestHub.Requests, request1)
requestHub.Requests = append(requestHub.Requests, request2)

requests := requestHub.Find_by_path("path1")

Expect(requests).NotTo(BeEmpty())
Expect(requests[0].Path).To(Equal("path1"))
})
})
Context("by path and method", func() {
It("returns a slice of requests with specific path and method", func() {
data := map[string]string{
"data": "foo",
}
request1 := NewRequest("path1", "method", data)
request2 := NewRequest("path2", "method1", data)
request3 := NewRequest("path2", "method2", data)
requestHub.Requests = append(requestHub.Requests, request1)
requestHub.Requests = append(requestHub.Requests, request2)
requestHub.Requests = append(requestHub.Requests, request3)

requests := requestHub.Find_by_path("path1")
requests := requestHub.Find_by_path_and_method("path2", "method2")

Expect(requests).NotTo(BeEmpty())
Expect(requests[0].Path).To(Equal("path1"))
Expect(requests).NotTo(BeEmpty())
Expect(requests[0].Path).To(Equal("path2"))
Expect(requests[0].Method).To(Equal("method2"))
})
})
})
})
Expand Down

0 comments on commit 9261f95

Please sign in to comment.