File: test-app.R

package info (click to toggle)
r-cran-httpuv 1.5.5%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,248 kB
  • sloc: ansic: 6,508; cpp: 4,476; makefile: 102; sh: 16
file content (140 lines) | stat: -rw-r--r-- 3,902 bytes parent folder | download
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
context("basic")

test_that("Basic functionality", {
  s1 <- startServer("127.0.0.1", randomPort(),
    list(
      call = function(req) {
        list(
          status = 200L,
          headers = list('Content-Type' = 'text/html'),
          body = "server 1"
        )
      }
    )
  )
  expect_equal(length(listServers()), 1)

  s2 <- startServer("127.0.0.1", randomPort(),
    list(
      call = function(req) {
        list(
          status = 200L,
          headers = list('Content-Type' = 'text/html'),
          body = "server 2"
        )
      }
    )
  )
  expect_equal(length(listServers()), 2)

  r1 <- fetch(local_url("/", s1$getPort()))
  r2 <- fetch(local_url("/", s2$getPort()))

  expect_equal(r1$status_code, 200)
  expect_equal(r2$status_code, 200)

  expect_identical(rawToChar(r1$content), "server 1")
  expect_identical(rawToChar(r2$content), "server 2")

  expect_identical(parse_headers_list(r1$headers)$`content-type`, "text/html")
  expect_identical(parse_headers_list(r1$headers)$`content-length`, "8")

  s1$stop()
  expect_equal(length(listServers()), 1)
  stopAllServers()
  expect_equal(length(listServers()), 0)
})


test_that("Empty and NULL headers are OK", {
  s <- startServer("127.0.0.1", randomPort(),
    list(
      call = function(req) {
        if (req$PATH_INFO == "/null") {
          list(
            status = 200L,
            headers = NULL,
            body = ""
          )
        } else if (req$PATH_INFO == "/emptylist") {
          list(
            status = 200L,
            headers = list(),
            body = ""
          )
        } else if (req$PATH_INFO == "/noheaders") {
          list(
            status = 200L,
            body = ""
          )
        }
      }
    )
  )
  on.exit(s$stop())
  expect_equal(length(listServers()), 1)

  r <- fetch(local_url("/null", s$getPort()))
  expect_equal(r$status_code, 200)
  expect_identical(r$content, raw())

  r <- fetch(local_url("/emptylist", s$getPort()))
  expect_equal(r$status_code, 200)
  expect_identical(r$content, raw())

  r <- fetch(local_url("/noheaders", s$getPort()))
  expect_equal(r$status_code, 200)
  expect_identical(r$content, raw())
})


test_that("Content length depends on the presence of 'body'", {
  s <- startServer("127.0.0.1", randomPort(),
    list(
      call = function(req) {
        if (req$PATH_INFO == "/ok") {
          list(
            status = 200L,
            headers = list('Content-Type' = 'text/html'),
            body = if (req$REQUEST_METHOD != "HEAD") raw()
          )
        } else if (req$PATH_INFO == "/nullbody") {
          list(
            status = 204L,
            headers = list('Content-Type' = 'text/html'),
            body = NULL
          )
        } else if (req$PATH_INFO == "/nobody") {
          list(
            status = 204L,
            headers = list('Content-Type' = 'text/html')
          )
        }
      }
    )
  )
  on.exit(s$stop())
  expect_equal(length(listServers()), 1)

  r1 <- fetch(local_url("/ok", s$getPort()))
  # HEAD requests should not have a body.
  r2 <- fetch(local_url("/ok", s$getPort()), new_handle(nobody = TRUE))
  r3 <- fetch(local_url("/nullbody", s$getPort()))
  r4 <- fetch(local_url("/nobody", s$getPort()))

  expect_equal(r1$status_code, 200)
  expect_equal(r2$status_code, 200)
  expect_equal(r3$status_code, 204)
  expect_equal(r3$status_code, 204)

  expect_equal(length(r1$content), 0)
  expect_equal(length(r2$content), 0)
  expect_equal(length(r3$content), 0)
  expect_equal(length(r4$content), 0)

  expect_identical(parse_headers_list(r1$headers)$`content-length`, "0")
  # HEAD requests *can* have a content-length, but they don't have to.
  expect_identical(parse_headers_list(r2$headers)$`content-length`, NULL)
  expect_identical(parse_headers_list(r3$headers)$`content-length`, NULL)
  expect_identical(parse_headers_list(r4$headers)$`content-length`, NULL)
})