File: session_test.go

package info (click to toggle)
golang-github-revel-revel 1.0.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,240 kB
  • sloc: xml: 7; makefile: 7; javascript: 1
file content (64 lines) | stat: -rw-r--r-- 1,534 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Copyright (c) 2012-2018 The Revel Framework Authors, All rights reserved.
// Revel Framework source code and usage is governed by a MIT style
// license that can be found in the LICENSE file.

package session_test

import (
	"fmt"
	"github.com/revel/revel"
	"github.com/revel/revel/session"
	"github.com/stretchr/testify/assert"
	"testing"
)

// test the commands
func TestSessionString(t *testing.T) {
	session.InitSession(revel.RevelLog)
	a := assert.New(t)
	s := session.NewSession()
	s.Set("happy", "day")
	a.Equal("day", s.GetDefault("happy", nil, ""), fmt.Sprintf("Session Data %#v\n", s))

}

func TestSessionStruct(t *testing.T) {
	session.InitSession(revel.RevelLog)
	a := assert.New(t)
	s := session.NewSession()
	setSharedDataTest(s)
	a.Equal("test", s.GetDefault("happy.a.aa", nil, ""), fmt.Sprintf("Session Data %#v\n", s))

	stringMap := s.Serialize()
	s1 := session.NewSession()
	s1.Load(stringMap)
	testSharedData(s, s1, t, a)

}

func setSharedDataTest(s session.Session) {
	data := struct {
		A struct {
			Aa string
		}
		B int
		C string
		D float32
	}{A: struct {
		Aa string
	}{Aa: "test"},
		B: 5,
		C: "test",
		D: -325.25}
	s.Set("happy", data)
}
func testSharedData(s, s1 session.Session, t *testing.T, a *assert.Assertions) {
	// Compress the session to a string
	t.Logf("Original session %#v\n", s)
	t.Logf("New built session %#v\n", s1)
	data,err := s1.Get("happy.a.aa")
	a.Nil(err,"Expected nil")
	a.Equal("test", data, fmt.Sprintf("Session Data %#v\n", s))
	t.Logf("After test session %#v\n", s1)

}