2022-03-17 17:02:08 +00:00
|
|
|
package http
|
2016-06-06 13:27:16 +00:00
|
|
|
|
|
|
|
import (
|
2022-06-27 13:16:08 +00:00
|
|
|
"bufio"
|
|
|
|
"bytes"
|
2016-06-06 13:27:16 +00:00
|
|
|
"net/http"
|
2019-01-30 15:24:07 +00:00
|
|
|
"net/http/httptest"
|
2016-06-06 13:27:16 +00:00
|
|
|
"testing"
|
2017-04-17 20:47:53 +00:00
|
|
|
|
2017-05-28 03:50:03 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2020-09-16 13:46:04 +00:00
|
|
|
"github.com/traefik/traefik/v2/pkg/middlewares/requestdecorator"
|
|
|
|
"github.com/traefik/traefik/v2/pkg/testhelpers"
|
2016-06-06 13:27:16 +00:00
|
|
|
)
|
|
|
|
|
2022-11-28 14:48:05 +00:00
|
|
|
func TestMuxer(t *testing.T) {
|
2019-01-30 15:24:07 +00:00
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
rule string
|
|
|
|
headers map[string]string
|
2021-06-07 16:14:09 +00:00
|
|
|
remoteAddr string
|
2019-01-30 15:24:07 +00:00
|
|
|
expected map[string]int
|
|
|
|
expectedError bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "no tree",
|
|
|
|
expectedError: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "Rule with no matcher",
|
|
|
|
rule: "rulewithnotmatcher",
|
|
|
|
expectedError: true,
|
|
|
|
},
|
2020-12-17 09:06:03 +00:00
|
|
|
{
|
2022-11-28 14:48:05 +00:00
|
|
|
desc: "Rule without quote",
|
|
|
|
rule: "Host(example.com)",
|
2021-03-22 20:16:04 +00:00
|
|
|
expectedError: true,
|
|
|
|
},
|
2019-01-30 15:24:07 +00:00
|
|
|
{
|
|
|
|
desc: "Host and PathPrefix",
|
2022-11-28 14:48:05 +00:00
|
|
|
rule: "Host(`localhost`) && PathPrefix(`/css`)",
|
2019-01-30 15:24:07 +00:00
|
|
|
expected: map[string]int{
|
2022-11-28 14:48:05 +00:00
|
|
|
"https://localhost/css": http.StatusOK,
|
|
|
|
"https://localhost/js": http.StatusNotFound,
|
2019-01-30 15:24:07 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "Rule with Host OR Host",
|
2022-11-28 14:48:05 +00:00
|
|
|
rule: "Host(`example.com`) || Host(`example.org`)",
|
2019-01-30 15:24:07 +00:00
|
|
|
expected: map[string]int{
|
2022-11-28 14:48:05 +00:00
|
|
|
"https://example.com/css": http.StatusOK,
|
|
|
|
"https://example.org/js": http.StatusOK,
|
|
|
|
"https://example.eu/html": http.StatusNotFound,
|
2019-01-30 15:24:07 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "Rule with host OR (host AND path)",
|
2022-11-28 14:48:05 +00:00
|
|
|
rule: `Host("example.com") || (Host("example.org") && Path("/css"))`,
|
2019-01-30 15:24:07 +00:00
|
|
|
expected: map[string]int{
|
2022-11-28 14:48:05 +00:00
|
|
|
"https://example.com/css": http.StatusOK,
|
|
|
|
"https://example.com/js": http.StatusOK,
|
|
|
|
"https://example.org/css": http.StatusOK,
|
|
|
|
"https://example.org/js": http.StatusNotFound,
|
|
|
|
"https://example.eu/css": http.StatusNotFound,
|
2019-01-30 15:24:07 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "Rule with host OR host AND path",
|
2022-11-28 14:48:05 +00:00
|
|
|
rule: `Host("example.com") || Host("example.org") && Path("/css")`,
|
2019-01-30 15:24:07 +00:00
|
|
|
expected: map[string]int{
|
2022-11-28 14:48:05 +00:00
|
|
|
"https://example.com/css": http.StatusOK,
|
|
|
|
"https://example.com/js": http.StatusOK,
|
|
|
|
"https://example.org/css": http.StatusOK,
|
|
|
|
"https://example.org/js": http.StatusNotFound,
|
|
|
|
"https://example.eu/css": http.StatusNotFound,
|
2019-01-30 15:24:07 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "Rule with (host OR host) AND path",
|
2022-11-28 14:48:05 +00:00
|
|
|
rule: `(Host("example.com") || Host("example.org")) && Path("/css")`,
|
2019-01-30 15:24:07 +00:00
|
|
|
expected: map[string]int{
|
2022-11-28 14:48:05 +00:00
|
|
|
"https://example.com/css": http.StatusOK,
|
|
|
|
"https://example.com/js": http.StatusNotFound,
|
|
|
|
"https://example.org/css": http.StatusOK,
|
|
|
|
"https://example.org/js": http.StatusNotFound,
|
|
|
|
"https://example.eu/css": http.StatusNotFound,
|
2019-01-30 15:24:07 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "Rule with (host AND path) OR (host AND path)",
|
2022-11-28 14:48:05 +00:00
|
|
|
rule: `(Host("example.com") && Path("/js")) || ((Host("example.org")) && Path("/css"))`,
|
2019-01-30 15:24:07 +00:00
|
|
|
expected: map[string]int{
|
2022-11-28 14:48:05 +00:00
|
|
|
"https://example.com/css": http.StatusNotFound,
|
|
|
|
"https://example.com/js": http.StatusOK,
|
|
|
|
"https://example.org/css": http.StatusOK,
|
|
|
|
"https://example.org/js": http.StatusNotFound,
|
|
|
|
"https://example.eu/css": http.StatusNotFound,
|
2019-01-30 15:24:07 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "Rule case UPPER",
|
2022-11-28 14:48:05 +00:00
|
|
|
rule: `PATHPREFIX("/css")`,
|
2019-01-30 15:24:07 +00:00
|
|
|
expected: map[string]int{
|
2022-11-28 14:48:05 +00:00
|
|
|
"https://example.com/css": http.StatusOK,
|
|
|
|
"https://example.com/js": http.StatusNotFound,
|
2019-01-30 15:24:07 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "Rule case lower",
|
2022-11-28 14:48:05 +00:00
|
|
|
rule: `pathprefix("/css")`,
|
2019-01-30 15:24:07 +00:00
|
|
|
expected: map[string]int{
|
2022-11-28 14:48:05 +00:00
|
|
|
"https://example.com/css": http.StatusOK,
|
|
|
|
"https://example.com/js": http.StatusNotFound,
|
2019-01-30 15:24:07 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "Rule case CamelCase",
|
2022-11-28 14:48:05 +00:00
|
|
|
rule: `PathPrefix("/css")`,
|
2019-01-30 15:24:07 +00:00
|
|
|
expected: map[string]int{
|
2022-11-28 14:48:05 +00:00
|
|
|
"https://example.com/css": http.StatusOK,
|
|
|
|
"https://example.com/js": http.StatusNotFound,
|
2019-01-30 15:24:07 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "Rule case Title",
|
2022-11-28 14:48:05 +00:00
|
|
|
rule: `Pathprefix("/css")`,
|
2019-01-30 15:24:07 +00:00
|
|
|
expected: map[string]int{
|
2022-11-28 14:48:05 +00:00
|
|
|
"https://example.com/css": http.StatusOK,
|
|
|
|
"https://example.com/js": http.StatusNotFound,
|
2019-01-30 15:24:07 +00:00
|
|
|
},
|
|
|
|
},
|
2021-05-31 16:58:05 +00:00
|
|
|
{
|
|
|
|
desc: "Rule with not",
|
2022-11-28 14:48:05 +00:00
|
|
|
rule: `!Host("example.com")`,
|
2021-05-31 16:58:05 +00:00
|
|
|
expected: map[string]int{
|
2022-11-28 14:48:05 +00:00
|
|
|
"https://example.org": http.StatusOK,
|
|
|
|
"https://example.com": http.StatusNotFound,
|
2021-05-31 16:58:05 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "Rule with not on multiple route with or",
|
2022-11-28 14:48:05 +00:00
|
|
|
rule: `!(Host("example.com") || Host("example.org"))`,
|
2021-05-31 16:58:05 +00:00
|
|
|
expected: map[string]int{
|
2022-11-28 14:48:05 +00:00
|
|
|
"https://example.eu/js": http.StatusOK,
|
|
|
|
"https://example.com/css": http.StatusNotFound,
|
|
|
|
"https://example.org/js": http.StatusNotFound,
|
2021-05-31 16:58:05 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "Rule with not on multiple route with and",
|
2022-11-28 14:48:05 +00:00
|
|
|
rule: `!(Host("example.com") && Path("/css"))`,
|
2021-05-31 16:58:05 +00:00
|
|
|
expected: map[string]int{
|
2022-11-28 14:48:05 +00:00
|
|
|
"https://example.com/js": http.StatusOK,
|
|
|
|
"https://example.eu/css": http.StatusOK,
|
|
|
|
"https://example.com/css": http.StatusNotFound,
|
2021-05-31 16:58:05 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-10-04 18:38:09 +00:00
|
|
|
desc: "Rule with not on multiple route with and another not",
|
2022-11-28 14:48:05 +00:00
|
|
|
rule: `!(Host("example.com") && !Path("/css"))`,
|
2021-05-31 16:58:05 +00:00
|
|
|
expected: map[string]int{
|
2022-11-28 14:48:05 +00:00
|
|
|
"https://example.com/css": http.StatusOK,
|
|
|
|
"https://example.org/css": http.StatusOK,
|
|
|
|
"https://example.com/js": http.StatusNotFound,
|
2021-05-31 16:58:05 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "Rule with not on two rule",
|
2022-11-28 14:48:05 +00:00
|
|
|
rule: `!Host("example.com") || !Path("/css")`,
|
2021-05-31 16:58:05 +00:00
|
|
|
expected: map[string]int{
|
2022-11-28 14:48:05 +00:00
|
|
|
"https://example.com/js": http.StatusOK,
|
|
|
|
"https://example.org/css": http.StatusOK,
|
|
|
|
"https://example.com/css": http.StatusNotFound,
|
2021-05-31 16:58:05 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "Rule case with double not",
|
2022-11-28 14:48:05 +00:00
|
|
|
rule: `!(!(Host("example.com") && Pathprefix("/css")))`,
|
2021-05-31 16:58:05 +00:00
|
|
|
expected: map[string]int{
|
2022-11-28 14:48:05 +00:00
|
|
|
"https://example.com/css": http.StatusOK,
|
|
|
|
"https://example.com/js": http.StatusNotFound,
|
|
|
|
"https://example.org/css": http.StatusNotFound,
|
2021-05-31 16:58:05 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "Rule case with not domain",
|
2022-11-28 14:48:05 +00:00
|
|
|
rule: `!Host("example.com") && Pathprefix("/css")`,
|
2021-05-31 16:58:05 +00:00
|
|
|
expected: map[string]int{
|
2022-11-28 14:48:05 +00:00
|
|
|
"https://example.org/css": http.StatusOK,
|
|
|
|
"https://example.org/js": http.StatusNotFound,
|
|
|
|
"https://example.com/css": http.StatusNotFound,
|
|
|
|
"https://example.com/js": http.StatusNotFound,
|
2021-05-31 16:58:05 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "Rule with multiple host AND multiple path AND not",
|
2022-11-28 14:48:05 +00:00
|
|
|
rule: `!(Host("example.com") && Path("/js"))`,
|
2021-06-07 16:14:09 +00:00
|
|
|
expected: map[string]int{
|
2022-11-28 14:48:05 +00:00
|
|
|
"https://example.com/js": http.StatusNotFound,
|
|
|
|
"https://example.com/html": http.StatusOK,
|
|
|
|
"https://example.org/js": http.StatusOK,
|
|
|
|
"https://example.com/css": http.StatusOK,
|
|
|
|
"https://example.org/css": http.StatusOK,
|
|
|
|
"https://example.org/html": http.StatusOK,
|
|
|
|
"https://example.eu/images": http.StatusOK,
|
2021-06-07 16:14:09 +00:00
|
|
|
},
|
|
|
|
},
|
2018-07-09 13:08:04 +00:00
|
|
|
}
|
2016-06-06 13:27:16 +00:00
|
|
|
|
2019-01-30 15:24:07 +00:00
|
|
|
for _, test := range testCases {
|
|
|
|
test := test
|
2021-05-31 16:58:05 +00:00
|
|
|
|
2019-01-30 15:24:07 +00:00
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
2016-06-06 13:27:16 +00:00
|
|
|
|
2022-03-17 17:02:08 +00:00
|
|
|
muxer, err := NewMuxer()
|
2019-01-30 15:24:07 +00:00
|
|
|
require.NoError(t, err)
|
2016-06-06 13:27:16 +00:00
|
|
|
|
2022-11-28 14:48:05 +00:00
|
|
|
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
|
2022-03-17 17:02:08 +00:00
|
|
|
err = muxer.AddRoute(test.rule, 0, handler)
|
2019-01-30 15:24:07 +00:00
|
|
|
if test.expectedError {
|
|
|
|
require.Error(t, err)
|
2022-11-28 14:48:05 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
require.NoError(t, err)
|
2016-06-06 13:27:16 +00:00
|
|
|
|
2022-11-28 14:48:05 +00:00
|
|
|
// RequestDecorator is necessary for the host rule
|
|
|
|
reqHost := requestdecorator.New(nil)
|
2016-11-17 14:36:10 +00:00
|
|
|
|
2022-11-28 14:48:05 +00:00
|
|
|
results := make(map[string]int)
|
|
|
|
for calledURL := range test.expected {
|
|
|
|
req := testhelpers.MustNewRequest(http.MethodGet, calledURL, http.NoBody)
|
2021-06-07 16:14:09 +00:00
|
|
|
|
2022-11-28 14:48:05 +00:00
|
|
|
// Useful for the ClientIP matcher
|
|
|
|
req.RemoteAddr = test.remoteAddr
|
2021-06-07 16:14:09 +00:00
|
|
|
|
2022-11-28 14:48:05 +00:00
|
|
|
for key, value := range test.headers {
|
|
|
|
req.Header.Set(key, value)
|
2019-01-30 15:24:07 +00:00
|
|
|
}
|
2022-11-28 14:48:05 +00:00
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
reqHost.ServeHTTP(w, req, muxer.ServeHTTP)
|
|
|
|
results[calledURL] = w.Code
|
2019-01-30 15:24:07 +00:00
|
|
|
}
|
2022-11-28 14:48:05 +00:00
|
|
|
|
|
|
|
assert.Equal(t, test.expected, results)
|
2019-01-30 15:24:07 +00:00
|
|
|
})
|
|
|
|
}
|
2016-06-06 13:27:16 +00:00
|
|
|
}
|
2016-06-06 12:54:45 +00:00
|
|
|
|
2019-01-30 15:24:07 +00:00
|
|
|
func Test_addRoutePriority(t *testing.T) {
|
|
|
|
type Case struct {
|
|
|
|
xFrom string
|
|
|
|
rule string
|
|
|
|
priority int
|
|
|
|
}
|
2017-05-28 03:50:03 +00:00
|
|
|
|
2019-01-30 15:24:07 +00:00
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
path string
|
|
|
|
cases []Case
|
|
|
|
expected string
|
2017-05-28 03:50:03 +00:00
|
|
|
}{
|
|
|
|
{
|
2019-01-30 15:24:07 +00:00
|
|
|
desc: "Higher priority on second rule",
|
|
|
|
path: "/my",
|
|
|
|
cases: []Case{
|
|
|
|
{
|
|
|
|
xFrom: "header1",
|
|
|
|
rule: "PathPrefix(`/my`)",
|
|
|
|
priority: 10,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
xFrom: "header2",
|
|
|
|
rule: "PathPrefix(`/my`)",
|
|
|
|
priority: 20,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: "header2",
|
2017-05-28 03:50:03 +00:00
|
|
|
},
|
|
|
|
{
|
2019-01-30 15:24:07 +00:00
|
|
|
desc: "Higher priority on first rule",
|
|
|
|
path: "/my",
|
|
|
|
cases: []Case{
|
|
|
|
{
|
|
|
|
xFrom: "header1",
|
|
|
|
rule: "PathPrefix(`/my`)",
|
|
|
|
priority: 20,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
xFrom: "header2",
|
|
|
|
rule: "PathPrefix(`/my`)",
|
|
|
|
priority: 10,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: "header1",
|
2017-05-28 03:50:03 +00:00
|
|
|
},
|
|
|
|
{
|
2019-01-30 15:24:07 +00:00
|
|
|
desc: "Higher priority on second rule with different rule",
|
|
|
|
path: "/mypath",
|
|
|
|
cases: []Case{
|
|
|
|
{
|
|
|
|
xFrom: "header1",
|
|
|
|
rule: "PathPrefix(`/mypath`)",
|
|
|
|
priority: 10,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
xFrom: "header2",
|
|
|
|
rule: "PathPrefix(`/my`)",
|
|
|
|
priority: 20,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: "header2",
|
2017-05-28 03:50:03 +00:00
|
|
|
},
|
|
|
|
{
|
2019-01-30 15:24:07 +00:00
|
|
|
desc: "Higher priority on longest rule (longest first)",
|
|
|
|
path: "/mypath",
|
|
|
|
cases: []Case{
|
|
|
|
{
|
|
|
|
xFrom: "header1",
|
|
|
|
rule: "PathPrefix(`/mypath`)",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
xFrom: "header2",
|
|
|
|
rule: "PathPrefix(`/my`)",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: "header1",
|
2018-09-04 15:14:04 +00:00
|
|
|
},
|
|
|
|
{
|
2019-01-30 15:24:07 +00:00
|
|
|
desc: "Higher priority on longest rule (longest second)",
|
|
|
|
path: "/mypath",
|
|
|
|
cases: []Case{
|
|
|
|
{
|
|
|
|
xFrom: "header1",
|
|
|
|
rule: "PathPrefix(`/my`)",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
xFrom: "header2",
|
|
|
|
rule: "PathPrefix(`/mypath`)",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: "header2",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "Higher priority on longest rule (longest third)",
|
|
|
|
path: "/mypath",
|
|
|
|
cases: []Case{
|
|
|
|
{
|
|
|
|
xFrom: "header1",
|
|
|
|
rule: "PathPrefix(`/my`)",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
xFrom: "header2",
|
|
|
|
rule: "PathPrefix(`/mypa`)",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
xFrom: "header3",
|
|
|
|
rule: "PathPrefix(`/mypath`)",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: "header3",
|
2017-05-28 03:50:03 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-01-30 15:24:07 +00:00
|
|
|
for _, test := range testCases {
|
2017-05-28 03:50:03 +00:00
|
|
|
test := test
|
2019-01-30 15:24:07 +00:00
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
2017-05-28 03:50:03 +00:00
|
|
|
t.Parallel()
|
2022-03-17 17:02:08 +00:00
|
|
|
muxer, err := NewMuxer()
|
2019-01-30 15:24:07 +00:00
|
|
|
require.NoError(t, err)
|
2017-05-28 03:50:03 +00:00
|
|
|
|
2019-01-30 15:24:07 +00:00
|
|
|
for _, route := range test.cases {
|
|
|
|
route := route
|
|
|
|
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("X-From", route.xFrom)
|
|
|
|
})
|
2018-09-04 15:14:04 +00:00
|
|
|
|
2022-03-17 17:02:08 +00:00
|
|
|
err := muxer.AddRoute(route.rule, route.priority, handler)
|
2019-01-30 15:24:07 +00:00
|
|
|
require.NoError(t, err, route.rule)
|
2018-09-04 15:14:04 +00:00
|
|
|
}
|
2017-05-28 03:50:03 +00:00
|
|
|
|
2019-01-30 15:24:07 +00:00
|
|
|
w := httptest.NewRecorder()
|
2022-11-28 14:48:05 +00:00
|
|
|
req := testhelpers.MustNewRequest(http.MethodGet, test.path, http.NoBody)
|
2017-05-28 03:50:03 +00:00
|
|
|
|
2022-03-17 17:02:08 +00:00
|
|
|
muxer.ServeHTTP(w, req)
|
2016-06-06 12:54:45 +00:00
|
|
|
|
2019-01-30 15:24:07 +00:00
|
|
|
assert.Equal(t, test.expected, w.Header().Get("X-From"))
|
|
|
|
})
|
|
|
|
}
|
2016-06-06 12:54:45 +00:00
|
|
|
}
|
|
|
|
|
2019-01-30 15:24:07 +00:00
|
|
|
func TestParseDomains(t *testing.T) {
|
2017-12-19 16:00:12 +00:00
|
|
|
testCases := []struct {
|
2019-01-30 15:24:07 +00:00
|
|
|
description string
|
|
|
|
expression string
|
|
|
|
domain []string
|
|
|
|
errorExpected bool
|
2017-12-19 16:00:12 +00:00
|
|
|
}{
|
|
|
|
{
|
2022-03-17 17:02:08 +00:00
|
|
|
description: "Unknown rule",
|
|
|
|
expression: "Foobar(`foo.bar`,`test.bar`)",
|
|
|
|
errorExpected: true,
|
2017-12-19 16:00:12 +00:00
|
|
|
},
|
|
|
|
{
|
2022-03-17 17:02:08 +00:00
|
|
|
description: "No host rule",
|
|
|
|
expression: "Path(`/test`)",
|
2017-12-19 16:00:12 +00:00
|
|
|
},
|
|
|
|
{
|
2022-03-17 17:02:08 +00:00
|
|
|
description: "Host rule and another rule",
|
|
|
|
expression: "Host(`foo.bar`) && Path(`/test`)",
|
|
|
|
domain: []string{"foo.bar"},
|
2019-01-30 15:24:07 +00:00
|
|
|
},
|
2022-11-28 14:48:05 +00:00
|
|
|
{
|
|
|
|
description: "Host rule to trim and another rule",
|
|
|
|
expression: "Host(`Foo.Bar`) || Host(`bar.buz`) && Path(`/test`)",
|
|
|
|
domain: []string{"foo.bar", "bar.buz"},
|
|
|
|
},
|
2019-01-30 15:24:07 +00:00
|
|
|
{
|
2022-03-17 17:02:08 +00:00
|
|
|
description: "Host rule to trim and another rule",
|
|
|
|
expression: "Host(`Foo.Bar`) && Path(`/test`)",
|
|
|
|
domain: []string{"foo.bar"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "Host rule with no domain",
|
|
|
|
expression: "Host() && Path(`/test`)",
|
2017-12-19 16:00:12 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
test := test
|
2019-01-30 15:24:07 +00:00
|
|
|
t.Run(test.expression, func(t *testing.T) {
|
2017-12-19 16:00:12 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2019-01-30 15:24:07 +00:00
|
|
|
domains, err := ParseDomains(test.expression)
|
2017-12-19 16:00:12 +00:00
|
|
|
|
2019-01-30 15:24:07 +00:00
|
|
|
if test.errorExpected {
|
|
|
|
require.Errorf(t, err, "unable to parse correctly the domains in the Host rule from %q", test.expression)
|
|
|
|
} else {
|
|
|
|
require.NoError(t, err, "%s: Error while parsing domain.", test.expression)
|
2017-12-19 16:00:12 +00:00
|
|
|
}
|
2019-01-30 15:24:07 +00:00
|
|
|
|
|
|
|
assert.EqualValues(t, test.domain, domains, "%s: Error parsing domains from expression.", test.expression)
|
2017-12-19 16:00:12 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-06-27 13:16:08 +00:00
|
|
|
|
2022-11-28 14:48:05 +00:00
|
|
|
// TestEmptyHost is a non regression test for
|
|
|
|
// https://github.com/traefik/traefik/pull/9131
|
|
|
|
func TestEmptyHost(t *testing.T) {
|
2022-06-27 13:16:08 +00:00
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
request string
|
|
|
|
rule string
|
|
|
|
expected int
|
|
|
|
}{
|
|
|
|
{
|
2022-11-28 14:48:05 +00:00
|
|
|
desc: "HostRegexp with absolute-form URL with empty host with non-matching host header",
|
|
|
|
request: "GET http://@/ HTTP/1.1\r\nHost: example.com\r\n\r\n",
|
|
|
|
rule: "HostRegexp(`example.com`)",
|
|
|
|
expected: http.StatusOK,
|
2022-06-27 13:16:08 +00:00
|
|
|
},
|
|
|
|
{
|
2022-11-28 14:48:05 +00:00
|
|
|
desc: "Host with absolute-form URL with empty host with non-matching host header",
|
|
|
|
request: "GET http://@/ HTTP/1.1\r\nHost: example.com\r\n\r\n",
|
|
|
|
rule: "Host(`example.com`)",
|
|
|
|
expected: http.StatusOK,
|
2022-06-27 13:16:08 +00:00
|
|
|
},
|
|
|
|
{
|
2022-11-28 14:48:05 +00:00
|
|
|
desc: "HostRegexp with absolute-form URL with matching host header",
|
|
|
|
request: "GET http://example.com/ HTTP/1.1\r\nHost: example.org\r\n\r\n",
|
|
|
|
rule: "HostRegexp(`example.com`)",
|
|
|
|
expected: http.StatusOK,
|
2022-06-27 13:16:08 +00:00
|
|
|
},
|
|
|
|
{
|
2022-11-28 14:48:05 +00:00
|
|
|
desc: "Host with absolute-form URL with matching host header",
|
|
|
|
request: "GET http://example.com/ HTTP/1.1\r\nHost: example.org\r\n\r\n",
|
|
|
|
rule: "Host(`example.com`)",
|
|
|
|
expected: http.StatusOK,
|
2022-06-27 13:16:08 +00:00
|
|
|
},
|
|
|
|
{
|
2022-11-28 14:48:05 +00:00
|
|
|
desc: "HostRegexp with absolute-form URL with non-matching host header",
|
|
|
|
request: "GET http://example.com/ HTTP/1.1\r\nHost: example.org\r\n\r\n",
|
|
|
|
rule: "HostRegexp(`example.org`)",
|
|
|
|
expected: http.StatusNotFound,
|
2022-06-27 13:16:08 +00:00
|
|
|
},
|
|
|
|
{
|
2022-11-28 14:48:05 +00:00
|
|
|
desc: "Host with absolute-form URL with non-matching host header",
|
|
|
|
request: "GET http://example.com/ HTTP/1.1\r\nHost: example.org\r\n\r\n",
|
|
|
|
rule: "Host(`example.org`)",
|
|
|
|
expected: http.StatusNotFound,
|
2022-06-27 13:16:08 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
test := test
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
|
|
|
|
muxer, err := NewMuxer()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = muxer.AddRoute(test.rule, 0, handler)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// RequestDecorator is necessary for the host rule
|
|
|
|
reqHost := requestdecorator.New(nil)
|
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
req, err := http.ReadRequest(bufio.NewReader(bytes.NewReader([]byte(test.request))))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
reqHost.ServeHTTP(w, req, muxer.ServeHTTP)
|
|
|
|
assert.Equal(t, test.expected, w.Code)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|