Skip to content

feat: add new endpoint to verify number of calls for stub #135

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 37 additions & 3 deletions stub/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ type matchFunc func(interface{}, interface{}) bool
var stubStorage = stubMapping{}

type storage struct {
Input Input
Output Output
Input Input
Output Output
timesCalled int
}

func storeStub(stub *Stub) error {
Expand Down Expand Up @@ -73,24 +74,27 @@ func findStub(stub *findStubPayload) (*Output, error) {
}

closestMatch := []closeMatch{}
for _, stubrange := range stubs {
for idx, stubrange := range stubs {
if expect := stubrange.Input.Equals; expect != nil {
closestMatch = append(closestMatch, closeMatch{"equals", expect})
if equals(stub.Data, expect) {
stubs[idx].timesCalled++
return &stubrange.Output, nil
}
}

if expect := stubrange.Input.Contains; expect != nil {
closestMatch = append(closestMatch, closeMatch{"contains", expect})
if contains(stubrange.Input.Contains, stub.Data) {
stubs[idx].timesCalled++
return &stubrange.Output, nil
}
}

if expect := stubrange.Input.Matches; expect != nil {
closestMatch = append(closestMatch, closeMatch{"matches", expect})
if matches(stubrange.Input.Matches, stub.Data) {
stubs[idx].timesCalled++
return &stubrange.Output, nil
}
}
Expand Down Expand Up @@ -318,3 +322,33 @@ func (sm *stubMapping) readStubFromFile(path string) {
sm.storeStub(stub)
}
}

func getStubTimesCalled(stub *verifyStubCallPayload) (*Output, error) {
mx.Lock()
defer mx.Unlock()

if _, ok := stubStorage[stub.Service]; !ok {
return nil, fmt.Errorf("Can't find stub for Service: %s", stub.Service)
}

if _, ok := stubStorage[stub.Service][stub.Method]; !ok {
return nil, fmt.Errorf("Can't find stub for Service:%s and Method:%s", stub.Service, stub.Method)
}

stubs := stubStorage[stub.Service][stub.Method]
if len(stubs) == 0 {
return nil, fmt.Errorf("Stub for Service:%s and Method:%s is empty", stub.Service, stub.Method)
}

totalTimesCalled := 0

for _, stubrange := range stubs {
totalTimesCalled += stubrange.timesCalled
}

return &Output{
Data: map[string]interface{}{
"timesCalled": totalTimesCalled,
},
}, nil
}
33 changes: 29 additions & 4 deletions stub/stub.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"log"
"net/http"
"strings"

"github.com/go-chi/chi"
)

Expand All @@ -29,6 +29,7 @@ func RunStubServer(opt Options) {
r.Get("/", listStub)
r.Post("/find", handleFindStub)
r.Get("/clear", handleClearStub)
r.Get("/verify", handleVerifyStubCalled)

if opt.StubPath != "" {
readStubFromFile(opt.StubPath)
Expand Down Expand Up @@ -106,7 +107,7 @@ func validateStub(stub *Stub) error {
if stub.Method == "" {
return fmt.Errorf("Method name can't be emtpy")
}

// due to golang implementation
// method name must capital
stub.Method = strings.Title(stub.Method)
Expand Down Expand Up @@ -143,11 +144,11 @@ func handleFindStub(w http.ResponseWriter, r *http.Request) {
responseError(err, w)
return
}

// due to golang implementation
// method name must capital
stub.Method = strings.Title(stub.Method)

output, err := findStub(stub)
if err != nil {
log.Println(err)
Expand All @@ -163,3 +164,27 @@ func handleClearStub(w http.ResponseWriter, r *http.Request) {
clearStorage()
w.Write([]byte("OK"))
}

type verifyStubCallPayload struct {
Service string `json:"service"`
Method string `json:"method"`
}

func handleVerifyStubCalled(w http.ResponseWriter, r *http.Request) {
verifyPayload := new(verifyStubCallPayload)
err := json.NewDecoder(r.Body).Decode(verifyPayload)
if err != nil {
responseError(err, w)
return
}

output, err := getStubTimesCalled(verifyPayload)
if err != nil {
log.Println(err)
responseError(err, w)
return
}

w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(output)
}
9 changes: 9 additions & 0 deletions stub/stub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,15 @@ func TestStub(t *testing.T) {
handler: handleFindStub,
expect: "Can't find stub \n\nService: Testing \n\nMethod: TestMethod \n\nInput\n\n{\n\tHola: Dunia\n}\n\nClosest Match \n\nequals:{\n\tHola: Mundo\n}",
},
{
name: "verify stub times called",
mock: func() *http.Request {
payload := `{"service":"Testing2","method":"TestMethod"}`
return httptest.NewRequest("GET", "/verify", bytes.NewReader([]byte(payload)))
},
handler: handleVerifyStubCalled,
expect: "{\"data\":{\"timesCalled\":1},\"error\":\"\"}\n",
},
}

for _, v := range cases {
Expand Down