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