Skip to contents

Requests are translated to mock file paths according to several rules that incorporate the request method, URL, query parameters, and body.

Usage

build_mock_url(req, method = "GET")

Arguments

req

A request object, or a character "URL" to convert

method

character HTTP method. If req is a 'request' object, its request method will override this argument

Value

A file path and name, without an extension. The file, or a file with some extension appended, may or may not exist: existence is not a concern of this function.

Details

First, the request protocol, such as "https://", is removed from the URL. Second, if the request URL contains a query string, it will be popped off, hashed by digest::digest(), and the first six characters appended to the file being read. Third, request bodies are similarly hashed and appended. Finally, if a request method other than GET is used it will be appended to the end of the end of the file name.

Mock file paths also have a file extension appended, based on the Content-Type of the response, though this function, which is only concerned with the request, does not add the extension. In an HTTP API, a "directory" itself is a resource, so the extension allows distinguishing directories and files in the file system. That is, a mocked GET("http://example.com/api/") may read a "example.com/api.json" file, while GET("http://example.com/api/object1/") reads "example.com/api/object1.json".

Other examples:

  • GET("http://example.com/api/object1/?a=1") may read "example.com/api/object1-b64371.xml".

  • POST("http://example.com/api/object1/?a=1") may read "example.com/api/object1-b64371-POST.json".

This function is exported so that other packages can construct similar mock behaviors or override specific requests at a higher level than with_mock_api mocks.

Note that if you are trying to guess the mock file paths corresponding to a test for which you intend to create a mock file manually, instead of trying to build the URL, you should run the test with with_mock_api as the error message will contain the mock file path.