Skip to content

Commit

Permalink
Added tests for NormalizeReqDecorators
Browse files Browse the repository at this point in the history
  • Loading branch information
evg4b committed Jul 23, 2022
1 parent 2ad2222 commit cf9c507
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions internal/infrastructure/normalize_test.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit cf9c507

Please sign in to comment.