1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: Apache-2.0
5 context("Http Request")
8 test_that("execute raises exception if http verb is not valid", {
10 http <- HttpRequest$new()
11 expect_that(http$exec("FAKE VERB", "url"),
12 throws_error("Http verb is not valid."))
15 test_that("createQuery generates and encodes query portion of http", {
17 http <- HttpRequest$new()
19 queryParams$filters <- list(list("color", "=", "red"))
20 queryParams$limit <- 20
21 queryParams$offset <- 50
22 expect_that(http$createQuery(queryParams),
23 equals(paste0("/?filters=%5B%5B%22color%22%2C%22%3D%22%2C%22red",
24 "%22%5D%5D&limit=20&offset=50")))
27 test_that("createQuery generates and empty string when queryParams is an empty list", {
29 http <- HttpRequest$new()
30 expect_that(http$createQuery(list()), equals(""))
33 test_that("exec calls httr functions correctly", {
34 httrNamespace <- getNamespace("httr")
36 # Monkeypatch httr functions and assert that they are called later
37 add_headersCalled <- FALSE
38 unlockBinding("add_headers", httrNamespace)
39 newAddHeaders <- function(h)
41 add_headersCalled <<- TRUE
44 httrNamespace$add_headers <- newAddHeaders
45 lockBinding("add_headers", httrNamespace)
47 expectedConfig <- list()
49 unlockBinding("RETRY", httrNamespace)
50 newRETRY <- function(verb, url, body, config, times)
53 expectedConfig <<- config
55 httrNamespace$RETRY <- newRETRY
56 lockBinding("RETRY", httrNamespace)
58 Sys.setenv("ARVADOS_API_HOST_INSECURE" = TRUE)
59 http <- HttpRequest$new()
60 http$exec("GET", "url")
62 expect_that(add_headersCalled, is_true())
63 expect_that(retryCalled, is_true())
64 expect_that(expectedConfig$options, equals(list(ssl_verifypeer = 0L)))
67 test_that("getConnection calls curl functions correctly", {
68 curlNamespace <- getNamespace("curl")
70 # Monkeypatch curl functions and assert that they are called later
72 unlockBinding("curl", curlNamespace)
73 newCurl <- function(url, open, handle) curlCalled <<- TRUE
74 curlNamespace$curl <- newCurl
75 lockBinding("curl", curlNamespace)
77 new_handleCalled <- FALSE
78 unlockBinding("new_handle", curlNamespace)
79 newHandleFun <- function()
81 new_handleCalled <<- TRUE
84 curlNamespace$new_handle <- newHandleFun
85 lockBinding("new_handle", curlNamespace)
87 handle_setheadersCalled <- FALSE
88 unlockBinding("handle_setheaders", curlNamespace)
89 newHandleSetHeaders <- function(h, .list) handle_setheadersCalled <<- TRUE
90 curlNamespace$handle_setheaders <- newHandleSetHeaders
91 lockBinding("handle_setheaders", curlNamespace)
93 handle_setoptCalled <- FALSE
94 unlockBinding("handle_setopt", curlNamespace)
95 newHandleSetOpt <- function(h, ssl_verifypeer) handle_setoptCalled <<- TRUE
96 curlNamespace$handle_setopt <- newHandleSetOpt
97 lockBinding("handle_setopt", curlNamespace)
100 Sys.setenv("ARVADOS_API_HOST_INSECURE" = TRUE)
101 http <- HttpRequest$new()
102 http$getConnection("location", list(), "r")
104 expect_that(new_handleCalled, is_true())
105 expect_that(handle_setheadersCalled, is_true())
106 expect_that(handle_setoptCalled, is_true())
107 expect_that(curlCalled, is_true())