1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: Apache-2.0
5 FakeHttpRequest <- R6::R6Class(
11 serverMaxElementsPerRequest = NULL,
15 URLIsProperlyConfigured = NULL,
16 expectedQueryFilters = NULL,
17 queryFiltersAreCorrect = NULL,
18 requestHeaderContainsAuthorizationField = NULL,
19 requestHeaderContainsDestinationField = NULL,
20 requestHeaderContainsRangeField = NULL,
21 requestHeaderContainsContentTypeField = NULL,
22 JSONEncodedBodyIsProvided = NULL,
23 requestBodyIsProvided = NULL,
25 numberOfGETRequests = NULL,
26 numberOfDELETERequests = NULL,
27 numberOfPUTRequests = NULL,
28 numberOfPOSTRequests = NULL,
29 numberOfMOVERequests = NULL,
30 numberOfCOPYRequests = NULL,
31 numberOfgetConnectionCalls = NULL,
33 initialize = function(expectedURL = NULL,
34 serverResponse = NULL,
35 expectedFilters = NULL)
37 if(is.null(serverResponse))
39 self$content <- list()
40 self$content$status_code <- 200
43 self$content <- serverResponse
45 self$expectedURL <- expectedURL
46 self$URLIsProperlyConfigured <- FALSE
47 self$expectedQueryFilters <- expectedFilters
48 self$queryFiltersAreCorrect <- FALSE
49 self$requestHeaderContainsAuthorizationField <- FALSE
50 self$requestHeaderContainsDestinationField <- FALSE
51 self$requestHeaderContainsRangeField <- FALSE
52 self$requestHeaderContainsContentTypeField <- FALSE
53 self$JSONEncodedBodyIsProvided <- FALSE
54 self$requestBodyIsProvided <- FALSE
56 self$numberOfGETRequests <- 0
57 self$numberOfDELETERequests <- 0
58 self$numberOfPUTRequests <- 0
59 self$numberOfPOSTRequests <- 0
60 self$numberOfMOVERequests <- 0
61 self$numberOfCOPYRequests <- 0
63 self$numberOfgetConnectionCalls <- 0
65 self$serverMaxElementsPerRequest <- 5
68 exec = function(verb, url, headers = NULL, body = NULL, query = NULL,
69 limit = NULL, offset = NULL, retryTimes = 0)
71 private$validateURL(url)
72 private$validateHeaders(headers)
73 private$validateFilters(queryFilters)
74 private$validateBody(body)
77 self$numberOfGETRequests <- self$numberOfGETRequests + 1
78 else if(verb == "POST")
79 self$numberOfPOSTRequests <- self$numberOfPOSTRequests + 1
80 else if(verb == "PUT")
81 self$numberOfPUTRequests <- self$numberOfPUTRequests + 1
82 else if(verb == "DELETE")
83 self$numberOfDELETERequests <- self$numberOfDELETERequests + 1
84 else if(verb == "MOVE")
85 self$numberOfMOVERequests <- self$numberOfMOVERequests + 1
86 else if(verb == "COPY")
87 self$numberOfCOPYRequests <- self$numberOfCOPYRequests + 1
88 else if(verb == "PROPFIND")
93 if(!is.null(self$content$items_available))
94 return(private$getElements(offset, limit))
99 getConnection = function(url, headers, openMode)
101 self$numberOfgetConnectionCalls <- self$numberOfgetConnectionCalls + 1
102 c(url, headers, openMode)
108 validateURL = function(url)
110 if(!is.null(self$expectedURL) && url == self$expectedURL)
111 self$URLIsProperlyConfigured <- TRUE
114 validateHeaders = function(headers)
116 if(!is.null(headers$Authorization))
117 self$requestHeaderContainsAuthorizationField <- TRUE
119 if(!is.null(headers$Destination))
120 self$requestHeaderContainsDestinationField <- TRUE
122 if(!is.null(headers$Range))
123 self$requestHeaderContainsRangeField <- TRUE
125 if(!is.null(headers[["Content-Type"]]))
126 self$requestHeaderContainsContentTypeField <- TRUE
129 validateBody = function(body)
133 self$requestBodyIsProvided <- TRUE
135 if(class(body) == "json")
136 self$JSONEncodedBodyIsProvided <- TRUE
140 validateFilters = function(filters)
142 if(!is.null(self$expectedQueryFilters) &&
144 all.equal(unname(filters), self$expectedQueryFilters))
146 self$queryFiltersAreCorrect <- TRUE
150 getElements = function(offset, limit)
153 elementCount <- self$serverMaxElementsPerRequest
157 if(offset > self$content$items_available)
158 stop("Invalid offset")
164 if(limit < self$serverMaxElementsPerRequest)
165 elementCount <- limit - 1
168 serverResponse <- list()
169 serverResponse$items_available <- self$content$items_available
170 serverResponse$items <- self$content$items[start:(start + elementCount - 1)]
172 if(start + elementCount > self$content$items_available)
174 elementCount = self$content$items_available - start
175 serverResponse$items <- self$content$items[start:(start + elementCount)]