-
Notifications
You must be signed in to change notification settings - Fork 8
/
assert.go
50 lines (41 loc) 路 1.28 KB
/
assert.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
package responsetest
import (
"net/http/httptest"
"testing"
response "github.com/linkernetworks/vortex/src/net/http"
"encoding/json"
"encoding/xml"
"github.com/stretchr/testify/assert"
)
// AssertStatusEqual will assert equal status
func AssertStatusEqual(t *testing.T, resp *httptest.ResponseRecorder, status int) {
assert.Equal(t, status, resp.Code)
}
// AssertErrorMessage will assert error message
func AssertErrorMessage(t *testing.T, resp *httptest.ResponseRecorder, msg string) (err error) {
var payload = response.ErrorPayload{}
var out = resp.Body.Bytes()
var contentType = resp.Header().Get("content-type")
switch contentType {
case "application/json", "text/json":
err = json.Unmarshal(out, &payload)
case "application/xml", "text/xml":
err = xml.Unmarshal(out, &payload)
}
assert.Equal(t, msg, payload.Message)
return err
}
// AssertError will assert error
func AssertError(t *testing.T, resp *httptest.ResponseRecorder) (err error) {
var payload = response.ErrorPayload{}
var out = resp.Body.Bytes()
var contentType = resp.Header().Get("content-type")
switch contentType {
case "application/json", "text/json":
err = json.Unmarshal(out, &payload)
case "application/xml", "text/xml":
err = xml.Unmarshal(out, &payload)
}
assert.NoError(t, err)
return err
}