2018-11-14 12:18:03 +03:00
package api
import (
2019-05-16 11:58:06 +03:00
"encoding/json"
"flag"
2018-11-14 12:18:03 +03:00
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
2019-08-03 04:58:23 +03: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 12:18:03 +03:00
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
2019-05-16 11:58:06 +03:00
var updateExpected = flag . Bool ( "update_expected" , false , "Update expected files in testdata" )
2019-07-12 12:10:03 +03:00
func TestHandler_RawData ( t * testing . T ) {
2018-11-14 12:18:03 +03:00
type expected struct {
statusCode int
2019-05-16 11:58:06 +03:00
json string
2018-11-14 12:18:03 +03:00
}
testCases := [ ] struct {
2019-05-16 11:58:06 +03:00
desc string
path string
2019-07-15 18:04:04 +03:00
conf runtime . Configuration
2019-05-16 11:58:06 +03:00
expected expected
2018-11-14 12:18:03 +03:00
} {
{
2019-05-16 11:58:06 +03:00
desc : "Get rawdata" ,
path : "/api/rawdata" ,
2019-07-15 18:04:04 +03:00
conf : runtime . Configuration {
Services : map [ string ] * runtime . ServiceInfo {
2019-06-21 10:54:04 +03:00
"foo-service@myprovider" : {
2019-07-10 10:26:04 +03:00
Service : & dynamic . Service {
2019-08-26 11:30:05 +03:00
LoadBalancer : & dynamic . ServersLoadBalancer {
2019-07-10 10:26:04 +03:00
Servers : [ ] dynamic . Server {
2019-05-16 11:58:06 +03:00
{
2019-06-05 23:18:06 +03:00
URL : "http://127.0.0.1" ,
2019-05-16 11:58:06 +03:00
} ,
} ,
} ,
2019-03-14 11:30:04 +03:00
} ,
2018-11-14 12:18:03 +03:00
} ,
} ,
2019-07-15 18:04:04 +03:00
Middlewares : map [ string ] * runtime . MiddlewareInfo {
2019-06-21 10:54:04 +03:00
"auth@myprovider" : {
2019-07-10 10:26:04 +03:00
Middleware : & dynamic . Middleware {
BasicAuth : & dynamic . BasicAuth {
2019-05-16 11:58:06 +03:00
Users : [ ] string { "admin:admin" } ,
2019-03-14 11:30:04 +03:00
} ,
} ,
2019-05-16 11:58:06 +03:00
} ,
2019-06-21 10:54:04 +03:00
"addPrefixTest@myprovider" : {
2019-07-10 10:26:04 +03:00
Middleware : & dynamic . Middleware {
AddPrefix : & dynamic . AddPrefix {
2019-05-16 11:58:06 +03:00
Prefix : "/titi" ,
2018-11-14 12:18:03 +03:00
} ,
} ,
} ,
2019-06-21 10:54:04 +03:00
"addPrefixTest@anotherprovider" : {
2019-07-10 10:26:04 +03:00
Middleware : & dynamic . Middleware {
AddPrefix : & dynamic . AddPrefix {
2019-05-16 11:58:06 +03:00
Prefix : "/toto" ,
} ,
2019-03-14 11:30:04 +03:00
} ,
2018-11-14 12:18:03 +03:00
} ,
} ,
2019-07-15 18:04:04 +03:00
Routers : map [ string ] * runtime . RouterInfo {
2019-06-21 10:54:04 +03:00
"bar@myprovider" : {
2019-07-10 10:26:04 +03:00
Router : & dynamic . Router {
2019-05-16 11:58:06 +03:00
EntryPoints : [ ] string { "web" } ,
2019-06-21 10:54:04 +03:00
Service : "foo-service@myprovider" ,
2019-05-16 11:58:06 +03:00
Rule : "Host(`foo.bar`)" ,
2019-06-21 10:54:04 +03:00
Middlewares : [ ] string { "auth" , "addPrefixTest@anotherprovider" } ,
2019-03-14 11:30:04 +03:00
} ,
2018-11-14 12:18:03 +03:00
} ,
2019-06-21 10:54:04 +03:00
"test@myprovider" : {
2019-07-10 10:26:04 +03:00
Router : & dynamic . Router {
2019-05-16 11:58:06 +03:00
EntryPoints : [ ] string { "web" } ,
2019-06-21 10:54:04 +03:00
Service : "foo-service@myprovider" ,
2019-05-16 11:58:06 +03:00
Rule : "Host(`foo.bar.other`)" ,
Middlewares : [ ] string { "addPrefixTest" , "auth" } ,
2018-11-14 12:18:03 +03:00
} ,
} ,
} ,
2019-07-15 18:04:04 +03:00
TCPServices : map [ string ] * runtime . TCPServiceInfo {
2019-06-21 10:54:04 +03:00
"tcpfoo-service@myprovider" : {
2019-07-10 10:26:04 +03:00
TCPService : & dynamic . TCPService {
2019-09-13 21:00:06 +03:00
LoadBalancer : & dynamic . TCPServersLoadBalancer {
2019-07-10 10:26:04 +03:00
Servers : [ ] dynamic . TCPServer {
2019-05-16 11:58:06 +03:00
{
Address : "127.0.0.1" ,
} ,
2019-03-14 11:30:04 +03:00
} ,
2018-11-14 12:18:03 +03:00
} ,
} ,
} ,
} ,
2019-07-15 18:04:04 +03:00
TCPRouters : map [ string ] * runtime . TCPRouterInfo {
2019-06-21 10:54:04 +03:00
"tcpbar@myprovider" : {
2019-07-10 10:26:04 +03:00
TCPRouter : & dynamic . TCPRouter {
2019-05-16 11:58:06 +03:00
EntryPoints : [ ] string { "web" } ,
2019-06-21 10:54:04 +03:00
Service : "tcpfoo-service@myprovider" ,
2019-05-16 11:58:06 +03:00
Rule : "HostSNI(`foo.bar`)" ,
2018-11-14 12:18:03 +03:00
} ,
} ,
2019-06-21 10:54:04 +03:00
"tcptest@myprovider" : {
2019-07-10 10:26:04 +03:00
TCPRouter : & dynamic . TCPRouter {
2019-05-16 11:58:06 +03:00
EntryPoints : [ ] string { "web" } ,
2019-06-21 10:54:04 +03:00
Service : "tcpfoo-service@myprovider" ,
2019-05-16 11:58:06 +03:00
Rule : "HostSNI(`foo.bar.other`)" ,
2018-11-14 12:18:03 +03:00
} ,
} ,
} ,
} ,
2019-05-16 11:58:06 +03:00
expected : expected {
statusCode : http . StatusOK ,
json : "testdata/getrawdata.json" ,
2018-11-14 12:18:03 +03:00
} ,
} ,
}
for _ , test := range testCases {
test := test
t . Run ( test . desc , func ( t * testing . T ) {
t . Parallel ( )
2019-06-19 19:34:04 +03:00
// TODO: server status
2019-05-16 11:58:06 +03:00
rtConf := & test . conf
2019-06-21 10:54:04 +03:00
2019-06-19 19:34:04 +03:00
rtConf . PopulateUsedBy ( )
2019-05-16 11:58:06 +03:00
handler := New ( static . Configuration { API : & static . API { } , Global : & static . Global { } } , rtConf )
2018-11-14 12:18:03 +03: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 18:44:03 +03:00
assert . Equal ( t , resp . Header . Get ( "Content-Type" ) , "application/json" )
2018-11-14 12:18:03 +03:00
2019-05-16 11:58:06 +03:00
contents , err := ioutil . ReadAll ( resp . Body )
2018-11-14 12:18:03 +03:00
require . NoError ( t , err )
2019-05-16 11:58:06 +03:00
2018-11-14 12:18:03 +03:00
err = resp . Body . Close ( )
require . NoError ( t , err )
2019-05-16 11:58:06 +03:00
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 12:18:03 +03:00
} )
}
}