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