Debian Codegraph @

server/broker/broker_test.go

raw
1 /*
2 Copyright 2013-2014 Canonical Ltd.
3
4 This program is free software: you can redistribute it and/or modify it
5 under the terms of the GNU General Public License version 3, as published
6 by the Free Software Foundation.
7
8 This program is distributed in the hope that it will be useful, but
9 WITHOUT ANY WARRANTY; without even the implied warranties of
10 MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11 PURPOSE. See the GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License along
14 with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17 package broker
18
19 import (
20 "encoding/json"
21 "fmt"
22
23 . "gopkg.in/check.v1"
24
25 "gitlab.com/ubports/development/core/lomiri-push-service/protocol"
26 )
27
28 type brokerSuite struct{}
29
30 var _ = Suite(&brokerSuite{})
31
32 func (s *brokerSuite) TestErrAbort(c *C) {
33 err := &ErrAbort{"expected FOO"}
34 c.Check(fmt.Sprintf("%s", err), Equals, "session aborted (expected FOO)")
35 }
36
37 func (s *brokerSuite) TestGetInfoString(c *C) {
38 connectMsg := &protocol.ConnectMsg{}
39 v, err := GetInfoString(connectMsg, "foo", "?")
40 c.Check(err, IsNil)
41 c.Check(v, Equals, "?")
42
43 connectMsg.Info = map[string]interface{}{"foo": "yay"}
44 v, err = GetInfoString(connectMsg, "foo", "?")
45 c.Check(err, IsNil)
46 c.Check(v, Equals, "yay")
47
48 connectMsg.Info["foo"] = 33
49 v, err = GetInfoString(connectMsg, "foo", "?")
50 c.Check(err, Equals, ErrUnexpectedValue)
51 }
52
53 func (s *brokerSuite) TestGetInfoInt(c *C) {
54 connectMsg := &protocol.ConnectMsg{}
55 v, err := GetInfoInt(connectMsg, "bar", -1)
56 c.Check(err, IsNil)
57 c.Check(v, Equals, -1)
58
59 err = json.Unmarshal([]byte(`{"bar": 233}`), &connectMsg.Info)
60 c.Assert(err, IsNil)
61 v, err = GetInfoInt(connectMsg, "bar", -1)
62 c.Check(err, IsNil)
63 c.Check(v, Equals, 233)
64
65 connectMsg.Info["bar"] = "garbage"
66 v, err = GetInfoInt(connectMsg, "bar", -1)
67 c.Check(err, Equals, ErrUnexpectedValue)
68 }