2016-07-01 20:28:24 +02:00
//Package test -v ./... builds all tests
package test
2016-07-02 17:44:01 +02:00
// Contains tests for sessions(sessions package) & flash messages(context)
2016-07-01 21:14:38 +02:00
2016-07-01 20:28:24 +02:00
import (
"testing"
"github.com/kataras/iris"
)
func TestSessions ( t * testing . T ) {
values := map [ string ] interface { } {
"Name" : "iris" ,
"Months" : "4" ,
"Secret" : "dsads£2132215£%%Ssdsa" ,
}
api := iris . New ( )
2016-07-01 21:14:38 +02:00
api . Config . Sessions . Cookie = "mycustomsessionid"
2016-07-01 20:28:24 +02:00
writeValues := func ( ctx * iris . Context ) {
sessValues := ctx . Session ( ) . GetAll ( )
ctx . JSON ( iris . StatusOK , sessValues )
}
2016-07-02 18:53:36 +02:00
if EnableSubdomainTests {
api . Party ( Subdomain + "." ) . Get ( "/get" , func ( ctx * iris . Context ) {
2016-07-01 20:28:24 +02:00
writeValues ( ctx )
} )
}
api . Post ( "set" , func ( ctx * iris . Context ) {
vals := make ( map [ string ] interface { } , 0 )
if err := ctx . ReadJSON ( & vals ) ; err != nil {
t . Fatalf ( "Cannot readjson. Trace %s" , err . Error ( ) )
}
for k , v := range vals {
ctx . Session ( ) . Set ( k , v )
}
} )
api . Get ( "/get" , func ( ctx * iris . Context ) {
writeValues ( ctx )
} )
api . Get ( "/clear" , func ( ctx * iris . Context ) {
ctx . Session ( ) . Clear ( )
writeValues ( ctx )
} )
api . Get ( "/destroy" , func ( ctx * iris . Context ) {
ctx . SessionDestroy ( )
writeValues ( ctx )
// the cookie and all values should be empty
} )
2016-07-02 18:53:36 +02:00
e := Tester ( api , t )
2016-07-01 20:28:24 +02:00
2016-07-01 21:14:38 +02:00
e . POST ( "/set" ) . WithJSON ( values ) . Expect ( ) . Status ( iris . StatusOK ) . Cookies ( ) . NotEmpty ( )
e . GET ( "/get" ) . Expect ( ) . Status ( iris . StatusOK ) . JSON ( ) . Object ( ) . Equal ( values )
2016-07-02 18:53:36 +02:00
if EnableSubdomainTests {
e . Request ( "GET" , SubdomainURL + "/get" ) . Expect ( ) . Status ( iris . StatusOK ) . JSON ( ) . Object ( ) . Equal ( values )
2016-07-01 20:28:24 +02:00
}
// test destory which also clears first
2016-07-01 21:14:38 +02:00
d := e . GET ( "/destroy" ) . Expect ( ) . Status ( iris . StatusOK )
2016-07-01 20:28:24 +02:00
d . JSON ( ) . Object ( ) . Empty ( )
2016-07-01 21:14:38 +02:00
d . Cookies ( ) . ContainsOnly ( api . Config . Sessions . Cookie )
2016-07-01 20:28:24 +02:00
// set and clear again
2016-07-01 21:14:38 +02:00
e . POST ( "/set" ) . WithJSON ( values ) . Expect ( ) . Status ( iris . StatusOK ) . Cookies ( ) . NotEmpty ( )
e . GET ( "/clear" ) . Expect ( ) . Status ( iris . StatusOK ) . JSON ( ) . Object ( ) . Empty ( )
2016-07-01 20:28:24 +02:00
}
2016-07-02 15:02:33 +02:00
func FlashMessagesTest ( t * testing . T ) {
api := iris . New ( )
values := map [ string ] string { "name" : "kataras" , "package" : "iris" }
api . Put ( "/set" , func ( ctx * iris . Context ) {
for k , v := range values {
ctx . SetFlash ( k , v )
}
} )
2016-07-02 15:16:42 +02:00
//we don't get the flash so on the next request the flash messages should be available.
api . Get ( "/get_no_getflash" , func ( ctx * iris . Context ) { } )
2016-07-02 15:02:33 +02:00
api . Get ( "/get" , func ( ctx * iris . Context ) {
// one time one handler
kv := make ( map [ string ] string )
for k := range values {
kv [ k ] , _ = ctx . GetFlash ( k )
}
//second time on the same handler
for k := range values {
kv [ k ] , _ = ctx . GetFlash ( k )
}
} , func ( ctx * iris . Context ) {
// third time on a next handler
// test the if next handler has access to them(must) because flash are request lifetime now.
kv := make ( map [ string ] string )
for k := range values {
kv [ k ] , _ = ctx . GetFlash ( k )
}
// print them to the client for test the response also
ctx . JSON ( iris . StatusOK , kv )
} )
2016-07-02 18:53:36 +02:00
e := Tester ( api , t )
2016-07-02 15:16:42 +02:00
e . PUT ( "/set" ) . Expect ( ) . Status ( iris . StatusOK ) . Cookies ( ) . NotEmpty ( )
// just a request which does not use the flash message, so flash messages should be available on the next request
e . GET ( "/get_no_getflash" ) . Expect ( ) . Status ( iris . StatusOK ) . Cookies ( ) . NotEmpty ( )
2016-07-02 15:02:33 +02:00
e . GET ( "/get" ) . Expect ( ) . Status ( iris . StatusOK ) . JSON ( ) . Object ( ) . Equal ( values )
2016-07-02 15:16:42 +02:00
// second request ,the flash messages here should be not available and cookie has been removed
// (the true is that the cookie is removed from the first GetFlash, but is available though the whole request saved on context's values for faster get, keep that secret!)*
g := e . GET ( "/get" ) . Expect ( ) . Status ( iris . StatusOK )
g . JSON ( ) . Object ( ) . Empty ( )
g . Cookies ( ) . Empty ( )
2016-07-02 15:02:33 +02:00
}