-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_test.go
More file actions
257 lines (232 loc) · 8.72 KB
/
Copy pathapi_test.go
File metadata and controls
257 lines (232 loc) · 8.72 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package main
import (
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/gorilla/mux"
)
func makeRequest(categoryName string, objectName string, objectVersion string, method string, dev string, body io.Reader) *http.Request {
target := fmt.Sprintf("/%s/%s", categoryName, objectName)
if len(objectVersion) > 0 {
target = fmt.Sprintf("/%s/%s/%s", categoryName, objectName, objectVersion)
}
req := httptest.NewRequest(method, target, body)
urlvars := map[string]string{
"category": categoryName,
"object": objectName,
"version": objectVersion,
}
req = mux.SetURLVars(req, urlvars)
if len(dev) > 0 {
req.URL.RawQuery = fmt.Sprintf("dev=%s", dev)
}
return req
}
func NewMockAPI() *API {
return &API{
Objects: &ObjectController{
bucket: aws.String("unit test"),
path: "dang",
table: aws.String("unit test"),
s3: &MockS3{
bucket: make(map[string]string),
},
ddb: &MockDynamo{
items: []map[string]*dynamodb.AttributeValue{},
},
},
}
}
func TestUpPageHandler(t *testing.T) {
api := NewMockAPI()
res := httptest.NewRecorder()
api.UpPageHandler(res, &http.Request{})
content := res.Body.String()
if content != "Happy" {
t.Fatalf("Up page should always return 'Happy'. Returned: %s", content)
}
}
func TestAddObjectHandler(t *testing.T) {
api := NewMockAPI()
res := httptest.NewRecorder()
req := makeRequest("foo", "test.map.yo", "123ABC", "POST", "", aws.ReadSeekCloser(strings.NewReader("secret sauce")))
api.AddObjectHandler(res, req)
if res.Code != http.StatusOK {
t.Fatalf("AddObjectHandler should have returned a success. Status code: %d", res.Code)
}
response := &JSONResponse{}
_ = json.Unmarshal(res.Body.Bytes(), response)
if response.Status != "ok" {
t.Fatalf("reponse status should be ok on successful response. Was: %s", response.Status)
}
objectContent, err := api.Objects.GetObject("foo/test.map.yo", "123ABC", false)
if err != nil {
t.Fatalf("Unable to get map after storing it. Error: %s", err.Error())
}
content, _ := ioutil.ReadAll(objectContent)
if string(content) != "secret sauce" {
t.Fatalf("Stored map should have the same content as retrieved map. Should be 'secret_sauce'. Was: '%s'", string(content))
}
}
func TestGetObjectHandler(t *testing.T) {
api := NewMockAPI()
res := httptest.NewRecorder()
req := makeRequest("foo", "test.map.yo", "123ABC", "POST", "", aws.ReadSeekCloser(strings.NewReader("secret sauce")))
api.AddObjectHandler(res, req)
if res.Code != http.StatusOK {
t.Fatalf("AddObjectHandler should have returned a success. Status code: %d", res.Code)
}
response := &JSONResponse{}
_ = json.Unmarshal(res.Body.Bytes(), response)
if response.Status != "ok" {
t.Fatalf("reponse status should be ok on successful response. Was: %s", response.Status)
}
getReq := makeRequest("foo", "test.map.yo", "123ABC", "GET", "", nil)
getRes := httptest.NewRecorder()
api.GetObjectHandler(getRes, getReq)
if getRes.Code != http.StatusOK {
t.Fatalf("GetObject request should be successful. Response code: %d", getRes.Code)
}
if getRes.Header().Get("Content-Type") != "application/java-archive" {
t.Fatalf("GetObjectHandler must set content type to application/java-archive. Is: %s", res.Header().Get("content-type"))
}
content := getRes.Body.String()
if string(content) != "secret sauce" {
t.Fatalf("Stored map should have the same content as retrieved map. Should be 'secret_sauce'. Was: '%s'", string(content))
}
}
func TestSetObjectVznHandler(t *testing.T) {
api := NewMockAPI()
// add object, dont set version
res := httptest.NewRecorder()
req := makeRequest("foo", "test.map.yo", "123ABC", "POST", "", aws.ReadSeekCloser(strings.NewReader("secret sauce")))
api.AddObjectHandler(res, req)
if res.Code != http.StatusOK {
t.Fatalf("AddObjectHandler should have returned a success. Status code: %d", res.Code)
}
// Get unversioned object. SHOULD FAIL b/c version has not been set
getObjReq := makeRequest("foo", "test.map.yo", "", "GET", "", nil)
getObjRes := httptest.NewRecorder()
api.GetObjectHandler(getObjRes, getObjReq)
if getObjRes.Code == http.StatusOK {
t.Fatalf("GetObjectHandler should fail when making an unversioned request to an object without a set version")
}
// set object dev version
setVznReqDev := makeRequest("foo", "test.map.yo", "123ABC", "PUT", "true", nil)
setVznResDev := httptest.NewRecorder()
api.SetObjectVersion(setVznResDev, setVznReqDev)
if setVznResDev.Code != http.StatusOK {
t.Fatalf("SetObjectVersion should have returned a success. Status code: %d", setVznResDev.Code)
}
// Get Object Dev Version should succeed
res = httptest.NewRecorder()
req = makeRequest("foo", "test.map.yo", "", "GET", "true", aws.ReadSeekCloser(strings.NewReader("secret sauce")))
api.GetObjectHandler(res, req)
if res.Code != http.StatusOK {
t.Fatalf("GetObjectHandler should have returned a success. Status code: %d", res.Code)
}
// Get Object Prod Version should fail
res = httptest.NewRecorder()
req = makeRequest("foo", "test.map.yo", "", "GET", "", aws.ReadSeekCloser(strings.NewReader("secret sauce")))
api.GetObjectHandler(res, req)
if res.Code == http.StatusOK {
t.Fatalf("GetObjectHandler should have failed because prod version was not set. Status code: %d", res.Code)
}
// set object prod version
setVznReq := makeRequest("foo", "test.map.yo", "123ABC", "PUT", "", nil)
setVznRes := httptest.NewRecorder()
api.SetObjectVersion(setVznRes, setVznReq)
if setVznRes.Code != http.StatusOK {
t.Fatalf("SetObjectVersion should have returned a success. Status code: %d", setVznResDev.Code)
}
// get object prod version should succeed
res = httptest.NewRecorder()
req = makeRequest("foo", "test.map.yo", "", "GET", "", aws.ReadSeekCloser(strings.NewReader("secret sauce")))
api.GetObjectHandler(res, req)
if res.Code != http.StatusOK {
t.Fatalf("GetObjectHandler should have succeeded. Status code: %d", res.Code)
}
}
func TestAPIListRequestsHappy(t *testing.T) {
happyAPI := &API{
Objects: &ObjectController{
bucket: aws.String("unit test"),
path: "dang",
s3: &MockS3{
bucket: map[string]string{
"dang/fun/foo.obj/123abc": "wonderful magic content",
"dang/work/bar.obj/456789": "more incredible content",
},
},
},
}
listCategoriesRes := httptest.NewRecorder()
listCategoriesReq := httptest.NewRequest("GET", "/", nil)
happyAPI.ListCategoriesHandler(listCategoriesRes, listCategoriesReq)
if listCategoriesRes.Code != 200 {
t.Fatalf("API.ListCategoriesHandler should return a 200. Got: %d", listCategoriesRes.Code)
}
listObjectsRes := httptest.NewRecorder()
listObjectsReq := mux.SetURLVars(httptest.NewRequest("GET", "/fun", nil), map[string]string{
"category": "fun",
})
happyAPI.ListObjectsHandler(listObjectsRes, listObjectsReq)
if listObjectsRes.Code != 200 {
t.Fatalf("API.ListObjectsHandler should return a 200. Got %d", listObjectsRes.Code)
}
listVersionsRes := httptest.NewRecorder()
listVersionsReq := mux.SetURLVars(httptest.NewRequest("GET", "/fun/foo.obj/versions", nil), map[string]string{
"category": "fun",
"object": "foo.obj",
})
happyAPI.ListObjectVersionsHandler(listVersionsRes, listVersionsReq)
if listVersionsRes.Code != 200 {
t.Fatalf("API.ListObjectVersionsHandler should return a 200. Got: %d", listVersionsRes.Code)
}
}
func TestAPIListRequestsSad(t *testing.T) {
sadAPI := &API{
Objects: &ObjectController{
bucket: aws.String("unit test"),
path: "dang",
s3: &MockS3{
bucket: map[string]string{
"dang/fun/foo.obj/123abc": "wonderful magic content",
"dang/work/bar.obj/456789": "more incredible content",
},
listObjectsErr: errors.New("boo hoo"),
},
},
}
listCategoriesRes := httptest.NewRecorder()
listCategoriesReq := httptest.NewRequest("GET", "/", nil)
sadAPI.ListCategoriesHandler(listCategoriesRes, listCategoriesReq)
if listCategoriesRes.Code != 500 {
t.Fatalf("API.ListCategoriesHandler should return a 500. Got: %d", listCategoriesRes.Code)
}
listObjectsRes := httptest.NewRecorder()
listObjectsReq := mux.SetURLVars(httptest.NewRequest("GET", "/fun", nil), map[string]string{
"category": "fun",
})
sadAPI.ListObjectsHandler(listObjectsRes, listObjectsReq)
if listObjectsRes.Code != 500 {
t.Fatalf("API.ListObjectsHandler should return a 500. Got %d", listObjectsRes.Code)
}
listVersionsRes := httptest.NewRecorder()
listVersionsReq := mux.SetURLVars(httptest.NewRequest("GET", "/fun/foo.obj/versions", nil), map[string]string{
"category": "fun",
"object": "foo.obj",
})
sadAPI.ListObjectVersionsHandler(listVersionsRes, listVersionsReq)
if listVersionsRes.Code != 500 {
t.Fatalf("API.ListObjectVersionsHandler should return a 500. Got: %d", listVersionsRes.Code)
}
}