1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: Apache-2.0
5 source("fakes/FakeArvados.R")
6 source("fakes/FakeHttpRequest.R")
7 source("fakes/FakeHttpParser.R")
9 context("REST service")
11 test_that("getWebDavHostName calls REST service properly", {
13 expectedURL <- "https://host/arvados/v1/config"
14 serverResponse <- list(Services = list(WebDAVDownload = list(ExternalURL = "https://myWebDavServer.com")))
15 httpRequest <- FakeHttpRequest$new(expectedURL, serverResponse)
17 REST <- RESTService$new("token", "host",
18 httpRequest, FakeHttpParser$new())
20 REST$getWebDavHostName()
22 expect_true(httpRequest$URLIsProperlyConfigured)
23 expect_false(httpRequest$requestHeaderContainsAuthorizationField)
24 expect_that(httpRequest$numberOfGETRequests, equals(1))
27 test_that("getWebDavHostName returns webDAV host name properly", {
29 serverResponse <- list(Services = list(WebDAVDownload = list(ExternalURL = "https://myWebDavServer.com")))
30 httpRequest <- FakeHttpRequest$new(expectedURL = NULL, serverResponse)
32 REST <- RESTService$new("token", "host",
33 httpRequest, FakeHttpParser$new())
35 expect_that("https://myWebDavServer.com", equals(REST$getWebDavHostName()))
38 test_that("create calls REST service properly", {
40 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
41 expectedURL <- "https://webDavHost/c=aaaaa-j7d0g-ccccccccccccccc/file"
42 fakeHttp <- FakeHttpRequest$new(expectedURL)
43 fakeHttpParser <- FakeHttpParser$new()
45 REST <- RESTService$new("token", "https://host/",
46 fakeHttp, fakeHttpParser,
47 0, "https://webDavHost/")
49 REST$create("file", uuid)
51 expect_true(fakeHttp$URLIsProperlyConfigured)
52 expect_true(fakeHttp$requestHeaderContainsAuthorizationField)
53 expect_that(fakeHttp$numberOfPUTRequests, equals(1))
56 test_that("create raises exception if server response code is not between 200 and 300", {
58 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
60 response$status_code <- 404
61 fakeHttp <- FakeHttpRequest$new(serverResponse = response)
63 REST <- RESTService$new("token", "https://host/",
64 fakeHttp, HttpParser$new(),
65 0, "https://webDavHost/")
67 expect_that(REST$create("file", uuid),
68 throws_error("Server code: 404"))
71 test_that("delete calls REST service properly", {
73 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
74 expectedURL <- "https://webDavHost/c=aaaaa-j7d0g-ccccccccccccccc/file"
75 fakeHttp <- FakeHttpRequest$new(expectedURL)
76 fakeHttpParser <- FakeHttpParser$new()
78 REST <- RESTService$new("token", "https://host/",
79 fakeHttp, fakeHttpParser,
80 0, "https://webDavHost/")
82 REST$delete("file", uuid)
84 expect_true(fakeHttp$URLIsProperlyConfigured)
85 expect_true(fakeHttp$requestHeaderContainsAuthorizationField)
86 expect_that(fakeHttp$numberOfDELETERequests, equals(1))
89 test_that("delete raises exception if server response code is not between 200 and 300", {
91 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
93 response$status_code <- 404
94 fakeHttp <- FakeHttpRequest$new(serverResponse = response)
96 REST <- RESTService$new("token", "https://host/",
97 fakeHttp, HttpParser$new(),
98 0, "https://webDavHost/")
100 expect_that(REST$delete("file", uuid),
101 throws_error("Server code: 404"))
104 test_that("move calls REST service properly", {
106 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
107 expectedURL <- "https://webDavHost/c=aaaaa-j7d0g-ccccccccccccccc/file"
108 fakeHttp <- FakeHttpRequest$new(expectedURL)
109 fakeHttpParser <- FakeHttpParser$new()
111 REST <- RESTService$new("token", "https://host/",
112 fakeHttp, fakeHttpParser,
113 0, "https://webDavHost/")
115 REST$move("file", "newDestination/file", uuid)
117 expect_true(fakeHttp$URLIsProperlyConfigured)
118 expect_true(fakeHttp$requestHeaderContainsAuthorizationField)
119 expect_true(fakeHttp$requestHeaderContainsDestinationField)
120 expect_that(fakeHttp$numberOfMOVERequests, equals(1))
123 test_that("move raises exception if server response code is not between 200 and 300", {
125 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
127 response$status_code <- 404
128 fakeHttp <- FakeHttpRequest$new(serverResponse = response)
130 REST <- RESTService$new("token", "https://host/",
131 fakeHttp, HttpParser$new(),
132 0, "https://webDavHost/")
134 expect_that(REST$move("file", "newDestination/file", uuid),
135 throws_error("Server code: 404"))
138 test_that("copy calls REST service properly", {
140 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
141 expectedURL <- "https://webDavHost/c=aaaaa-j7d0g-ccccccccccccccc/file"
142 fakeHttp <- FakeHttpRequest$new(expectedURL)
143 fakeHttpParser <- FakeHttpParser$new()
145 REST <- RESTService$new("token", "https://host/",
146 fakeHttp, fakeHttpParser,
147 0, "https://webDavHost/")
149 REST$copy("file", "newDestination/file", uuid)
151 expect_true(fakeHttp$URLIsProperlyConfigured)
152 expect_true(fakeHttp$requestHeaderContainsAuthorizationField)
153 expect_true(fakeHttp$requestHeaderContainsDestinationField)
154 expect_that(fakeHttp$numberOfCOPYRequests, equals(1))
157 test_that("copy raises exception if server response code is not between 200 and 300", {
159 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
161 response$status_code <- 404
162 fakeHttp <- FakeHttpRequest$new(serverResponse = response)
164 REST <- RESTService$new("token", "https://host/",
165 fakeHttp, HttpParser$new(),
166 0, "https://webDavHost/")
168 expect_that(REST$copy("file", "newDestination/file", uuid),
169 throws_error("Server code: 404"))
172 test_that("getCollectionContent retreives correct content from WebDAV server", {
174 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
175 expectedURL <- "https://webDavHost/c=aaaaa-j7d0g-ccccccccccccccc"
176 returnContent <- list()
177 returnContent$status_code <- 200
178 returnContent$content <- c("animal", "animal/dog", "ball")
180 fakeHttp <- FakeHttpRequest$new(expectedURL, returnContent)
182 REST <- RESTService$new("token", "https://host/",
183 fakeHttp, FakeHttpParser$new(),
184 0, "https://webDavHost/")
186 returnResult <- REST$getCollectionContent(uuid)
187 returnedContentMatchExpected <- all.equal(returnResult,
188 c("animal", "animal/dog", "ball"))
190 expect_true(returnedContentMatchExpected)
191 expect_true(fakeHttp$requestHeaderContainsAuthorizationField)
194 test_that("getCollectionContent raises exception if server returns empty response", {
196 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
198 fakeHttp <- FakeHttpRequest$new(serverResponse = response)
200 REST <- RESTService$new("token", "https://host/",
201 fakeHttp, FakeHttpParser$new(),
202 0, "https://webDavHost/")
204 expect_that(REST$getCollectionContent(uuid),
205 throws_error("Response is empty, request may be misconfigured"))
208 test_that("getCollectionContent parses server response", {
210 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
211 fakeHttpParser <- FakeHttpParser$new()
212 REST <- RESTService$new("token", "https://host/",
213 FakeHttpRequest$new(), fakeHttpParser,
214 0, "https://webDavHost/")
216 REST$getCollectionContent(uuid)
218 expect_that(fakeHttpParser$parserCallCount, equals(1))
221 test_that("getCollectionContent raises exception if server returns empty response", {
223 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
225 fakeHttp <- FakeHttpRequest$new(serverResponse = response)
227 REST <- RESTService$new("token", "https://host/",
228 fakeHttp, FakeHttpParser$new(),
229 0, "https://webDavHost/")
231 expect_that(REST$getCollectionContent(uuid),
232 throws_error("Response is empty, request may be misconfigured"))
235 test_that(paste("getCollectionContent raises exception if server",
236 "response code is not between 200 and 300"), {
238 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
240 response$status_code <- 404
241 fakeHttp <- FakeHttpRequest$new(serverResponse = response)
243 REST <- RESTService$new("token", "https://host/",
244 fakeHttp, HttpParser$new(),
245 0, "https://webDavHost/")
247 expect_that(REST$getCollectionContent(uuid),
248 throws_error("Server code: 404"))
252 test_that("getResourceSize calls REST service properly", {
254 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
255 expectedURL <- "https://webDavHost/c=aaaaa-j7d0g-ccccccccccccccc/file"
257 response$status_code <- 200
258 response$content <- c(6, 2, 931, 12003)
259 fakeHttp <- FakeHttpRequest$new(expectedURL, response)
261 REST <- RESTService$new("token", "https://host/",
262 fakeHttp, FakeHttpParser$new(),
263 0, "https://webDavHost/")
265 returnResult <- REST$getResourceSize("file", uuid)
266 returnedContentMatchExpected <- all.equal(returnResult,
269 expect_true(fakeHttp$URLIsProperlyConfigured)
270 expect_true(fakeHttp$requestHeaderContainsAuthorizationField)
271 expect_true(returnedContentMatchExpected)
274 test_that("getResourceSize raises exception if server returns empty response", {
276 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
278 fakeHttp <- FakeHttpRequest$new(serverResponse = response)
280 REST <- RESTService$new("token", "https://host/",
281 fakeHttp, FakeHttpParser$new(),
282 0, "https://webDavHost/")
284 expect_that(REST$getResourceSize("file", uuid),
285 throws_error("Response is empty, request may be misconfigured"))
288 test_that(paste("getResourceSize raises exception if server",
289 "response code is not between 200 and 300"), {
291 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
293 response$status_code <- 404
294 fakeHttp <- FakeHttpRequest$new(serverResponse = response)
296 REST <- RESTService$new("token", "https://host/",
297 fakeHttp, HttpParser$new(),
298 0, "https://webDavHost/")
300 expect_that(REST$getResourceSize("file", uuid),
301 throws_error("Server code: 404"))
304 test_that("getResourceSize parses server response", {
306 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
307 fakeHttpParser <- FakeHttpParser$new()
308 REST <- RESTService$new("token", "https://host/",
309 FakeHttpRequest$new(), fakeHttpParser,
310 0, "https://webDavHost/")
312 REST$getResourceSize("file", uuid)
314 expect_that(fakeHttpParser$parserCallCount, equals(1))
317 test_that("read calls REST service properly", {
319 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
320 expectedURL <- "https://webDavHost/c=aaaaa-j7d0g-ccccccccccccccc/file"
321 serverResponse <- list()
322 serverResponse$status_code <- 200
323 serverResponse$content <- "file content"
325 fakeHttp <- FakeHttpRequest$new(expectedURL, serverResponse)
327 REST <- RESTService$new("token", "https://host/",
328 fakeHttp, FakeHttpParser$new(),
329 0, "https://webDavHost/")
331 returnResult <- REST$read("file", uuid, "text", 1024, 512)
333 expect_true(fakeHttp$URLIsProperlyConfigured)
334 expect_true(fakeHttp$requestHeaderContainsAuthorizationField)
335 expect_true(fakeHttp$requestHeaderContainsRangeField)
336 expect_that(returnResult, equals("file content"))
339 test_that("read raises exception if server response code is not between 200 and 300", {
341 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
343 response$status_code <- 404
344 fakeHttp <- FakeHttpRequest$new(serverResponse = response)
346 REST <- RESTService$new("token", "https://host/",
347 fakeHttp, HttpParser$new(),
348 0, "https://webDavHost/")
350 expect_that(REST$read("file", uuid),
351 throws_error("Server code: 404"))
354 test_that("read raises exception if contentType is not valid", {
356 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
357 fakeHttp <- FakeHttpRequest$new()
359 REST <- RESTService$new("token", "https://host/",
360 fakeHttp, HttpParser$new(),
361 0, "https://webDavHost/")
363 expect_that(REST$read("file", uuid, "some invalid content type"),
364 throws_error("Invalid contentType. Please use text or raw."))
367 test_that("read parses server response", {
369 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
370 fakeHttpParser <- FakeHttpParser$new()
371 REST <- RESTService$new("token", "https://host/",
372 FakeHttpRequest$new(), fakeHttpParser,
373 0, "https://webDavHost/")
375 REST$read("file", uuid, "text", 1024, 512)
377 expect_that(fakeHttpParser$parserCallCount, equals(1))
380 test_that("write calls REST service properly", {
382 fileContent <- "new file content"
383 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
384 expectedURL <- "https://webDavHost/c=aaaaa-j7d0g-ccccccccccccccc/file"
385 fakeHttp <- FakeHttpRequest$new(expectedURL)
387 REST <- RESTService$new("token", "https://host/",
388 fakeHttp, FakeHttpParser$new(),
389 0, "https://webDavHost/")
391 REST$write("file", uuid, fileContent, "text/html")
393 expect_true(fakeHttp$URLIsProperlyConfigured)
394 expect_true(fakeHttp$requestBodyIsProvided)
395 expect_true(fakeHttp$requestHeaderContainsAuthorizationField)
396 expect_true(fakeHttp$requestHeaderContainsContentTypeField)
399 test_that("write raises exception if server response code is not between 200 and 300", {
401 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
402 fileContent <- "new file content"
404 response$status_code <- 404
405 fakeHttp <- FakeHttpRequest$new(serverResponse = response)
407 REST <- RESTService$new("token", "https://host/",
408 fakeHttp, HttpParser$new(),
409 0, "https://webDavHost/")
411 expect_that(REST$write("file", uuid, fileContent, "text/html"),
412 throws_error("Server code: 404"))
415 test_that("getConnection calls REST service properly", {
416 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
417 fakeHttp <- FakeHttpRequest$new()
419 REST <- RESTService$new("token", "https://host/",
420 fakeHttp, FakeHttpParser$new(),
421 0, "https://webDavHost/")
423 REST$getConnection("file", uuid, "r")
425 expect_that(fakeHttp$numberOfgetConnectionCalls, equals(1))