-
Notifications
You must be signed in to change notification settings - Fork 1
/
replyer_test.go
executable file
·74 lines (66 loc) · 1.95 KB
/
replyer_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
package gridas
import (
"encoding/json"
"net/http"
"net/http/httptest"
"reflect"
"testing"
"labix.org/v2/mgo/bson"
)
func TestReplyer(t *testing.T) {
setUp(t)
defer tearDown(t)
var obj = Reply{
ID: "1234_abcxxxxxxxxxxxx",
Error: "Errare humanum est",
StatusCode: 102,
Header: make(http.Header), // So it is retrieved from mongoDB. nil does not work
Trailer: make(http.Header), // So it is retrieved from mongoDB. nil does not work
Proto: "protocol",
Body: []byte{1, 2, 3, 0, 9, 8, 7},
Done: bson.Now(),
Created: bson.Now(),
}
replyer := &Replyer{Cfg: cfgTest, SessionSeed: sessionTest}
db := sessionTest.DB(cfgTest.Database)
respColl := db.C(cfgTest.ResponsesColl)
respColl.Insert(obj)
if err := sessionTest.Fsync(false); err != nil {
t.Fatal(err)
}
request, err := http.NewRequest("GET", "/one/two/three/"+obj.ID, nil)
if err != nil {
t.Fatal(err)
}
response := httptest.NewRecorder()
replyer.ServeHTTP(response, request)
if response.Code != 200 {
t.Errorf("response should be 200 for existent reply. reponse.Code %d", response.Code)
}
if contentType := response.Header().Get("Content-Type"); contentType != "application/json" {
t.Errorf("content type of response should be \"application/json\". It is %q", contentType)
}
var objR = Reply{}
err = json.Unmarshal(response.Body.Bytes(), &objR)
if err != nil {
t.Fatalf("%v : %q", err, response.Body.Bytes())
}
if !reflect.DeepEqual(obj, objR) {
t.Error("response body should be equal to body reply %#v %#v", obj, objR)
}
}
func TestReplyerNotFound(t *testing.T) {
setUp(t)
defer tearDown(t)
const id = "1234_abc"
replyer := &Replyer{Cfg: cfgTest, SessionSeed: sessionTest}
request, err := http.NewRequest("GET", "/one/two/three/"+id, nil)
if err != nil {
t.Fatal(err)
}
response := httptest.NewRecorder()
replyer.ServeHTTP(response, request)
if response.Code != 404 {
t.Error("response should be 404 for inexistent reply")
}
}