Skip to content

Commit

Permalink
Merge proxy and options midelwares
Browse files Browse the repository at this point in the history
  • Loading branch information
evg4b committed Oct 14, 2022
1 parent fffece1 commit 0a673b0
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 165 deletions.
50 changes: 0 additions & 50 deletions internal/options/middleware.go

This file was deleted.

112 changes: 0 additions & 112 deletions internal/options/middleware_test.go

This file was deleted.

23 changes: 23 additions & 0 deletions internal/proxy/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package proxy
import (
"net/http"
"strings"

"github.com/evg4b/uncors/internal/responseprinter"
"github.com/pterm/pterm"
)

type modificationsMap = map[string]func(string) (string, error)
Expand Down Expand Up @@ -30,3 +33,23 @@ func copyHeaders(source, dest http.Header, modifications modificationsMap) error

return nil
}

func makeOptionsResponse(printer pterm.PrefixPrinter, writer http.ResponseWriter, req *http.Request) error {
header := writer.Header()
for key, values := range req.Header {
lowerKey := strings.ToLower(key)
if strings.Contains(lowerKey, "access-control-request") {
for _, value := range values {
transformedKey := strings.Replace(lowerKey, "request", "allow", 1)
header.Add(transformedKey, value)
}
}
}

printer.Printfln(responseprinter.PrintResponse(&http.Response{
StatusCode: http.StatusOK,
Request: req,
}))

return nil
}
5 changes: 5 additions & 0 deletions internal/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io"
"net/http"
"strings"

"github.com/evg4b/uncors/internal/processor"
"github.com/evg4b/uncors/internal/responseprinter"
Expand Down Expand Up @@ -37,6 +38,10 @@ func (pm *ProxyMiddleware) Wrap(_ processor.HandlerFunc) processor.HandlerFunc {
}

return func(resp http.ResponseWriter, req *http.Request) error {
if strings.EqualFold(req.Method, http.MethodOptions) {
return makeOptionsResponse(proxyWriter, resp, req)
}

targetR, sourceR, err := pm.replacerFactory.Make(req.URL)
if err != nil {
return fmt.Errorf("failed to transform general url: %w", err)
Expand Down
69 changes: 69 additions & 0 deletions internal/proxy/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/evg4b/uncors/internal/proxy"
"github.com/evg4b/uncors/internal/urlreplacer"
"github.com/evg4b/uncors/pkg/urlx"
"github.com/evg4b/uncors/testing/mocks"
"github.com/evg4b/uncors/testing/testutils"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -178,3 +179,71 @@ func TestProxyMiddlewareWrap(t *testing.T) {
)
})
}

func TestOptionsMiddlewareWrap(t *testing.T) {
middleware := proxy.NewProxyMiddleware()

t.Run("should handle OPTIONS request", func(t *testing.T) {
tracker := mocks.NewMiddlewaresTracker(t)
proc := processor.NewRequestProcessor(
processor.WithMiddleware(middleware),
processor.WithMiddleware(tracker.MakeFinalMiddleware("final")),
)

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

proc.ServeHTTP(httptest.NewRecorder(), req)

assert.Equal(t, []string{}, tracker.CallsOrder)
})

t.Run("should correctly create response", func(t *testing.T) {
testMethods := []struct {
name string
headers http.Header
expected http.Header
}{
{
name: "should do not change empty headers",
headers: http.Header(map[string][]string{}),
expected: http.Header(map[string][]string{}),
},
{
name: "should do not skip not access-control-request-* headers",
headers: http.Header{
"Host": {"www.host.com"},
"Content-Type": {"application/json"},
"Authorization": {"Bearer Token"},
},
expected: http.Header{},
},
{
name: "should allow all access-control-request-* headers",
headers: http.Header{
"Access-Control-Request-Headers": {"X-PINGOTHER, Content-Type"},
"Access-Control-Request-Method": {http.MethodPost, http.MethodDelete},
},
expected: http.Header{
"Access-Control-Allow-Headers": {"X-PINGOTHER, Content-Type"},
"Access-Control-Allow-Method": {http.MethodPost, http.MethodDelete},
},
},
}
for _, testCase := range testMethods {
t.Run(testCase.name, func(t *testing.T) {
proc := processor.NewRequestProcessor(processor.WithMiddleware(middleware))
req, err := http.NewRequestWithContext(context.TODO(), http.MethodOptions, "/", nil)
testutils.CheckNoError(t, err)

req.Header = testCase.headers

recorder := httptest.NewRecorder()
proc.ServeHTTP(recorder, req)

assert.Equal(t, http.StatusOK, recorder.Code)
assert.Equal(t, testCase.expected, recorder.Header())
})
}
})
}
3 changes: 0 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"strings"

"github.com/evg4b/uncors/internal/infrastructure"
"github.com/evg4b/uncors/internal/options"
"github.com/evg4b/uncors/internal/processor"
"github.com/evg4b/uncors/internal/proxy"
"github.com/evg4b/uncors/internal/urlreplacer"
Expand Down Expand Up @@ -67,14 +66,12 @@ func main() {
pterm.Fatal.Println(err)
}

optionsMiddleware := options.NewOptionsMiddleware()
proxyMiddleware := proxy.NewProxyMiddleware(
proxy.WithURLReplacerFactory(factory),
proxy.WithHTTPClient(httpClient),
)

requestProcessor := processor.NewRequestProcessor(
processor.WithMiddleware(optionsMiddleware),
processor.WithMiddleware(proxyMiddleware),
)

Expand Down

0 comments on commit 0a673b0

Please sign in to comment.