Skip to content

Commit

Permalink
fix: Cleanup main and use httptest.NewServer
Browse files Browse the repository at this point in the history
  • Loading branch information
Richard87 committed Oct 23, 2024
1 parent ccff4db commit c7a0e51
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 21 deletions.
10 changes: 3 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,16 @@ func main() {

log.Info().Interface("config", config).Msg("Starting")

err := Run(ctx, config)

log.Err(err).Msg("Terminated")
}

func Run(ctx context.Context, config Config) error {
router := NewRouter(
NewBackendController(config.ErrorFilesPath, config.DefaultFormat),
NewMetricsController(),
NewHealthzController(),
)

log.Ctx(ctx).Info().Msgf("Listening on http://localhost:%d", config.Port)
return Serve(ctx, config.Port, router)

err := Serve(ctx, config.Port, router)
log.Err(err).Msg("Terminated")
}

func NewHealthzController() RouteMapper {
Expand Down
20 changes: 6 additions & 14 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,22 @@
package main_test

import (
"context"
"io"
"net/http"
"net/http/httptest"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
import main "github.com/equinor/radix-ingress-default-backend"

var testConfig = main.Config{Port: 65332, DefaultFormat: "text/html", ErrorFilesPath: "./www"}

func TestRun(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

go func() {
err := main.Run(ctx, testConfig)
require.NoError(t, err)
}()
time.Sleep(2 * time.Second)
router := main.NewRouter(main.NewBackendController("./www", "text/html"))
server := httptest.NewServer(router)
defer server.Close()

req, err := http.NewRequest(http.MethodGet, "http://localhost:65332", nil)
req, err := http.NewRequest(http.MethodGet, server.URL, nil)
req.Header.Add("X-Namespace", "hello-world-dev")
req.Header.Add("X-Code", "503")
regularRequest, err := http.DefaultClient.Do(req)
Expand All @@ -35,7 +27,7 @@ func TestRun(t *testing.T) {
require.NoError(t, err)
assert.Contains(t, string(bytes), "Server error - Radix")

req, err = http.NewRequest(http.MethodGet, "http://localhost:65332", nil)
req, err = http.NewRequest(http.MethodGet, server.URL, nil)
req.Header.Add("X-Namespace", "equinor-web-sites-dev")
req.Header.Add("X-Code", "503")
equinorRequst, err := http.DefaultClient.Do(req)
Expand Down

0 comments on commit c7a0e51

Please sign in to comment.