traefik/pkg/api/handler_test.go

177 lines
4.3 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"
2019-08-03 01:58:23 +00:00
"github.com/containous/traefik/v2/pkg/config/dynamic"
"github.com/containous/traefik/v2/pkg/config/runtime"
"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"
"github.com/stretchr/testify/require"
)
var updateExpected = flag.Bool("update_expected", false, "Update expected files in testdata")
2019-07-12 09:10:03 +00:00
func TestHandler_RawData(t *testing.T) {
2018-11-14 09:18:03 +00:00
type expected struct {
statusCode int
json string
2018-11-14 09:18:03 +00:00
}
testCases := []struct {
desc string
path string
conf runtime.Configuration
expected expected
2018-11-14 09:18:03 +00:00
}{
{
desc: "Get rawdata",
path: "/api/rawdata",
conf: runtime.Configuration{
Services: map[string]*runtime.ServiceInfo{
"foo-service@myprovider": {
Service: &dynamic.Service{
LoadBalancer: &dynamic.ServersLoadBalancer{
Servers: []dynamic.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]*runtime.MiddlewareInfo{
"auth@myprovider": {
Middleware: &dynamic.Middleware{
BasicAuth: &dynamic.BasicAuth{
Users: []string{"admin:admin"},
},
},
},
"addPrefixTest@myprovider": {
Middleware: &dynamic.Middleware{
AddPrefix: &dynamic.AddPrefix{
Prefix: "/titi",
2018-11-14 09:18:03 +00:00
},
},
},
"addPrefixTest@anotherprovider": {
Middleware: &dynamic.Middleware{
AddPrefix: &dynamic.AddPrefix{
Prefix: "/toto",
},
},
2018-11-14 09:18:03 +00:00
},
},
Routers: map[string]*runtime.RouterInfo{
"bar@myprovider": {
Router: &dynamic.Router{
EntryPoints: []string{"web"},
Service: "foo-service@myprovider",
Rule: "Host(`foo.bar`)",
Middlewares: []string{"auth", "addPrefixTest@anotherprovider"},
},
2018-11-14 09:18:03 +00:00
},
"test@myprovider": {
Router: &dynamic.Router{
EntryPoints: []string{"web"},
Service: "foo-service@myprovider",
Rule: "Host(`foo.bar.other`)",
Middlewares: []string{"addPrefixTest", "auth"},
2018-11-14 09:18:03 +00:00
},
},
},
TCPServices: map[string]*runtime.TCPServiceInfo{
"tcpfoo-service@myprovider": {
TCPService: &dynamic.TCPService{
LoadBalancer: &dynamic.TCPLoadBalancerService{
Servers: []dynamic.TCPServer{
{
Address: "127.0.0.1",
},
},
2018-11-14 09:18:03 +00:00
},
},
},
},
TCPRouters: map[string]*runtime.TCPRouterInfo{
"tcpbar@myprovider": {
TCPRouter: &dynamic.TCPRouter{
EntryPoints: []string{"web"},
Service: "tcpfoo-service@myprovider",
Rule: "HostSNI(`foo.bar`)",
2018-11-14 09:18:03 +00:00
},
},
"tcptest@myprovider": {
TCPRouter: &dynamic.TCPRouter{
EntryPoints: []string{"web"},
Service: "tcpfoo-service@myprovider",
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()
// TODO: server status
rtConf := &test.conf
rtConf.PopulateUsedBy()
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)
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)
2019-06-25 15:44:03 +00:00
assert.Equal(t, resp.Header.Get("Content-Type"), "application/json")
2018-11-14 09:18:03 +00:00
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
})
}
}