-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi_test.go
145 lines (123 loc) · 3.65 KB
/
api_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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package main
import (
"encoding/json"
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestApiHandler(t *testing.T) {
req := httptest.NewRequest("GET", "http://localhost/api?tag=fred", nil)
w := httptest.NewRecorder()
s := stubStorage{
GetTagWikisFunc: func(tag string) Tag {
return Tag{TagName: "fred"}
},
}
innerAPIHandler(w, req, &s)
resp := w.Result()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Errorf("Failed to read response, error: %v", err.Error())
}
if resp.StatusCode != 200 {
t.Errorf("Failed to get a 200 response, got %v", resp.StatusCode)
}
var results Tag
err = json.Unmarshal(data, &results)
if err != nil {
t.Errorf("Failed to read json data, error: %v, data: '%v'", err.Error(), string(data))
}
if results.TagName != "fred" {
t.Errorf("Got the wrong tag back, expected 'fred' but got '%v'", results.TagName)
}
}
func TestApiHandlerNoTag(t *testing.T) {
req := httptest.NewRequest("GET", "http://localhost/api", nil)
w := httptest.NewRecorder()
s := stubStorage{}
innerAPIHandler(w, req, &s)
resp := w.Result()
if resp.StatusCode != http.StatusBadRequest {
t.Errorf("Failed to get a %v response, got %v", http.StatusBadRequest, resp.StatusCode)
}
}
func TestWikiApiGetHandler(t *testing.T) {
req := httptest.NewRequest("GET", "http://localhost/api?wiki=fred", nil)
w := httptest.NewRecorder()
s := stubStorage{
getPageFunc: func(pg *wikiPage) (*wikiPage, error) {
return pg, nil
},
GetTagWikisFunc: func(tag string) Tag {
return Tag{TagName: "fred"}
},
}
innerAPIHandler(w, req, &s)
resp := w.Result()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Errorf("Failed to read response, error: %v", err.Error())
}
if resp.StatusCode != 200 {
t.Errorf("Failed to get a 200 response, got %v", resp.StatusCode)
}
var results wikiPage
err = json.Unmarshal(data, &results)
if err != nil {
t.Errorf("Failed to read json data, error: %v, data: '%v'", err.Error(), string(data))
}
if results.Title != "fred" {
t.Errorf("Got the wrong page back, expected 'fred' but got '%v'", results.Title)
}
}
func TestWikiApiPostHandler(t *testing.T) {
data, _ := json.Marshal(wikiPage{basePage: basePage{Title: "fred"}, Body: "Some markup"})
req := httptest.NewRequest("POST", "http://localhost/api?wiki=fred", strings.NewReader(string(data)))
w := httptest.NewRecorder()
s := stubStorage{
getPageFunc: func(pg *wikiPage) (*wikiPage, error) {
return pg, nil
},
storeFileFunc: func(name string, body []byte) error {
t.Logf("storeFile: Got %v/n", name)
// Check the first part of the string as the store file func will be called for the tags file and the
// md file
if !strings.HasPrefix(name, "fred") {
t.Errorf("expecting %v but got %v", "fred", name)
}
return nil
},
}
innerAPIHandler(w, req, &s)
resp := w.Result()
if resp.StatusCode != 200 {
t.Errorf("Failed to get a 200 response, got %v", resp.StatusCode)
}
}
func TestWApiListHandler(t *testing.T) {
req := httptest.NewRequest("GET", "http://localhost/api?list=fred", nil)
w := httptest.NewRecorder()
s := stubStorage{}
innerAPIHandler(w, req, &s)
resp := w.Result()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Errorf("Failed to read response, error: %v", err.Error())
}
if resp.StatusCode != 200 {
t.Errorf("Failed to get a 200 response, got %v", resp.StatusCode)
}
if len(data) == 0 {
t.Errorf("Got zero data back: %v", len(data))
}
var results []string
err = json.Unmarshal(data, &results)
if err != nil {
t.Errorf("Error back from json unmarshal: %v", err)
}
if len(results) != 2 {
t.Errorf("Expecting 2 results but got : %v", len(results))
}
}