2018-12-03 13:32:05 +03:00
package integration
import (
"bytes"
"encoding/json"
2021-11-25 13:10:06 +03:00
"net"
2018-12-03 13:32:05 +03:00
"net/http"
2019-09-06 16:08:04 +03:00
"strings"
2024-01-09 19:00:07 +03:00
"testing"
2018-12-03 13:32:05 +03:00
"time"
2024-01-09 19:00:07 +03:00
"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
)
2021-11-25 13:10:06 +03:00
type RestSuite struct {
BaseSuite
whoamiAddr string
}
2018-12-03 13:32:05 +03:00
2024-01-09 19:00:07 +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
2024-01-09 19:00:07 +03:00
s . whoamiAddr = net . JoinHostPort ( s . getComposeServiceIP ( "whoami1" ) , "80" )
2018-12-03 13:32:05 +03:00
}
2024-01-09 19:00:07 +03:00
func ( s * RestSuite ) TearDownSuite ( ) {
s . BaseSuite . TearDownSuite ( )
}
2018-12-03 13:32:05 +03:00
2024-01-09 19:00:07 +03:00
func ( s * RestSuite ) TestSimpleConfigurationInsecure ( ) {
s . traefikCmd ( withConfigFile ( "fixtures/rest/simple.toml" ) )
2018-12-03 13:32:05 +03:00
2019-11-14 18:40:05 +03:00
// wait for Traefik
2024-01-09 19:00:07 +03:00
err := try . GetRequest ( "http://127.0.0.1:8080/api/rawdata" , 1000 * time . Millisecond , try . BodyContains ( "rest@internal" ) )
require . NoError ( s . T ( ) , err )
2019-11-14 18:40:05 +03:00
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 ) )
2024-01-09 19:00:07 +03:00
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 {
2019-11-14 18:40:05 +03:00
"routerHTTP" : {
2019-07-13 02:24:03 +03:00
EntryPoints : [ ] string { "web" } ,
Middlewares : [ ] string { } ,
2019-11-14 18:40:05 +03:00
Service : "serviceHTTP" ,
2019-07-13 02:24:03 +03:00
Rule : "PathPrefix(`/`)" ,
} ,
} ,
Services : map [ string ] * dynamic . Service {
2019-11-14 18:40:05 +03:00
"serviceHTTP" : {
2019-08-26 11:30:05 +03:00
LoadBalancer : & dynamic . ServersLoadBalancer {
2019-07-13 02:24:03 +03:00
Servers : [ ] dynamic . Server {
{
2021-11-25 13:10:06 +03:00
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 {
2019-11-14 18:40:05 +03:00
"routerTCP" : {
2019-07-13 02:24:03 +03:00
EntryPoints : [ ] string { "web" } ,
2019-11-14 18:40:05 +03:00
Service : "serviceTCP" ,
2019-07-13 02:24:03 +03:00
Rule : "HostSNI(`*`)" ,
} ,
} ,
Services : map [ string ] * dynamic . TCPService {
2019-11-14 18:40:05 +03:00
"serviceTCP" : {
2019-09-13 21:00:06 +03:00
LoadBalancer : & dynamic . TCPServersLoadBalancer {
2019-07-13 02:24:03 +03:00
Servers : [ ] dynamic . TCPServer {
{
2021-11-25 13:10:06 +03:00
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 {
2019-11-14 18:40:05 +03:00
data , err := json . Marshal ( test . config )
2024-01-09 19:00:07 +03:00
require . NoError ( s . T ( ) , err )
2018-12-03 13:32:05 +03:00
2019-11-14 18:40:05 +03:00
request , err := http . NewRequest ( http . MethodPut , "http://127.0.0.1:8080/api/providers/rest" , bytes . NewReader ( data ) )
2024-01-09 19:00:07 +03:00
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 )
2024-01-09 19:00:07 +03:00
require . NoError ( s . T ( ) , err )
assert . Equal ( s . T ( ) , http . StatusOK , response . StatusCode )
2018-12-03 13:32:05 +03:00
2019-11-14 18:40:05 +03:00
err = try . GetRequest ( "http://127.0.0.1:8080/api/rawdata" , 3 * time . Second , try . BodyContains ( test . ruleMatch ) )
2024-01-09 19:00:07 +03:00
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 ) )
2024-01-09 19:00:07 +03:00
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
2024-01-09 19:00:07 +03:00
func ( s * RestSuite ) TestSimpleConfiguration ( ) {
file := s . adaptFile ( "fixtures/rest/simple_secure.toml" , struct { } { } )
2019-09-06 16:08:04 +03:00
2024-01-09 19:00:07 +03:00
s . traefikCmd ( withConfigFile ( file ) )
2019-09-06 16:08:04 +03:00
// Expected a 404 as we did not configure anything.
2024-01-09 19:00:07 +03:00
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`)" ) )
2024-01-09 19:00:07 +03:00
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 ( "{}" ) )
2024-01-09 19:00:07 +03:00
require . NoError ( s . T ( ) , err )
2019-09-06 16:08:04 +03:00
response , err := http . DefaultClient . Do ( request )
2024-01-09 19:00:07 +03:00
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 {
{
2021-11-25 13:10:06 +03:00
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" : {
2019-09-13 21:00:06 +03:00
LoadBalancer : & dynamic . TCPServersLoadBalancer {
2019-09-06 16:08:04 +03:00
Servers : [ ] dynamic . TCPServer {
{
2021-11-25 13:10:06 +03:00
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 )
2024-01-09 19:00:07 +03:00
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 ) )
2024-01-09 19:00:07 +03:00
require . NoError ( s . T ( ) , err )
2019-09-06 16:08:04 +03:00
response , err := http . DefaultClient . Do ( request )
2024-01-09 19:00:07 +03:00
require . NoError ( s . T ( ) , err )
assert . Equal ( s . T ( ) , http . StatusOK , response . StatusCode )
2019-09-06 16:08:04 +03:00
2021-11-25 13:10:06 +03:00
err = try . GetRequest ( "http://127.0.0.1:8080/api/rawdata" , time . Second , try . BodyContains ( test . ruleMatch ) )
2024-01-09 19:00:07 +03:00
require . NoError ( s . T ( ) , err )
2019-09-06 16:08:04 +03:00
2021-11-25 13:10:06 +03:00
err = try . GetRequest ( "http://127.0.0.1:8000/" , time . Second , try . StatusCodeIs ( http . StatusOK ) )
2024-01-09 19:00:07 +03:00
require . NoError ( s . T ( ) , err )
2019-09-06 16:08:04 +03:00
}
}