File: test-HttpInteraction.R

package info (click to toggle)
r-cran-vcr 0.2.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 596 kB
  • sloc: sh: 13; makefile: 2
file content (44 lines) | stat: -rw-r--r-- 1,240 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
context("HttpInteraction")

test_that("HttpInteraction", {
  crul::mock(FALSE)

  url <- "https://eu.httpbin.org/post"
  body <- list(foo = "bar")
  cli <- crul::HttpClient$new(url = url)
  res <- cli$post(body = body)

  # request
  request <- Request$new("POST", uri = url,
    body = body, headers = res$response_headers)
  # response
  response <- VcrResponse$new(
     res$status_http(),
     res$response_headers,
     res$parse("UTF-8"),
     res$response_headers$status)

  x <- HTTPInteraction$new(request = request, response = response)

  # timestamp
  expect_is(x, "HTTPInteraction")
  expect_is(x$recorded_at, 'POSIXct')
  expect_type(x$recorded_at, 'double')

  # methods and objects
  expect_is(x$to_hash, "function")
  expect_is(x$from_hash, "function")
  expect_is(x$request, "Request")
  expect_is(x$response, "VcrResponse")

  # to_hash method
  expect_is(x$to_hash(), "list")
  expect_named(x$to_hash(), c('request', 'response', 'recorded_at'))

  # make an HTTPInteraction from a hash with the object already made
  expect_is(x$from_hash(x$to_hash()), "HTTPInteraction")

  # Make an HTTPInteraction from a hash alone
  my_hash <- x$to_hash()
  expect_is(HTTPInteraction$new()$from_hash(my_hash), "HTTPInteraction")
})