Move code to pkg
This commit is contained in:
parent
bd4c822670
commit
f1b085fa36
465 changed files with 656 additions and 680 deletions
62
pkg/middlewares/addprefix/add_prefix.go
Normal file
62
pkg/middlewares/addprefix/add_prefix.go
Normal file
|
@ -0,0 +1,62 @@
|
|||
package addprefix
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/containous/traefik/pkg/config"
|
||||
"github.com/containous/traefik/pkg/middlewares"
|
||||
"github.com/containous/traefik/pkg/tracing"
|
||||
"github.com/opentracing/opentracing-go/ext"
|
||||
)
|
||||
|
||||
const (
|
||||
typeName = "AddPrefix"
|
||||
)
|
||||
|
||||
// AddPrefix is a middleware used to add prefix to an URL request.
|
||||
type addPrefix struct {
|
||||
next http.Handler
|
||||
prefix string
|
||||
name string
|
||||
}
|
||||
|
||||
// New creates a new handler.
|
||||
func New(ctx context.Context, next http.Handler, config config.AddPrefix, name string) (http.Handler, error) {
|
||||
middlewares.GetLogger(ctx, name, typeName).Debug("Creating middleware")
|
||||
var result *addPrefix
|
||||
|
||||
if len(config.Prefix) > 0 {
|
||||
result = &addPrefix{
|
||||
prefix: config.Prefix,
|
||||
next: next,
|
||||
name: name,
|
||||
}
|
||||
} else {
|
||||
return nil, fmt.Errorf("prefix cannot be empty")
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (ap *addPrefix) GetTracingInformation() (string, ext.SpanKindEnum) {
|
||||
return ap.name, tracing.SpanKindNoneEnum
|
||||
}
|
||||
|
||||
func (ap *addPrefix) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
||||
logger := middlewares.GetLogger(req.Context(), ap.name, typeName)
|
||||
|
||||
oldURLPath := req.URL.Path
|
||||
req.URL.Path = ap.prefix + req.URL.Path
|
||||
logger.Debugf("URL.Path is now %s (was %s).", req.URL.Path, oldURLPath)
|
||||
|
||||
if req.URL.RawPath != "" {
|
||||
oldURLRawPath := req.URL.RawPath
|
||||
req.URL.RawPath = ap.prefix + req.URL.RawPath
|
||||
logger.Debugf("URL.RawPath is now %s (was %s).", req.URL.RawPath, oldURLRawPath)
|
||||
}
|
||||
req.RequestURI = req.URL.RequestURI()
|
||||
|
||||
ap.next.ServeHTTP(rw, req)
|
||||
}
|
104
pkg/middlewares/addprefix/add_prefix_test.go
Normal file
104
pkg/middlewares/addprefix/add_prefix_test.go
Normal file
|
@ -0,0 +1,104 @@
|
|||
package addprefix
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"github.com/containous/traefik/pkg/config"
|
||||
"github.com/containous/traefik/pkg/testhelpers"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestNewAddPrefix(t *testing.T) {
|
||||
testCases := []struct {
|
||||
desc string
|
||||
prefix config.AddPrefix
|
||||
expectsError bool
|
||||
}{
|
||||
{
|
||||
desc: "Works with a non empty prefix",
|
||||
prefix: config.AddPrefix{Prefix: "/a"},
|
||||
},
|
||||
{
|
||||
desc: "Fails if prefix is empty",
|
||||
prefix: config.AddPrefix{Prefix: ""},
|
||||
expectsError: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range testCases {
|
||||
test := test
|
||||
t.Run(test.desc, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
next := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
|
||||
|
||||
_, err := New(context.Background(), next, test.prefix, "foo-add-prefix")
|
||||
if test.expectsError {
|
||||
assert.Error(t, err)
|
||||
} else {
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAddPrefix(t *testing.T) {
|
||||
logrus.SetLevel(logrus.DebugLevel)
|
||||
testCases := []struct {
|
||||
desc string
|
||||
prefix config.AddPrefix
|
||||
path string
|
||||
expectedPath string
|
||||
expectedRawPath string
|
||||
}{
|
||||
{
|
||||
desc: "Works with a regular path",
|
||||
prefix: config.AddPrefix{Prefix: "/a"},
|
||||
path: "/b",
|
||||
expectedPath: "/a/b",
|
||||
},
|
||||
{
|
||||
desc: "Works with a raw path",
|
||||
prefix: config.AddPrefix{Prefix: "/a"},
|
||||
path: "/b%2Fc",
|
||||
expectedPath: "/a/b/c",
|
||||
expectedRawPath: "/a/b%2Fc",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range testCases {
|
||||
test := test
|
||||
t.Run(test.desc, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
var actualPath, actualRawPath, requestURI string
|
||||
|
||||
next := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
actualPath = r.URL.Path
|
||||
actualRawPath = r.URL.RawPath
|
||||
requestURI = r.RequestURI
|
||||
})
|
||||
|
||||
req := testhelpers.MustNewRequest(http.MethodGet, "http://localhost"+test.path, nil)
|
||||
|
||||
handler, err := New(context.Background(), next, test.prefix, "foo-add-prefix")
|
||||
require.NoError(t, err)
|
||||
|
||||
handler.ServeHTTP(nil, req)
|
||||
|
||||
assert.Equal(t, test.expectedPath, actualPath)
|
||||
assert.Equal(t, test.expectedRawPath, actualRawPath)
|
||||
|
||||
expectedURI := test.expectedPath
|
||||
if test.expectedRawPath != "" {
|
||||
// go HTTP uses the raw path when existent in the RequestURI
|
||||
expectedURI = test.expectedRawPath
|
||||
}
|
||||
assert.Equal(t, expectedURI, requestURI)
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue