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
/
clientSessionInfo_test.go
181 lines (166 loc) · 4.9 KB
/
clientSessionInfo_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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package test
import (
"context"
"reflect"
"testing"
"time"
webwire "github.com/qbeon/webwire-go"
webwireClient "github.com/qbeon/webwire-go/client"
)
// TestClientSessionInfo tests the client.SessionInfo getter method
func TestClientSessionInfo(t *testing.T) {
expectedBool := true
expectedString := "somesamplestring1234"
expectedInt := uint32(404)
expectedNumber := float64(7.62)
expectedArray := []string{"first", "second"}
expectedStruct := struct {
SampleString string `json:"struct_string"`
}{
SampleString: "sample struct string value",
}
// 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)
// Try to create a new session
if err := msg.Client.CreateSession(struct {
SampleBool bool `json:"bool"`
SampleString string `json:"string"`
SampleInt uint32 `json:"int"`
SampleNumber float64 `json:"number"`
SampleArray []string `json:"array"`
SampleStruct struct {
SampleString string `json:"struct_string"`
} `json:"struct"`
}{
SampleBool: expectedBool,
SampleString: expectedString,
SampleInt: expectedInt,
SampleNumber: expectedNumber,
SampleArray: expectedArray,
SampleStruct: struct {
SampleString string `json:"struct_string"`
}{
SampleString: expectedStruct.SampleString,
},
}); err != nil {
return webwire.Payload{}, err
}
return webwire.Payload{}, nil
},
// Define dummy hooks to enable sessions on this server
OnSessionCreated: func(_ *webwire.Client) error { return nil },
OnSessionLookup: func(_ string) (*webwire.Session, error) { return nil, nil },
OnSessionClosed: func(_ *webwire.Client) error { return nil },
},
},
)
// Initialize client
client := webwireClient.NewClient(
addr,
webwireClient.Options{
DefaultRequestTimeout: 2 * time.Second,
},
)
defer client.Close()
if err := client.Connect(); err != nil {
t.Fatalf("Couldn't connect: %s", err)
}
// Send authentication request and await reply
if _, err := client.Request(
"login",
webwire.Payload{Data: []byte("credentials")},
); err != nil {
t.Fatalf("Request failed: %s", err)
}
// Verify getting inexistent field
inexistent := client.SessionInfo("inexistent")
if inexistent != nil {
t.Fatalf("Expected nil for inexistent session info field, got: %v", inexistent)
}
// Verify field: bool
samplebool, ok := client.SessionInfo("bool").(bool)
if !ok {
t.Fatalf(
"Expected field 'bool' to be a boolean, got: %v",
reflect.TypeOf(client.SessionInfo("bool")),
)
}
if samplebool != expectedBool {
t.Fatalf("Expected bool %t for field %s", expectedBool, "bool")
}
// Verify field: string
samplestring, ok := client.SessionInfo("string").(string)
if !ok {
t.Fatalf(
"Expected field 'string' to be a string, got: %v",
reflect.TypeOf(client.SessionInfo("string")),
)
}
if samplestring != expectedString {
t.Fatalf("Expected string %s for field %s", expectedString, "string")
}
// Verify field: int
sampleint, ok := client.SessionInfo("int").(float64)
if !ok {
t.Fatalf(
"Expected field 'int' to be a float64, got: %v",
reflect.TypeOf(client.SessionInfo("int")),
)
}
if uint32(sampleint) != expectedInt {
t.Fatalf("Expected uint32 (from float64) %d for field %s", expectedInt, "int")
}
// Verify field: number
samplenumber, ok := client.SessionInfo("number").(float64)
if !ok {
t.Fatalf(
"Expected field 'number' to be a float64, got: %v",
reflect.TypeOf(client.SessionInfo("number")),
)
}
if samplenumber != expectedNumber {
t.Fatalf("Expected float64 number %f for field %s", expectedNumber, "number")
}
// Verify field: array
samplearray, ok := client.SessionInfo("array").([]interface{})
if !ok {
t.Fatalf(
"Expected field 'array' to be an array of empty interfaces, got: %v",
reflect.TypeOf(client.SessionInfo("array")),
)
}
for index, value := range samplearray {
valStr, ok := value.(string)
if !ok || expectedArray[index] != valStr {
t.Fatalf(
"Expected array item at index %d to be string('%s'), got: %v",
index,
expectedArray[index],
value,
)
}
}
// Verify field: struct
samplestruct, ok := client.SessionInfo("struct").(map[string]interface{})
if !ok {
t.Fatalf(
"Expected field 'struct' to be a map of string -> empty interface, got: %v",
reflect.TypeOf(client.SessionInfo("struct")),
)
}
samplestructString, ok := samplestruct["struct_string"].(string)
if !ok || samplestructString != expectedStruct.SampleString {
t.Fatalf(
"Expected struct field 'struct_string' to be a string('%s'), got: %v",
expectedStruct.SampleString,
samplestruct["struct_string"],
)
}
}