Skip to content

Commit 8370fc2

Browse files
committed
Add test
1 parent 0f92ddd commit 8370fc2

2 files changed

Lines changed: 67 additions & 12 deletions

File tree

cmd/golang-docker-build-tutorial/main.go

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,52 @@ import (
44
"net/http"
55

66
"github.com/go-chi/chi/v5"
7+
"github.com/go-chi/chi/v5/middleware"
78
"github.com/go-chi/render"
89

910
internal "github.com/miguno/golang-docker-build-tutorial/internal/pkg"
1011
)
1112

12-
// Response is just a basic example.
13-
type Response struct {
14-
Status string `json:"status,omitempty"`
13+
type Server struct {
14+
Router *chi.Mux
15+
// Other configs such as DB settings can be added here
1516
}
1617

17-
func main() {
18-
r := chi.NewRouter()
19-
18+
func CreateNewServer() *Server {
19+
s := &Server{}
20+
s.Router = chi.NewRouter()
2021
// Uncomment to enable logging of incoming HTTP requests to STDOUT.
2122
// Requires `import "github.com/go-chi/chi/v5/middleware"`.
22-
//r.Use(middleware.Logger)
23+
//s.Router.Use(middleware.Logger)
24+
return s
25+
}
26+
27+
// StatusResponse is just a basic example.
28+
type StatusResponse struct {
29+
Status string `json:"status,omitempty"`
30+
}
31+
32+
func (s *Server) MountHandlers() {
33+
// Mount all Middleware here
34+
s.Router.Use(middleware.Logger)
2335

24-
r.Get("/status", func(w http.ResponseWriter, r *http.Request) {
36+
// Mount all handlers here
37+
s.Router.Get("/status", func(w http.ResponseWriter, r *http.Request) {
2538
// Create a Response object
26-
var response Response
39+
var response StatusResponse
2740
if internal.IsIdleToyFunction() {
28-
response = Response{Status: "idle"}
41+
response = StatusResponse{Status: "idle"}
2942
} else {
30-
response = Response{Status: "busy"}
43+
response = StatusResponse{Status: "busy"}
3144
}
3245

3346
render.JSON(w, r, response)
3447
})
35-
_ = http.ListenAndServe(":8123", r)
48+
49+
}
50+
51+
func main() {
52+
s := CreateNewServer()
53+
s.MountHandlers()
54+
_ = http.ListenAndServe(":8123", s.Router)
3655
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"net/http"
6+
"net/http/httptest"
7+
"testing"
8+
9+
"github.com/stretchr/testify/require"
10+
)
11+
12+
func executeRequest(req *http.Request, s *Server) *httptest.ResponseRecorder {
13+
rr := httptest.NewRecorder()
14+
s.Router.ServeHTTP(rr, req)
15+
return rr
16+
}
17+
18+
func verifyResponseCode(t *testing.T, expected, actual int) {
19+
if expected != actual {
20+
t.Errorf("Expected HTTP response code %d but got %d\n", expected, actual)
21+
}
22+
}
23+
24+
func TestStatusEndpoint(t *testing.T) {
25+
s := CreateNewServer()
26+
s.MountHandlers()
27+
28+
req, _ := http.NewRequest("GET", "/status", nil)
29+
res := executeRequest(req, s)
30+
31+
verifyResponseCode(t, http.StatusOK, res.Code)
32+
var sr StatusResponse
33+
_ = json.Unmarshal(res.Body.Bytes(), &sr)
34+
expectedResponse := StatusResponse{Status: "idle"}
35+
require.Equal(t, expectedResponse, sr)
36+
}

0 commit comments

Comments
 (0)