Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
theruziev committed May 25, 2024
1 parent 1684e27 commit fafc5a6
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 11 deletions.
21 changes: 13 additions & 8 deletions internal/app/server/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"net/http"
"sync"

"github.com/go-chi/chi/v5"
"github.com/theruziev/starter/internal/pkg/closer"
Expand All @@ -20,6 +21,8 @@ type Server struct {
closer *closer.Closer
r chi.Router
healthcheck *health.Server

routerOnce sync.Once
}

type Options struct {
Expand All @@ -46,15 +49,17 @@ func (s *Server) initHealthCheck(_ context.Context) {
}

func (s *Server) initRouter(_ context.Context) {
r := chi.NewRouter()
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("Hello world!"))
s.routerOnce.Do(func() {
r := chi.NewRouter()
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("Hello world!"))
})
r.Get("/live", healthcheck.NewLivenessHandler())
r.Get("/ready", healthcheck.NewReadinessHandler(s.healthcheck))
r.Get("/version", info.Handler())
s.r = r
})
r.Get("/live", healthcheck.NewLivenessHandler())
r.Get("/ready", healthcheck.NewReadinessHandler(s.healthcheck))
r.Get("/version", info.Handler())
s.r = r
}

func (s *Server) Run(ctx context.Context) error {
Expand Down
44 changes: 41 additions & 3 deletions internal/pkg/healthcheck/healthcheck_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,50 @@
package healthcheck

import (
"net/http"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/require"
)

func TestNewHealthCheck(t *testing.T) {
healthcheck := NewHealthCheck()
require.NotNil(t, healthcheck)
func TestNewLivenessHandler(t *testing.T) {
req, err := http.NewRequest("GET", "/live", nil)
require.NoError(t, err)

rr := httptest.NewRecorder()
handler := NewLivenessHandler()

handler.ServeHTTP(rr, req)

require.Equal(t, http.StatusOK, rr.Code)
}

func TestNewReadinessHandler(t *testing.T) {
t.Run("not serving", func(t *testing.T) {
healthcheck := NewHealthCheck()
req, err := http.NewRequest("GET", "/ready", nil)
require.NoError(t, err)

healthcheck.Shutdown()
rr := httptest.NewRecorder()
handler := NewReadinessHandler(healthcheck)

handler.ServeHTTP(rr, req)
require.Equal(t, http.StatusServiceUnavailable, rr.Code)
})

t.Run("serving", func(t *testing.T) {
healthcheck := NewHealthCheck()
req, err := http.NewRequest("GET", "/ready", nil)
require.NoError(t, err)

healthcheck.Resume()
rr := httptest.NewRecorder()
handler := NewReadinessHandler(healthcheck)

handler.ServeHTTP(rr, req)
require.Equal(t, http.StatusOK, rr.Code)
})

}
35 changes: 35 additions & 0 deletions internal/pkg/info/info_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package info

import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/require"
)

func TestHandler(t *testing.T) {
req, err := http.NewRequest("GET", "/info", nil)
if err != nil {
t.Fatal(err)
}

rr := httptest.NewRecorder()
handler := Handler()

handler.ServeHTTP(rr, req)

require.Equal(t, http.StatusOK, rr.Code)

expected := map[string]any{
"version": Version,
"commit": Commit,
}

var got map[string]any
err = json.NewDecoder(rr.Body).Decode(&got)
require.NoError(t, err)
require.EqualValues(t, expected, got)

}
29 changes: 29 additions & 0 deletions internal/pkg/logx/ctx_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package logx

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
)

func TestWithLogger(t *testing.T) {
ctx := context.Background()
log := zap.NewExample().Sugar()

newCtx := WithLogger(ctx, log)

require.Equal(t, log, newCtx.Value(loggerKey))
}

func TestFromContext(t *testing.T) {
log := zap.NewExample().Sugar()
ctx := context.WithValue(context.Background(), loggerKey, log)

assert.Equal(t, log, FromContext(ctx))

emptyCtx := context.Background()
require.Equal(t, DefaultLogger(), FromContext(emptyCtx))
}

0 comments on commit fafc5a6

Please sign in to comment.