Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: [#31] added a new handler to list all the endpoints in a map #32

Merged
merged 4 commits into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
Loading