Skip to content

Commit

Permalink
Add method to test server to allow multiple responses from the same e…
Browse files Browse the repository at this point in the history
…ndpoint (#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 <[email protected]>
  • Loading branch information
jkimbo and jkim authored Apr 18, 2024
1 parent da768df commit f97ae2a
Showing 1 changed file with 61 additions and 4 deletions.
65 changes: 61 additions & 4 deletions test/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,25 @@ package server

import (
"fmt"
"log"
"net/http"
"net/http/httptest"
"sync"

"github.com/gin-gonic/gin"
)

type mockResponse struct {
HTTPCode int
Response map[string]interface{}
}

// Server is a mock conduit server.
type Server struct {
engine *gin.Engine
server *httptest.Server
engine *gin.Engine
server *httptest.Server
mu sync.Mutex
responses map[string][]mockResponse
}

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

return &Server{
engine: r,
server: ts,
engine: r,
server: ts,
responses: make(map[string][]mockResponse),
}
}

Expand Down Expand Up @@ -51,6 +62,52 @@ func (s *Server) RegisterMethod(
})
}

// RegisterMethodResponse adds a response to return from the conduit API method.
// Call this multiple times to register multiple responses from the API server.
func (s *Server) RegisterMethodResponse(
method string,
httpCode int,
response map[string]interface{},
) {
s.mu.Lock()
defer s.mu.Unlock()

path := fmt.Sprintf("/api/%s", method)
resp := mockResponse{
HTTPCode: httpCode,
Response: response,
}
if responses, exists := s.responses[path]; exists {
s.responses[path] = append(responses, resp)
} else {
var responses []mockResponse
responses = append(responses, resp)
s.responses[path] = responses

// create handler
s.engine.POST(path, func(c *gin.Context) {
s.mu.Lock()
defer s.mu.Unlock()
if responses, exists := s.responses[path]; exists {
if len(responses) == 0 {
log.Printf("no mock responses left for path %s", path)
c.JSON(http.StatusInternalServerError, nil)
return
}

mockResponse, rest := responses[0], responses[1:]
c.JSON(mockResponse.HTTPCode, mockResponse.Response)
s.responses[path] = rest
return
}

log.Printf("no mock responses defined for path %s", path)
c.JSON(http.StatusInternalServerError, nil)
return
})
}
}

// GetURL returns the URL of the root of the server.
func (s *Server) GetURL() string {
return s.server.URL
Expand Down

0 comments on commit f97ae2a

Please sign in to comment.