2019-03-14 08:30:04 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"context"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2019-08-03 01:58:23 +00:00
|
|
|
"github.com/containous/traefik/v2/pkg/config/static"
|
|
|
|
"github.com/containous/traefik/v2/pkg/tcp"
|
|
|
|
"github.com/containous/traefik/v2/pkg/types"
|
2019-03-14 08:30:04 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestShutdownHTTP(t *testing.T) {
|
|
|
|
entryPoint, err := NewTCPEntryPoint(context.Background(), &static.EntryPoint{
|
|
|
|
Address: ":0",
|
|
|
|
Transport: &static.EntryPointsTransport{
|
|
|
|
LifeCycle: &static.LifeCycle{
|
|
|
|
RequestAcceptGraceTimeout: 0,
|
2019-06-17 09:48:05 +00:00
|
|
|
GraceTimeOut: types.Duration(5 * time.Second),
|
2019-03-14 08:30:04 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
ForwardedHeaders: &static.ForwardedHeaders{},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
go entryPoint.startTCP(context.Background())
|
|
|
|
|
|
|
|
router := &tcp.Router{}
|
|
|
|
router.HTTPHandler(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
2019-03-14 14:56:06 +00:00
|
|
|
time.Sleep(1 * time.Second)
|
2019-03-14 08:30:04 +00:00
|
|
|
rw.WriteHeader(http.StatusOK)
|
|
|
|
}))
|
|
|
|
entryPoint.switchRouter(router)
|
|
|
|
|
|
|
|
conn, err := net.Dial("tcp", entryPoint.listener.Addr().String())
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
go entryPoint.Shutdown(context.Background())
|
|
|
|
|
2019-03-14 14:56:06 +00:00
|
|
|
request, err := http.NewRequest(http.MethodGet, "http://127.0.0.1:8082", nil)
|
2019-03-14 08:30:04 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = request.Write(conn)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
resp, err := http.ReadResponse(bufio.NewReader(conn), request)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, resp.StatusCode, http.StatusOK)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestShutdownHTTPHijacked(t *testing.T) {
|
|
|
|
entryPoint, err := NewTCPEntryPoint(context.Background(), &static.EntryPoint{
|
|
|
|
Address: ":0",
|
|
|
|
Transport: &static.EntryPointsTransport{
|
|
|
|
LifeCycle: &static.LifeCycle{
|
|
|
|
RequestAcceptGraceTimeout: 0,
|
2019-06-17 09:48:05 +00:00
|
|
|
GraceTimeOut: types.Duration(5 * time.Second),
|
2019-03-14 08:30:04 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
ForwardedHeaders: &static.ForwardedHeaders{},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
go entryPoint.startTCP(context.Background())
|
|
|
|
|
|
|
|
router := &tcp.Router{}
|
|
|
|
router.HTTPHandler(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
conn, _, err := rw.(http.Hijacker).Hijack()
|
|
|
|
require.NoError(t, err)
|
2019-03-14 14:56:06 +00:00
|
|
|
time.Sleep(1 * time.Second)
|
2019-03-14 08:30:04 +00:00
|
|
|
|
|
|
|
resp := http.Response{StatusCode: http.StatusOK}
|
|
|
|
err = resp.Write(conn)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
}))
|
|
|
|
entryPoint.switchRouter(router)
|
|
|
|
|
|
|
|
conn, err := net.Dial("tcp", entryPoint.listener.Addr().String())
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
go entryPoint.Shutdown(context.Background())
|
|
|
|
|
2019-03-14 14:56:06 +00:00
|
|
|
request, err := http.NewRequest(http.MethodGet, "http://127.0.0.1:8082", nil)
|
2019-03-14 08:30:04 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = request.Write(conn)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
resp, err := http.ReadResponse(bufio.NewReader(conn), request)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, resp.StatusCode, http.StatusOK)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestShutdownTCPConn(t *testing.T) {
|
|
|
|
entryPoint, err := NewTCPEntryPoint(context.Background(), &static.EntryPoint{
|
|
|
|
Address: ":0",
|
|
|
|
Transport: &static.EntryPointsTransport{
|
|
|
|
LifeCycle: &static.LifeCycle{
|
|
|
|
RequestAcceptGraceTimeout: 0,
|
2019-06-17 09:48:05 +00:00
|
|
|
GraceTimeOut: types.Duration(5 * time.Second),
|
2019-03-14 08:30:04 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
ForwardedHeaders: &static.ForwardedHeaders{},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
go entryPoint.startTCP(context.Background())
|
|
|
|
|
|
|
|
router := &tcp.Router{}
|
|
|
|
router.AddCatchAllNoTLS(tcp.HandlerFunc(func(conn net.Conn) {
|
|
|
|
_, err := http.ReadRequest(bufio.NewReader(conn))
|
|
|
|
require.NoError(t, err)
|
2019-03-14 14:56:06 +00:00
|
|
|
time.Sleep(1 * time.Second)
|
2019-03-14 08:30:04 +00:00
|
|
|
|
|
|
|
resp := http.Response{StatusCode: http.StatusOK}
|
|
|
|
err = resp.Write(conn)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}))
|
|
|
|
|
|
|
|
entryPoint.switchRouter(router)
|
|
|
|
|
|
|
|
conn, err := net.Dial("tcp", entryPoint.listener.Addr().String())
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
go entryPoint.Shutdown(context.Background())
|
|
|
|
|
2019-03-14 14:56:06 +00:00
|
|
|
request, err := http.NewRequest(http.MethodGet, "http://127.0.0.1:8082", nil)
|
2019-03-14 08:30:04 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = request.Write(conn)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
resp, err := http.ReadResponse(bufio.NewReader(conn), request)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, resp.StatusCode, http.StatusOK)
|
|
|
|
}
|