-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1374 from ssttevee/path-replace-rule
Add Path Replacement Rule
- Loading branch information
Showing
5 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package middlewares | ||
|
||
import ( | ||
"net/http" | ||
) | ||
|
||
// ReplacePath is a middleware used to replace the path of a URL request | ||
type ReplacePath struct { | ||
Handler http.Handler | ||
Path string | ||
} | ||
|
||
// ReplacedPathHeader is the default header to set the old path to | ||
const ReplacedPathHeader = "X-Replaced-Path" | ||
|
||
func (s *ReplacePath) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
r.Header.Add(ReplacedPathHeader, r.URL.Path) | ||
r.URL.Path = s.Path | ||
s.Handler.ServeHTTP(w, r) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package middlewares_test | ||
|
||
import ( | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/containous/traefik/middlewares" | ||
) | ||
|
||
func TestReplacePath(t *testing.T) { | ||
const replacementPath = "/replacement-path" | ||
|
||
paths := []string{ | ||
"/example", | ||
"/some/really/long/path", | ||
} | ||
|
||
for _, path := range paths { | ||
t.Run(path, func(t *testing.T) { | ||
var newPath, oldPath string | ||
handler := &middlewares.ReplacePath{ | ||
Path: replacementPath, | ||
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
newPath = r.URL.Path | ||
oldPath = r.Header.Get("X-Replaced-Path") | ||
}), | ||
} | ||
|
||
req, err := http.NewRequest("GET", "http://localhost"+path, nil) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
handler.ServeHTTP(nil, req) | ||
if newPath != replacementPath { | ||
t.Fatalf("new path should be '%s'", replacementPath) | ||
} | ||
|
||
if oldPath != path { | ||
t.Fatalf("old path should be '%s'", path) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters