Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

delay endpoint method sanitization until required #162

Merged
merged 1 commit into from
Oct 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}
}
Expand Down
3 changes: 2 additions & 1 deletion proxy/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package proxy
import (
"context"
"net/http"
"strings"

"github.com/devopsfaith/krakend/config"
"github.com/devopsfaith/krakend/encoding"
Expand Down Expand Up @@ -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
}
Expand Down
14 changes: 8 additions & 6 deletions router/gin/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package gin
import (
"context"
"net/http"
"strings"

"github.com/gin-gonic/gin"

Expand Down Expand Up @@ -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)
Expand Down
7 changes: 4 additions & 3 deletions router/gin/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"io/ioutil"
"net/http"
"regexp"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -50,15 +51,15 @@ func TestDefaultFactory_ok(t *testing.T) {
},
{
Endpoint: "/some",
Method: "POST",
Method: "post",
Timeout: 10,
Backend: []*config.Backend{
{},
},
},
{
Endpoint: "/some",
Method: "PUT",
Method: "put",
Timeout: 10,
Backend: []*config.Backend{
{},
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 3 additions & 1 deletion router/mux/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"net/http"
"strings"

"github.com/devopsfaith/krakend/config"
"github.com/devopsfaith/krakend/core"
Expand Down Expand Up @@ -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
Expand Down
14 changes: 8 additions & 6 deletions router/mux/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package mux
import (
"context"
"net/http"
"strings"

"github.com/devopsfaith/krakend/config"
"github.com/devopsfaith/krakend/logging"
Expand Down Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions router/mux/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"io/ioutil"
"net/http"
"regexp"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -49,15 +50,15 @@ func TestDefaultFactory_ok(t *testing.T) {
},
{
Endpoint: "/post",
Method: "POST",
Method: "Post",
Timeout: 10,
Backend: []*config.Backend{
{},
},
},
{
Endpoint: "/put",
Method: "PUT",
Method: "put",
Timeout: 10,
Backend: []*config.Backend{
{},
Expand Down Expand Up @@ -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 {
Expand Down