2017-07-06 17:28:13 +03:00
package integration
2017-07-01 02:04:18 +03:00
import (
"net/http"
2023-01-02 19:00:05 +03:00
"net/http/httptest"
2017-07-01 02:04:18 +03:00
"os"
"time"
"github.com/go-check/check"
2020-09-16 16:46:04 +03:00
"github.com/traefik/traefik/v2/integration/try"
2017-07-01 02:04:18 +03:00
checker "github.com/vdemeester/shakers"
)
2021-11-25 13:10:06 +03:00
// ErrorPagesSuite test suites.
2017-07-10 15:58:31 +03:00
type ErrorPagesSuite struct {
BaseSuite
ErrorPageIP string
BackendIP string
2017-07-01 02:04:18 +03:00
}
2017-07-10 15:58:31 +03:00
func ( s * ErrorPagesSuite ) SetUpSuite ( c * check . C ) {
s . createComposeProject ( c , "error_pages" )
2021-11-25 13:10:06 +03:00
s . composeUp ( c )
2017-07-10 15:58:31 +03:00
2021-11-25 13:10:06 +03:00
s . ErrorPageIP = s . getComposeServiceIP ( c , "nginx2" )
s . BackendIP = s . getComposeServiceIP ( c , "nginx1" )
2017-07-10 15:58:31 +03:00
}
2017-07-01 02:04:18 +03:00
2017-07-10 15:58:31 +03:00
func ( s * ErrorPagesSuite ) TestSimpleConfiguration ( c * check . C ) {
file := s . adaptFile ( c , "fixtures/error_pages/simple.toml" , struct {
2017-07-01 02:04:18 +03:00
Server1 string
Server2 string
2023-01-02 19:00:05 +03:00
} { "http://" + s . BackendIP + ":80" , s . ErrorPageIP } )
2017-07-01 02:04:18 +03:00
defer os . Remove ( file )
2017-09-13 11:34:04 +03:00
cmd , display := s . traefikCmd ( withConfigFile ( file ) )
defer display ( c )
2017-07-01 02:04:18 +03:00
err := cmd . Start ( )
c . Assert ( err , checker . IsNil )
2020-10-09 10:32:03 +03:00
defer s . killCmd ( cmd )
2017-07-01 02:04:18 +03:00
2018-02-23 12:52:03 +03:00
frontendReq , err := http . NewRequest ( http . MethodGet , "http://127.0.0.1:8080" , nil )
2017-07-01 02:04:18 +03:00
c . Assert ( err , checker . IsNil )
frontendReq . Host = "test.local"
err = try . Request ( frontendReq , 2 * time . Second , try . BodyContains ( "nginx" ) )
c . Assert ( err , checker . IsNil )
}
2017-07-10 15:58:31 +03:00
func ( s * ErrorPagesSuite ) TestErrorPage ( c * check . C ) {
2018-07-03 11:02:03 +03:00
// error.toml contains a mis-configuration of the backend host
2017-07-10 15:58:31 +03:00
file := s . adaptFile ( c , "fixtures/error_pages/error.toml" , struct {
2017-07-01 02:04:18 +03:00
Server1 string
Server2 string
2017-07-10 15:58:31 +03:00
} { s . BackendIP , s . ErrorPageIP } )
2017-07-01 02:04:18 +03:00
defer os . Remove ( file )
2017-09-13 11:34:04 +03:00
cmd , display := s . traefikCmd ( withConfigFile ( file ) )
defer display ( c )
2017-07-01 02:04:18 +03:00
err := cmd . Start ( )
c . Assert ( err , checker . IsNil )
2020-10-09 10:32:03 +03:00
defer s . killCmd ( cmd )
2017-07-01 02:04:18 +03:00
2018-02-23 12:52:03 +03:00
frontendReq , err := http . NewRequest ( http . MethodGet , "http://127.0.0.1:8080" , nil )
2017-07-01 02:04:18 +03:00
c . Assert ( err , checker . IsNil )
frontendReq . Host = "test.local"
err = try . Request ( frontendReq , 2 * time . Second , try . BodyContains ( "An error occurred." ) )
c . Assert ( err , checker . IsNil )
}
2023-01-02 19:00:05 +03:00
func ( s * ErrorPagesSuite ) TestErrorPageFlush ( c * check . C ) {
srv := httptest . NewServer ( http . HandlerFunc ( func ( rw http . ResponseWriter , r * http . Request ) {
rw . Header ( ) . Add ( "Transfer-Encoding" , "chunked" )
rw . WriteHeader ( http . StatusInternalServerError )
_ , _ = rw . Write ( [ ] byte ( "KO" ) )
} ) )
file := s . adaptFile ( c , "fixtures/error_pages/simple.toml" , struct {
Server1 string
Server2 string
} { srv . URL , s . ErrorPageIP } )
defer os . Remove ( file )
cmd , display := s . traefikCmd ( withConfigFile ( file ) )
defer display ( c )
err := cmd . Start ( )
c . Assert ( err , checker . IsNil )
defer s . killCmd ( cmd )
frontendReq , err := http . NewRequest ( http . MethodGet , "http://127.0.0.1:8080" , nil )
c . Assert ( err , checker . IsNil )
frontendReq . Host = "test.local"
err = try . Request ( frontendReq , 2 * time . Second ,
try . BodyContains ( "An error occurred." ) ,
try . HasHeaderValue ( "Content-Type" , "text/html" , true ) ,
)
c . Assert ( err , checker . IsNil )
}