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/discovery/v1/apis/arvados/v1/rest"
14 serverResponse <- list(keepWebServiceUrl = "https://myWebDavServer.com")
15 httpRequest <- FakeHttpRequest$new(expectedURL, serverResponse)
17 REST <- RESTService$new("token", "host",
18 httpRequest, FakeHttpParser$new())
20 REST$getWebDavHostName()
22 expect_that(httpRequest$URLIsProperlyConfigured, is_true())
23 expect_that(httpRequest$requestHeaderContainsAuthorizationField, is_true())
24 expect_that(httpRequest$numberOfGETRequests, equals(1))
27 test_that("getWebDavHostName returns webDAV host name properly", {
29 serverResponse <- list(keepWebServiceUrl = "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_that(fakeHttp$URLIsProperlyConfigured, is_true())
52 expect_that(fakeHttp$requestHeaderContainsAuthorizationField, is_true())
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_that(fakeHttp$URLIsProperlyConfigured, is_true())
85 expect_that(fakeHttp$requestHeaderContainsAuthorizationField, is_true())
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_that(fakeHttp$URLIsProperlyConfigured, is_true())
118 expect_that(fakeHttp$requestHeaderContainsAuthorizationField, is_true())
119 expect_that(fakeHttp$requestHeaderContainsDestinationField, is_true())
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("getCollectionContent retreives correct content from WebDAV server", {
140 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
141 expectedURL <- "https://webDavHost/c=aaaaa-j7d0g-ccccccccccccccc"
142 returnContent <- list()
143 returnContent$status_code <- 200
144 returnContent$content <- c("animal", "animal/dog", "ball")
146 fakeHttp <- FakeHttpRequest$new(expectedURL, returnContent)
148 REST <- RESTService$new("token", "https://host/",
149 fakeHttp, FakeHttpParser$new(),
150 0, "https://webDavHost/")
152 returnResult <- REST$getCollectionContent(uuid)
153 returnedContentMatchExpected <- all.equal(returnResult,
154 c("animal", "animal/dog", "ball"))
156 expect_that(returnedContentMatchExpected, is_true())
157 expect_that(fakeHttp$requestHeaderContainsAuthorizationField, is_true())
160 test_that("getCollectionContent raises exception if server returns empty response", {
162 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
164 fakeHttp <- FakeHttpRequest$new(serverResponse = response)
166 REST <- RESTService$new("token", "https://host/",
167 fakeHttp, FakeHttpParser$new(),
168 0, "https://webDavHost/")
170 expect_that(REST$getCollectionContent(uuid),
171 throws_error("Response is empty, request may be misconfigured"))
174 test_that("getCollectionContent parses server response", {
176 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
177 fakeHttpParser <- FakeHttpParser$new()
178 REST <- RESTService$new("token", "https://host/",
179 FakeHttpRequest$new(), fakeHttpParser,
180 0, "https://webDavHost/")
182 REST$getCollectionContent(uuid)
184 expect_that(fakeHttpParser$parserCallCount, equals(1))
187 test_that("getCollectionContent raises exception if server returns empty response", {
189 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
191 fakeHttp <- FakeHttpRequest$new(serverResponse = response)
193 REST <- RESTService$new("token", "https://host/",
194 fakeHttp, FakeHttpParser$new(),
195 0, "https://webDavHost/")
197 expect_that(REST$getCollectionContent(uuid),
198 throws_error("Response is empty, request may be misconfigured"))
201 test_that(paste("getCollectionContent raises exception if server",
202 "response code is not between 200 and 300"), {
204 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
206 response$status_code <- 404
207 fakeHttp <- FakeHttpRequest$new(serverResponse = response)
209 REST <- RESTService$new("token", "https://host/",
210 fakeHttp, HttpParser$new(),
211 0, "https://webDavHost/")
213 expect_that(REST$getCollectionContent(uuid),
214 throws_error("Server code: 404"))
218 test_that("getResourceSize calls REST service properly", {
220 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
221 expectedURL <- "https://webDavHost/c=aaaaa-j7d0g-ccccccccccccccc/file"
223 response$status_code <- 200
224 response$content <- c(6, 2, 931, 12003)
225 fakeHttp <- FakeHttpRequest$new(expectedURL, response)
227 REST <- RESTService$new("token", "https://host/",
228 fakeHttp, FakeHttpParser$new(),
229 0, "https://webDavHost/")
231 returnResult <- REST$getResourceSize("file", uuid)
232 returnedContentMatchExpected <- all.equal(returnResult,
235 expect_that(fakeHttp$URLIsProperlyConfigured, is_true())
236 expect_that(fakeHttp$requestHeaderContainsAuthorizationField, is_true())
237 expect_that(returnedContentMatchExpected, is_true())
240 test_that("getResourceSize raises exception if server returns empty response", {
242 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
244 fakeHttp <- FakeHttpRequest$new(serverResponse = response)
246 REST <- RESTService$new("token", "https://host/",
247 fakeHttp, FakeHttpParser$new(),
248 0, "https://webDavHost/")
250 expect_that(REST$getResourceSize("file", uuid),
251 throws_error("Response is empty, request may be misconfigured"))
254 test_that(paste("getResourceSize raises exception if server",
255 "response code is not between 200 and 300"), {
257 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
259 response$status_code <- 404
260 fakeHttp <- FakeHttpRequest$new(serverResponse = response)
262 REST <- RESTService$new("token", "https://host/",
263 fakeHttp, HttpParser$new(),
264 0, "https://webDavHost/")
266 expect_that(REST$getResourceSize("file", uuid),
267 throws_error("Server code: 404"))
270 test_that("getResourceSize parses server response", {
272 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
273 fakeHttpParser <- FakeHttpParser$new()
274 REST <- RESTService$new("token", "https://host/",
275 FakeHttpRequest$new(), fakeHttpParser,
276 0, "https://webDavHost/")
278 REST$getResourceSize("file", uuid)
280 expect_that(fakeHttpParser$parserCallCount, equals(1))
283 test_that("read calls REST service properly", {
285 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
286 expectedURL <- "https://webDavHost/c=aaaaa-j7d0g-ccccccccccccccc/file"
287 serverResponse <- list()
288 serverResponse$status_code <- 200
289 serverResponse$content <- "file content"
291 fakeHttp <- FakeHttpRequest$new(expectedURL, serverResponse)
293 REST <- RESTService$new("token", "https://host/",
294 fakeHttp, FakeHttpParser$new(),
295 0, "https://webDavHost/")
297 returnResult <- REST$read("file", uuid, "text", 1024, 512)
299 expect_that(fakeHttp$URLIsProperlyConfigured, is_true())
300 expect_that(fakeHttp$requestHeaderContainsAuthorizationField, is_true())
301 expect_that(fakeHttp$requestHeaderContainsRangeField, is_true())
302 expect_that(returnResult, equals("file content"))
305 test_that("read raises exception if server response code is not between 200 and 300", {
307 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
309 response$status_code <- 404
310 fakeHttp <- FakeHttpRequest$new(serverResponse = response)
312 REST <- RESTService$new("token", "https://host/",
313 fakeHttp, HttpParser$new(),
314 0, "https://webDavHost/")
316 expect_that(REST$read("file", uuid),
317 throws_error("Server code: 404"))
320 test_that("read raises exception if contentType is not valid", {
322 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
323 fakeHttp <- FakeHttpRequest$new()
325 REST <- RESTService$new("token", "https://host/",
326 fakeHttp, HttpParser$new(),
327 0, "https://webDavHost/")
329 expect_that(REST$read("file", uuid, "some invalid content type"),
330 throws_error("Invalid contentType. Please use text or raw."))
333 test_that("read parses server response", {
335 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
336 fakeHttpParser <- FakeHttpParser$new()
337 REST <- RESTService$new("token", "https://host/",
338 FakeHttpRequest$new(), fakeHttpParser,
339 0, "https://webDavHost/")
341 REST$read("file", uuid, "text", 1024, 512)
343 expect_that(fakeHttpParser$parserCallCount, equals(1))
346 test_that("write calls REST service properly", {
348 fileContent <- "new file content"
349 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
350 expectedURL <- "https://webDavHost/c=aaaaa-j7d0g-ccccccccccccccc/file"
351 fakeHttp <- FakeHttpRequest$new(expectedURL)
353 REST <- RESTService$new("token", "https://host/",
354 fakeHttp, FakeHttpParser$new(),
355 0, "https://webDavHost/")
357 REST$write("file", uuid, fileContent, "text/html")
359 expect_that(fakeHttp$URLIsProperlyConfigured, is_true())
360 expect_that(fakeHttp$requestBodyIsProvided, is_true())
361 expect_that(fakeHttp$requestHeaderContainsAuthorizationField, is_true())
362 expect_that(fakeHttp$requestHeaderContainsContentTypeField, is_true())
365 test_that("write raises exception if server response code is not between 200 and 300", {
367 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
368 fileContent <- "new file content"
370 response$status_code <- 404
371 fakeHttp <- FakeHttpRequest$new(serverResponse = response)
373 REST <- RESTService$new("token", "https://host/",
374 fakeHttp, HttpParser$new(),
375 0, "https://webDavHost/")
377 expect_that(REST$write("file", uuid, fileContent, "text/html"),
378 throws_error("Server code: 404"))