/
errors.go
57 lines (49 loc) · 1.69 KB
/
errors.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
// Copyright 2017 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package websockettest
import (
"bufio"
"encoding/json"
"github.com/gorilla/websocket"
jc "github.com/juju/testing/checkers"
gc "gopkg.in/check.v1"
"github.com/juju/juju/rpc/params"
)
// AssertWebsocketClosed checks that the given websocket connection
// is closed.
func AssertWebsocketClosed(c *gc.C, ws *websocket.Conn) {
_, _, err := ws.NextReader()
goodClose := []int{
websocket.CloseNormalClosure,
websocket.CloseGoingAway,
websocket.CloseNoStatusReceived,
}
c.Logf("%#v", err)
c.Assert(websocket.IsCloseError(err, goodClose...), jc.IsTrue)
}
// AssertJSONError checks the JSON encoded error returned by the log
// and logsink APIs matches the expected value.
func AssertJSONError(c *gc.C, ws *websocket.Conn, expected string) {
errResult := ReadJSONErrorLine(c, ws)
c.Assert(errResult.Error, gc.NotNil)
c.Assert(errResult.Error.Message, gc.Matches, expected)
}
// AssertJSONInitialErrorNil checks the JSON encoded error returned by the log
// and logsink APIs are nil.
func AssertJSONInitialErrorNil(c *gc.C, ws *websocket.Conn) {
errResult := ReadJSONErrorLine(c, ws)
c.Assert(errResult.Error, gc.IsNil)
}
// ReadJSONErrorLine returns the error line returned by the log and
// logsink APIS.
func ReadJSONErrorLine(c *gc.C, ws *websocket.Conn) params.ErrorResult {
messageType, reader, err := ws.NextReader()
c.Assert(err, jc.ErrorIsNil)
c.Assert(messageType, gc.Equals, websocket.TextMessage)
line, err := bufio.NewReader(reader).ReadSlice('\n')
c.Assert(err, jc.ErrorIsNil)
var errResult params.ErrorResult
err = json.Unmarshal(line, &errResult)
c.Assert(err, jc.ErrorIsNil)
return errResult
}