Skip to content

Commit

Permalink
Fixed middelware tesrs
Browse files Browse the repository at this point in the history
  • Loading branch information
evg4b committed May 8, 2023
1 parent 18ea089 commit 5d255c2
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 160 deletions.
4 changes: 3 additions & 1 deletion internal/handler/helpres.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package handler

import "github.com/gorilla/mux"
import (
"github.com/gorilla/mux"
)

func setPath(route *mux.Route, path string) {
if len(path) > 0 {
Expand Down
8 changes: 5 additions & 3 deletions internal/handler/proxy/middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"strings"
"testing"

"github.com/evg4b/uncors/internal/helpers"

"github.com/evg4b/uncors/internal/configuration"

"github.com/evg4b/uncors/internal/handler/proxy"
Expand Down Expand Up @@ -92,7 +94,7 @@ func TestProxyMiddleware(t *testing.T) {
headerKey string
}{
{
name: "transform Location",
name: "transform location",
URL: "https://premium.api.com/app",
expectedURL: "http://premium.local.com/app",
headerKey: headers.Location,
Expand Down Expand Up @@ -125,10 +127,10 @@ func TestProxyMiddleware(t *testing.T) {

req, err := http.NewRequestWithContext(context.TODO(), http.MethodPost, expectedURL.Path, nil)
testutils.CheckNoError(t, err)

req.URL.Scheme = expectedURL.Scheme
req.Host = expectedURL.Host
req.URL.Path = expectedURL.Path
helpers.NormaliseRequest(req)

recorder := httptest.NewRecorder()

Expand Down Expand Up @@ -159,9 +161,9 @@ func TestProxyMiddleware(t *testing.T) {

req, err := http.NewRequestWithContext(context.TODO(), http.MethodPost, "/", nil)
testutils.CheckNoError(t, err)

req.URL.Scheme = "http"
req.Host = "premium.local.com"
helpers.NormaliseRequest(req)

recorder := httptest.NewRecorder()

Expand Down
145 changes: 0 additions & 145 deletions internal/handler/static/make_statics_test.go

This file was deleted.

13 changes: 13 additions & 0 deletions internal/helpers/http.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package helpers

import "net/http"

func NormaliseRequest(request *http.Request) {
request.URL.Host = request.Host

if request.TLS != nil {
request.URL.Scheme = "https"
} else {
request.URL.Scheme = "http"
}
}
14 changes: 3 additions & 11 deletions internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"net/http"
"time"

"github.com/evg4b/uncors/internal/helpers"

"github.com/evg4b/uncors/internal/log"

"golang.org/x/net/context"
Expand All @@ -28,7 +30,7 @@ func NewUncorsServer(ctx context.Context, handler http.Handler) *UncorsServer {
},
ReadHeaderTimeout: readHeaderTimeout,
Handler: http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
patchRequest(request)
helpers.NormaliseRequest(request)
handler.ServeHTTP(writer, request)
}),
ErrorLog: log.StandardErrorLogAdapter(),
Expand Down Expand Up @@ -121,13 +123,3 @@ func (srv *UncorsServer) internalShutdown() {
log.Debug("finish: UNCORS server closed")
}
}

func patchRequest(request *http.Request) {
request.URL.Host = request.Host

if request.TLS != nil {
request.URL.Scheme = "https"
} else {
request.URL.Scheme = "http"
}
}

0 comments on commit 5d255c2

Please sign in to comment.