2020-07-15 14:56:03 +00:00
|
|
|
package integration
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2024-01-09 16:00:07 +00:00
|
|
|
"testing"
|
2020-07-15 14:56:03 +00:00
|
|
|
"time"
|
|
|
|
|
2024-01-09 16:00:07 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/stretchr/testify/suite"
|
2020-09-16 13:46:04 +00:00
|
|
|
"github.com/traefik/traefik/v2/integration/try"
|
|
|
|
"github.com/traefik/traefik/v2/pkg/config/dynamic"
|
2020-07-15 14:56:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type HTTPSuite struct{ BaseSuite }
|
|
|
|
|
2024-01-09 16:00:07 +00:00
|
|
|
func TestHTTPSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(HTTPSuite))
|
|
|
|
}
|
2020-07-15 14:56:03 +00:00
|
|
|
|
2024-01-09 16:00:07 +00:00
|
|
|
func (s *HTTPSuite) TestSimpleConfiguration() {
|
|
|
|
s.traefikCmd(withConfigFile("fixtures/http/simple.toml"))
|
2020-07-15 14:56:03 +00:00
|
|
|
|
|
|
|
// Expect a 404 as we configured nothing.
|
2024-01-09 16:00:07 +00:00
|
|
|
err := try.GetRequest("http://127.0.0.1:8000/", time.Second, try.StatusCodeIs(http.StatusNotFound))
|
|
|
|
require.NoError(s.T(), err)
|
2020-07-15 14:56:03 +00:00
|
|
|
|
|
|
|
// Provide a configuration, fetched by Traefik provider.
|
|
|
|
configuration := &dynamic.Configuration{
|
|
|
|
HTTP: &dynamic.HTTPConfiguration{
|
|
|
|
Routers: map[string]*dynamic.Router{
|
|
|
|
"routerHTTP": {
|
|
|
|
EntryPoints: []string{"web"},
|
|
|
|
Middlewares: []string{},
|
|
|
|
Service: "serviceHTTP",
|
|
|
|
Rule: "PathPrefix(`/`)",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Services: map[string]*dynamic.Service{
|
|
|
|
"serviceHTTP": {
|
|
|
|
LoadBalancer: &dynamic.ServersLoadBalancer{
|
|
|
|
PassHostHeader: boolRef(true),
|
|
|
|
Servers: []dynamic.Server{
|
|
|
|
{
|
|
|
|
URL: "http://bacon:80",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
configData, err := json.Marshal(configuration)
|
2024-01-09 16:00:07 +00:00
|
|
|
require.NoError(s.T(), err)
|
2020-07-15 14:56:03 +00:00
|
|
|
|
|
|
|
server := startTestServerWithResponse(configData)
|
|
|
|
defer server.Close()
|
|
|
|
|
|
|
|
// Expect configuration to be applied.
|
|
|
|
err = try.GetRequest("http://127.0.0.1:9090/api/rawdata", 3*time.Second, try.BodyContains("routerHTTP@http", "serviceHTTP@http", "http://bacon:80"))
|
2024-01-09 16:00:07 +00:00
|
|
|
require.NoError(s.T(), err)
|
2020-07-15 14:56:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func startTestServerWithResponse(response []byte) (ts *httptest.Server) {
|
|
|
|
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
_, _ = w.Write(response)
|
|
|
|
})
|
|
|
|
listener, err := net.Listen("tcp", "127.0.0.1:9000")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ts = &httptest.Server{
|
|
|
|
Listener: listener,
|
|
|
|
Config: &http.Server{Handler: handler},
|
|
|
|
}
|
|
|
|
ts.Start()
|
|
|
|
return ts
|
|
|
|
}
|
|
|
|
|
|
|
|
func boolRef(b bool) *bool {
|
|
|
|
return &b
|
|
|
|
}
|