File: test-user-agent.R

package info (click to toggle)
r-cran-crul 1.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,620 kB
  • sloc: sh: 13; makefile: 2
file content (64 lines) | stat: -rw-r--r-- 1,856 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
context("user-agent")

test_that("user-agent internal helper fxn works as expected", {
  skip_on_cran()

  aa <- make_ua()

  expect_is(aa, "character")
  expect_match(aa, 'libcurl')
  expect_match(aa, 'r-curl')
  expect_match(aa, 'crul')
})


test_that("user-agent: default behavior", {
  skip_on_cran()

  cli <- HttpClient$new(url = hb())
  res_get <- cli$get("get")
  res_head <- cli$head("get")
  res_post <- cli$post("post")

  expect_is(cli, "HttpClient")
  expect_equal(length(cli$headers), 0)
  expect_equal(length(cli$opts), 0)
  expect_equal(res_get$request_headers$`User-Agent`, make_ua()) 
  expect_equal(res_head$request_headers$`User-Agent`, make_ua()) 
  expect_equal(res_post$request_headers$`User-Agent`, make_ua()) 
})

test_that("user-agent: passed as option", {
  skip_on_cran()

  cli <- HttpClient$new(url = hb(), 
    opts = list(useragent = "hello world"))
  res_get <- cli$get("get")
  res_head <- cli$head("get")
  res_post <- cli$post("post")

  expect_is(cli, "HttpClient")
  expect_equal(length(cli$headers), 0)
  expect_named(cli$opts, "useragent")
  expect_equal(res_get$request_headers$`User-Agent`, "hello world") 
  expect_equal(res_head$request_headers$`User-Agent`, "hello world") 
  expect_equal(res_post$request_headers$`User-Agent`, "hello world") 
})

test_that("user-agent: passed as header", {
  skip_on_cran()

  cli <- HttpClient$new(url = hb(), 
    headers = list(`User-Agent` = "hello world")
  )
  res_get <- cli$get("get")
  res_head <- cli$head("get")
  res_post <- cli$post("post")

  expect_is(cli, "HttpClient")
  expect_equal(length(cli$opts), 0)
  expect_named(cli$headers, "User-Agent")
  expect_equal(res_get$request_headers$`User-Agent`, "hello world") 
  expect_equal(res_head$request_headers$`User-Agent`, "hello world") 
  expect_equal(res_post$request_headers$`User-Agent`, "hello world") 
})