add tests for exec and getConnection methods in HttpRequest class
[arvados.git] / sdk / R / tests / testthat / test-RESTService.R
index 859b6180f3380c2d834b99e126aa0c7761155368..26f459b17355903640676b7d4a717bd2c9052890 100644 (file)
@@ -22,7 +22,7 @@ test_that("getWebDavHostName calls REST service properly", {
     expect_that(httpRequest$URLIsProperlyConfigured, is_true())
     expect_that(httpRequest$requestHeaderContainsAuthorizationField, is_true())
     expect_that(httpRequest$numberOfGETRequests, equals(1))
-}) 
+})
 
 test_that("getWebDavHostName returns webDAV host name properly", {
 
@@ -32,8 +32,8 @@ test_that("getWebDavHostName returns webDAV host name properly", {
     REST <- RESTService$new("token", "host",
                             httpRequest, FakeHttpParser$new())
 
-    expect_that("https://myWebDavServer.com", equals(REST$getWebDavHostName())) 
-}) 
+    expect_that("https://myWebDavServer.com", equals(REST$getWebDavHostName()))
+})
 
 test_that("create calls REST service properly", {
 
@@ -51,7 +51,7 @@ test_that("create calls REST service properly", {
     expect_that(fakeHttp$URLIsProperlyConfigured, is_true())
     expect_that(fakeHttp$requestHeaderContainsAuthorizationField, is_true())
     expect_that(fakeHttp$numberOfPUTRequests, equals(1))
-}) 
+})
 
 test_that("create raises exception if server response code is not between 200 and 300", {
 
@@ -60,13 +60,13 @@ test_that("create raises exception if server response code is not between 200 an
     response$status_code <- 404
     fakeHttp <- FakeHttpRequest$new(serverResponse = response)
 
-    REST <- RESTService$new("token", "https://host/", 
+    REST <- RESTService$new("token", "https://host/",
                             fakeHttp, HttpParser$new(),
                             0, "https://webDavHost/")
 
     expect_that(REST$create("file", uuid),
                 throws_error("Server code: 404"))
-}) 
+})
 
 test_that("delete calls REST service properly", {
 
@@ -75,7 +75,7 @@ test_that("delete calls REST service properly", {
     fakeHttp <- FakeHttpRequest$new(expectedURL)
     fakeHttpParser <- FakeHttpParser$new()
 
-    REST <- RESTService$new("token", "https://host/", 
+    REST <- RESTService$new("token", "https://host/",
                             fakeHttp, fakeHttpParser,
                             0, "https://webDavHost/")
 
@@ -84,7 +84,7 @@ test_that("delete calls REST service properly", {
     expect_that(fakeHttp$URLIsProperlyConfigured, is_true())
     expect_that(fakeHttp$requestHeaderContainsAuthorizationField, is_true())
     expect_that(fakeHttp$numberOfDELETERequests, equals(1))
-}) 
+})
 
 test_that("delete raises exception if server response code is not between 200 and 300", {
 
@@ -99,7 +99,7 @@ test_that("delete raises exception if server response code is not between 200 an
 
     expect_that(REST$delete("file", uuid),
                 throws_error("Server code: 404"))
-}) 
+})
 
 test_that("move calls REST service properly", {
 
@@ -108,7 +108,7 @@ test_that("move calls REST service properly", {
     fakeHttp <- FakeHttpRequest$new(expectedURL)
     fakeHttpParser <- FakeHttpParser$new()
 
-    REST <- RESTService$new("token", "https://host/", 
+    REST <- RESTService$new("token", "https://host/",
                             fakeHttp, fakeHttpParser,
                             0, "https://webDavHost/")
 
@@ -118,7 +118,7 @@ test_that("move calls REST service properly", {
     expect_that(fakeHttp$requestHeaderContainsAuthorizationField, is_true())
     expect_that(fakeHttp$requestHeaderContainsDestinationField, is_true())
     expect_that(fakeHttp$numberOfMOVERequests, equals(1))
-}) 
+})
 
 test_that("move raises exception if server response code is not between 200 and 300", {
 
@@ -127,13 +127,13 @@ test_that("move raises exception if server response code is not between 200 and
     response$status_code <- 404
     fakeHttp <- FakeHttpRequest$new(serverResponse = response)
 
-    REST <- RESTService$new("token", "https://host/", 
+    REST <- RESTService$new("token", "https://host/",
                             fakeHttp, HttpParser$new(),
                             0, "https://webDavHost/")
 
     expect_that(REST$move("file", "newDestination/file", uuid),
                 throws_error("Server code: 404"))
-}) 
+})
 
 test_that("getCollectionContent retreives correct content from WebDAV server", {
 
@@ -145,7 +145,7 @@ test_that("getCollectionContent retreives correct content from WebDAV server", {
 
     fakeHttp <- FakeHttpRequest$new(expectedURL, returnContent)
 
-    REST <- RESTService$new("token", "https://host/", 
+    REST <- RESTService$new("token", "https://host/",
                             fakeHttp, FakeHttpParser$new(),
                             0, "https://webDavHost/")
 
@@ -155,7 +155,7 @@ test_that("getCollectionContent retreives correct content from WebDAV server", {
 
     expect_that(returnedContentMatchExpected, is_true())
     expect_that(fakeHttp$requestHeaderContainsAuthorizationField, is_true())
-}) 
+})
 
 test_that("getCollectionContent raises exception if server returns empty response", {
 
@@ -163,26 +163,26 @@ test_that("getCollectionContent raises exception if server returns empty respons
     response <- ""
     fakeHttp <- FakeHttpRequest$new(serverResponse = response)
 
-    REST <- RESTService$new("token", "https://host/", 
+    REST <- RESTService$new("token", "https://host/",
                             fakeHttp, FakeHttpParser$new(),
                             0, "https://webDavHost/")
 
     expect_that(REST$getCollectionContent(uuid),
                 throws_error("Response is empty, request may be misconfigured"))
-}) 
+})
 
 test_that("getCollectionContent parses server response", {
 
     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
     fakeHttpParser <- FakeHttpParser$new()
-    REST <- RESTService$new("token", "https://host/", 
+    REST <- RESTService$new("token", "https://host/",
                             FakeHttpRequest$new(), fakeHttpParser,
                             0, "https://webDavHost/")
 
     REST$getCollectionContent(uuid)
 
     expect_that(fakeHttpParser$parserCallCount, equals(1))
-}) 
+})
 
 test_that("getCollectionContent raises exception if server returns empty response", {
 
@@ -190,13 +190,13 @@ test_that("getCollectionContent raises exception if server returns empty respons
     response <- ""
     fakeHttp <- FakeHttpRequest$new(serverResponse = response)
 
-    REST <- RESTService$new("token", "https://host/", 
+    REST <- RESTService$new("token", "https://host/",
                             fakeHttp, FakeHttpParser$new(),
                             0, "https://webDavHost/")
 
     expect_that(REST$getCollectionContent(uuid),
                 throws_error("Response is empty, request may be misconfigured"))
-}) 
+})
 
 test_that(paste("getCollectionContent raises exception if server",
                 "response code is not between 200 and 300"), {
@@ -206,13 +206,13 @@ test_that(paste("getCollectionContent raises exception if server",
     response$status_code <- 404
     fakeHttp <- FakeHttpRequest$new(serverResponse = response)
 
-    REST <- RESTService$new("token", "https://host/", 
+    REST <- RESTService$new("token", "https://host/",
                             fakeHttp, HttpParser$new(),
                             0, "https://webDavHost/")
 
     expect_that(REST$getCollectionContent(uuid),
                 throws_error("Server code: 404"))
-}) 
+})
 
 
 test_that("getResourceSize calls REST service properly", {
@@ -235,7 +235,7 @@ test_that("getResourceSize calls REST service properly", {
     expect_that(fakeHttp$URLIsProperlyConfigured, is_true())
     expect_that(fakeHttp$requestHeaderContainsAuthorizationField, is_true())
     expect_that(returnedContentMatchExpected, is_true())
-}) 
+})
 
 test_that("getResourceSize raises exception if server returns empty response", {
 
@@ -243,13 +243,13 @@ test_that("getResourceSize raises exception if server returns empty response", {
     response <- ""
     fakeHttp <- FakeHttpRequest$new(serverResponse = response)
 
-    REST <- RESTService$new("token", "https://host/", 
+    REST <- RESTService$new("token", "https://host/",
                             fakeHttp, FakeHttpParser$new(),
                             0, "https://webDavHost/")
 
     expect_that(REST$getResourceSize("file", uuid),
                 throws_error("Response is empty, request may be misconfigured"))
-}) 
+})
 
 test_that(paste("getResourceSize raises exception if server",
                 "response code is not between 200 and 300"), {
@@ -259,26 +259,26 @@ test_that(paste("getResourceSize raises exception if server",
     response$status_code <- 404
     fakeHttp <- FakeHttpRequest$new(serverResponse = response)
 
-    REST <- RESTService$new("token", "https://host/", 
+    REST <- RESTService$new("token", "https://host/",
                             fakeHttp, HttpParser$new(),
                             0, "https://webDavHost/")
 
     expect_that(REST$getResourceSize("file", uuid),
                 throws_error("Server code: 404"))
-}) 
+})
 
 test_that("getResourceSize parses server response", {
 
     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
     fakeHttpParser <- FakeHttpParser$new()
-    REST <- RESTService$new("token", "https://host/", 
+    REST <- RESTService$new("token", "https://host/",
                             FakeHttpRequest$new(), fakeHttpParser,
                             0, "https://webDavHost/")
 
     REST$getResourceSize("file", uuid)
 
     expect_that(fakeHttpParser$parserCallCount, equals(1))
-}) 
+})
 
 test_that("read calls REST service properly", {
 
@@ -290,7 +290,7 @@ test_that("read calls REST service properly", {
 
     fakeHttp <- FakeHttpRequest$new(expectedURL, serverResponse)
 
-    REST <- RESTService$new("token", "https://host/", 
+    REST <- RESTService$new("token", "https://host/",
                             fakeHttp, FakeHttpParser$new(),
                             0, "https://webDavHost/")
 
@@ -300,7 +300,7 @@ test_that("read calls REST service properly", {
     expect_that(fakeHttp$requestHeaderContainsAuthorizationField, is_true())
     expect_that(fakeHttp$requestHeaderContainsRangeField, is_true())
     expect_that(returnResult, equals("file content"))
-}) 
+})
 
 test_that("read raises exception if server response code is not between 200 and 300", {
 
@@ -309,48 +309,48 @@ test_that("read raises exception if server response code is not between 200 and
     response$status_code <- 404
     fakeHttp <- FakeHttpRequest$new(serverResponse = response)
 
-    REST <- RESTService$new("token", "https://host/", 
+    REST <- RESTService$new("token", "https://host/",
                             fakeHttp, HttpParser$new(),
                             0, "https://webDavHost/")
 
     expect_that(REST$read("file", uuid),
                 throws_error("Server code: 404"))
-}) 
+})
 
 test_that("read raises exception if contentType is not valid", {
 
     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
     fakeHttp <- FakeHttpRequest$new()
 
-    REST <- RESTService$new("token", "https://host/", 
+    REST <- RESTService$new("token", "https://host/",
                             fakeHttp, HttpParser$new(),
                             0, "https://webDavHost/")
 
     expect_that(REST$read("file", uuid, "some invalid content type"),
                 throws_error("Invalid contentType. Please use text or raw."))
-}) 
+})
 
 test_that("read parses server response", {
 
     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
     fakeHttpParser <- FakeHttpParser$new()
-    REST <- RESTService$new("token", "https://host/", 
+    REST <- RESTService$new("token", "https://host/",
                             FakeHttpRequest$new(), fakeHttpParser,
                             0, "https://webDavHost/")
 
     REST$read("file", uuid, "text", 1024, 512)
 
     expect_that(fakeHttpParser$parserCallCount, equals(1))
-}) 
+})
 
 test_that("write calls REST service properly", {
 
-    fileContent <- "new file content" 
+    fileContent <- "new file content"
     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
     expectedURL <- "https://webDavHost/c=aaaaa-j7d0g-ccccccccccccccc/file"
     fakeHttp <- FakeHttpRequest$new(expectedURL)
 
-    REST <- RESTService$new("token", "https://host/", 
+    REST <- RESTService$new("token", "https://host/",
                             fakeHttp, FakeHttpParser$new(),
                             0, "https://webDavHost/")
 
@@ -360,12 +360,12 @@ test_that("write calls REST service properly", {
     expect_that(fakeHttp$requestBodyIsProvided, is_true())
     expect_that(fakeHttp$requestHeaderContainsAuthorizationField, is_true())
     expect_that(fakeHttp$requestHeaderContainsContentTypeField, is_true())
-}) 
+})
 
 test_that("write raises exception if server response code is not between 200 and 300", {
 
     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
-    fileContent <- "new file content" 
+    fileContent <- "new file content"
     response <- list()
     response$status_code <- 404
     fakeHttp <- FakeHttpRequest$new(serverResponse = response)
@@ -376,4 +376,17 @@ test_that("write raises exception if server response code is not between 200 and
 
     expect_that(REST$write("file", uuid, fileContent, "text/html"),
                 throws_error("Server code: 404"))
-}) 
+})
+
+test_that("getConnection calls REST service properly", {
+    uuid <- "aaaaa-j7d0g-ccccccccccccccc"
+    fakeHttp <- FakeHttpRequest$new()
+
+    REST <- RESTService$new("token", "https://host/",
+                            fakeHttp, FakeHttpParser$new(),
+                            0, "https://webDavHost/")
+
+    REST$getConnection("file", uuid, "r")
+
+    expect_that(fakeHttp$numberOfgetConnectionCalls, equals(1))
+})