Skip to content

Commit f97ae2a

Browse files
jkimbojkim
andauthored
Add method to test server to allow multiple responses from the same endpoint (#40)
Currently the test server only returns one response value from a mocked endpoint. This PR adds a new method `RegisterMethodResponse` that registers a list of responses that an endpoint will return when it is called. For example: ``` testServer := testphabserver.New() defer testServer.Close() testServer.RegisterCapabilities() // first PHID lookup testServer.RegisterMethodResponse("phid.lookup", http.StatusOK, m{ "result": m{ "PHID-USER-user2": m{ "type": "USER", "name": "id-user2", }, }, }) // second PHID lookup testServer.RegisterMethodResponse("phid.lookup", http.StatusOK, m{ "result": m{ "PHID-USER-user1": m{ "type": "USER", "name": "id-user1", }, }, }) Co-authored-by: jkim <jkim@thoughtmachine.net>
1 parent da768df commit f97ae2a

1 file changed

Lines changed: 61 additions & 4 deletions

File tree

test/server/server.go

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,25 @@ package server
22

33
import (
44
"fmt"
5+
"log"
6+
"net/http"
57
"net/http/httptest"
8+
"sync"
69

710
"github.com/gin-gonic/gin"
811
)
912

13+
type mockResponse struct {
14+
HTTPCode int
15+
Response map[string]interface{}
16+
}
17+
1018
// Server is a mock conduit server.
1119
type Server struct {
12-
engine *gin.Engine
13-
server *httptest.Server
20+
engine *gin.Engine
21+
server *httptest.Server
22+
mu sync.Mutex
23+
responses map[string][]mockResponse
1424
}
1525

1626
// New creates a new empty conduit server.
@@ -22,8 +32,9 @@ func New() *Server {
2232
ts := httptest.NewServer(r)
2333

2434
return &Server{
25-
engine: r,
26-
server: ts,
35+
engine: r,
36+
server: ts,
37+
responses: make(map[string][]mockResponse),
2738
}
2839
}
2940

@@ -51,6 +62,52 @@ func (s *Server) RegisterMethod(
5162
})
5263
}
5364

65+
// RegisterMethodResponse adds a response to return from the conduit API method.
66+
// Call this multiple times to register multiple responses from the API server.
67+
func (s *Server) RegisterMethodResponse(
68+
method string,
69+
httpCode int,
70+
response map[string]interface{},
71+
) {
72+
s.mu.Lock()
73+
defer s.mu.Unlock()
74+
75+
path := fmt.Sprintf("/api/%s", method)
76+
resp := mockResponse{
77+
HTTPCode: httpCode,
78+
Response: response,
79+
}
80+
if responses, exists := s.responses[path]; exists {
81+
s.responses[path] = append(responses, resp)
82+
} else {
83+
var responses []mockResponse
84+
responses = append(responses, resp)
85+
s.responses[path] = responses
86+
87+
// create handler
88+
s.engine.POST(path, func(c *gin.Context) {
89+
s.mu.Lock()
90+
defer s.mu.Unlock()
91+
if responses, exists := s.responses[path]; exists {
92+
if len(responses) == 0 {
93+
log.Printf("no mock responses left for path %s", path)
94+
c.JSON(http.StatusInternalServerError, nil)
95+
return
96+
}
97+
98+
mockResponse, rest := responses[0], responses[1:]
99+
c.JSON(mockResponse.HTTPCode, mockResponse.Response)
100+
s.responses[path] = rest
101+
return
102+
}
103+
104+
log.Printf("no mock responses defined for path %s", path)
105+
c.JSON(http.StatusInternalServerError, nil)
106+
return
107+
})
108+
}
109+
}
110+
54111
// GetURL returns the URL of the root of the server.
55112
func (s *Server) GetURL() string {
56113
return s.server.URL

0 commit comments

Comments
 (0)