From 54826794486e709f4099181145fc48a747cd2b33 Mon Sep 17 00:00:00 2001 From: kpacha Date: Fri, 26 Oct 2018 16:23:54 +0200 Subject: [PATCH] delay endpoint method sanitization until required --- config/config.go | 7 +------ config/config_test.go | 6 +++--- proxy/http.go | 3 ++- router/gin/router.go | 14 ++++++++------ router/gin/router_test.go | 7 ++++--- router/mux/endpoint.go | 4 +++- router/mux/router.go | 14 ++++++++------ router/mux/router_test.go | 7 ++++--- 8 files changed, 33 insertions(+), 29 deletions(-) diff --git a/config/config.go b/config/config.go index 67524ce12..2f9fe3b25 100644 --- a/config/config.go +++ b/config/config.go @@ -310,12 +310,9 @@ func (s *ServiceConfig) Init() error { return errInvalidNoOpEncoding } - for j, b := range e.Backend { - + for j := range e.Backend { s.initBackendDefaults(i, j) - b.Method = strings.ToTitle(b.Method) - if err := s.initBackendURLMappings(i, j, inputSet); err != nil { return err } @@ -345,8 +342,6 @@ func (s *ServiceConfig) initEndpointDefaults(e int) { endpoint := s.Endpoints[e] if endpoint.Method == "" { endpoint.Method = "GET" - } else { - endpoint.Method = strings.ToTitle(endpoint.Method) } if s.CacheTTL != 0 && endpoint.CacheTTL == 0 { endpoint.CacheTTL = s.CacheTTL diff --git a/config/config_test.go b/config/config_test.go index d2c369312..49618ac1e 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -177,8 +177,8 @@ func TestConfig_init(t *testing.T) { } } - if supuBackend.Method != "POST" { - t.Error("supuBackend method not sanitized") + if supuBackend.Method != "post" { + t.Error("unexpected supuBackend") } if userBackend.Timeout != subject.Timeout { @@ -194,7 +194,7 @@ func TestConfig_init(t *testing.T) { t.Error(err.Error()) } - if hash != "Gs5PSt/5CG3jHUvnz8t1AH1IC/czFU4c3ycBoMSs3KA=" { + if hash != "lF/kGEBmuFV1Gdn3mjxXpRU5wHp51iFxf/o75PJu4LY=" { t.Errorf("unexpected hash: %s", hash) } } diff --git a/proxy/http.go b/proxy/http.go index ab48f322b..e857906f7 100644 --- a/proxy/http.go +++ b/proxy/http.go @@ -3,6 +3,7 @@ package proxy import ( "context" "net/http" + "strings" "github.com/devopsfaith/krakend/config" "github.com/devopsfaith/krakend/encoding" @@ -42,7 +43,7 @@ func NewHTTPProxyWithHTTPExecutor(remote *config.Backend, re client.HTTPRequestE // NewHTTPProxyDetailed creates a http proxy with the injected configuration, HTTPRequestExecutor, Decoder and HTTPResponseParser func NewHTTPProxyDetailed(remote *config.Backend, re client.HTTPRequestExecutor, ch client.HTTPStatusHandler, rp HTTPResponseParser) Proxy { return func(ctx context.Context, request *Request) (*Response, error) { - requestToBakend, err := http.NewRequest(request.Method, request.URL.String(), request.Body) + requestToBakend, err := http.NewRequest(strings.ToTitle(request.Method), request.URL.String(), request.Body) if err != nil { return nil, err } diff --git a/router/gin/router.go b/router/gin/router.go index fcdb6c697..4ee827d4f 100644 --- a/router/gin/router.go +++ b/router/gin/router.go @@ -4,6 +4,7 @@ package gin import ( "context" "net/http" + "strings" "github.com/gin-gonic/gin" @@ -119,20 +120,21 @@ func (r ginRouter) registerKrakendEndpoints(endpoints []*config.EndpointConfig) } func (r ginRouter) registerKrakendEndpoint(method, path string, handler gin.HandlerFunc, totBackends int) { - if method != "GET" && totBackends > 1 { + method = strings.ToTitle(method) + if method != http.MethodGet && totBackends > 1 { r.cfg.Logger.Error(method, "endpoints must have a single backend! Ignoring", path) return } switch method { - case "GET": + case http.MethodGet: r.cfg.Engine.GET(path, handler) - case "POST": + case http.MethodPost: r.cfg.Engine.POST(path, handler) - case "PUT": + case http.MethodPut: r.cfg.Engine.PUT(path, handler) - case "PATCH": + case http.MethodPatch: r.cfg.Engine.PATCH(path, handler) - case "DELETE": + case http.MethodDelete: r.cfg.Engine.DELETE(path, handler) default: r.cfg.Logger.Error("Unsupported method", method) diff --git a/router/gin/router_test.go b/router/gin/router_test.go index 9031a92c9..d194fe700 100644 --- a/router/gin/router_test.go +++ b/router/gin/router_test.go @@ -10,6 +10,7 @@ import ( "io/ioutil" "net/http" "regexp" + "strings" "testing" "time" @@ -50,7 +51,7 @@ func TestDefaultFactory_ok(t *testing.T) { }, { Endpoint: "/some", - Method: "POST", + Method: "post", Timeout: 10, Backend: []*config.Backend{ {}, @@ -58,7 +59,7 @@ func TestDefaultFactory_ok(t *testing.T) { }, { Endpoint: "/some", - Method: "PUT", + Method: "put", Timeout: 10, Backend: []*config.Backend{ {}, @@ -88,7 +89,7 @@ func TestDefaultFactory_ok(t *testing.T) { time.Sleep(5 * time.Millisecond) for _, endpoint := range serviceCfg.Endpoints { - req, _ := http.NewRequest(endpoint.Method, fmt.Sprintf("http://127.0.0.1:8072%s", endpoint.Endpoint), nil) + req, _ := http.NewRequest(strings.ToTitle(endpoint.Method), fmt.Sprintf("http://127.0.0.1:8072%s", endpoint.Endpoint), nil) req.Header.Set("Content-Type", "application/json") resp, err := http.DefaultClient.Do(req) if err != nil { diff --git a/router/mux/endpoint.go b/router/mux/endpoint.go index 793f214c9..14838f583 100644 --- a/router/mux/endpoint.go +++ b/router/mux/endpoint.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "net/http" + "strings" "github.com/devopsfaith/krakend/config" "github.com/devopsfaith/krakend/core" @@ -34,10 +35,11 @@ func CustomEndpointHandlerWithHTTPError(rb RequestBuilder, errF router.ToHTTPErr if len(headersToSend) == 0 { headersToSend = router.HeadersToSend } + method := strings.ToTitle(configuration.Method) return func(w http.ResponseWriter, r *http.Request) { w.Header().Set(core.KrakendHeaderName, core.KrakendHeaderValue) - if r.Method != configuration.Method { + if r.Method != method { w.Header().Set(router.CompleteResponseHeaderName, router.HeaderIncompleteResponseValue) http.Error(w, "", http.StatusMethodNotAllowed) return diff --git a/router/mux/router.go b/router/mux/router.go index 8835db3d6..e45108e7e 100644 --- a/router/mux/router.go +++ b/router/mux/router.go @@ -4,6 +4,7 @@ package mux import ( "context" "net/http" + "strings" "github.com/devopsfaith/krakend/config" "github.com/devopsfaith/krakend/logging" @@ -106,17 +107,18 @@ func (r httpRouter) registerKrakendEndpoints(endpoints []*config.EndpointConfig) } func (r httpRouter) registerKrakendEndpoint(method, path string, handler http.HandlerFunc, totBackends int) { - if method != "GET" && totBackends > 1 { + method = strings.ToTitle(method) + if method != http.MethodGet && totBackends > 1 { r.cfg.Logger.Error(method, "endpoints must have a single backend! Ignoring", path) return } switch method { - case "GET": - case "POST": - case "PUT": - case "PATCH": - case "DELETE": + case http.MethodGet: + case http.MethodPost: + case http.MethodPut: + case http.MethodPatch: + case http.MethodDelete: default: r.cfg.Logger.Error("Unsupported method", method) return diff --git a/router/mux/router_test.go b/router/mux/router_test.go index 7edd46a56..e35d24e5d 100644 --- a/router/mux/router_test.go +++ b/router/mux/router_test.go @@ -10,6 +10,7 @@ import ( "io/ioutil" "net/http" "regexp" + "strings" "testing" "time" @@ -49,7 +50,7 @@ func TestDefaultFactory_ok(t *testing.T) { }, { Endpoint: "/post", - Method: "POST", + Method: "Post", Timeout: 10, Backend: []*config.Backend{ {}, @@ -57,7 +58,7 @@ func TestDefaultFactory_ok(t *testing.T) { }, { Endpoint: "/put", - Method: "PUT", + Method: "put", Timeout: 10, Backend: []*config.Backend{ {}, @@ -87,7 +88,7 @@ func TestDefaultFactory_ok(t *testing.T) { time.Sleep(5 * time.Millisecond) for _, endpoint := range serviceCfg.Endpoints { - req, _ := http.NewRequest(endpoint.Method, fmt.Sprintf("http://127.0.0.1:8062%s", endpoint.Endpoint), nil) + req, _ := http.NewRequest(strings.ToTitle(endpoint.Method), fmt.Sprintf("http://127.0.0.1:8062%s", endpoint.Endpoint), nil) req.Header.Set("Content-Type", "application/json") resp, err := http.DefaultClient.Do(req) if err != nil {