Skip to content

Commit

Permalink
chore: add tests for use of WithListener
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanhitt committed Dec 20, 2024
1 parent 625a60b commit 401b5e4
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 13 deletions.
5 changes: 1 addition & 4 deletions serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,8 @@ func (s *Server) setupDefaultListener() error {
return nil
}
listener, err := net.Listen("tcp", s.Addr)
if err != nil {
return err
}
s.listener = listener
return nil
return err
}

func (s *Server) printStartupMessage() {
Expand Down
44 changes: 35 additions & 9 deletions serve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,27 +390,29 @@ func TestIni(t *testing.T) {
}

func TestServer_Run(t *testing.T) {
// This is not a standard test, it is here to ensure that the server can run.
// Please do not run this kind of test for your controllers, it is NOT unit testing.
t.Run("can run server", func(t *testing.T) {
s := NewServer(
WithoutLogger(),
)

runServer := func(s *Server) (*Server, func()) {
Get(s, "/test", func(ctx ContextNoBody) (string, error) {
return "OK", nil
})

go func() {
s.Run()
}()
defer func() { // stop our test server when we are done
return s, func() { // stop our test server when we are done
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
if err := s.Server.Shutdown(ctx); err != nil {
t.Log(err)
}
cancel()
}()
}
}
// This is not a standard test, it is here to ensure that the server can run.
// Please do not run this kind of test for your controllers, it is NOT unit testing.
t.Run("can run server", func(t *testing.T) {
s, shutdown := runServer(NewServer(
WithoutLogger(),
))
defer shutdown()

require.Eventually(t, func() bool {
req := httptest.NewRequest("GET", "/test", nil)
Expand All @@ -420,6 +422,30 @@ func TestServer_Run(t *testing.T) {
return w.Body.String() == `OK`
}, 5*time.Second, 500*time.Millisecond)
})

t.Run("can run server WithListener", func(t *testing.T) {
listener, err := net.Listen("tcp", ":8080")
require.NoError(t, err)
s, shutdown := runServer(NewServer(
WithListener(listener),
))
defer shutdown()

require.Eventually(t, func() bool {
req := httptest.NewRequest("GET", "/test", nil)
w := httptest.NewRecorder()
s.Mux.ServeHTTP(w, req)

return w.Body.String() == `OK`
}, 5*time.Second, 500*time.Millisecond)
})

t.Run("invalid address", func(t *testing.T) {
s := NewServer(
WithAddr("----:nope"),
)
require.Error(t, s.Run())
})
}

func TestServer_RunTLS(t *testing.T) {
Expand Down
17 changes: 17 additions & 0 deletions server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"html/template"
"io"
"log/slog"
"net"
"net/http"
"net/http/httptest"
"testing"
Expand Down Expand Up @@ -339,6 +340,22 @@ func TestWithRequestContentType(t *testing.T) {
})
}

func TestWithListener(t *testing.T) {
t.Run("with custom listener", func(t *testing.T) {
listener, err := net.Listen("tcp", ":8080")
require.NoError(t, err)
s := NewServer(
WithListener(listener),
)
require.NotNil(t, s.listener)
})

t.Run("no custom listener", func(t *testing.T) {
s := NewServer()
require.Nil(t, s.listener)
})
}

func TestCustomSerialization(t *testing.T) {
s := NewServer(
WithSerializer(func(w http.ResponseWriter, r *http.Request, a any) error {
Expand Down

0 comments on commit 401b5e4

Please sign in to comment.