Skip to content

Commit 2e2ddee

Browse files
authored
Merge pull request #4 from chipim/feat/verify-stub-times-called
Merge: add new endpoint to verify number of calls for stub
2 parents 2d242b4 + 8903ade commit 2e2ddee

File tree

3 files changed

+71
-3
lines changed

3 files changed

+71
-3
lines changed

stub/storage.go

+37-3
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ type matchFunc func(interface{}, interface{}) bool
2222
var stubStorage = stubMapping{}
2323

2424
type storage struct {
25-
Input Input
26-
Output Output
25+
Input Input
26+
Output Output
27+
timesCalled int
2728
}
2829

2930
func storeStub(stub *Stub) error {
@@ -73,24 +74,27 @@ func findStub(stub *findStubPayload) (*Output, error) {
7374
}
7475

7576
closestMatch := []closeMatch{}
76-
for _, stubrange := range stubs {
77+
for idx, stubrange := range stubs {
7778
if expect := stubrange.Input.Equals; expect != nil {
7879
closestMatch = append(closestMatch, closeMatch{"equals", expect})
7980
if equals(stub.Data, expect) {
81+
stubs[idx].timesCalled++
8082
return &stubrange.Output, nil
8183
}
8284
}
8385

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

9194
if expect := stubrange.Input.Matches; expect != nil {
9295
closestMatch = append(closestMatch, closeMatch{"matches", expect})
9396
if matches(stubrange.Input.Matches, stub.Data) {
97+
stubs[idx].timesCalled++
9498
return &stubrange.Output, nil
9599
}
96100
}
@@ -318,3 +322,33 @@ func (sm *stubMapping) readStubFromFile(path string) {
318322
sm.storeStub(stub)
319323
}
320324
}
325+
326+
func getStubTimesCalled(stub *verifyStubCallPayload) (*Output, error) {
327+
mx.Lock()
328+
defer mx.Unlock()
329+
330+
if _, ok := stubStorage[stub.Service]; !ok {
331+
return nil, fmt.Errorf("Can't find stub for Service: %s", stub.Service)
332+
}
333+
334+
if _, ok := stubStorage[stub.Service][stub.Method]; !ok {
335+
return nil, fmt.Errorf("Can't find stub for Service:%s and Method:%s", stub.Service, stub.Method)
336+
}
337+
338+
stubs := stubStorage[stub.Service][stub.Method]
339+
if len(stubs) == 0 {
340+
return nil, fmt.Errorf("Stub for Service:%s and Method:%s is empty", stub.Service, stub.Method)
341+
}
342+
343+
totalTimesCalled := 0
344+
345+
for _, stubrange := range stubs {
346+
totalTimesCalled += stubrange.timesCalled
347+
}
348+
349+
return &Output{
350+
Data: map[string]interface{}{
351+
"timesCalled": totalTimesCalled,
352+
},
353+
}, nil
354+
}

stub/stub.go

+25
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ func RunStubServer(opt Options) {
3030
r.Get("/", listStub)
3131
r.Post("/find", handleFindStub)
3232
r.Get("/clear", handleClearStub)
33+
r.Get("/verify", handleVerifyStubCalled)
3334

3435
if opt.StubPath != "" {
3536
readStubFromFile(opt.StubPath)
@@ -165,3 +166,27 @@ func handleClearStub(w http.ResponseWriter, r *http.Request) {
165166
clearStorage()
166167
w.Write([]byte("OK"))
167168
}
169+
170+
type verifyStubCallPayload struct {
171+
Service string `json:"service"`
172+
Method string `json:"method"`
173+
}
174+
175+
func handleVerifyStubCalled(w http.ResponseWriter, r *http.Request) {
176+
verifyPayload := new(verifyStubCallPayload)
177+
err := json.NewDecoder(r.Body).Decode(verifyPayload)
178+
if err != nil {
179+
responseError(err, w)
180+
return
181+
}
182+
183+
output, err := getStubTimesCalled(verifyPayload)
184+
if err != nil {
185+
log.Println(err)
186+
responseError(err, w)
187+
return
188+
}
189+
190+
w.Header().Set("Content-Type", "application/json")
191+
json.NewEncoder(w).Encode(output)
192+
}

stub/stub_test.go

+9
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,15 @@ func TestStub(t *testing.T) {
397397
handler: handleFindStub,
398398
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}",
399399
},
400+
{
401+
name: "verify stub times called",
402+
mock: func() *http.Request {
403+
payload := `{"service":"Testing2","method":"TestMethod"}`
404+
return httptest.NewRequest("GET", "/verify", bytes.NewReader([]byte(payload)))
405+
},
406+
handler: handleVerifyStubCalled,
407+
expect: "{\"data\":{\"timesCalled\":1},\"error\":\"\"}\n",
408+
},
400409
}
401410

402411
for _, v := range cases {

0 commit comments

Comments
 (0)