2016-10-13 16:25:01 +02:00
// Black-box Testing
package iris_test
2016-07-05 13:37:10 +02:00
import (
"fmt"
2016-12-13 06:33:51 +01:00
"io/ioutil"
"math/rand"
2017-01-02 20:20:17 +01:00
"net/http"
2016-12-13 06:33:51 +01:00
"os"
"strconv"
"testing"
"time"
2017-01-02 20:20:17 +01:00
"github.com/gavv/httpexpect"
"github.com/kataras/iris"
"github.com/kataras/iris/httptest"
2016-07-07 01:36:38 +02:00
)
const (
testTLSCert = ` -- -- - BEGIN CERTIFICATE -- -- -
2017-01-02 20:20:17 +01:00
MIIDAzCCAeugAwIBAgIJAP0pWSuIYyQCMA0GCSqGSIb3DQEBBQUAMBgxFjAUBgNV
BAMMDWxvY2FsaG9zdDozMzEwHhcNMTYxMjI1MDk1OTI3WhcNMjYxMjIzMDk1OTI3
WjAYMRYwFAYDVQQDDA1sb2NhbGhvc3Q6MzMxMIIBIjANBgkqhkiG9w0BAQEFAAOC
AQ8AMIIBCgKCAQEA5vETjLa + 8 W856rWXO1xMF / CLss9vn5xZhPXKhgz + D7ogSAXm
mWP53eeBUGC2r26J ++ CYfVqwOmfJEu9kkGUVi8cGMY9dHeIFPfxD31MYX175jJQe
tu0WeUII7ciNsSUDyBMqsl7yi1IgN7iLONM ++ 1 + QfbbmNiEbghRV6icEH6M + bWlz
3 YSAMEdpK3mg2gsugfLKMwJkaBKEehUNMySRlIhyLITqt1exYGaggRd1zjqUpqpD
sL2sRVHJ3qHGkSh8nVy8MvG8BXiFdYQJP3mCQDZzruCyMWj5 / 19 KAyu7Cto3Bcvu
PgujnwRtU + itt8WhZUVtU1n7Ivf6lMJTBcc4OQIDAQABo1AwTjAdBgNVHQ4EFgQU
MXrBvbILQmiwjUj19aecF2N + 6 IkwHwYDVR0jBBgwFoAUMXrBvbILQmiwjUj19aec
F2N + 6 IkwDAYDVR0TBAUwAwEB / zANBgkqhkiG9w0BAQUFAAOCAQEA4zbFml1t9KXJ
OijAV8gALePR8v04DQwJP + jsRxXw5zzhc8Wqzdd2hjUd07mfRWAvmyywrmhCV6zq
OHznR + aqIqHtm0vV8OpKxLoIQXavfBd6axEXt3859RDM4xJNwIlxs3 + LWGPgINud
wjJqjyzSlhJpQpx4YZ5Da + VMiqAp8N1UeaZ5lBvmSDvoGh6HLODSqtPlWMrldRW9
AfsXVxenq81MIMeKW2fSOoPnWZ4Vjf1 + dSlbJE / DD4zzcfbyfgY6Ep / RrUltJ3ag
FQbuNTQlgKabe21dSL9zJ2PengVKXl4Trl + 4 t / Kina9N9Jw535IRCSwinD6a / 2 Ca
m7DnVXFiVA ==
2016-09-27 15:28:38 +02:00
-- -- - END CERTIFICATE -- -- -
2016-07-07 01:36:38 +02:00
`
2016-09-27 15:28:38 +02:00
2016-07-07 01:36:38 +02:00
testTLSKey = ` -- -- - BEGIN RSA PRIVATE KEY -- -- -
2017-01-02 20:20:17 +01:00
MIIEpAIBAAKCAQEA5vETjLa + 8 W856rWXO1xMF / CLss9vn5xZhPXKhgz + D7ogSAXm
mWP53eeBUGC2r26J ++ CYfVqwOmfJEu9kkGUVi8cGMY9dHeIFPfxD31MYX175jJQe
tu0WeUII7ciNsSUDyBMqsl7yi1IgN7iLONM ++ 1 + QfbbmNiEbghRV6icEH6M + bWlz
3 YSAMEdpK3mg2gsugfLKMwJkaBKEehUNMySRlIhyLITqt1exYGaggRd1zjqUpqpD
sL2sRVHJ3qHGkSh8nVy8MvG8BXiFdYQJP3mCQDZzruCyMWj5 / 19 KAyu7Cto3Bcvu
PgujnwRtU + itt8WhZUVtU1n7Ivf6lMJTBcc4OQIDAQABAoIBAQCTLE0eHpPevtg0
+ FaRUMd5diVA5asoF3aBIjZXaU47bY0G + SO02x6wSMmDFK83a4Vpy / 7 B3Bp0jhF5
DLCUyKaLdmE / EjLwSUq37ty + JHFizd7QtNBCGSN6URfpmSabHpCjX3uVQqblHIhF
mki3BQCdJ5CoXPemxUCHjDgYSZb6JVNIPJExjekc0 + 4 A2MYWMXV6Wr86C7AY3659
KmveZpC3gOkLA / g / IqDQL / QgTq7 / 3 eloHaO + uPBihdF56do4eaOO0jgFYpl8V7ek
PZhHfhuPZV3oq15 + 8 Vt77ngtjUWVI6qX0E3ilh + V5cof + 03 q0FzHPVe3zBUNXcm0
OGz19u / FAoGBAPSm4Aa4xs / ybyjQakMNix9rak66ehzGkmlfeK5yuQ / fHmTg8Ac +
ahGs6A3lFWQiyU6hqm6Qp0iKuxuDh35DJGCWAw5OUS / 7 WLJtu8fNFch6iIG29rFs
s + Uz2YLxJPebpBsKymZUp7NyDRgEElkiqsREmbYjLrc8uNKkDy + k14YnAoGBAPGn
ZlN0Mo5iNgQStulYEP5pI7WOOax9KOYVnBNguqgY9c7fXVXBxChoxt5ebQJWG45y
KPG0hB0bkA4YPu4bTRf5acIMpjFwcxNlmwdc4oCkT4xqAFs9B / AKYZgkf4IfKHqW
P9PD7TbUpkaxv25bPYwUSEB7lPa + hBtRyN9Wo6qfAoGAPBkeISiU1hJE0i7YW55h
FZfKZoqSYq043B + ywo + 1 / Dsf + UH0VKM1ZSAnZPpoVc / hyaoW9tAb98r0iZ620wJl
VkCjgYklknbY5APmw / 8 SIcxP6iVq1kzQqDYjcXIRVa3rEyWEcLzM8VzL8KFXbIQC
lPIRHFfqKuMEt + HLRTXmJ7MCgYAHGvv4QjdmVl7uObqlG9DMGj1RjlAF0VxNf58q
NrLmVG2N2qV86wigg4wtZ6te4TdINfUcPkmQLYpLz8yx5Z2bsdq5OPP + CidoD5nC
WqnSTIKGR2uhQycjmLqL5a7WHaJsEFTqHh2wego1k + 5 kCUzC / KmvM7MKmkl6ICp +
3 qZLUwKBgQCDOhKDwYo1hdiXoOOQqg / LZmpWOqjO3b4p99B9iJqhmXN0GKXIPSBh
5 nqqmGsG8asSQhchs7EPMh8B80KbrDTeidWskZuUoQV27Al1UEmL6Zcl83qXD6sf
k9X9TwWyZtp5IL1CAEd / Il9ZTXFzr3lNaN8LCFnU + EIsz1YgUW8LTg ==
2016-09-27 15:28:38 +02:00
-- -- - END RSA PRIVATE KEY -- -- -
2017-01-02 20:20:17 +01:00
`
2016-07-05 13:37:10 +02:00
)
2016-09-27 15:28:38 +02:00
func TestParseAddr ( t * testing . T ) {
// test hosts
expectedHost1 := "mydomain.com:1993"
expectedHost2 := "mydomain.com"
2016-10-13 16:25:01 +02:00
expectedHost3 := iris . DefaultServerHostname + ":9090"
2016-09-27 15:28:38 +02:00
expectedHost4 := "mydomain.com:443"
2016-10-13 16:25:01 +02:00
host1 := iris . ParseHost ( expectedHost1 )
host2 := iris . ParseHost ( expectedHost2 )
host3 := iris . ParseHost ( ":9090" )
host4 := iris . ParseHost ( expectedHost4 )
2016-09-27 15:28:38 +02:00
if host1 != expectedHost1 {
t . Fatalf ( "Expecting server 1's host to be %s but we got %s" , expectedHost1 , host1 )
2016-07-07 17:17:34 +02:00
}
2016-09-27 15:28:38 +02:00
if host2 != expectedHost2 {
t . Fatalf ( "Expecting server 2's host to be %s but we got %s" , expectedHost2 , host2 )
2016-07-07 17:17:34 +02:00
}
2016-09-27 15:28:38 +02:00
if host3 != expectedHost3 {
t . Fatalf ( "Expecting server 3's host to be %s but we got %s" , expectedHost3 , host3 )
}
if host4 != expectedHost4 {
t . Fatalf ( "Expecting server 4's host to be %s but we got %s" , expectedHost4 , host4 )
2016-07-07 17:17:34 +02:00
}
2016-09-27 15:28:38 +02:00
// test hostname
expectedHostname1 := "mydomain.com"
expectedHostname2 := "mydomain.com"
2016-10-13 16:25:01 +02:00
expectedHostname3 := iris . DefaultServerHostname
2016-09-27 15:28:38 +02:00
expectedHostname4 := "mydomain.com"
2016-10-13 16:25:01 +02:00
hostname1 := iris . ParseHostname ( host1 )
hostname2 := iris . ParseHostname ( host2 )
hostname3 := iris . ParseHostname ( host3 )
hostname4 := iris . ParseHostname ( host4 )
2016-09-27 15:28:38 +02:00
if hostname1 != expectedHostname1 {
t . Fatalf ( "Expecting server 1's hostname to be %s but we got %s" , expectedHostname1 , hostname1 )
2016-07-07 17:17:34 +02:00
}
2016-09-27 15:28:38 +02:00
if hostname2 != expectedHostname2 {
t . Fatalf ( "Expecting server 2's hostname to be %s but we got %s" , expectedHostname2 , hostname2 )
2016-07-07 23:59:00 +02:00
}
2016-09-27 15:28:38 +02:00
if hostname3 != expectedHostname3 {
t . Fatalf ( "Expecting server 3's hostname to be %s but we got %s" , expectedHostname3 , hostname3 )
2016-07-07 23:59:00 +02:00
}
2016-09-27 15:28:38 +02:00
if hostname4 != expectedHostname4 {
t . Fatalf ( "Expecting server 4's hostname to be %s but we got %s" , expectedHostname4 , hostname4 )
}
// test scheme, no need to test fullhost(scheme+host)
2016-10-13 16:25:01 +02:00
expectedScheme1 := iris . SchemeHTTP
expectedScheme2 := iris . SchemeHTTP
expectedScheme3 := iris . SchemeHTTP
expectedScheme4 := iris . SchemeHTTPS
scheme1 := iris . ParseScheme ( host1 )
scheme2 := iris . ParseScheme ( host2 )
scheme3 := iris . ParseScheme ( host3 )
scheme4 := iris . ParseScheme ( host4 )
2016-09-27 15:28:38 +02:00
if scheme1 != expectedScheme1 {
t . Fatalf ( "Expecting server 1's hostname to be %s but we got %s" , expectedScheme1 , scheme1 )
}
2016-07-07 23:59:00 +02:00
2016-09-27 15:28:38 +02:00
if scheme2 != expectedScheme2 {
t . Fatalf ( "Expecting server 2's hostname to be %s but we got %s" , expectedScheme2 , scheme2 )
2016-07-07 17:17:34 +02:00
}
2016-09-27 15:28:38 +02:00
if scheme3 != expectedScheme3 {
t . Fatalf ( "Expecting server 3's hostname to be %s but we got %s" , expectedScheme3 , scheme3 )
}
if scheme4 != expectedScheme4 {
t . Fatalf ( "Expecting server 4's hostname to be %s but we got %s" , expectedScheme4 , scheme4 )
2016-07-07 17:17:34 +02:00
}
}
2016-12-13 06:33:51 +01:00
func getRandomNumber ( min int , max int ) int {
rand . Seed ( time . Now ( ) . Unix ( ) )
return rand . Intn ( max - min ) + min
}
2016-12-13 07:09:27 +01:00
// works as
// defer listenTLS(iris.Default, hostTLS)()
func listenTLS ( api * iris . Framework , hostTLS string ) func ( ) {
2017-01-02 20:20:17 +01:00
api . Close ( ) // close any prev listener
2016-12-13 07:09:27 +01:00
api . Config . DisableBanner = true
2016-07-07 01:36:38 +02:00
// create the key and cert files on the fly, and delete them when this test finished
certFile , ferr := ioutil . TempFile ( "" , "cert" )
if ferr != nil {
2016-12-13 07:09:27 +01:00
api . Logger . Panic ( ferr . Error ( ) )
2016-07-07 01:36:38 +02:00
}
keyFile , ferr := ioutil . TempFile ( "" , "key" )
if ferr != nil {
2016-12-13 07:09:27 +01:00
api . Logger . Panic ( ferr . Error ( ) )
2016-07-07 01:36:38 +02:00
}
2016-12-13 07:09:27 +01:00
certFile . WriteString ( testTLSCert )
keyFile . WriteString ( testTLSKey )
go api . ListenTLS ( hostTLS , certFile . Name ( ) , keyFile . Name ( ) )
if ok := <- api . Available ; ! ok {
api . Logger . Panic ( "Unexpected error: server cannot start, please report this as bug!!" )
}
return func ( ) {
2016-07-07 01:36:38 +02:00
certFile . Close ( )
2017-01-02 20:20:17 +01:00
time . Sleep ( 50 * time . Millisecond )
2016-07-07 01:36:38 +02:00
os . Remove ( certFile . Name ( ) )
keyFile . Close ( )
2017-01-02 20:20:17 +01:00
time . Sleep ( 50 * time . Millisecond )
2016-07-07 01:36:38 +02:00
os . Remove ( keyFile . Name ( ) )
2016-12-13 07:09:27 +01:00
}
}
// Contains the server test for multi running servers
func TestMultiRunningServers_v1_PROXY ( t * testing . T ) {
2017-01-02 20:20:17 +01:00
api := iris . New ( )
2016-12-13 07:09:27 +01:00
2017-01-02 20:20:17 +01:00
host := "localhost"
hostTLS := host + ":" + strconv . Itoa ( getRandomNumber ( 1919 , 2221 ) )
api . Get ( "/" , func ( ctx * iris . Context ) {
ctx . Writef ( "Hello from %s" , hostTLS )
2016-07-07 01:36:38 +02:00
} )
2017-01-02 20:20:17 +01:00
// println("running main on: " + hostTLS)
defer listenTLS ( api , hostTLS ) ( )
2016-07-07 01:36:38 +02:00
2017-01-02 20:20:17 +01:00
e := httptest . New ( api , t , httptest . ExplicitURL ( true ) )
e . Request ( "GET" , "/" ) . Expect ( ) . Status ( iris . StatusOK ) . Body ( ) . Equal ( "Hello from " + hostTLS )
2016-09-27 15:28:38 +02:00
2017-01-02 20:20:17 +01:00
// proxy http to https
proxyHost := host + ":" + strconv . Itoa ( getRandomNumber ( 3300 , 3340 ) )
// println("running proxy on: " + proxyHost)
2016-12-13 07:09:27 +01:00
2017-01-02 20:20:17 +01:00
iris . Proxy ( proxyHost , "https://" + hostTLS )
2016-07-07 01:36:38 +02:00
2017-01-02 20:20:17 +01:00
// proxySrv := &http.Server{Addr: proxyHost, Handler: iris.ProxyHandler("https://" + hostTLS)}
// go proxySrv.ListenAndServe()
// time.Sleep(3 * time.Second)
2016-07-07 01:36:38 +02:00
2017-01-02 20:20:17 +01:00
eproxy := httptest . NewInsecure ( "http://" + proxyHost , t , httptest . ExplicitURL ( true ) )
eproxy . Request ( "GET" , "/" ) . Expect ( ) . Status ( iris . StatusOK ) . Body ( ) . Equal ( "Hello from " + hostTLS )
2016-07-07 01:36:38 +02:00
}
// Contains the server test for multi running servers
func TestMultiRunningServers_v2 ( t * testing . T ) {
2017-01-02 20:20:17 +01:00
api := iris . New ( )
2016-12-13 07:09:27 +01:00
2016-09-27 15:49:37 +02:00
domain := "localhost"
2017-01-02 20:20:17 +01:00
hostTLS := domain + ":" + strconv . Itoa ( getRandomNumber ( 2222 , 2229 ) )
2016-12-13 06:33:51 +01:00
srv1Host := domain + ":" + strconv . Itoa ( getRandomNumber ( 4446 , 5444 ) )
srv2Host := domain + ":" + strconv . Itoa ( getRandomNumber ( 7778 , 8887 ) )
2016-09-27 15:28:38 +02:00
2017-01-02 20:20:17 +01:00
api . Get ( "/" , func ( ctx * iris . Context ) {
ctx . Writef ( "Hello from %s" , hostTLS )
2016-07-07 01:36:38 +02:00
} )
2017-01-02 20:20:17 +01:00
defer listenTLS ( api , hostTLS ) ( )
2016-09-27 15:28:38 +02:00
// using the same iris' handler but not as proxy, just the same handler
2017-01-02 20:20:17 +01:00
srv2 := & http . Server { Handler : api . Router , Addr : srv2Host }
go srv2 . ListenAndServe ( )
// using the proxy handler
srv1 := & http . Server { Handler : iris . ProxyHandler ( "https://" + hostTLS ) , Addr : srv1Host }
go srv1 . ListenAndServe ( )
time . Sleep ( 500 * time . Millisecond ) // wait a little for the http servers
2016-07-07 01:36:38 +02:00
2017-01-02 20:20:17 +01:00
e := httptest . New ( api , t , httptest . ExplicitURL ( true ) )
e . Request ( "GET" , "/" ) . Expect ( ) . Status ( iris . StatusOK ) . Body ( ) . Equal ( "Hello from " + hostTLS )
2016-07-07 01:36:38 +02:00
2017-01-02 20:20:17 +01:00
eproxy1 := httptest . NewInsecure ( "http://" + srv1Host , t , httptest . ExplicitURL ( true ) )
eproxy1 . Request ( "GET" , "/" ) . Expect ( ) . Status ( iris . StatusOK ) . Body ( ) . Equal ( "Hello from " + hostTLS )
2016-07-07 01:36:38 +02:00
2017-01-02 20:20:17 +01:00
eproxy2 := httptest . NewInsecure ( "http://" + srv2Host , t )
eproxy2 . Request ( "GET" , "/" ) . Expect ( ) . Status ( iris . StatusOK ) . Body ( ) . Equal ( "Hello from " + hostTLS )
2016-07-07 01:36:38 +02:00
}
2016-07-05 13:37:10 +02:00
const (
2016-07-21 19:33:00 +02:00
testEnableSubdomain = true
testSubdomain = "mysubdomain"
2016-07-05 13:37:10 +02:00
)
func testSubdomainHost ( ) string {
2016-10-13 16:25:01 +02:00
s := testSubdomain + "." + iris . Default . Config . VHost
2016-07-21 19:33:00 +02:00
return s
2016-07-05 13:37:10 +02:00
}
2016-09-27 15:28:38 +02:00
func testSubdomainURL ( ) string {
2016-07-05 13:37:10 +02:00
subdomainHost := testSubdomainHost ( )
2016-10-13 16:25:01 +02:00
return iris . Default . Config . VScheme + subdomainHost
2016-07-05 13:37:10 +02:00
}
func subdomainTester ( e * httpexpect . Expect ) * httpexpect . Expect {
es := e . Builder ( func ( req * httpexpect . Request ) {
req . WithURL ( testSubdomainURL ( ) )
} )
return es
}
type param struct {
Key string
Value string
}
type testRoute struct {
Method string
Path string
RequestPath string
RequestQuery string
Body string
Status int
Register bool
Params [ ] param
URLParams [ ] param
}
func TestMuxSimple ( t * testing . T ) {
testRoutes := [ ] testRoute {
// FOUND - registed
{ "GET" , "/test_get" , "/test_get" , "" , "hello, get!" , 200 , true , nil , nil } ,
{ "POST" , "/test_post" , "/test_post" , "" , "hello, post!" , 200 , true , nil , nil } ,
{ "PUT" , "/test_put" , "/test_put" , "" , "hello, put!" , 200 , true , nil , nil } ,
{ "DELETE" , "/test_delete" , "/test_delete" , "" , "hello, delete!" , 200 , true , nil , nil } ,
{ "HEAD" , "/test_head" , "/test_head" , "" , "hello, head!" , 200 , true , nil , nil } ,
{ "OPTIONS" , "/test_options" , "/test_options" , "" , "hello, options!" , 200 , true , nil , nil } ,
{ "CONNECT" , "/test_connect" , "/test_connect" , "" , "hello, connect!" , 200 , true , nil , nil } ,
{ "PATCH" , "/test_patch" , "/test_patch" , "" , "hello, patch!" , 200 , true , nil , nil } ,
{ "TRACE" , "/test_trace" , "/test_trace" , "" , "hello, trace!" , 200 , true , nil , nil } ,
// NOT FOUND - not registed
{ "GET" , "/test_get_nofound" , "/test_get_nofound" , "" , "Not Found" , 404 , false , nil , nil } ,
{ "POST" , "/test_post_nofound" , "/test_post_nofound" , "" , "Not Found" , 404 , false , nil , nil } ,
{ "PUT" , "/test_put_nofound" , "/test_put_nofound" , "" , "Not Found" , 404 , false , nil , nil } ,
{ "DELETE" , "/test_delete_nofound" , "/test_delete_nofound" , "" , "Not Found" , 404 , false , nil , nil } ,
{ "HEAD" , "/test_head_nofound" , "/test_head_nofound" , "" , "Not Found" , 404 , false , nil , nil } ,
{ "OPTIONS" , "/test_options_nofound" , "/test_options_nofound" , "" , "Not Found" , 404 , false , nil , nil } ,
{ "CONNECT" , "/test_connect_nofound" , "/test_connect_nofound" , "" , "Not Found" , 404 , false , nil , nil } ,
{ "PATCH" , "/test_patch_nofound" , "/test_patch_nofound" , "" , "Not Found" , 404 , false , nil , nil } ,
{ "TRACE" , "/test_trace_nofound" , "/test_trace_nofound" , "" , "Not Found" , 404 , false , nil , nil } ,
// Parameters
{ "GET" , "/test_get_parameter1/:name" , "/test_get_parameter1/iris" , "" , "name=iris" , 200 , true , [ ] param { { "name" , "iris" } } , nil } ,
{ "GET" , "/test_get_parameter2/:name/details/:something" , "/test_get_parameter2/iris/details/anything" , "" , "name=iris,something=anything" , 200 , true , [ ] param { { "name" , "iris" } , { "something" , "anything" } } , nil } ,
{ "GET" , "/test_get_parameter2/:name/details/:something/*else" , "/test_get_parameter2/iris/details/anything/elsehere" , "" , "name=iris,something=anything,else=/elsehere" , 200 , true , [ ] param { { "name" , "iris" } , { "something" , "anything" } , { "else" , "elsehere" } } , nil } ,
// URL Parameters
{ "GET" , "/test_get_urlparameter1/first" , "/test_get_urlparameter1/first" , "name=irisurl" , "name=irisurl" , 200 , true , nil , [ ] param { { "name" , "irisurl" } } } ,
{ "GET" , "/test_get_urlparameter2/second" , "/test_get_urlparameter2/second" , "name=irisurl&something=anything" , "name=irisurl,something=anything" , 200 , true , nil , [ ] param { { "name" , "irisurl" } , { "something" , "anything" } } } ,
{ "GET" , "/test_get_urlparameter2/first/second/third" , "/test_get_urlparameter2/first/second/third" , "name=irisurl&something=anything&else=elsehere" , "name=irisurl,something=anything,else=elsehere" , 200 , true , nil , [ ] param { { "name" , "irisurl" } , { "something" , "anything" } , { "else" , "elsehere" } } } ,
}
2017-01-10 07:54:39 +01:00
2016-10-13 16:25:01 +02:00
iris . ResetDefault ( )
2016-07-05 13:37:10 +02:00
for idx := range testRoutes {
r := testRoutes [ idx ]
if r . Register {
2016-10-13 16:25:01 +02:00
iris . HandleFunc ( r . Method , r . Path , func ( ctx * iris . Context ) {
2016-07-05 13:37:10 +02:00
ctx . SetStatusCode ( r . Status )
if r . Params != nil && len ( r . Params ) > 0 {
2017-01-04 14:16:53 +01:00
ctx . WriteString ( ctx . ParamsSentence ( ) )
2016-07-05 13:37:10 +02:00
} else if r . URLParams != nil && len ( r . URLParams ) > 0 {
if len ( r . URLParams ) != len ( ctx . URLParams ( ) ) {
t . Fatalf ( "Error when comparing length of url parameters %d != %d" , len ( r . URLParams ) , len ( ctx . URLParams ( ) ) )
}
paramsKeyVal := ""
for idxp , p := range r . URLParams {
val := ctx . URLParam ( p . Key )
paramsKeyVal += p . Key + "=" + val + ","
if idxp == len ( r . URLParams ) - 1 {
paramsKeyVal = paramsKeyVal [ 0 : len ( paramsKeyVal ) - 1 ]
}
}
2017-01-04 14:16:53 +01:00
ctx . WriteString ( paramsKeyVal )
2016-07-05 13:37:10 +02:00
} else {
2017-01-04 14:16:53 +01:00
ctx . WriteString ( r . Body )
2016-07-05 13:37:10 +02:00
}
} )
}
}
2017-01-10 07:54:39 +01:00
e := httptest . New ( iris . Default , t , httptest . Debug ( true ) )
2016-07-05 13:37:10 +02:00
// run the tests (1)
for idx := range testRoutes {
r := testRoutes [ idx ]
e . Request ( r . Method , r . RequestPath ) . WithQueryString ( r . RequestQuery ) .
Expect ( ) .
Status ( r . Status ) . Body ( ) . Equal ( r . Body )
}
}
func TestMuxSimpleParty ( t * testing . T ) {
2016-10-13 16:25:01 +02:00
iris . ResetDefault ( )
2016-07-05 13:37:10 +02:00
2017-01-02 20:20:17 +01:00
h := func ( c * iris . Context ) { c . WriteString ( c . Request . URL . Host + c . Request . RequestURI ) }
2016-07-05 13:37:10 +02:00
if testEnableSubdomain {
2016-10-13 16:25:01 +02:00
subdomainParty := iris . Party ( testSubdomain + "." )
2016-07-05 13:37:10 +02:00
{
subdomainParty . Get ( "/" , h )
subdomainParty . Get ( "/path1" , h )
subdomainParty . Get ( "/path2" , h )
subdomainParty . Get ( "/namedpath/:param1/something/:param2" , h )
subdomainParty . Get ( "/namedpath/:param1/something/:param2/else" , h )
}
}
// simple
2016-10-13 16:25:01 +02:00
p := iris . Party ( "/party1" )
2016-07-05 13:37:10 +02:00
{
p . Get ( "/" , h )
p . Get ( "/path1" , h )
p . Get ( "/path2" , h )
p . Get ( "/namedpath/:param1/something/:param2" , h )
p . Get ( "/namedpath/:param1/something/:param2/else" , h )
}
2016-12-14 10:57:07 +01:00
iris . Default . Config . VHost = "0.0.0.0:" + strconv . Itoa ( getRandomNumber ( 2222 , 2399 ) )
2016-10-13 16:25:01 +02:00
// iris.Default.Config.Tester.Debug = true
// iris.Default.Config.Tester.ExplicitURL = true
e := httptest . New ( iris . Default , t )
2016-07-05 13:37:10 +02:00
request := func ( reqPath string ) {
2016-09-30 17:48:48 +02:00
2016-07-05 13:37:10 +02:00
e . Request ( "GET" , reqPath ) .
Expect ( ) .
2016-10-13 16:25:01 +02:00
Status ( iris . StatusOK ) . Body ( ) . Equal ( iris . Default . Config . VHost + reqPath )
2016-07-05 13:37:10 +02:00
}
// run the tests
2016-10-04 00:18:17 +02:00
request ( "/party1/" )
2016-07-05 13:37:10 +02:00
request ( "/party1/path1" )
request ( "/party1/path2" )
request ( "/party1/namedpath/theparam1/something/theparam2" )
request ( "/party1/namedpath/theparam1/something/theparam2/else" )
if testEnableSubdomain {
es := subdomainTester ( e )
subdomainRequest := func ( reqPath string ) {
es . Request ( "GET" , reqPath ) .
Expect ( ) .
2016-10-13 16:25:01 +02:00
Status ( iris . StatusOK ) . Body ( ) . Equal ( testSubdomainHost ( ) + reqPath )
2016-07-05 13:37:10 +02:00
}
subdomainRequest ( "/" )
subdomainRequest ( "/path1" )
subdomainRequest ( "/path2" )
subdomainRequest ( "/namedpath/theparam1/something/theparam2" )
subdomainRequest ( "/namedpath/theparam1/something/theparam2/else" )
}
}
2017-01-04 18:50:54 +01:00
// TestRealSubdomainSimple exists because the local examples some times passed but...
// hope that travis will not has problem with this
func TestRealSubdomainSimple ( t * testing . T ) {
api := iris . New ( )
host := "localhost:" + strconv . Itoa ( getRandomNumber ( 4732 , 4958 ) )
subdomain := "admin"
subdomainHost := subdomain + "." + host
// no order, you can register subdomains at the end also.
admin := api . Party ( subdomain + "." )
{
// admin.mydomain.com
admin . Get ( "/" , func ( c * iris . Context ) {
c . Writef ( "INDEX FROM %s" , subdomainHost )
} )
// admin.mydomain.com/hey
admin . Get ( "/hey" , func ( c * iris . Context ) {
c . Writef ( subdomainHost + c . Request . RequestURI )
} )
// admin.mydomain.com/hey2
admin . Get ( "/hey2" , func ( c * iris . Context ) {
c . Writef ( subdomainHost + c . Request . RequestURI )
} )
}
// mydomain.com/
api . Get ( "/" , func ( c * iris . Context ) {
c . Writef ( "INDEX FROM no-subdomain hey" )
} )
// mydomain.com/hey
api . Get ( "/hey" , func ( c * iris . Context ) {
c . Writef ( "HEY FROM no-subdomain hey" )
} )
api . Config . DisableBanner = true
go api . Listen ( host )
<- api . Available
e := httptest . New ( api , t , httptest . ExplicitURL ( true ) )
e . GET ( "/" ) . Expect ( ) . Status ( iris . StatusOK ) . Body ( ) . Equal ( "INDEX FROM no-subdomain hey" )
e . GET ( "/hey" ) . Expect ( ) . Status ( iris . StatusOK ) . Body ( ) . Equal ( "HEY FROM no-subdomain hey" )
sub := e . Builder ( func ( req * httpexpect . Request ) {
req . WithURL ( "http://admin." + host )
} )
sub . GET ( "/" ) . Expect ( ) . Status ( iris . StatusOK ) . Body ( ) . Equal ( "INDEX FROM " + subdomainHost )
sub . GET ( "/hey" ) . Expect ( ) . Status ( iris . StatusOK ) . Body ( ) . Equal ( subdomainHost + "/hey" )
sub . GET ( "/hey2" ) . Expect ( ) . Status ( iris . StatusOK ) . Body ( ) . Equal ( subdomainHost + "/hey2" )
}
2016-07-05 13:37:10 +02:00
func TestMuxPathEscape ( t * testing . T ) {
2016-10-13 16:25:01 +02:00
iris . ResetDefault ( )
2017-01-10 07:54:39 +01:00
iris . Config . EnablePathEscape = true
2016-07-05 13:37:10 +02:00
2016-10-13 16:25:01 +02:00
iris . Get ( "/details/:name" , func ( ctx * iris . Context ) {
2017-01-02 20:20:17 +01:00
name := ctx . ParamDecoded ( "name" )
2016-07-05 13:37:10 +02:00
highlight := ctx . URLParam ( "highlight" )
2017-01-02 20:20:17 +01:00
2016-10-13 16:25:01 +02:00
ctx . Text ( iris . StatusOK , fmt . Sprintf ( "name=%s,highlight=%s" , name , highlight ) )
2016-07-05 13:37:10 +02:00
} )
2016-10-13 16:25:01 +02:00
e := httptest . New ( iris . Default , t )
2016-07-05 13:37:10 +02:00
e . GET ( "/details/Sakamoto desu ga" ) .
WithQuery ( "highlight" , "text" ) .
2016-10-13 16:25:01 +02:00
Expect ( ) . Status ( iris . StatusOK ) . Body ( ) . Equal ( "name=Sakamoto desu ga,highlight=text" )
2016-07-05 13:37:10 +02:00
}
2016-07-21 22:08:42 +02:00
func TestMuxDecodeURL ( t * testing . T ) {
2016-10-13 16:25:01 +02:00
iris . ResetDefault ( )
2016-07-21 19:33:00 +02:00
2016-10-13 16:25:01 +02:00
iris . Get ( "/encoding/:url" , func ( ctx * iris . Context ) {
2017-01-02 20:20:17 +01:00
url := ctx . ParamDecoded ( "url" )
2016-10-13 16:25:01 +02:00
ctx . SetStatusCode ( iris . StatusOK )
2017-01-02 20:20:17 +01:00
ctx . WriteString ( url )
2016-07-21 19:33:00 +02:00
} )
2016-10-13 16:25:01 +02:00
e := httptest . New ( iris . Default , t )
2016-07-21 19:33:00 +02:00
2016-10-13 16:25:01 +02:00
e . GET ( "/encoding/http%3A%2F%2Fsome-url.com" ) . Expect ( ) . Status ( iris . StatusOK ) . Body ( ) . Equal ( "http://some-url.com" )
2016-07-21 19:33:00 +02:00
}
2016-07-05 13:37:10 +02:00
func TestMuxCustomErrors ( t * testing . T ) {
var (
notFoundMessage = "Iris custom message for 404 not found"
internalServerMessage = "Iris custom message for 500 internal server error"
testRoutesCustomErrors = [ ] testRoute {
// NOT FOUND CUSTOM ERRORS - not registed
{ "GET" , "/test_get_nofound_custom" , "/test_get_nofound_custom" , "" , notFoundMessage , 404 , false , nil , nil } ,
{ "POST" , "/test_post_nofound_custom" , "/test_post_nofound_custom" , "" , notFoundMessage , 404 , false , nil , nil } ,
{ "PUT" , "/test_put_nofound_custom" , "/test_put_nofound_custom" , "" , notFoundMessage , 404 , false , nil , nil } ,
{ "DELETE" , "/test_delete_nofound_custom" , "/test_delete_nofound_custom" , "" , notFoundMessage , 404 , false , nil , nil } ,
{ "HEAD" , "/test_head_nofound_custom" , "/test_head_nofound_custom" , "" , notFoundMessage , 404 , false , nil , nil } ,
{ "OPTIONS" , "/test_options_nofound_custom" , "/test_options_nofound_custom" , "" , notFoundMessage , 404 , false , nil , nil } ,
{ "CONNECT" , "/test_connect_nofound_custom" , "/test_connect_nofound_custom" , "" , notFoundMessage , 404 , false , nil , nil } ,
{ "PATCH" , "/test_patch_nofound_custom" , "/test_patch_nofound_custom" , "" , notFoundMessage , 404 , false , nil , nil } ,
{ "TRACE" , "/test_trace_nofound_custom" , "/test_trace_nofound_custom" , "" , notFoundMessage , 404 , false , nil , nil } ,
// SERVER INTERNAL ERROR 500 PANIC CUSTOM ERRORS - registed
{ "GET" , "/test_get_panic_custom" , "/test_get_panic_custom" , "" , internalServerMessage , 500 , true , nil , nil } ,
{ "POST" , "/test_post_panic_custom" , "/test_post_panic_custom" , "" , internalServerMessage , 500 , true , nil , nil } ,
{ "PUT" , "/test_put_panic_custom" , "/test_put_panic_custom" , "" , internalServerMessage , 500 , true , nil , nil } ,
{ "DELETE" , "/test_delete_panic_custom" , "/test_delete_panic_custom" , "" , internalServerMessage , 500 , true , nil , nil } ,
{ "HEAD" , "/test_head_panic_custom" , "/test_head_panic_custom" , "" , internalServerMessage , 500 , true , nil , nil } ,
{ "OPTIONS" , "/test_options_panic_custom" , "/test_options_panic_custom" , "" , internalServerMessage , 500 , true , nil , nil } ,
{ "CONNECT" , "/test_connect_panic_custom" , "/test_connect_panic_custom" , "" , internalServerMessage , 500 , true , nil , nil } ,
{ "PATCH" , "/test_patch_panic_custom" , "/test_patch_panic_custom" , "" , internalServerMessage , 500 , true , nil , nil } ,
{ "TRACE" , "/test_trace_panic_custom" , "/test_trace_panic_custom" , "" , internalServerMessage , 500 , true , nil , nil } ,
}
)
2016-10-13 16:25:01 +02:00
iris . ResetDefault ( )
2016-07-05 13:37:10 +02:00
// first register the testRoutes needed
for _ , r := range testRoutesCustomErrors {
if r . Register {
2016-10-13 16:25:01 +02:00
iris . HandleFunc ( r . Method , r . Path , func ( ctx * iris . Context ) {
2016-07-05 13:37:10 +02:00
ctx . EmitError ( r . Status )
} )
}
}
// register the custom errors
2016-10-13 16:25:01 +02:00
iris . OnError ( iris . StatusNotFound , func ( ctx * iris . Context ) {
2017-01-02 20:20:17 +01:00
ctx . Writef ( "%s" , notFoundMessage )
2016-07-05 13:37:10 +02:00
} )
2016-10-13 16:25:01 +02:00
iris . OnError ( iris . StatusInternalServerError , func ( ctx * iris . Context ) {
2017-01-02 20:20:17 +01:00
ctx . Writef ( "%s" , internalServerMessage )
2016-07-05 13:37:10 +02:00
} )
// create httpexpect instance that will call fasthtpp.RequestHandler directly
2016-10-13 16:25:01 +02:00
e := httptest . New ( iris . Default , t )
2016-07-05 13:37:10 +02:00
// run the tests
for _ , r := range testRoutesCustomErrors {
e . Request ( r . Method , r . RequestPath ) .
Expect ( ) .
Status ( r . Status ) . Body ( ) . Equal ( r . Body )
}
}
type testUserAPI struct {
2016-10-13 16:25:01 +02:00
* iris . Context
2016-07-05 13:37:10 +02:00
}
// GET /users
func ( u testUserAPI ) Get ( ) {
2017-01-02 20:20:17 +01:00
u . WriteString ( "Get Users\n" )
2016-07-05 13:37:10 +02:00
}
// GET /users/:param1 which its value passed to the id argument
func ( u testUserAPI ) GetBy ( id string ) { // id equals to u.Param("param1")
2017-01-02 20:20:17 +01:00
u . Writef ( "Get By %s\n" , id )
2016-07-05 13:37:10 +02:00
}
// PUT /users
func ( u testUserAPI ) Put ( ) {
2017-01-02 20:20:17 +01:00
u . Writef ( "Put, name: %s\n" , u . FormValue ( "name" ) )
2016-07-05 13:37:10 +02:00
}
// POST /users/:param1
func ( u testUserAPI ) PostBy ( id string ) {
2017-01-02 20:20:17 +01:00
u . Writef ( "Post By %s, name: %s\n" , id , u . FormValue ( "name" ) )
2016-07-05 13:37:10 +02:00
}
// DELETE /users/:param1
func ( u testUserAPI ) DeleteBy ( id string ) {
2017-01-02 20:20:17 +01:00
u . Writef ( "Delete By %s\n" , id )
2016-07-05 13:37:10 +02:00
}
func TestMuxAPI ( t * testing . T ) {
2016-10-13 16:25:01 +02:00
iris . ResetDefault ( )
2016-07-05 13:37:10 +02:00
middlewareResponseText := "I assume that you are authenticated\n"
2016-12-13 07:09:27 +01:00
h := [ ] iris . HandlerFunc { func ( ctx * iris . Context ) { // optional middleware for .API
2016-07-05 13:37:10 +02:00
// do your work here, or render a login window if not logged in, get the user and send it to the next middleware, or do all here
ctx . Set ( "user" , "username" )
ctx . Next ( )
2016-10-13 16:25:01 +02:00
} , func ( ctx * iris . Context ) {
2016-07-05 13:37:10 +02:00
if ctx . Get ( "user" ) == "username" {
2017-01-02 20:20:17 +01:00
ctx . WriteString ( middlewareResponseText )
2016-07-05 13:37:10 +02:00
ctx . Next ( )
} else {
2016-10-13 16:25:01 +02:00
ctx . SetStatusCode ( iris . StatusUnauthorized )
2016-07-05 13:37:10 +02:00
}
2016-12-13 07:09:27 +01:00
} }
iris . API ( "/users" , testUserAPI { } , h ... )
// test a simple .Party with compination of .API
iris . Party ( "sites/:site" ) . API ( "/users" , testUserAPI { } , h ... )
2016-07-05 13:37:10 +02:00
2016-10-13 16:25:01 +02:00
e := httptest . New ( iris . Default , t )
2016-07-05 13:37:10 +02:00
2016-12-13 07:09:27 +01:00
siteID := "1"
apiPath := "/sites/" + siteID + "/users"
2016-07-05 13:37:10 +02:00
userID := "4077"
formname := "kataras"
2016-12-13 07:09:27 +01:00
// .API
2016-10-13 16:25:01 +02:00
e . GET ( "/users" ) . Expect ( ) . Status ( iris . StatusOK ) . Body ( ) . Equal ( middlewareResponseText + "Get Users\n" )
e . GET ( "/users/" + userID ) . Expect ( ) . Status ( iris . StatusOK ) . Body ( ) . Equal ( middlewareResponseText + "Get By " + userID + "\n" )
e . PUT ( "/users" ) . WithFormField ( "name" , formname ) . Expect ( ) . Status ( iris . StatusOK ) . Body ( ) . Equal ( middlewareResponseText + "Put, name: " + formname + "\n" )
e . POST ( "/users/" + userID ) . WithFormField ( "name" , formname ) . Expect ( ) . Status ( iris . StatusOK ) . Body ( ) . Equal ( middlewareResponseText + "Post By " + userID + ", name: " + formname + "\n" )
e . DELETE ( "/users/" + userID ) . Expect ( ) . Status ( iris . StatusOK ) . Body ( ) . Equal ( middlewareResponseText + "Delete By " + userID + "\n" )
2016-07-21 22:08:42 +02:00
2016-12-13 07:09:27 +01:00
// .Party
2016-10-13 16:25:01 +02:00
e . GET ( apiPath ) . Expect ( ) . Status ( iris . StatusOK ) . Body ( ) . Equal ( middlewareResponseText + "Get Users\n" )
e . GET ( apiPath + "/" + userID ) . Expect ( ) . Status ( iris . StatusOK ) . Body ( ) . Equal ( middlewareResponseText + "Get By " + userID + "\n" )
e . PUT ( apiPath ) . WithFormField ( "name" , formname ) . Expect ( ) . Status ( iris . StatusOK ) . Body ( ) . Equal ( middlewareResponseText + "Put, name: " + formname + "\n" )
e . POST ( apiPath + "/" + userID ) . WithFormField ( "name" , formname ) . Expect ( ) . Status ( iris . StatusOK ) . Body ( ) . Equal ( middlewareResponseText + "Post By " + userID + ", name: " + formname + "\n" )
e . DELETE ( apiPath + "/" + userID ) . Expect ( ) . Status ( iris . StatusOK ) . Body ( ) . Equal ( middlewareResponseText + "Delete By " + userID + "\n" )
2016-12-13 07:09:27 +01:00
2016-07-21 22:08:42 +02:00
}
2016-07-05 13:37:10 +02:00
type myTestHandlerData struct {
Sysname string // this will be the same for all requests
Version int // this will be the same for all requests
DynamicPathParameter string // this will be different for each request
}
type myTestCustomHandler struct {
data myTestHandlerData
}
2016-10-13 16:25:01 +02:00
func ( m * myTestCustomHandler ) Serve ( ctx * iris . Context ) {
2016-07-05 13:37:10 +02:00
data := & m . data
data . DynamicPathParameter = ctx . Param ( "myparam" )
2016-10-13 16:25:01 +02:00
ctx . JSON ( iris . StatusOK , data )
2016-07-05 13:37:10 +02:00
}
func TestMuxCustomHandler ( t * testing . T ) {
2016-10-13 16:25:01 +02:00
iris . ResetDefault ( )
2016-07-05 13:37:10 +02:00
myData := myTestHandlerData {
Sysname : "Redhat" ,
Version : 1 ,
}
2016-10-13 16:25:01 +02:00
iris . Handle ( "GET" , "/custom_handler_1/:myparam" , & myTestCustomHandler { myData } )
iris . Handle ( "GET" , "/custom_handler_2/:myparam" , & myTestCustomHandler { myData } )
2016-07-05 13:37:10 +02:00
2016-10-13 16:25:01 +02:00
e := httptest . New ( iris . Default , t )
2016-07-05 13:37:10 +02:00
// two times per testRoute
param1 := "thisimyparam1"
expectedData1 := myData
expectedData1 . DynamicPathParameter = param1
2016-10-13 16:25:01 +02:00
e . GET ( "/custom_handler_1/" + param1 ) . Expect ( ) . Status ( iris . StatusOK ) . JSON ( ) . Equal ( expectedData1 )
2016-07-05 13:37:10 +02:00
param2 := "thisimyparam2"
expectedData2 := myData
expectedData2 . DynamicPathParameter = param2
2016-10-13 16:25:01 +02:00
e . GET ( "/custom_handler_1/" + param2 ) . Expect ( ) . Status ( iris . StatusOK ) . JSON ( ) . Equal ( expectedData2 )
2016-07-05 13:37:10 +02:00
param3 := "thisimyparam3"
expectedData3 := myData
expectedData3 . DynamicPathParameter = param3
2016-10-13 16:25:01 +02:00
e . GET ( "/custom_handler_2/" + param3 ) . Expect ( ) . Status ( iris . StatusOK ) . JSON ( ) . Equal ( expectedData3 )
2016-07-05 13:37:10 +02:00
param4 := "thisimyparam4"
expectedData4 := myData
expectedData4 . DynamicPathParameter = param4
2016-10-13 16:25:01 +02:00
e . GET ( "/custom_handler_2/" + param4 ) . Expect ( ) . Status ( iris . StatusOK ) . JSON ( ) . Equal ( expectedData4 )
2016-07-05 13:37:10 +02:00
}
2016-10-11 21:35:12 +02:00
func TestMuxFireMethodNotAllowed ( t * testing . T ) {
2016-10-13 16:25:01 +02:00
iris . ResetDefault ( )
iris . Default . Config . FireMethodNotAllowed = true
h := func ( ctx * iris . Context ) {
2017-01-02 20:20:17 +01:00
ctx . WriteString ( ctx . Method ( ) )
2016-10-11 21:35:12 +02:00
}
2016-10-13 16:25:01 +02:00
iris . Default . OnError ( iris . StatusMethodNotAllowed , func ( ctx * iris . Context ) {
2017-01-02 20:20:17 +01:00
ctx . WriteString ( "Hello from my custom 405 page" )
2016-10-11 21:35:12 +02:00
} )
2016-10-13 16:25:01 +02:00
iris . Get ( "/mypath" , h )
iris . Put ( "/mypath" , h )
2016-10-11 21:35:12 +02:00
2016-10-13 16:25:01 +02:00
e := httptest . New ( iris . Default , t )
2016-10-11 21:35:12 +02:00
2016-10-13 16:25:01 +02:00
e . GET ( "/mypath" ) . Expect ( ) . Status ( iris . StatusOK ) . Body ( ) . Equal ( "GET" )
e . PUT ( "/mypath" ) . Expect ( ) . Status ( iris . StatusOK ) . Body ( ) . Equal ( "PUT" )
2016-10-11 21:35:12 +02:00
// this should fail with 405 and catch by the custom http error
2016-10-13 16:25:01 +02:00
e . POST ( "/mypath" ) . Expect ( ) . Status ( iris . StatusMethodNotAllowed ) . Body ( ) . Equal ( "Hello from my custom 405 page" )
iris . Close ( )
2016-10-11 21:35:12 +02:00
}
2016-11-15 19:20:29 +01:00
2017-01-02 20:20:17 +01:00
/ *
2016-11-15 19:20:29 +01:00
var (
2016-12-13 07:53:55 +01:00
cacheDuration = 2 * time . Second
2016-11-15 19:20:29 +01:00
errCacheTestFailed = errors . New ( "Expected the main handler to be executed %d times instead of %d." )
)
// ~14secs
func runCacheTest ( e * httpexpect . Expect , path string , counterPtr * uint32 , expectedBodyStr , expectedContentType string ) error {
e . GET ( path ) . Expect ( ) . Status ( iris . StatusOK ) . Body ( ) . Equal ( expectedBodyStr )
time . Sleep ( cacheDuration / 5 ) // lets wait for a while, cache should be saved and ready
e . GET ( path ) . Expect ( ) . Status ( iris . StatusOK ) . Body ( ) . Equal ( expectedBodyStr )
counter := atomic . LoadUint32 ( counterPtr )
if counter > 1 {
// n should be 1 because it doesn't changed after the first call
return errCacheTestFailed . Format ( 1 , counter )
}
time . Sleep ( cacheDuration )
// cache should be cleared now
e . GET ( path ) . Expect ( ) . Status ( iris . StatusOK ) . ContentType ( expectedContentType , "utf-8" ) . Body ( ) . Equal ( expectedBodyStr )
time . Sleep ( cacheDuration / 5 )
// let's call again , the cache should be saved
e . GET ( path ) . Expect ( ) . Status ( iris . StatusOK ) . ContentType ( expectedContentType , "utf-8" ) . Body ( ) . Equal ( expectedBodyStr )
counter = atomic . LoadUint32 ( counterPtr )
if counter != 2 {
return errCacheTestFailed . Format ( 2 , counter )
}
return nil
}
2017-01-02 20:20:17 +01:00
2016-12-13 07:53:55 +01:00
Inside github . com / geekypanda / httpcache are enough , no need to add 10 + seconds of testing here .
2016-11-15 19:20:29 +01:00
func TestCache ( t * testing . T ) {
iris . ResetDefault ( )
expectedBodyStr := "Imagine it as a big message to achieve x20 response performance!"
var textCounter , htmlCounter uint32
iris . Get ( "/text" , iris . Cache ( func ( ctx * iris . Context ) {
atomic . AddUint32 ( & textCounter , 1 )
ctx . Text ( iris . StatusOK , expectedBodyStr )
} , cacheDuration ) )
iris . Get ( "/html" , iris . Cache ( func ( ctx * iris . Context ) {
atomic . AddUint32 ( & htmlCounter , 1 )
ctx . HTML ( iris . StatusOK , expectedBodyStr )
} , cacheDuration ) )
e := httptest . New ( iris . Default , t )
// test cache on text/plain
if err := runCacheTest ( e , "/text" , & textCounter , expectedBodyStr , "text/plain" ) ; err != nil {
t . Fatal ( err )
}
// text cache on text/html
if err := runCacheTest ( e , "/html" , & htmlCounter , expectedBodyStr , "text/html" ) ; err != nil {
t . Fatal ( err )
}
}
2016-12-13 07:53:55 +01:00
* /
2016-12-04 05:53:08 +01:00
func TestRedirectHTTPS ( t * testing . T ) {
2017-01-02 20:20:17 +01:00
api := iris . New ( iris . OptionIsDevelopment ( true ) )
host := "localhost:" + strconv . Itoa ( getRandomNumber ( 1717 , 9281 ) )
expectedBody := "Redirected to /redirected"
api . Get ( "/redirect" , func ( ctx * iris . Context ) { ctx . Redirect ( "/redirected" ) } )
api . Get ( "/redirected" , func ( ctx * iris . Context ) { ctx . Text ( iris . StatusOK , "Redirected to " + ctx . Path ( ) ) } )
defer listenTLS ( api , host ) ( )
2016-12-04 05:53:08 +01:00
2017-01-02 20:20:17 +01:00
e := httptest . New ( api , t )
e . GET ( "/redirect" ) . Expect ( ) . Status ( iris . StatusOK ) . Body ( ) . Equal ( expectedBody )
2016-12-04 05:53:08 +01:00
}