traefik/pkg/server/router/route_appender_aggregator_test.go

80 lines
1.7 KiB
Go
Raw Normal View History

2018-11-14 09:18:03 +00:00
package router
import (
"context"
"net/http"
"net/http/httptest"
"testing"
2019-08-03 01:58:23 +00:00
"github.com/containous/traefik/v2/pkg/config/static"
"github.com/gorilla/mux"
2018-11-14 09:18:03 +00:00
"github.com/stretchr/testify/assert"
)
func TestNewRouteAppenderAggregator(t *testing.T) {
testCases := []struct {
desc string
staticConf static.Configuration
expected map[string]int
}{
{
2019-09-06 13:08:04 +00:00
desc: "Secure API",
2018-11-14 09:18:03 +00:00
staticConf: static.Configuration{
Global: &static.Global{},
2019-09-06 13:08:04 +00:00
API: &static.API{
Insecure: false,
2018-11-14 09:18:03 +00:00
},
EntryPoints: static.EntryPoints{
"traefik": {},
2018-11-14 09:18:03 +00:00
},
},
expected: map[string]int{
2019-09-06 13:08:04 +00:00
"/api/providers": http.StatusBadGateway,
2018-11-14 09:18:03 +00:00
},
},
{
2019-09-06 13:08:04 +00:00
desc: "Insecure API",
2018-11-14 09:18:03 +00:00
staticConf: static.Configuration{
Global: &static.Global{},
2019-09-06 13:08:04 +00:00
API: &static.API{
Insecure: true,
2018-11-14 09:18:03 +00:00
},
EntryPoints: static.EntryPoints{
"traefik": {},
2018-11-14 09:18:03 +00:00
},
},
expected: map[string]int{
2019-09-06 13:08:04 +00:00
"/api/rawdata": http.StatusOK,
2018-11-14 09:18:03 +00:00
},
},
}
for _, test := range testCases {
test := test
t.Run(test.desc, func(t *testing.T) {
t.Parallel()
ctx := context.Background()
2019-09-06 13:08:04 +00:00
router := NewRouteAppenderAggregator(ctx, test.staticConf, "traefik", nil)
2018-11-14 09:18:03 +00:00
internalMuxRouter := mux.NewRouter()
router.Append(internalMuxRouter)
internalMuxRouter.NotFoundHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusBadGateway)
})
actual := make(map[string]int)
for calledURL := range test.expected {
recorder := httptest.NewRecorder()
request := httptest.NewRequest(http.MethodGet, calledURL, nil)
internalMuxRouter.ServeHTTP(recorder, request)
actual[calledURL] = recorder.Code
}
assert.Equal(t, test.expected, actual)
})
}
}