-
Notifications
You must be signed in to change notification settings - Fork 13
/
recorder_test.go
55 lines (47 loc) · 1.37 KB
/
recorder_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
package appenginetesting
import (
"net/http"
"net/http/httptest"
"fmt"
"testing"
"bytes"
"appengine"
"appengine/datastore"
)
// Using wrapper for unit test.
var contextCreator func(r *http.Request) appengine.Context = appengine.NewContext
func sampleHandler(w http.ResponseWriter, r *http.Request) {
c := contextCreator(r)
k := datastore.NewKey(c, "Entity", "", 1, nil)
e := &Entity{Foo:"foo", Bar:"bar"}
_, err := datastore.Put(c, k, e)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
fmt.Fprint(w, "OK")
}
func TestHandler(t *testing.T) {
r, _ := http.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()
recorder := NewContextRecorder(nil)
contextCreator = recorder.Creator()
sampleHandler(w, r)
defer recorder.Context().Close()
defer func(){
contextCreator = appengine.NewContext
}()
body := w.Body.Bytes()
if !bytes.Equal(body, []byte("OK")) {
t.Errorf("got response %v ; want %v", body, []byte("OK"))
}
var e Entity
c := recorder.Context()
k := datastore.NewKey(c, "Entity", "", 1, nil)
if err := datastore.Get(c, k, &e); err != nil {
t.Errorf(err.Error())
}
if e.Foo != "foo" || e.Bar != "bar" {
t.Errorf("got response %v ; want %v", e, Entity{Foo:"foo", Bar:"bar"})
}
}