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("create calls REST service properly", {
36 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
37 expectedURL <- "https://webDavHost/c=aaaaa-j7d0g-ccccccccccccccc/file"
38 fakeHttp <- FakeHttpRequest$new(expectedURL)
39 fakeHttpParser <- FakeHttpParser$new()
41 REST <- RESTService$new("token", "https://host/",
42 fakeHttp, fakeHttpParser,
43 0, "https://webDavHost/")
45 REST$create("file", uuid)
47 expect_that(fakeHttp$URLIsProperlyConfigured, is_true())
48 expect_that(fakeHttp$requestHeaderContainsAuthorizationField, is_true())
49 expect_that(fakeHttp$numberOfPUTRequests, equals(1))
52 test_that("create raises exception if server response code is not between 200 and 300", {
54 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
56 response$status_code <- 404
57 fakeHttp <- FakeHttpRequest$new(serverResponse = response)
59 REST <- RESTService$new("token", "https://host/",
60 fakeHttp, HttpParser$new(),
61 0, "https://webDavHost/")
63 expect_that(REST$create("file", uuid),
64 throws_error("Server code: 404"))
67 test_that("delete calls REST service properly", {
69 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
70 expectedURL <- "https://webDavHost/c=aaaaa-j7d0g-ccccccccccccccc/file"
71 fakeHttp <- FakeHttpRequest$new(expectedURL)
72 fakeHttpParser <- FakeHttpParser$new()
74 REST <- RESTService$new("token", "https://host/",
75 fakeHttp, fakeHttpParser,
76 0, "https://webDavHost/")
78 REST$delete("file", uuid)
80 expect_that(fakeHttp$URLIsProperlyConfigured, is_true())
81 expect_that(fakeHttp$requestHeaderContainsAuthorizationField, is_true())
82 expect_that(fakeHttp$numberOfDELETERequests, equals(1))
85 test_that("delete raises exception if server response code is not between 200 and 300", {
87 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
89 response$status_code <- 404
90 fakeHttp <- FakeHttpRequest$new(serverResponse = response)
92 REST <- RESTService$new("token", "https://host/",
93 fakeHttp, HttpParser$new(),
94 0, "https://webDavHost/")
96 expect_that(REST$delete("file", uuid),
97 throws_error("Server code: 404"))
100 test_that("move calls REST service properly", {
102 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
103 expectedURL <- "https://webDavHost/c=aaaaa-j7d0g-ccccccccccccccc/file"
104 fakeHttp <- FakeHttpRequest$new(expectedURL)
105 fakeHttpParser <- FakeHttpParser$new()
107 REST <- RESTService$new("token", "https://host/",
108 fakeHttp, fakeHttpParser,
109 0, "https://webDavHost/")
111 REST$move("file", "newDestination/file", uuid)
113 expect_that(fakeHttp$URLIsProperlyConfigured, is_true())
114 expect_that(fakeHttp$requestHeaderContainsAuthorizationField, is_true())
115 expect_that(fakeHttp$requestHeaderContainsDestinationField, is_true())
116 expect_that(fakeHttp$numberOfMOVERequests, equals(1))
119 test_that("move raises exception if server response code is not between 200 and 300", {
121 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
123 response$status_code <- 404
124 fakeHttp <- FakeHttpRequest$new(serverResponse = response)
126 REST <- RESTService$new("token", "https://host/",
127 fakeHttp, HttpParser$new(),
128 0, "https://webDavHost/")
130 expect_that(REST$move("file", "newDestination/file", uuid),
131 throws_error("Server code: 404"))
134 test_that("getCollectionContent retreives correct content from WebDAV server", {
136 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
137 expectedURL <- "https://webDavHost/c=aaaaa-j7d0g-ccccccccccccccc"
138 returnContent <- list()
139 returnContent$status_code <- 200
140 returnContent$content <- c("animal", "animal/dog", "ball")
142 fakeHttp <- FakeHttpRequest$new(expectedURL, returnContent)
144 REST <- RESTService$new("token", "https://host/",
145 fakeHttp, FakeHttpParser$new(),
146 0, "https://webDavHost/")
148 returnResult <- REST$getCollectionContent(uuid)
149 returnedContentMatchExpected <- all.equal(returnResult,
150 c("animal", "animal/dog", "ball"))
152 expect_that(returnedContentMatchExpected, is_true())
153 expect_that(fakeHttp$requestHeaderContainsAuthorizationField, is_true())
156 test_that("getCollectionContent raises exception if server returns empty response", {
158 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
160 fakeHttp <- FakeHttpRequest$new(serverResponse = response)
162 REST <- RESTService$new("token", "https://host/",
163 fakeHttp, FakeHttpParser$new(),
164 0, "https://webDavHost/")
166 expect_that(REST$getCollectionContent(uuid),
167 throws_error("Response is empty, request may be misconfigured"))
170 test_that("getCollectionContent parses server response", {
172 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
173 fakeHttpParser <- FakeHttpParser$new()
174 REST <- RESTService$new("token", "https://host/",
175 FakeHttpRequest$new(), fakeHttpParser,
176 0, "https://webDavHost/")
178 REST$getCollectionContent(uuid)
180 expect_that(fakeHttpParser$parserCallCount, equals(1))
183 test_that("getCollectionContent raises exception if server returns empty response", {
185 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
187 fakeHttp <- FakeHttpRequest$new(serverResponse = response)
189 REST <- RESTService$new("token", "https://host/",
190 fakeHttp, FakeHttpParser$new(),
191 0, "https://webDavHost/")
193 expect_that(REST$getCollectionContent(uuid),
194 throws_error("Response is empty, request may be misconfigured"))
197 test_that(paste("getCollectionContent raises exception if server",
198 "response code is not between 200 and 300"), {
200 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
202 response$status_code <- 404
203 fakeHttp <- FakeHttpRequest$new(serverResponse = response)
205 REST <- RESTService$new("token", "https://host/",
206 fakeHttp, HttpParser$new(),
207 0, "https://webDavHost/")
209 expect_that(REST$getCollectionContent(uuid),
210 throws_error("Server code: 404"))
214 test_that("getResourceSize calls REST service properly", {
216 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
217 expectedURL <- "https://webDavHost/c=aaaaa-j7d0g-ccccccccccccccc/file"
219 response$status_code <- 200
220 response$content <- c(6, 2, 931, 12003)
221 fakeHttp <- FakeHttpRequest$new(expectedURL, response)
223 REST <- RESTService$new("token", "https://host/",
224 fakeHttp, FakeHttpParser$new(),
225 0, "https://webDavHost/")
227 returnResult <- REST$getResourceSize("file", uuid)
228 returnedContentMatchExpected <- all.equal(returnResult,
231 expect_that(fakeHttp$URLIsProperlyConfigured, is_true())
232 expect_that(fakeHttp$requestHeaderContainsAuthorizationField, is_true())
233 expect_that(returnedContentMatchExpected, is_true())
236 test_that("getResourceSize raises exception if server returns empty response", {
238 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
240 fakeHttp <- FakeHttpRequest$new(serverResponse = response)
242 REST <- RESTService$new("token", "https://host/",
243 fakeHttp, FakeHttpParser$new(),
244 0, "https://webDavHost/")
246 expect_that(REST$getResourceSize("file", uuid),
247 throws_error("Response is empty, request may be misconfigured"))
250 test_that(paste("getResourceSize raises exception if server",
251 "response code is not between 200 and 300"), {
253 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
255 response$status_code <- 404
256 fakeHttp <- FakeHttpRequest$new(serverResponse = response)
258 REST <- RESTService$new("token", "https://host/",
259 fakeHttp, HttpParser$new(),
260 0, "https://webDavHost/")
262 expect_that(REST$getResourceSize("file", uuid),
263 throws_error("Server code: 404"))
266 test_that("getResourceSize parses server response", {
268 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
269 fakeHttpParser <- FakeHttpParser$new()
270 REST <- RESTService$new("token", "https://host/",
271 FakeHttpRequest$new(), fakeHttpParser,
272 0, "https://webDavHost/")
274 REST$getResourceSize("file", uuid)
276 expect_that(fakeHttpParser$parserCallCount, equals(1))
279 test_that("read calls REST service properly", {
281 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
282 expectedURL <- "https://webDavHost/c=aaaaa-j7d0g-ccccccccccccccc/file"
283 serverResponse <- list()
284 serverResponse$status_code <- 200
285 serverResponse$content <- "file content"
287 fakeHttp <- FakeHttpRequest$new(expectedURL, serverResponse)
289 REST <- RESTService$new("token", "https://host/",
290 fakeHttp, FakeHttpParser$new(),
291 0, "https://webDavHost/")
293 returnResult <- REST$read("file", uuid, "text", 1024, 512)
295 expect_that(fakeHttp$URLIsProperlyConfigured, is_true())
296 expect_that(fakeHttp$requestHeaderContainsAuthorizationField, is_true())
297 expect_that(fakeHttp$requestHeaderContainsRangeField, is_true())
298 expect_that(returnResult, equals("file content"))
301 test_that("read raises exception if server response code is not between 200 and 300", {
303 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
305 response$status_code <- 404
306 fakeHttp <- FakeHttpRequest$new(serverResponse = response)
308 REST <- RESTService$new("token", "https://host/",
309 fakeHttp, HttpParser$new(),
310 0, "https://webDavHost/")
312 expect_that(REST$read("file", uuid),
313 throws_error("Server code: 404"))
316 test_that("read raises exception if contentType is not valid", {
318 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
319 fakeHttp <- FakeHttpRequest$new()
321 REST <- RESTService$new("token", "https://host/",
322 fakeHttp, HttpParser$new(),
323 0, "https://webDavHost/")
325 expect_that(REST$read("file", uuid, "some invalid content type"),
326 throws_error("Invalid contentType. Please use text or raw."))
329 test_that("read parses server response", {
331 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
332 fakeHttpParser <- FakeHttpParser$new()
333 REST <- RESTService$new("token", "https://host/",
334 FakeHttpRequest$new(), fakeHttpParser,
335 0, "https://webDavHost/")
337 REST$read("file", uuid, "text", 1024, 512)
339 expect_that(fakeHttpParser$parserCallCount, equals(1))
342 test_that("write calls REST service properly", {
344 fileContent <- "new file content"
345 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
346 expectedURL <- "https://webDavHost/c=aaaaa-j7d0g-ccccccccccccccc/file"
347 fakeHttp <- FakeHttpRequest$new(expectedURL)
349 REST <- RESTService$new("token", "https://host/",
350 fakeHttp, FakeHttpParser$new(),
351 0, "https://webDavHost/")
353 REST$write("file", uuid, fileContent, "text/html")
355 expect_that(fakeHttp$URLIsProperlyConfigured, is_true())
356 expect_that(fakeHttp$requestBodyIsProvided, is_true())
357 expect_that(fakeHttp$requestHeaderContainsAuthorizationField, is_true())
358 expect_that(fakeHttp$requestHeaderContainsContentTypeField, is_true())
361 test_that("write raises exception if server response code is not between 200 and 300", {
363 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
364 fileContent <- "new file content"
366 response$status_code <- 404
367 fakeHttp <- FakeHttpRequest$new(serverResponse = response)
369 REST <- RESTService$new("token", "https://host/",
370 fakeHttp, HttpParser$new(),
371 0, "https://webDavHost/")
373 expect_that(REST$write("file", uuid, fileContent, "text/html"),
374 throws_error("Server code: 404"))