From ef1b7a89da4af7ea71f3c913542288667b999e93 Mon Sep 17 00:00:00 2001 From: Jimmy Herrera Date: Mon, 23 Dec 2024 13:39:05 +0100 Subject: [PATCH] fix: [#31] added a new handler to list all the endpoints in a map (#32) * 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 % --- .github/workflows/golang.yml | 2 +- cmd/mcvs-stub-server/main.go | 23 ++++++++++++++++ cmd/mcvs-stub-server/main_test.go | 44 +++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 1 deletion(-) diff --git a/.github/workflows/golang.yml b/.github/workflows/golang.yml index e64fcc4..c08100c 100644 --- a/.github/workflows/golang.yml +++ b/.github/workflows/golang.yml @@ -29,7 +29,7 @@ jobs: - uses: actions/checkout@v4.2.2 - uses: schubergphilis/mcvs-golang-action@v0.15.3 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 }} diff --git a/cmd/mcvs-stub-server/main.go b/cmd/mcvs-stub-server/main.go index a0ad877..9dc0238 100644 --- a/cmd/mcvs-stub-server/main.go +++ b/cmd/mcvs-stub-server/main.go @@ -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 { @@ -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) + } +} diff --git a/cmd/mcvs-stub-server/main_test.go b/cmd/mcvs-stub-server/main_test.go index e1764b9..57522b0 100644 --- a/cmd/mcvs-stub-server/main_test.go +++ b/cmd/mcvs-stub-server/main_test.go @@ -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) +}