traefik/pkg/api/handler_test.go

173 lines
4.1 KiB
Go
Raw Normal View History

2018-11-14 09:18:03 +00:00
package api
import (
"encoding/json"
"flag"
2018-11-14 09:18:03 +00:00
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"github.com/containous/mux"
2019-03-15 08:42:03 +00:00
"github.com/containous/traefik/pkg/config"
"github.com/containous/traefik/pkg/config/static"
2018-11-14 09:18:03 +00:00
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var updateExpected = flag.Bool("update_expected", false, "Update expected files in testdata")
2018-11-14 09:18:03 +00:00
func TestHandler_Configuration(t *testing.T) {
type expected struct {
statusCode int
json string
2018-11-14 09:18:03 +00:00
}
testCases := []struct {
desc string
path string
conf config.RuntimeConfiguration
expected expected
2018-11-14 09:18:03 +00:00
}{
{
desc: "Get rawdata",
path: "/api/rawdata",
conf: config.RuntimeConfiguration{
Services: map[string]*config.ServiceInfo{
"myprovider.foo-service": {
Service: &config.Service{
LoadBalancer: &config.LoadBalancerService{
Servers: []config.Server{
{
2019-06-05 20:18:06 +00:00
URL: "http://127.0.0.1",
},
},
},
},
2018-11-14 09:18:03 +00:00
},
},
Middlewares: map[string]*config.MiddlewareInfo{
"myprovider.auth": {
Middleware: &config.Middleware{
BasicAuth: &config.BasicAuth{
Users: []string{"admin:admin"},
},
},
},
"myprovider.addPrefixTest": {
Middleware: &config.Middleware{
AddPrefix: &config.AddPrefix{
Prefix: "/titi",
2018-11-14 09:18:03 +00:00
},
},
},
"anotherprovider.addPrefixTest": {
Middleware: &config.Middleware{
AddPrefix: &config.AddPrefix{
Prefix: "/toto",
},
},
2018-11-14 09:18:03 +00:00
},
},
Routers: map[string]*config.RouterInfo{
"myprovider.bar": {
Router: &config.Router{
EntryPoints: []string{"web"},
Service: "myprovider.foo-service",
Rule: "Host(`foo.bar`)",
Middlewares: []string{"auth", "anotherprovider.addPrefixTest"},
},
2018-11-14 09:18:03 +00:00
},
"myprovider.test": {
Router: &config.Router{
EntryPoints: []string{"web"},
Service: "myprovider.foo-service",
Rule: "Host(`foo.bar.other`)",
Middlewares: []string{"addPrefixTest", "auth"},
2018-11-14 09:18:03 +00:00
},
},
},
TCPServices: map[string]*config.TCPServiceInfo{
"myprovider.tcpfoo-service": {
TCPService: &config.TCPService{
LoadBalancer: &config.TCPLoadBalancerService{
Servers: []config.TCPServer{
{
Address: "127.0.0.1",
},
},
2018-11-14 09:18:03 +00:00
},
},
},
},
TCPRouters: map[string]*config.TCPRouterInfo{
"myprovider.tcpbar": {
TCPRouter: &config.TCPRouter{
EntryPoints: []string{"web"},
Service: "myprovider.tcpfoo-service",
Rule: "HostSNI(`foo.bar`)",
2018-11-14 09:18:03 +00:00
},
},
"myprovider.tcptest": {
TCPRouter: &config.TCPRouter{
EntryPoints: []string{"web"},
Service: "myprovider.tcpfoo-service",
Rule: "HostSNI(`foo.bar.other`)",
2018-11-14 09:18:03 +00:00
},
},
},
},
expected: expected{
statusCode: http.StatusOK,
json: "testdata/getrawdata.json",
2018-11-14 09:18:03 +00:00
},
},
}
for _, test := range testCases {
test := test
t.Run(test.desc, func(t *testing.T) {
t.Parallel()
rtConf := &test.conf
handler := New(static.Configuration{API: &static.API{}, Global: &static.Global{}}, rtConf)
2018-11-14 09:18:03 +00:00
router := mux.NewRouter()
handler.Append(router)
rtConf.PopulateUsedBy()
2018-11-14 09:18:03 +00:00
server := httptest.NewServer(router)
resp, err := http.DefaultClient.Get(server.URL + test.path)
require.NoError(t, err)
assert.Equal(t, test.expected.statusCode, resp.StatusCode)
contents, err := ioutil.ReadAll(resp.Body)
2018-11-14 09:18:03 +00:00
require.NoError(t, err)
2018-11-14 09:18:03 +00:00
err = resp.Body.Close()
require.NoError(t, err)
if test.expected.json == "" {
return
}
if *updateExpected {
var rtRepr RunTimeRepresentation
err := json.Unmarshal(contents, &rtRepr)
require.NoError(t, err)
newJSON, err := json.MarshalIndent(rtRepr, "", "\t")
require.NoError(t, err)
err = ioutil.WriteFile(test.expected.json, newJSON, 0644)
require.NoError(t, err)
}
data, err := ioutil.ReadFile(test.expected.json)
require.NoError(t, err)
assert.JSONEq(t, string(data), string(contents))
2018-11-14 09:18:03 +00:00
})
}
}