traefik/pkg/api/handler_test.go

282 lines
6.8 KiB
Go
Raw Normal View History

2018-11-14 09:18:03 +00:00
package api
import (
"encoding/json"
"flag"
2021-03-04 19:08:03 +00:00
"io"
2018-11-14 09:18:03 +00:00
"net/http"
"net/http/httptest"
2021-03-04 19:08:03 +00:00
"os"
2018-11-14 09:18:03 +00:00
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
2023-02-03 14:24:05 +00:00
"github.com/traefik/traefik/v3/pkg/config/dynamic"
"github.com/traefik/traefik/v3/pkg/config/runtime"
"github.com/traefik/traefik/v3/pkg/config/static"
2018-11-14 09:18:03 +00:00
)
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{
2024-11-12 09:56:06 +00:00
PassHostHeader: pointer(true),
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.TCPServersLoadBalancer{
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 {
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)
server := httptest.NewServer(handler.createRouter())
2018-11-14 09:18:03 +00:00
resp, err := http.DefaultClient.Get(server.URL + test.path)
require.NoError(t, err)
assert.Equal(t, test.expected.statusCode, resp.StatusCode)
2023-11-17 00:50:06 +00:00
assert.Equal(t, "application/json", resp.Header.Get("Content-Type"))
2018-11-14 09:18:03 +00:00
2021-03-04 19:08:03 +00:00
contents, err := io.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)
2021-03-04 19:08:03 +00:00
err = os.WriteFile(test.expected.json, newJSON, 0o644)
require.NoError(t, err)
}
2021-03-04 19:08:03 +00:00
data, err := os.ReadFile(test.expected.json)
require.NoError(t, err)
assert.JSONEq(t, string(data), string(contents))
2018-11-14 09:18:03 +00:00
})
}
}
func TestHandler_GetMiddleware(t *testing.T) {
testCases := []struct {
desc string
middlewareName string
conf runtime.Configuration
expectedStatus int
expected interface{}
}{
{
desc: "Middleware not found",
middlewareName: "auth@myprovider",
conf: runtime.Configuration{
Middlewares: map[string]*runtime.MiddlewareInfo{},
},
expectedStatus: http.StatusNotFound,
},
{
desc: "Get middleware",
middlewareName: "auth@myprovider",
conf: runtime.Configuration{
Middlewares: map[string]*runtime.MiddlewareInfo{
"auth@myprovider": {
Middleware: &dynamic.Middleware{
BasicAuth: &dynamic.BasicAuth{
Users: []string{"admin:admin"},
},
},
},
},
},
expectedStatus: http.StatusOK,
expected: middlewareRepresentation{
MiddlewareInfo: &runtime.MiddlewareInfo{
Middleware: &dynamic.Middleware{
BasicAuth: &dynamic.BasicAuth{
Users: []string{"admin:admin"},
},
},
},
Name: "auth@myprovider",
Provider: "myprovider",
Type: "basicauth",
},
},
{
desc: "Get plugin middleware",
middlewareName: "myplugin@myprovider",
conf: runtime.Configuration{
Middlewares: map[string]*runtime.MiddlewareInfo{
"myplugin@myprovider": {
Middleware: &dynamic.Middleware{
Plugin: map[string]dynamic.PluginConf{
"mysuperplugin": {
"foo": "bar",
},
},
},
},
},
},
expectedStatus: http.StatusOK,
expected: middlewareRepresentation{
MiddlewareInfo: &runtime.MiddlewareInfo{
Middleware: &dynamic.Middleware{
Plugin: map[string]dynamic.PluginConf{
"mysuperplugin": {
"foo": "bar",
},
},
},
},
Name: "myplugin@myprovider",
Provider: "myprovider",
Type: "mysuperplugin",
},
},
}
for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
t.Parallel()
handler := New(static.Configuration{API: &static.API{}, Global: &static.Global{}}, &test.conf)
server := httptest.NewServer(handler.createRouter())
resp, err := http.DefaultClient.Get(server.URL + "/api/http/middlewares/" + test.middlewareName)
require.NoError(t, err)
assert.Equal(t, test.expectedStatus, resp.StatusCode)
if test.expected == nil {
return
}
2021-03-04 19:08:03 +00:00
data, err := io.ReadAll(resp.Body)
require.NoError(t, err)
err = resp.Body.Close()
require.NoError(t, err)
expected, err := json.Marshal(test.expected)
require.NoError(t, err)
assert.JSONEq(t, string(expected), string(data))
})
}
}