From cf9c5072141779207fb14b404c9ac16eb9df96a7 Mon Sep 17 00:00:00 2001 From: Evgeny Abramovich Date: Sat, 23 Jul 2022 16:20:23 +0300 Subject: [PATCH] Added tests for NormalizeReqDecorators --- internal/infrastructure/normalize_test.go | 47 +++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 internal/infrastructure/normalize_test.go diff --git a/internal/infrastructure/normalize_test.go b/internal/infrastructure/normalize_test.go new file mode 100644 index 00000000..814bc80a --- /dev/null +++ b/internal/infrastructure/normalize_test.go @@ -0,0 +1,47 @@ +package infrastructure_test + +import ( + "context" + "net/http" + "net/http/httptest" + "testing" + + "github.com/evg4b/uncors/internal/infrastructure" + "github.com/stretchr/testify/assert" +) + +func TestNormalizeHTTPReqDecorator(t *testing.T) { + expectedHost := "localhost:3000" + expectedScheme := "http" + hadnlerFunc := infrastructure.NormalizeHTTPReqDecorator(func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, expectedHost, r.URL.Host) + assert.Equal(t, expectedScheme, r.URL.Scheme) + }) + + req, err := http.NewRequestWithContext(context.TODO(), "POST", "/", nil) + req.Host = expectedHost + if err != nil { + t.Fatal(err) + } + + http.HandlerFunc(hadnlerFunc). + ServeHTTP(httptest.NewRecorder(), req) +} + +func TestNormalizeNormalizeHTTPSReqDecorator(t *testing.T) { + expectedHost := "localhost:3000" + expectedScheme := "https" + hadnlerFunc := infrastructure.NormalizeHTTPSReqDecorator(func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, expectedHost, r.URL.Host) + assert.Equal(t, expectedScheme, r.URL.Scheme) + }) + + req, err := http.NewRequestWithContext(context.TODO(), "POST", "/", nil) + req.Host = expectedHost + if err != nil { + t.Fatal(err) + } + + http.HandlerFunc(hadnlerFunc). + ServeHTTP(httptest.NewRecorder(), req) +}