-
Notifications
You must be signed in to change notification settings - Fork 0
/
simpleresponse_test.go
75 lines (69 loc) · 2.37 KB
/
simpleresponse_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
package gorest
import (
"bytes"
"testing"
)
// TestNewSimpleResponse creates a SimpleResponse (with and without format) and
// verifies internal fields
func TestNewSimpleResponse(t *testing.T) {
// Create without format.
r := NewSimpleResponse("TAG", "XXX")
if r.Status != "TAG" {
t.Fatalf("Unexpected status. Expected: %s - Found: %s.", "TAG", r.Status)
}
if r.Message != "XXX" {
t.Fatalf("Unexpected message. Expected: %s - Found: %s.", "XXX", r.Message)
}
// Create with format.
r = NewSimpleResponsef("TAG2", "the%s", "message")
if r.Status != "TAG2" {
t.Fatalf("Unexpected status. Expected: %s - Found: %s.", "TAG2", r.Status)
}
if r.Message != "themessage" {
t.Fatalf("Unexpected message. Expected: %s - Found: %s.", "themessage", r.Message)
}
}
// TestNewFailResponse creates a SimpleResponse with NAK status by using
// NewFailResponse(f) functions and verifies internal fields
func TestNewFailResponse(t *testing.T) {
// Create without format.
r := NewFailResponse("XXX")
if r.Status != NAK {
t.Fatalf("Unexpected status. Expected: %s - Found: %s.", NAK, r.Status)
}
if r.Message != "XXX" {
t.Fatalf("Unexpected message. Expected: %s - Found: %s.", "XXX", r.Message)
}
// Create with format.
r = NewFailResponsef("the%s", "message")
if r.Status != NAK {
t.Fatalf("Unexpected status. Expected: %s - Found: %s.", NAK, r.Status)
}
if r.Message != "themessage" {
t.Fatalf("Unexpected message. Expected: %s - Found: %s.", "themessage", r.Message)
}
}
// TestSimpleResponseGetBody verifies the body of the SimpleResponse is
// properly created.
func TestSimpleResponseGetBody(t *testing.T) {
r := NewSimpleResponse("X", "Y")
body, err := r.GetBody()
if err != nil {
t.Fatalf("Unexpected error: %s.", err.Error())
}
expected := []byte("{\"status\":\"X\",\"message\":\"Y\"}")
if bytes.Compare(body, expected) != 0 {
t.Fatalf("Unexpected body. Expected: %s - Found: %s.", string(expected), string(body))
}
}
// TestSimpleResponseGetCookieAndHeaders verifies that nil is returned by the
// GetCookie and GetHeaders functions.
func TestSimpleResponseGetCookieAndHeaders(t *testing.T) {
r := NewSimpleResponse("A", "B")
if cookie := r.GetCookie(); cookie != nil {
t.Fatalf("Unexpected cookie found. Should be nil, found: %+v.", cookie)
}
if headers := r.GetHeaders(); headers != nil {
t.Fatalf("Unexpected headers found. Should be nil, found: %+v.", headers)
}
}