Added unit tests for RESTService and HttpParser classes
[arvados.git] / sdk / R / tests / testthat / test-RESTService.R
1 source("fakes/FakeArvados.R")
2
3 context("REST service")
4
5 test_that("create calls REST service properly", {
6
7     expectedURL <- "https://webdavHost/c=aaaaa-j7d0g-ccccccccccccccc/file"
8     fakeHttp <- FakeHttpRequest$new(expectedURL)
9     fakeHttpParser <- FakeHttpParser$new()
10
11     arv <- FakeArvados$new("token",
12                            "https://host/",
13                            "https://webdavHost/",
14                            fakeHttp,
15                            fakeHttpParser)
16
17     REST <- RESTService$new(arv)
18
19     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
20     REST$create("file", uuid)
21
22     expect_that(fakeHttp$URLIsProperlyConfigured, is_true())
23     expect_that(fakeHttp$requestHeaderContainsAuthorizationField, is_true())
24     expect_that(fakeHttp$numberOfPUTRequests, equals(1))
25 }) 
26
27 test_that("create raises exception if server response code is not between 200 and 300", {
28
29     response <- list()
30     response$status_code <- 404
31     fakeHttp <- FakeHttpRequest$new(serverResponse = response)
32
33     arv <- FakeArvados$new("token",
34                            "https://host/",
35                            "https://webdavHost/",
36                            fakeHttp,
37                            FakeHttpParser$new())
38
39     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
40     REST <- RESTService$new(arv)
41
42     expect_that(REST$create("file", uuid),
43                 throws_error("Server code: 404"))
44 }) 
45
46 test_that("delete calls REST service properly", {
47
48     expectedURL <- "https://webdavHost/c=aaaaa-j7d0g-ccccccccccccccc/file"
49     fakeHttp <- FakeHttpRequest$new(expectedURL)
50     fakeHttpParser <- FakeHttpParser$new()
51
52     arv <- FakeArvados$new("token",
53                            "https://host/",
54                            "https://webdavHost/",
55                            fakeHttp,
56                            fakeHttpParser)
57
58     REST <- RESTService$new(arv)
59
60     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
61     REST$delete("file", uuid)
62
63     expect_that(fakeHttp$URLIsProperlyConfigured, is_true())
64     expect_that(fakeHttp$requestHeaderContainsAuthorizationField, is_true())
65     expect_that(fakeHttp$numberOfDELETERequests, equals(1))
66 }) 
67
68 test_that("delete raises exception if server response code is not between 200 and 300", {
69
70     response <- list()
71     response$status_code <- 404
72     fakeHttp <- FakeHttpRequest$new(serverResponse = response)
73
74     arv <- FakeArvados$new("token",
75                            "https://host/",
76                            "https://webdavHost/",
77                            fakeHttp,
78                            FakeHttpParser$new())
79
80     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
81     REST <- RESTService$new(arv)
82
83     expect_that(REST$delete("file", uuid),
84                 throws_error("Server code: 404"))
85 }) 
86
87 test_that("move calls REST service properly", {
88
89     expectedURL <- "https://webdavHost/c=aaaaa-j7d0g-ccccccccccccccc/file"
90     fakeHttp <- FakeHttpRequest$new(expectedURL)
91     fakeHttpParser <- FakeHttpParser$new()
92
93     arv <- FakeArvados$new("token",
94                            "https://host/",
95                            "https://webdavHost/",
96                            fakeHttp,
97                            fakeHttpParser)
98
99     REST <- RESTService$new(arv)
100
101     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
102     REST$move("file", "newDestination/file", uuid)
103
104     expect_that(fakeHttp$URLIsProperlyConfigured, is_true())
105     expect_that(fakeHttp$requestHeaderContainsAuthorizationField, is_true())
106     expect_that(fakeHttp$requestHeaderContainsDestinationField, is_true())
107     expect_that(fakeHttp$numberOfMOVERequests, equals(1))
108 }) 
109
110 test_that("move raises exception if server response code is not between 200 and 300", {
111
112     response <- list()
113     response$status_code <- 404
114     fakeHttp <- FakeHttpRequest$new(serverResponse = response)
115
116     arv <- FakeArvados$new("token",
117                            "https://host/",
118                            "https://webdavHost/",
119                            fakeHttp,
120                            FakeHttpParser$new())
121
122     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
123     REST <- RESTService$new(arv)
124
125     expect_that(REST$move("file", "newDestination/file", uuid),
126                 throws_error("Server code: 404"))
127 }) 
128
129 test_that("getCollectionContent retreives correct content from WebDAV server", {
130
131     expectedURL <- "https://webdavHost/c=aaaaa-j7d0g-ccccccccccccccc"
132     returnContent <- c("animal", "animal/dog", "ball")
133
134     fakeHttp <- FakeHttpRequest$new(expectedURL, returnContent)
135     fakeHttpParser <- FakeHttpParser$new()
136
137     arv <- FakeArvados$new("token",
138                            "https://host/",
139                            "https://webdavHost/",
140                            fakeHttp,
141                            fakeHttpParser)
142
143     REST <- RESTService$new(arv)
144
145     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
146     returnResult <- REST$getCollectionContent(uuid)
147     returnedContentMatchExpected <- all.equal(returnResult,
148                                               c("animal", "animal/dog", "ball"))
149
150     expect_that(returnedContentMatchExpected, is_true())
151     expect_that(fakeHttp$requestHeaderContainsAuthorizationField, is_true())
152 }) 
153
154 test_that("getCollectionContent raises exception if server returns empty response", {
155
156     response <- ""
157     fakeHttp <- FakeHttpRequest$new(serverResponse = response)
158
159     arv <- FakeArvados$new("token",
160                            "https://host/",
161                            "https://webdavHost/",
162                            fakeHttp,
163                            FakeHttpParser$new())
164
165     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
166     REST <- RESTService$new(arv)
167
168     expect_that(REST$getCollectionContent(uuid),
169                 throws_error("Response is empty, request may be misconfigured"))
170 }) 
171
172 test_that("getCollectionContent parses server response", {
173
174     fakeHttp <- FakeHttpRequest$new()
175     fakeHttpParser <- FakeHttpParser$new()
176
177     arv <- FakeArvados$new("token",
178                            "https://host/",
179                            "https://webdavHost/",
180                            fakeHttp,
181                            fakeHttpParser)
182
183     REST <- RESTService$new(arv)
184
185     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
186     REST$getCollectionContent(uuid)
187
188     expect_that(fakeHttpParser$parserCallCount, equals(1))
189 }) 
190
191 test_that("getResourceSize calls REST service properly", {
192
193     expectedURL <- "https://webdavHost/c=aaaaa-j7d0g-ccccccccccccccc/file"
194     expectedContent <- c("6", "2", "931", "12003")
195     fakeHttp <- FakeHttpRequest$new(expectedURL, expectedContent)
196     fakeHttpParser <- FakeHttpParser$new()
197
198     arv <- FakeArvados$new("token",
199                            "https://host/",
200                            "https://webdavHost/",
201                            fakeHttp,
202                            fakeHttpParser)
203
204     REST <- RESTService$new(arv)
205
206     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
207     returnResult <- REST$getResourceSize("file", uuid)
208     returnedContentMatchExpected <- all.equal(returnResult,
209                                               as.numeric(expectedContent))
210
211     expect_that(fakeHttp$URLIsProperlyConfigured, is_true())
212     expect_that(fakeHttp$requestHeaderContainsAuthorizationField, is_true())
213     expect_that(returnedContentMatchExpected, is_true())
214 }) 
215
216 test_that("getResourceSize raises exception if server returns empty response", {
217
218     response <- ""
219     fakeHttp <- FakeHttpRequest$new(serverResponse = response)
220
221     arv <- FakeArvados$new("token",
222                            "https://host/",
223                            "https://webdavHost/",
224                            fakeHttp,
225                            FakeHttpParser$new())
226
227     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
228     REST <- RESTService$new(arv)
229
230     expect_that(REST$getResourceSize("file", uuid),
231                 throws_error("Response is empty, request may be misconfigured"))
232 }) 
233
234 test_that("getResourceSize parses server response", {
235
236     fakeHttp <- FakeHttpRequest$new()
237     fakeHttpParser <- FakeHttpParser$new()
238
239     arv <- FakeArvados$new("token",
240                            "https://host/",
241                            "https://webdavHost/",
242                            fakeHttp,
243                            fakeHttpParser)
244
245     REST <- RESTService$new(arv)
246
247     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
248     REST$getResourceSize("file", uuid)
249
250     expect_that(fakeHttpParser$parserCallCount, equals(1))
251 }) 
252
253 test_that("read calls REST service properly", {
254
255     expectedURL <- "https://webdavHost/c=aaaaa-j7d0g-ccccccccccccccc/file"
256     serverResponse <- list()
257     serverResponse$status_code <- 200
258     serverResponse$content <- "file content"
259
260     fakeHttp <- FakeHttpRequest$new(expectedURL, serverResponse)
261     fakeHttpParser <- FakeHttpParser$new()
262
263     arv <- FakeArvados$new("token",
264                            "https://host/",
265                            "https://webdavHost/",
266                            fakeHttp,
267                            fakeHttpParser)
268
269     REST <- RESTService$new(arv)
270
271     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
272     returnResult <- REST$read("file", uuid, "text", 1024, 512)
273
274     expect_that(fakeHttp$URLIsProperlyConfigured, is_true())
275     expect_that(fakeHttp$requestHeaderContainsAuthorizationField, is_true())
276     expect_that(fakeHttp$requestHeaderContainsRangeField, is_true())
277     expect_that(returnResult, equals("file content"))
278 }) 
279
280 test_that("read raises exception if server response code is not between 200 and 300", {
281
282     response <- list()
283     response$status_code <- 404
284     fakeHttp <- FakeHttpRequest$new(serverResponse = response)
285
286     arv <- FakeArvados$new("token",
287                            "https://host/",
288                            "https://webdavHost/",
289                            fakeHttp,
290                            FakeHttpParser$new())
291
292     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
293     REST <- RESTService$new(arv)
294
295     expect_that(REST$read("file", uuid),
296                 throws_error("Server code: 404"))
297 }) 
298
299 test_that("read raises exception if contentType is not valid", {
300
301     fakeHttp <- FakeHttpRequest$new()
302
303     arv <- FakeArvados$new("token",
304                            "https://host/",
305                            "https://webdavHost/",
306                            fakeHttp,
307                            FakeHttpParser$new())
308
309     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
310     REST <- RESTService$new(arv)
311
312     expect_that(REST$read("file", uuid, "some invalid content type"),
313                 throws_error("Invalid contentType. Please use text or raw."))
314 }) 
315
316 test_that("read parses server response", {
317
318     fakeHttp <- FakeHttpRequest$new()
319     fakeHttpParser <- FakeHttpParser$new()
320
321     arv <- FakeArvados$new("token",
322                            "https://host/",
323                            "https://webdavHost/",
324                            fakeHttp,
325                            fakeHttpParser)
326
327     REST <- RESTService$new(arv)
328
329     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
330     REST$getCollectionContent(uuid)
331
332     expect_that(fakeHttpParser$parserCallCount, equals(1))
333 }) 
334
335 test_that("write calls REST service properly", {
336
337     expectedURL <- "https://webdavHost/c=aaaaa-j7d0g-ccccccccccccccc/file"
338     fakeHttp <- FakeHttpRequest$new(expectedURL)
339     fakeHttpParser <- FakeHttpParser$new()
340
341     arv <- FakeArvados$new("token",
342                            "https://host/",
343                            "https://webdavHost/",
344                            fakeHttp,
345                            fakeHttpParser)
346
347     REST <- RESTService$new(arv)
348
349     fileContent <- "new file content" 
350     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
351     REST$write("file", uuid, fileContent, "text/html")
352
353     expect_that(fakeHttp$URLIsProperlyConfigured, is_true())
354     expect_that(fakeHttp$requestBodyIsProvided, is_true())
355     expect_that(fakeHttp$requestHeaderContainsAuthorizationField, is_true())
356     expect_that(fakeHttp$requestHeaderContainsContentTypeField, is_true())
357 }) 
358
359 test_that("write raises exception if server response code is not between 200 and 300", {
360
361     response <- list()
362     response$status_code <- 404
363     fakeHttp <- FakeHttpRequest$new(serverResponse = response)
364
365     arv <- FakeArvados$new("token",
366                            "https://host/",
367                            "https://webdavHost/",
368                            fakeHttp,
369                            FakeHttpParser$new())
370
371     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
372     fileContent <- "new file content" 
373     REST <- RESTService$new(arv)
374
375     expect_that(REST$write("file", uuid, fileContent, "text/html"),
376                 throws_error("Server code: 404"))
377 })