Skip to content

Commit c74893a

Browse files
committed
server: health endpoint
1 parent adb6c90 commit c74893a

2 files changed

Lines changed: 36 additions & 4 deletions

File tree

internal/server/router.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
func NewRouter() http.Handler {
1818
mux := http.NewServeMux()
1919
mux.HandleFunc("/v1/exec", enableCORS(exec))
20+
mux.HandleFunc("/v1/health", enableCORS(health))
2021
return mux
2122
}
2223

@@ -89,3 +90,8 @@ func exec(w http.ResponseWriter, r *http.Request) {
8990
return
9091
}
9192
}
93+
94+
// health returns server health status.
95+
func health(w http.ResponseWriter, _ *http.Request) {
96+
_, _ = w.Write([]byte("OK"))
97+
}

internal/server/router_test.go

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func Test_exec(t *testing.T) {
7474
}
7575
resp, err := srv.post("/v1/exec", in)
7676
be.Err(t, err, nil)
77-
out := decodeResp[engine.Execution](t, resp)
77+
out := decodeJSON[engine.Execution](t, resp)
7878
be.True(t, out.OK)
7979
be.Equal(t, out.Stdout, "hello")
8080
be.Equal(t, out.Stderr, "")
@@ -89,7 +89,7 @@ func Test_exec(t *testing.T) {
8989
resp, err := srv.post("/v1/exec", in)
9090
be.Err(t, err, nil)
9191
be.Equal(t, resp.StatusCode, http.StatusNotFound)
92-
out := decodeResp[engine.Execution](t, resp)
92+
out := decodeJSON[engine.Execution](t, resp)
9393
be.Equal(t, out.OK, false)
9494
be.Equal(t, out.Stdout, "")
9595
be.Equal(t, out.Stderr, "unknown sandbox")
@@ -104,18 +104,44 @@ func Test_exec(t *testing.T) {
104104
resp, err := srv.post("/v1/exec", in)
105105
be.Err(t, err, nil)
106106
be.Equal(t, resp.StatusCode, http.StatusBadRequest)
107-
out := decodeResp[engine.Execution](t, resp)
107+
out := decodeJSON[engine.Execution](t, resp)
108108
be.Equal(t, out.OK, false)
109109
be.Equal(t, out.Stdout, "")
110110
be.Equal(t, out.Stderr, "empty request")
111111
be.Equal(t, out.Err, nil)
112112
})
113113
}
114114

115-
func decodeResp[T any](t *testing.T, resp *http.Response) T {
115+
func Test_health(t *testing.T) {
116+
srv := newServer()
117+
defer srv.close()
118+
119+
t.Run("get", func(t *testing.T) {
120+
resp, err := srv.cli.Get(srv.srv.URL + "/v1/health")
121+
be.Err(t, err, nil)
122+
be.Equal(t, resp.StatusCode, http.StatusOK)
123+
got := decodeText(t, resp)
124+
be.Equal(t, got, "OK")
125+
})
126+
t.Run("head", func(t *testing.T) {
127+
resp, err := srv.cli.Head(srv.srv.URL + "/v1/health")
128+
be.Err(t, err, nil)
129+
be.Equal(t, resp.StatusCode, http.StatusOK)
130+
})
131+
}
132+
133+
func decodeJSON[T any](t *testing.T, resp *http.Response) T {
116134
defer func() { _ = resp.Body.Close() }()
117135
var val T
118136
err := json.NewDecoder(resp.Body).Decode(&val)
119137
be.Err(t, err, nil)
120138
return val
121139
}
140+
141+
func decodeText(t *testing.T, resp *http.Response) string {
142+
defer func() { _ = resp.Body.Close() }()
143+
buf := new(bytes.Buffer)
144+
_, err := buf.ReadFrom(resp.Body)
145+
be.Err(t, err, nil)
146+
return buf.String()
147+
}

0 commit comments

Comments
 (0)