This repository has been archived by the owner on May 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
clientAutomaticSessionRestoration_test.go
131 lines (111 loc) · 3.57 KB
/
clientAutomaticSessionRestoration_test.go
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package test
import (
"context"
"fmt"
"testing"
webwire "github.com/qbeon/webwire-go"
webwireClient "github.com/qbeon/webwire-go/client"
)
// TestClientAutomaticSessionRestoration verifies automatic session restoration
// on connection establishment
func TestClientAutomaticSessionRestoration(t *testing.T) {
sessionStorage := make(map[string]*webwire.Session)
currentStep := 1
var createdSession *webwire.Session
// Initialize webwire server
_, addr := setupServer(
t,
webwire.ServerOptions{
SessionsEnabled: true,
Hooks: webwire.Hooks{
OnRequest: func(ctx context.Context) (webwire.Payload, error) {
// Extract request message and requesting client from the context
msg := ctx.Value(webwire.Msg).(webwire.Message)
if currentStep == 2 {
// Expect the session to have been automatically restored
compareSessions(t, createdSession, msg.Client.Session)
return webwire.Payload{}, nil
}
// Try to create a new session
if err := msg.Client.CreateSession(nil); err != nil {
return webwire.Payload{}, err
}
// Return the key of the newly created session
return webwire.Payload{}, nil
},
// Permanently store the session
OnSessionCreated: func(client *webwire.Client) error {
sessionStorage[client.Session.Key] = client.Session
return nil
},
// Find session by key
OnSessionLookup: func(key string) (*webwire.Session, error) {
// Expect the key of the created session to be looked up
if key != createdSession.Key {
err := fmt.Errorf(
"Expected and looked up session keys differ: %s | %s",
createdSession.Key,
key,
)
t.Fatalf("Session lookup mismatch: %s", err)
return nil, err
}
if session, exists := sessionStorage[key]; exists {
return session, nil
}
// Expect the session to be found
t.Fatalf(
"Expected session (%s) not found in: %v",
createdSession.Key,
sessionStorage,
)
return nil, nil
},
// Define dummy hook to enable sessions on this server
OnSessionClosed: func(_ *webwire.Client) error { return nil },
},
},
)
// Initialize client
client := webwireClient.NewClient(addr, webwireClient.Options{})
if err := client.Connect(); err != nil {
t.Fatalf("Couldn't connect: %s", err)
}
/*****************************************************************\
Step 1 - Create session and disconnect
\*****************************************************************/
// Create a new session
if _, err := client.Request(
"login",
webwire.Payload{Data: []byte("auth")},
); err != nil {
t.Fatalf("Auth request failed: %s", err)
}
tmp := client.Session()
createdSession = &tmp
// Disconnect client without closing the session
client.Close()
// Ensure the session isn't lost
if client.IsConnected() {
t.Fatal("Client is expected to be disconnected")
}
if client.Session().Key == "" {
t.Fatal("Session lost after disconnection")
}
/*****************************************************************\
Step 2 - Reconnect, restore and verify authentication
\*****************************************************************/
currentStep = 2
// Reconnect (this should automatically try to restore the session)
if err := client.Connect(); err != nil {
t.Fatalf("Couldn't reconnect: %s", err)
}
// Verify whether the previous session was restored automatically
// and the server authenticates the user
if _, err := client.Request(
"verify",
webwire.Payload{Data: []byte("isrestored?")},
); err != nil {
t.Fatalf("Second request failed: %s", err)
}
}