Skip to content

Commit

Permalink
fix: [#31] added a new handler to list all the endpoints in a map (#32)
Browse files Browse the repository at this point in the history
* fix: [#31] added a new handler to list all the endpoints in a map

* fix: [#31] added extra unit test for list handler

* fix: [#31] added test case for invalid endpoints map

* fix: [#31] increased test coverage %
  • Loading branch information
jherrerasbp authored Dec 23, 2024
1 parent e491c56 commit ef1b7a8
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/golang.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
- uses: actions/[email protected]
- uses: schubergphilis/[email protected]
with:
code-coverage-expected: 63.1
code-coverage-expected: 64.6
golang-unit-tests-exclusions: |-
\(cmd\/mcvs-integrationtest-services\|cmd\/oktamock\)
testing-type: ${{ matrix.testing-type }}
Expand Down
23 changes: 23 additions & 0 deletions cmd/mcvs-stub-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ func main() {
http.HandleFunc("/health", h.health)
http.HandleFunc("/reset", h.reset)
http.HandleFunc("/configure", h.configure)
http.HandleFunc("/list", h.list)
http.HandleFunc("/", h.catchAll)
err := http.ListenAndServe(":8080", nil)
if err != nil {
Expand Down Expand Up @@ -93,3 +94,25 @@ func (h *handler) catchAll(w http.ResponseWriter, r *http.Request) {
log.Default().Println("Failed to write response:", err)
}
}

func (h *handler) list(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "Invalid request method", http.StatusMethodNotAllowed)
return
}

h.mu.RLock()
defer h.mu.RUnlock()

b, err := json.Marshal(h.endpoints)
if err != nil {
log.Default().Println("Failed to marshal endpoints:", err)
http.Error(w, "Internal server error", http.StatusInternalServerError)
return
}

_, err = w.Write(b)
if err != nil {
log.Default().Println("Failed to write response:", err)
}
}
44 changes: 44 additions & 0 deletions cmd/mcvs-stub-server/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,47 @@ func TestCatchAllHandlerNotFound(t *testing.T) {
handler.catchAll(httptestRecorder, httptestRequest)
assert.Equal(t, http.StatusNotFound, httptestRecorder.Code)
}

func TestListHandler(t *testing.T) {
// given
handler := newHandler()
handler.endpoints["/foo"] = "bar"
handler.endpoints["/bar"] = "foo"

// when
httptestRecorder := httptest.NewRecorder()
httptestRequest := httptest.NewRequest("GET", "/list", nil)

// then
handler.list(httptestRecorder, httptestRequest)
assert.Equal(t, http.StatusOK, httptestRecorder.Code)
assert.Len(t, handler.endpoints, 2)
assert.Equal(t, []byte(`{"/bar":"foo","/foo":"bar"}`), httptestRecorder.Body.Bytes())
}

func TestListHandlerInvalidMethod(t *testing.T) {
// given
handler := newHandler()

// when
httptestRecorder := httptest.NewRecorder()
httptestRequest := httptest.NewRequest("POST", "/list", nil)

// then
handler.list(httptestRecorder, httptestRequest)
assert.Equal(t, http.StatusMethodNotAllowed, httptestRecorder.Code)
}

func TestListHandlerInvalidEndpointsMap(t *testing.T) {
// given
handler := newHandler()
handler.endpoints["/bad"] = func() {}

// when
httptestRecorder := httptest.NewRecorder()
httptestRequest := httptest.NewRequest("GET", "/list", nil)

// then
handler.list(httptestRecorder, httptestRequest)
assert.Equal(t, http.StatusInternalServerError, httptestRecorder.Code)
}

0 comments on commit ef1b7a8

Please sign in to comment.