This repository has been archived by the owner on Oct 15, 2019. It is now read-only.
forked from seven5/seven5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase_test.go
119 lines (104 loc) · 3.13 KB
/
base_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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package seven5
import (
"fmt"
"io/ioutil"
"net/http"
"strings"
"testing"
)
var called = 0x00
var allow = false
var newRezId = int64(1200010)
type allowResource struct {
//resource
}
func (self *allowResource) Index(pb PBundle) (interface{}, error) {
called |= 0x01
return nil, nil
}
func (self *allowResource) Find(id int64, pb PBundle) (interface{}, error) {
called |= 0x02
return nil, nil
}
func (self *allowResource) Delete(id int64, pb PBundle) (interface{}, error) {
called |= 0x04
return nil, nil
}
func (self *allowResource) Put(id int64, i interface{}, pb PBundle) (interface{}, error) {
called |= 0x08
return nil, nil
}
func (self *allowResource) Post(i interface{}, pb PBundle) (interface{}, error) {
called |= 0x10
return &someWire{newRezId, ""}, nil
}
func (self *allowResource) AllowRead(pb PBundle) bool {
return allow
}
func (self *allowResource) AllowWrite(pb PBundle) bool {
return allow
}
func (self *allowResource) Allow(id int64, method string, pb PBundle) bool {
return allow
}
func TestAllow(t *testing.T) {
sm := NewDumbSessionManager()
cm := NewSimpleCookieMapper("myappname")
base := NewBaseDispatcher(sm, cm)
serveMux := NewServeMux()
serveMux.Dispatch("/rest/", base)
res := &allowResource{}
base.Rez(&someWire{}, res)
go func() {
http.ListenAndServe(":8191", serveMux)
}()
client := new(http.Client)
for _, b := range []bool{false, true} {
allow = b
status := http.StatusUnauthorized
if b {
status = http.StatusOK
}
makeRequestCheckStatusNullBody(t, client, "GET", "http://localhost:8191/rest/somewire", "", status)
makeRequestCheckStatusNullBody(t, client, "GET", "http://localhost:8191/rest/somewire/12", "", status)
makeRequestCheckStatusNullBody(t, client, "POST", "http://localhost:8191/rest/somewire", "{}", status)
makeRequestCheckStatusNullBody(t, client, "PUT", "http://localhost:8191/rest/somewire/345", "{}", status)
makeRequestCheckStatusNullBody(t, client, "DELETE", "http://localhost:8191/rest/somewire/678", "", status)
}
}
func makeRequestCheckStatusNullBody(t *testing.T, client *http.Client, method string, url string, body string,
status int) {
req := makeReq(t, method, url, body)
resp, err := client.Do(req)
if method == "POST" && status == http.StatusOK {
status = http.StatusCreated
}
checkHttpStatus(t, resp, err, status)
if method == "POST" && status == http.StatusOK {
loc := resp.Header.Get("Location")
if loc != fmt.Sprintf("/rest/frobnitz/%d", newRezId) {
t.Fatalf("Didn't find the new resource we were expecting on POST (create): %s\n", loc)
}
}
all, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Fatalf("failed to read the body: %s", err)
}
read := string(all)
if status == http.StatusUnauthorized {
if !strings.HasPrefix(read, "Not authorized") {
t.Errorf("expected not authorized message but got '%s'", read)
}
} else {
if method == "POST" {
if strings.Index(read, fmt.Sprintf("%d", newRezId)) == -1 {
t.Errorf("Probably a bad body found on POST (%d) '%s'", len(read), read)
}
} else {
//nil is "null" in JSON
if read != "null" {
t.Errorf("expected null body but got (%d) '%s'", len(read), read)
}
}
}
}