Skip to content

Commit

Permalink
Merge pull request #57 from krakend/remove-ctx-headers
Browse files Browse the repository at this point in the history
Allow deleting ctx headers
  • Loading branch information
kpacha authored Dec 17, 2024
2 parents 84b7b3e + a567651 commit 18dfaf3
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 0 deletions.
6 changes: 6 additions & 0 deletions router/gin/lua.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/luraproject/lura/v2/logging"
"github.com/luraproject/lura/v2/proxy"
krakendgin "github.com/luraproject/lura/v2/router/gin"
glua "github.com/yuin/gopher-lua"
)

func Register(l logging.Logger, extraConfig config.ExtraConfig, engine *gin.Engine) {
Expand Down Expand Up @@ -223,6 +224,11 @@ func (*ginContext) requestHeaders(c *binder.Context) error {
case 2:
c.Push().String(req.Request.Header.Get(c.Arg(2).String()))
case 3:
_, isNil := c.Arg(3).Any().(*glua.LNilType)
if isNil {
req.Request.Header.Del(c.Arg(2).String())
return nil
}
req.Request.Header.Set(c.Arg(2).String(), c.Arg(3).String())
}

Expand Down
10 changes: 10 additions & 0 deletions router/gin/lua_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ func TestHandlerFactory(t *testing.T) {
req:method("POST")
req:params("foo", "some_new_value")
req:headers("Accept", "application/xml")
req:headers("X-To-Delete", nil)
req:headers("X-TO-DELETE-LOWER", nil)
req:url(req:url() .. "&more=true")
req:host(req:host() .. ".newtld")
req:query("extra", "foo")
Expand All @@ -46,6 +48,12 @@ func TestHandlerFactory(t *testing.T) {
if accept := c.Request.Header.Get("Accept"); accept != "application/xml" {
t.Errorf("unexpected accept header: %s", accept)
}
if toDelete := c.Request.Header.Get("X-To-Delete"); len(toDelete) > 0 {
t.Error("unexpected header 'X-To-Delete', should have been deleted")
}
if toDeleteLower := c.Request.Header.Get("X-To-Delete-Lower"); len(toDeleteLower) > 0 {
t.Error("unexpected header 'X-To-Delete-Lower', should have been deleted")
}
if c.Request.Method != "POST" {
t.Errorf("unexpected method: %s", c.Request.Method)
}
Expand Down Expand Up @@ -76,6 +84,8 @@ func TestHandlerFactory(t *testing.T) {
req, _ := http.NewRequest("GET", "/some-path/42?id=1", http.NoBody)
req.Host = "domain.tld"
req.Header.Set("Accept", "application/json")
req.Header.Set("X-To-Delete", "deleteme")
req.Header.Set("x-to-delete-lower", "deleteme")
w := httptest.NewRecorder()

engine.ServeHTTP(w, req)
Expand Down
6 changes: 6 additions & 0 deletions router/mux/lua.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/luraproject/lura/v2/logging"
"github.com/luraproject/lura/v2/proxy"
mux "github.com/luraproject/lura/v2/router/mux"
glua "github.com/yuin/gopher-lua"
)

func RegisterMiddleware(l logging.Logger, e config.ExtraConfig, pe mux.ParamExtractor, mws []mux.HandlerMiddleware) []mux.HandlerMiddleware {
Expand Down Expand Up @@ -202,6 +203,11 @@ func (*muxContext) headers(c *binder.Context) error {
case 2:
c.Push().String(req.Header.Get(c.Arg(2).String()))
case 3:
_, isNil := c.Arg(3).Any().(*glua.LNilType)
if isNil {
req.Header.Del(c.Arg(2).String())
return nil
}
req.Header.Set(c.Arg(2).String(), c.Arg(3).String())
}

Expand Down
10 changes: 10 additions & 0 deletions router/mux/lua_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ func TestHandlerFactory(t *testing.T) {
req:method("POST")
req:params("foo", "some_new_value")
req:headers("Accept", "application/xml")
req:headers("X-To-Delete", nil)
req:headers("X-TO-DELETE-LOWER", nil)
req:url(req:url() .. "&more=true")
req:query("extra", "foo")
req:body(req:body().."fooooooo")`,
Expand All @@ -41,6 +43,12 @@ func TestHandlerFactory(t *testing.T) {
if accept := r.Header.Get("Accept"); accept != "application/xml" {
t.Errorf("unexpected accept header: %s", accept)
}
if toDelete := r.Header.Get("X-To-Delete"); len(toDelete) > 0 {
t.Error("unexpected header 'X-To-Delete', should have been deleted")
}
if toDeleteLower := r.Header.Get("X-To-Delete-Lower"); len(toDeleteLower) > 0 {
t.Error("unexpected header 'X-To-Delete-Lower', should have been deleted")
}
if r.Method != "POST" {
t.Errorf("unexpected method: %s", r.Method)
}
Expand Down Expand Up @@ -69,6 +77,8 @@ func TestHandlerFactory(t *testing.T) {

req, _ := http.NewRequest("GET", "/some-path/42?id=1", http.NoBody)
req.Header.Set("Accept", "application/json")
req.Header.Set("X-To-Delete", "deleteme")
req.Header.Set("x-to-delete-lower", "deleteme")
w := httptest.NewRecorder()

handler(w, req)
Expand Down

0 comments on commit 18dfaf3

Please sign in to comment.