1 source("fakes/FakeArvados.R")
2 source("fakes/FakeHttpRequest.R")
3 source("fakes/FakeHttpParser.R")
5 context("REST service")
7 test_that("getWebDavHostName calls REST service properly", {
9 expectedURL <- "https://host/discovery/v1/apis/arvados/v1/rest"
10 serverResponse <- list(keepWebServiceUrl = "https://myWebDavServer.com")
11 httpRequest <- FakeHttpRequest$new(expectedURL, serverResponse)
13 REST <- RESTService$new("token", "host",
14 httpRequest, FakeHttpParser$new())
16 REST$getWebDavHostName()
18 expect_that(httpRequest$URLIsProperlyConfigured, is_true())
19 expect_that(httpRequest$requestHeaderContainsAuthorizationField, is_true())
20 expect_that(httpRequest$numberOfGETRequests, equals(1))
23 test_that("getWebDavHostName returns webDAV host name properly", {
25 serverResponse <- list(keepWebServiceUrl = "https://myWebDavServer.com")
26 httpRequest <- FakeHttpRequest$new(expectedURL = NULL, serverResponse)
28 REST <- RESTService$new("token", "host",
29 httpRequest, FakeHttpParser$new())
31 expect_that("https://myWebDavServer.com", equals(REST$getWebDavHostName()))
34 test_that("fetchAllItems always returns all resource items from server", {
37 serverResponse <- list(items_available = 8,
38 items = list("collection1",
47 httpParser <- FakeHttpParser$new()
48 httpRequest <- FakeHttpRequest$new(expectedURL, serverResponse)
49 httpRequest$serverMaxElementsPerRequest <- 3
51 REST <- RESTService$new("token", "host",
52 httpRequest, httpParser,
55 result <- REST$fetchAllItems(NULL, NULL)
57 expect_that(length(result), equals(8))
58 expect_that(httpRequest$numberOfGETRequests, equals(3))
59 expect_that(httpParser$parserCallCount, equals(3))
62 test_that("create calls REST service properly", {
64 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
65 expectedURL <- "https://webDavHost/c=aaaaa-j7d0g-ccccccccccccccc/file"
66 fakeHttp <- FakeHttpRequest$new(expectedURL)
67 fakeHttpParser <- FakeHttpParser$new()
69 REST <- RESTService$new("token", "https://host/",
70 fakeHttp, fakeHttpParser,
71 0, "https://webDavHost/")
73 REST$create("file", uuid)
75 expect_that(fakeHttp$URLIsProperlyConfigured, is_true())
76 expect_that(fakeHttp$requestHeaderContainsAuthorizationField, is_true())
77 expect_that(fakeHttp$numberOfPUTRequests, equals(1))
80 test_that("create raises exception if server response code is not between 200 and 300", {
82 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
84 response$status_code <- 404
85 fakeHttp <- FakeHttpRequest$new(serverResponse = response)
87 REST <- RESTService$new("token", "https://host/",
88 fakeHttp, HttpParser$new(),
89 0, "https://webDavHost/")
91 expect_that(REST$create("file", uuid),
92 throws_error("Server code: 404"))
95 test_that("delete calls REST service properly", {
97 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
98 expectedURL <- "https://webDavHost/c=aaaaa-j7d0g-ccccccccccccccc/file"
99 fakeHttp <- FakeHttpRequest$new(expectedURL)
100 fakeHttpParser <- FakeHttpParser$new()
102 REST <- RESTService$new("token", "https://host/",
103 fakeHttp, fakeHttpParser,
104 0, "https://webDavHost/")
106 REST$delete("file", uuid)
108 expect_that(fakeHttp$URLIsProperlyConfigured, is_true())
109 expect_that(fakeHttp$requestHeaderContainsAuthorizationField, is_true())
110 expect_that(fakeHttp$numberOfDELETERequests, equals(1))
113 test_that("delete raises exception if server response code is not between 200 and 300", {
115 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
117 response$status_code <- 404
118 fakeHttp <- FakeHttpRequest$new(serverResponse = response)
120 REST <- RESTService$new("token", "https://host/",
121 fakeHttp, HttpParser$new(),
122 0, "https://webDavHost/")
124 expect_that(REST$delete("file", uuid),
125 throws_error("Server code: 404"))
128 test_that("move calls REST service properly", {
130 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
131 expectedURL <- "https://webDavHost/c=aaaaa-j7d0g-ccccccccccccccc/file"
132 fakeHttp <- FakeHttpRequest$new(expectedURL)
133 fakeHttpParser <- FakeHttpParser$new()
135 REST <- RESTService$new("token", "https://host/",
136 fakeHttp, fakeHttpParser,
137 0, "https://webDavHost/")
139 REST$move("file", "newDestination/file", uuid)
141 expect_that(fakeHttp$URLIsProperlyConfigured, is_true())
142 expect_that(fakeHttp$requestHeaderContainsAuthorizationField, is_true())
143 expect_that(fakeHttp$requestHeaderContainsDestinationField, is_true())
144 expect_that(fakeHttp$numberOfMOVERequests, equals(1))
147 test_that("move raises exception if server response code is not between 200 and 300", {
149 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
151 response$status_code <- 404
152 fakeHttp <- FakeHttpRequest$new(serverResponse = response)
154 REST <- RESTService$new("token", "https://host/",
155 fakeHttp, HttpParser$new(),
156 0, "https://webDavHost/")
158 expect_that(REST$move("file", "newDestination/file", uuid),
159 throws_error("Server code: 404"))
162 test_that("getCollectionContent retreives correct content from WebDAV server", {
164 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
165 expectedURL <- "https://webDavHost/c=aaaaa-j7d0g-ccccccccccccccc"
166 returnContent <- list()
167 returnContent$status_code <- 200
168 returnContent$content <- c("animal", "animal/dog", "ball")
170 fakeHttp <- FakeHttpRequest$new(expectedURL, returnContent)
172 REST <- RESTService$new("token", "https://host/",
173 fakeHttp, FakeHttpParser$new(),
174 0, "https://webDavHost/")
176 returnResult <- REST$getCollectionContent(uuid)
177 returnedContentMatchExpected <- all.equal(returnResult,
178 c("animal", "animal/dog", "ball"))
180 expect_that(returnedContentMatchExpected, is_true())
181 expect_that(fakeHttp$requestHeaderContainsAuthorizationField, is_true())
184 test_that("getCollectionContent raises exception if server returns empty response", {
186 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
188 fakeHttp <- FakeHttpRequest$new(serverResponse = response)
190 REST <- RESTService$new("token", "https://host/",
191 fakeHttp, FakeHttpParser$new(),
192 0, "https://webDavHost/")
194 expect_that(REST$getCollectionContent(uuid),
195 throws_error("Response is empty, request may be misconfigured"))
198 test_that("getCollectionContent parses server response", {
200 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
201 fakeHttpParser <- FakeHttpParser$new()
202 REST <- RESTService$new("token", "https://host/",
203 FakeHttpRequest$new(), fakeHttpParser,
204 0, "https://webDavHost/")
206 REST$getCollectionContent(uuid)
208 expect_that(fakeHttpParser$parserCallCount, equals(1))
211 test_that("getCollectionContent raises exception if server returns empty response", {
213 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
215 fakeHttp <- FakeHttpRequest$new(serverResponse = response)
217 REST <- RESTService$new("token", "https://host/",
218 fakeHttp, FakeHttpParser$new(),
219 0, "https://webDavHost/")
221 expect_that(REST$getCollectionContent(uuid),
222 throws_error("Response is empty, request may be misconfigured"))
225 test_that(paste("getCollectionContent raises exception if server",
226 "response code is not between 200 and 300"), {
228 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
230 response$status_code <- 404
231 fakeHttp <- FakeHttpRequest$new(serverResponse = response)
233 REST <- RESTService$new("token", "https://host/",
234 fakeHttp, HttpParser$new(),
235 0, "https://webDavHost/")
237 expect_that(REST$getCollectionContent(uuid),
238 throws_error("Server code: 404"))
242 test_that("getResourceSize calls REST service properly", {
244 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
245 expectedURL <- "https://webDavHost/c=aaaaa-j7d0g-ccccccccccccccc/file"
247 response$status_code <- 200
248 response$content <- c(6, 2, 931, 12003)
249 fakeHttp <- FakeHttpRequest$new(expectedURL, response)
251 REST <- RESTService$new("token", "https://host/",
252 fakeHttp, FakeHttpParser$new(),
253 0, "https://webDavHost/")
255 returnResult <- REST$getResourceSize("file", uuid)
256 returnedContentMatchExpected <- all.equal(returnResult,
259 expect_that(fakeHttp$URLIsProperlyConfigured, is_true())
260 expect_that(fakeHttp$requestHeaderContainsAuthorizationField, is_true())
261 expect_that(returnedContentMatchExpected, is_true())
264 test_that("getResourceSize raises exception if server returns empty response", {
266 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
268 fakeHttp <- FakeHttpRequest$new(serverResponse = response)
270 REST <- RESTService$new("token", "https://host/",
271 fakeHttp, FakeHttpParser$new(),
272 0, "https://webDavHost/")
274 expect_that(REST$getResourceSize("file", uuid),
275 throws_error("Response is empty, request may be misconfigured"))
278 test_that(paste("getResourceSize raises exception if server",
279 "response code is not between 200 and 300"), {
281 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
283 response$status_code <- 404
284 fakeHttp <- FakeHttpRequest$new(serverResponse = response)
286 REST <- RESTService$new("token", "https://host/",
287 fakeHttp, HttpParser$new(),
288 0, "https://webDavHost/")
290 expect_that(REST$getResourceSize("file", uuid),
291 throws_error("Server code: 404"))
294 test_that("getResourceSize parses server response", {
296 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
297 fakeHttpParser <- FakeHttpParser$new()
298 REST <- RESTService$new("token", "https://host/",
299 FakeHttpRequest$new(), fakeHttpParser,
300 0, "https://webDavHost/")
302 REST$getResourceSize("file", uuid)
304 expect_that(fakeHttpParser$parserCallCount, equals(1))
307 test_that("read calls REST service properly", {
309 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
310 expectedURL <- "https://webDavHost/c=aaaaa-j7d0g-ccccccccccccccc/file"
311 serverResponse <- list()
312 serverResponse$status_code <- 200
313 serverResponse$content <- "file content"
315 fakeHttp <- FakeHttpRequest$new(expectedURL, serverResponse)
317 REST <- RESTService$new("token", "https://host/",
318 fakeHttp, FakeHttpParser$new(),
319 0, "https://webDavHost/")
321 returnResult <- REST$read("file", uuid, "text", 1024, 512)
323 expect_that(fakeHttp$URLIsProperlyConfigured, is_true())
324 expect_that(fakeHttp$requestHeaderContainsAuthorizationField, is_true())
325 expect_that(fakeHttp$requestHeaderContainsRangeField, is_true())
326 expect_that(returnResult, equals("file content"))
329 test_that("read raises exception if server response code is not between 200 and 300", {
331 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
333 response$status_code <- 404
334 fakeHttp <- FakeHttpRequest$new(serverResponse = response)
336 REST <- RESTService$new("token", "https://host/",
337 fakeHttp, HttpParser$new(),
338 0, "https://webDavHost/")
340 expect_that(REST$read("file", uuid),
341 throws_error("Server code: 404"))
344 test_that("read raises exception if contentType is not valid", {
346 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
347 fakeHttp <- FakeHttpRequest$new()
349 REST <- RESTService$new("token", "https://host/",
350 fakeHttp, HttpParser$new(),
351 0, "https://webDavHost/")
353 expect_that(REST$read("file", uuid, "some invalid content type"),
354 throws_error("Invalid contentType. Please use text or raw."))
357 test_that("read parses server response", {
359 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
360 fakeHttpParser <- FakeHttpParser$new()
361 REST <- RESTService$new("token", "https://host/",
362 FakeHttpRequest$new(), fakeHttpParser,
363 0, "https://webDavHost/")
365 REST$read("file", uuid, "text", 1024, 512)
367 expect_that(fakeHttpParser$parserCallCount, equals(1))
370 test_that("write calls REST service properly", {
372 fileContent <- "new file content"
373 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
374 expectedURL <- "https://webDavHost/c=aaaaa-j7d0g-ccccccccccccccc/file"
375 fakeHttp <- FakeHttpRequest$new(expectedURL)
377 REST <- RESTService$new("token", "https://host/",
378 fakeHttp, FakeHttpParser$new(),
379 0, "https://webDavHost/")
381 REST$write("file", uuid, fileContent, "text/html")
383 expect_that(fakeHttp$URLIsProperlyConfigured, is_true())
384 expect_that(fakeHttp$requestBodyIsProvided, is_true())
385 expect_that(fakeHttp$requestHeaderContainsAuthorizationField, is_true())
386 expect_that(fakeHttp$requestHeaderContainsContentTypeField, is_true())
389 test_that("write raises exception if server response code is not between 200 and 300", {
391 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
392 fileContent <- "new file content"
394 response$status_code <- 404
395 fakeHttp <- FakeHttpRequest$new(serverResponse = response)
397 REST <- RESTService$new("token", "https://host/",
398 fakeHttp, HttpParser$new(),
399 0, "https://webDavHost/")
401 expect_that(REST$write("file", uuid, fileContent, "text/html"),
402 throws_error("Server code: 404"))