2019-03-14 09:30:04 +01:00
package server
import (
"bufio"
"context"
"net"
"net/http"
"testing"
"time"
2019-03-14 15:56:06 +01:00
"github.com/containous/flaeg/parse"
2019-03-15 09:42:03 +01:00
"github.com/containous/traefik/pkg/config/static"
"github.com/containous/traefik/pkg/tcp"
2019-03-14 09:30:04 +01: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-03-14 15:56:06 +01:00
GraceTimeOut : parse . Duration ( 5 * time . Second ) ,
2019-03-14 09:30:04 +01: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 15:56:06 +01:00
time . Sleep ( 1 * time . Second )
2019-03-14 09:30:04 +01: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 15:56:06 +01:00
request , err := http . NewRequest ( http . MethodGet , "http://127.0.0.1:8082" , nil )
2019-03-14 09:30:04 +01: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-03-14 15:56:06 +01:00
GraceTimeOut : parse . Duration ( 5 * time . Second ) ,
2019-03-14 09:30:04 +01: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 15:56:06 +01:00
time . Sleep ( 1 * time . Second )
2019-03-14 09:30:04 +01: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 15:56:06 +01:00
request , err := http . NewRequest ( http . MethodGet , "http://127.0.0.1:8082" , nil )
2019-03-14 09:30:04 +01: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-03-14 15:56:06 +01:00
GraceTimeOut : parse . Duration ( 5 * time . Second ) ,
2019-03-14 09:30:04 +01: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 15:56:06 +01:00
time . Sleep ( 1 * time . Second )
2019-03-14 09:30:04 +01: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 15:56:06 +01:00
request , err := http . NewRequest ( http . MethodGet , "http://127.0.0.1:8082" , nil )
2019-03-14 09:30:04 +01: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 )
}