21128: Merge commit 'adbbc9e3c7a36d39b30f403555ee5889e32adcc0' into 21128-toolbar...
[arvados.git] / sdk / R / tests / testthat / test-HttpParser.R
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: Apache-2.0
4
5 context("Http Parser")
6
7
8 test_that("parseJSONResponse generates and returns JSON object from server response", {
9
10     JSONContent <- "{\"bar\":{\"foo\":[10]}}"
11     serverResponse <- list()
12     serverResponse$content <- charToRaw(JSONContent)
13     serverResponse$headers[["Content-Type"]] <- "application/json; charset=utf-8"
14     class(serverResponse) <- c("response")
15
16     parser <- HttpParser$new()
17
18     result <- parser$parseJSONResponse(serverResponse)
19     barExists <- !is.null(result$bar)
20
21     expect_true(barExists)
22     expect_that(unlist(result$bar$foo), equals(10))
23 })
24
25 test_that(paste("parseResponse generates and returns character vector",
26                 "from server response if outputType is text"), {
27
28     content <- "random text"
29     serverResponse <- list()
30     serverResponse$content <- charToRaw(content)
31     serverResponse$headers[["Content-Type"]] <- "text/plain; charset=utf-8"
32     class(serverResponse) <- c("response")
33
34     parser <- HttpParser$new()
35     parsedResponse <- parser$parseResponse(serverResponse, "text")
36
37     expect_that(parsedResponse, equals("random text"))
38 })
39
40
41 webDAVResponseSample =
42     paste0("<?xml version=\"1.0\" encoding=\"UTF-8\"?><D:multistatus xmlns:",
43            "D=\"DAV:\"><D:response><D:href>/c=aaaaa-bbbbb-ccccccccccccccc/</D",
44            ":href><D:propstat><D:prop><D:resourcetype><D:collection xmlns:D=",
45            "\"DAV:\"/></D:resourcetype><D:getlastmodified>Fri, 11 Jan 2018 1",
46            "1:11:11 GMT</D:getlastmodified><D:displayname></D:displayname><D",
47            ":supportedlock><D:lockentry xmlns:D=\"DAV:\"><D:lockscope><D:exc",
48            "lusive/></D:lockscope><D:locktype><D:write/></D:locktype></D:loc",
49            "kentry></D:supportedlock></D:prop><D:status>HTTP/1.1 200 OK</D:s",
50            "tatus></D:propstat></D:response><D:response><D:href>/c=aaaaa-bbb",
51            "bb-ccccccccccccccc/myFile.exe</D:href><D:propstat><D:prop><D:r",
52            "esourcetype></D:resourcetype><D:getlastmodified>Fri, 12 Jan 2018",
53            " 22:22:22 GMT</D:getlastmodified><D:getcontenttype>text/x-c++src",
54            "; charset=utf-8</D:getcontenttype><D:displayname>myFile.exe</D",
55            ":displayname><D:getcontentlength>25</D:getcontentlength><D:getet",
56            "ag>\"123b12dd1234567890\"</D:getetag><D:supportedlock><D:lockent",
57            "ry xmlns:D=\"DAV:\"><D:lockscope><D:exclusive/></D:lockscope><D:",
58            "locktype><D:write/></D:locktype></D:lockentry></D:supportedlock>",
59            "</D:prop><D:status>HTTP/1.1 200 OK</D:status></D:propstat></D:re",
60            "sponse></D:multistatus>")
61
62
63
64 test_that(paste("getFileNamesFromResponse returns file names belonging to specific",
65                 "collection parsed from webDAV server response"), {
66
67     serverResponse <- list()
68     serverResponse$content <- charToRaw(webDAVResponseSample)
69     serverResponse$headers[["Content-Type"]] <- "text/xml; charset=utf-8"
70     class(serverResponse) <- c("response")
71     url <- URLencode("https://webdav/c=aaaaa-bbbbb-ccccccccccccccc")
72
73     parser <- HttpParser$new()
74     result <- parser$getFileNamesFromResponse(serverResponse, url)
75     expectedResult <- "myFile.exe"
76     resultMatchExpected <- all.equal(result, expectedResult)
77
78     expect_true(resultMatchExpected)
79 })
80
81 test_that(paste("getFileSizesFromResponse returns file sizes",
82                 "parsed from webDAV server response"), {
83
84     serverResponse <- list()
85     serverResponse$content <- charToRaw(webDAVResponseSample)
86     serverResponse$headers[["Content-Type"]] <- "text/xml; charset=utf-8"
87     class(serverResponse) <- c("response")
88     url <- URLencode("https://webdav/c=aaaaa-bbbbb-ccccccccccccccc")
89
90     parser <- HttpParser$new()
91     expectedResult <- "25"
92     result <- parser$getFileSizesFromResponse(serverResponse, url)
93     resultMatchExpected <- result == expectedResult
94
95     expect_true(resultMatchExpected)
96 })