1
0
mirror of https://github.com/containous/traefik.git synced 2024-12-22 13:34:03 +03:00
traefik/integration/rest_test.go

228 lines
5.9 KiB
Go
Raw Normal View History

2018-12-03 13:32:05 +03:00
package integration
import (
"bytes"
"encoding/json"
"net"
2018-12-03 13:32:05 +03:00
"net/http"
2019-09-06 16:08:04 +03:00
"strings"
"testing"
2018-12-03 13:32:05 +03:00
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
2023-02-03 17:24:05 +03:00
"github.com/traefik/traefik/v3/integration/try"
"github.com/traefik/traefik/v3/pkg/config/dynamic"
2018-12-03 13:32:05 +03:00
)
type RestSuite struct {
BaseSuite
whoamiAddr string
}
2018-12-03 13:32:05 +03:00
func TestRestSuite(t *testing.T) {
suite.Run(t, new(RestSuite))
}
func (s *RestSuite) SetupSuite() {
s.BaseSuite.SetupSuite()
s.createComposeProject("rest")
s.composeUp()
2018-12-03 13:32:05 +03:00
s.whoamiAddr = net.JoinHostPort(s.getComposeServiceIP("whoami1"), "80")
2018-12-03 13:32:05 +03:00
}
func (s *RestSuite) TearDownSuite() {
s.BaseSuite.TearDownSuite()
}
2018-12-03 13:32:05 +03:00
func (s *RestSuite) TestSimpleConfigurationInsecure() {
s.traefikCmd(withConfigFile("fixtures/rest/simple.toml"))
2018-12-03 13:32:05 +03:00
// wait for Traefik
err := try.GetRequest("http://127.0.0.1:8080/api/rawdata", 1000*time.Millisecond, try.BodyContains("rest@internal"))
require.NoError(s.T(), err)
2018-12-03 13:32:05 +03:00
// Expected a 404 as we did not configure anything.
err = try.GetRequest("http://127.0.0.1:8000/", 1000*time.Millisecond, try.StatusCodeIs(http.StatusNotFound))
require.NoError(s.T(), err)
2018-12-03 13:32:05 +03:00
2019-07-13 02:24:03 +03:00
testCase := []struct {
desc string
config *dynamic.Configuration
ruleMatch string
}{
{
desc: "deploy http configuration",
config: &dynamic.Configuration{
HTTP: &dynamic.HTTPConfiguration{
Routers: map[string]*dynamic.Router{
"routerHTTP": {
2019-07-13 02:24:03 +03:00
EntryPoints: []string{"web"},
Middlewares: []string{},
Service: "serviceHTTP",
2019-07-13 02:24:03 +03:00
Rule: "PathPrefix(`/`)",
},
},
Services: map[string]*dynamic.Service{
"serviceHTTP": {
LoadBalancer: &dynamic.ServersLoadBalancer{
2019-07-13 02:24:03 +03:00
Servers: []dynamic.Server{
{
URL: "http://" + s.whoamiAddr,
2019-07-13 02:24:03 +03:00
},
},
},
},
},
},
2018-12-03 13:32:05 +03:00
},
2019-07-13 02:24:03 +03:00
ruleMatch: "PathPrefix(`/`)",
2018-12-03 13:32:05 +03:00
},
2019-07-13 02:24:03 +03:00
{
desc: "deploy tcp configuration",
config: &dynamic.Configuration{
TCP: &dynamic.TCPConfiguration{
Routers: map[string]*dynamic.TCPRouter{
"routerTCP": {
2019-07-13 02:24:03 +03:00
EntryPoints: []string{"web"},
Service: "serviceTCP",
2019-07-13 02:24:03 +03:00
Rule: "HostSNI(`*`)",
},
},
Services: map[string]*dynamic.TCPService{
"serviceTCP": {
LoadBalancer: &dynamic.TCPServersLoadBalancer{
2019-07-13 02:24:03 +03:00
Servers: []dynamic.TCPServer{
{
Address: s.whoamiAddr,
2019-07-13 02:24:03 +03:00
},
},
},
2018-12-03 13:32:05 +03:00
},
},
},
},
2019-07-13 02:24:03 +03:00
ruleMatch: "HostSNI(`*`)",
2018-12-03 13:32:05 +03:00
},
}
2019-07-13 02:24:03 +03:00
for _, test := range testCase {
data, err := json.Marshal(test.config)
require.NoError(s.T(), err)
2018-12-03 13:32:05 +03:00
request, err := http.NewRequest(http.MethodPut, "http://127.0.0.1:8080/api/providers/rest", bytes.NewReader(data))
require.NoError(s.T(), err)
2018-12-03 13:32:05 +03:00
2019-07-13 02:24:03 +03:00
response, err := http.DefaultClient.Do(request)
require.NoError(s.T(), err)
assert.Equal(s.T(), http.StatusOK, response.StatusCode)
2018-12-03 13:32:05 +03:00
err = try.GetRequest("http://127.0.0.1:8080/api/rawdata", 3*time.Second, try.BodyContains(test.ruleMatch))
require.NoError(s.T(), err)
2018-12-03 13:32:05 +03:00
2019-07-13 02:24:03 +03:00
err = try.GetRequest("http://127.0.0.1:8000/", 1000*time.Millisecond, try.StatusCodeIs(http.StatusOK))
require.NoError(s.T(), err)
2019-07-13 02:24:03 +03:00
}
2018-12-03 13:32:05 +03:00
}
2019-09-06 16:08:04 +03:00
func (s *RestSuite) TestSimpleConfiguration() {
file := s.adaptFile("fixtures/rest/simple_secure.toml", struct{}{})
2019-09-06 16:08:04 +03:00
s.traefikCmd(withConfigFile(file))
2019-09-06 16:08:04 +03:00
// Expected a 404 as we did not configure anything.
err := try.GetRequest("http://127.0.0.1:8000/", 1000*time.Millisecond, try.StatusCodeIs(http.StatusNotFound))
require.NoError(s.T(), err)
2019-09-06 16:08:04 +03:00
err = try.GetRequest("http://127.0.0.1:8080/api/rawdata", 2000*time.Millisecond, try.BodyContains("PathPrefix(`/secure`)"))
require.NoError(s.T(), err)
2019-09-06 16:08:04 +03:00
request, err := http.NewRequest(http.MethodPut, "http://127.0.0.1:8080/api/providers/rest", strings.NewReader("{}"))
require.NoError(s.T(), err)
2019-09-06 16:08:04 +03:00
response, err := http.DefaultClient.Do(request)
require.NoError(s.T(), err)
assert.Equal(s.T(), http.StatusNotFound, response.StatusCode)
2019-09-06 16:08:04 +03:00
testCase := []struct {
desc string
config *dynamic.Configuration
ruleMatch string
}{
{
desc: "deploy http configuration",
config: &dynamic.Configuration{
HTTP: &dynamic.HTTPConfiguration{
Routers: map[string]*dynamic.Router{
"router1": {
EntryPoints: []string{"web"},
Middlewares: []string{},
Service: "service1",
Rule: "PathPrefix(`/`)",
},
},
Services: map[string]*dynamic.Service{
"service1": {
LoadBalancer: &dynamic.ServersLoadBalancer{
Servers: []dynamic.Server{
{
URL: "http://" + s.whoamiAddr,
2019-09-06 16:08:04 +03:00
},
},
},
},
},
},
},
ruleMatch: "PathPrefix(`/`)",
},
{
desc: "deploy tcp configuration",
config: &dynamic.Configuration{
TCP: &dynamic.TCPConfiguration{
Routers: map[string]*dynamic.TCPRouter{
"router1": {
EntryPoints: []string{"web"},
Service: "service1",
Rule: "HostSNI(`*`)",
},
},
Services: map[string]*dynamic.TCPService{
"service1": {
LoadBalancer: &dynamic.TCPServersLoadBalancer{
2019-09-06 16:08:04 +03:00
Servers: []dynamic.TCPServer{
{
Address: s.whoamiAddr,
2019-09-06 16:08:04 +03:00
},
},
},
},
},
},
},
ruleMatch: "HostSNI(`*`)",
},
}
for _, test := range testCase {
2021-03-04 11:02:03 +03:00
data, err := json.Marshal(test.config)
require.NoError(s.T(), err)
2019-09-06 16:08:04 +03:00
2021-03-04 11:02:03 +03:00
request, err := http.NewRequest(http.MethodPut, "http://127.0.0.1:8000/secure/api/providers/rest", bytes.NewReader(data))
require.NoError(s.T(), err)
2019-09-06 16:08:04 +03:00
response, err := http.DefaultClient.Do(request)
require.NoError(s.T(), err)
assert.Equal(s.T(), http.StatusOK, response.StatusCode)
2019-09-06 16:08:04 +03:00
err = try.GetRequest("http://127.0.0.1:8080/api/rawdata", time.Second, try.BodyContains(test.ruleMatch))
require.NoError(s.T(), err)
2019-09-06 16:08:04 +03:00
err = try.GetRequest("http://127.0.0.1:8000/", time.Second, try.StatusCodeIs(http.StatusOK))
require.NoError(s.T(), err)
2019-09-06 16:08:04 +03:00
}
}