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 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809
|
context("static")
index_file_content <- raw_file_content(test_path("apps/content/index.html"))
data_file_content <- raw_file_content(test_path("apps/content/data.txt"))
subdir_index_file_content <- raw_file_content(test_path("apps/content/subdir/index.html"))
index_file_1_content <- raw_file_content(test_path("apps/content_1/index.html"))
test_that("Basic static file serving", {
s <- startServer("127.0.0.1", randomPort(),
list(
staticPaths = list(
# Testing out various leading and trailing slashes
"/" = test_path("apps/content"),
"/1" = test_path("apps/content"),
"/2/" = test_path("apps/content/"),
"3" = test_path("apps/content"),
"4/" = test_path("apps/content/")
),
staticPathOptions = staticPathOptions(
headers = list("Test-Code-Path" = "C++")
)
)
)
on.exit(s$stop())
# Fetch index.html
r <- fetch(local_url("/", s$getPort()), gzip = FALSE)
expect_equal(r$status_code, 200)
expect_identical(r$content, index_file_content)
# index.html for subdirectory
r_subdir <- fetch(local_url("/subdir", s$getPort()))
expect_equal(r_subdir$status_code, 200)
expect_identical(r_subdir$content, subdir_index_file_content)
h <- parse_headers_list(r$headers)
expect_equal(as.integer(h$`content-length`), length(index_file_content))
expect_equal(as.integer(h$`content-length`), length(r$content))
expect_identical(h$`content-type`, "text/html; charset=utf-8")
expect_identical(h$`test-code-path`, "C++")
# Check that response time is within 1 minute of now. (Possible DST problems?)
expect_true(abs(as.numeric(parse_http_date(h$date)) - as.numeric(Sys.time())) < 60)
# Testing index for other paths
r1 <- fetch(local_url("/1", s$getPort()), gzip = FALSE)
h1 <- parse_headers_list(r1$headers)
expect_identical(r$content, r1$content)
expect_identical(h$`content-length`, h1$`content-length`)
expect_identical(h$`content-type`, h1$`content-type`)
r2 <- fetch(local_url("/1/", s$getPort()), gzip = FALSE)
h2 <- parse_headers_list(r2$headers)
expect_identical(r$content, r2$content)
expect_identical(h$`content-length`, h2$`content-length`)
expect_identical(h$`content-type`, h2$`content-type`)
r3 <- fetch(local_url("/1/index.html", s$getPort()), gzip = FALSE)
h3 <- parse_headers_list(r3$headers)
expect_identical(r$content, r3$content)
expect_identical(h$`content-length`, h3$`content-length`)
expect_identical(h$`content-type`, h3$`content-type`)
# Missing file (404)
r <- fetch(local_url("/foo", s$getPort()), gzip = FALSE)
h <- parse_headers_list(r$headers)
expect_equal(r$status_code, 404)
expect_identical(rawToChar(r$content), "404 Not Found\n")
expect_equal(h$`content-length`, "14")
# Missing directory in path (404)
r <- fetch(local_url("/foo/bar", s$getPort()), gzip = FALSE)
h <- parse_headers_list(r$headers)
expect_equal(r$status_code, 404)
expect_identical(rawToChar(r$content), "404 Not Found\n")
expect_equal(h$`content-length`, "14")
# MIME types for other files
r <- fetch(local_url("/mtcars.csv", s$getPort()))
h <- parse_headers_list(r$headers)
expect_equal(h$`content-type`, "text/csv")
r <- fetch(local_url("/data.txt", s$getPort()))
h <- parse_headers_list(r$headers)
expect_equal(h$`content-type`, "text/plain")
})
test_that("Missing file fallthrough", {
s <- startServer("127.0.0.1", randomPort(),
list(
call = function(req) {
return(list(
status = 404,
headers = list("Test-Code-Path" = "R"),
body = paste0("404 file not found: ", req$PATH_INFO)
))
},
staticPaths = list(
# Testing out various leading and trailing slashes
"/" = staticPath(
test_path("apps/content"),
indexhtml = FALSE,
fallthrough = TRUE
)
)
)
)
on.exit(s$stop())
r <- fetch(local_url("/", s$getPort()))
h <- parse_headers_list(r$headers)
expect_equal(r$status_code, 404)
expect_identical(h$`test-code-path`, "R")
expect_identical(rawToChar(r$content), "404 file not found: /")
})
test_that("Longer paths override shorter ones", {
s <- startServer("127.0.0.1", randomPort(),
list(
staticPaths = list(
# Testing out various leading and trailing slashes
"/" = test_path("apps/content"),
"/a" = staticPath(
test_path("apps/content"),
indexhtml = FALSE
),
"/a/b" = staticPath(
test_path("apps/content"),
indexhtml = NULL
),
"/a/b/c" = staticPath(
test_path("apps/content"),
indexhtml = TRUE
)
)
)
)
on.exit(s$stop())
r <- fetch(local_url("/", s$getPort()))
expect_equal(r$status_code, 200)
expect_identical(r$content, index_file_content)
r <- fetch(local_url("/a/", s$getPort()))
expect_equal(r$status_code, 404)
# When NULL, option values are not inherited from the parent dir, "/a";
# they're inherited from the overall options for the app.
r <- fetch(local_url("/a/b", s$getPort()))
expect_equal(r$status_code, 200)
expect_identical(r$content, index_file_content)
r <- fetch(local_url("/a/b/c", s$getPort()))
expect_equal(r$status_code, 200)
expect_identical(r$content, index_file_content)
})
test_that("Options and option inheritance", {
s <- startServer("127.0.0.1", randomPort(),
list(
call = function(req) {
return(list(
status = 404,
headers = list("Test-Code-Path" = "R"),
body = paste0("404 file not found: ", req$PATH_INFO)
))
},
staticPaths = list(
"/default" = staticPath(test_path("apps/content")),
# This path overrides options
"/override" = staticPath(
test_path("apps/content"),
indexhtml = FALSE,
fallthrough = TRUE,
html_charset = "ISO-8859-1",
headers = list("Test-Code-Path" = "C++2")
),
# This path unsets some options
"/unset" = staticPath(
test_path("apps/content"),
html_charset = "",
headers = list()
)
),
staticPathOptions = staticPathOptions(
indexhtml = TRUE,
fallthrough = FALSE,
headers = list("Test-Code-Path" = "C++")
)
)
)
on.exit(s$stop())
r <- fetch(local_url("/default", s$getPort()))
h <- parse_headers_list(r$headers)
expect_equal(r$status_code, 200)
expect_identical(h$`content-type`, "text/html; charset=utf-8")
expect_identical(h$`test-code-path`, "C++")
expect_identical(r$content, index_file_content)
r <- fetch(local_url("/override", s$getPort()))
h <- parse_headers_list(r$headers)
expect_equal(r$status_code, 404)
expect_identical(h$`test-code-path`, "R")
expect_identical(rawToChar(r$content), "404 file not found: /override")
r <- fetch(local_url("/override/index.html", s$getPort()))
h <- parse_headers_list(r$headers)
expect_equal(r$status_code, 200)
expect_identical(h$`test-code-path`, "C++2")
expect_identical(h$`content-type`, "text/html; charset=ISO-8859-1")
r <- fetch(local_url("/unset", s$getPort()))
h <- parse_headers_list(r$headers)
expect_equal(r$status_code, 200)
expect_false("test-code-path" %in% names(h))
expect_identical(h$`content-type`, "text/html")
expect_identical(r$content, index_file_content)
r <- fetch(local_url("/unset/index.html", s$getPort()))
h <- parse_headers_list(r$headers)
expect_equal(r$status_code, 200)
expect_false("test-code-path" %in% names(h))
expect_identical(h$`content-type`, "text/html")
expect_identical(r$content, index_file_content)
})
test_that("Excluding subpaths", {
s <- startServer("127.0.0.1", randomPort(),
list(
call = function(req) {
# Return a 403 for the R code path; the C++ code path will return 404
# for missing files.
return(list(
status = 403,
headers = list("Test-Code-Path" = "R"),
body = paste0("403 forbidden: ", req$PATH_INFO)
))
},
staticPaths = list(
"/" = staticPath(test_path("apps/content")),
"/exclude" = excludeStaticPath(),
"/subdi" = excludeStaticPath(),
"/a" = staticPath(test_path("apps/content")),
"/a/exclude" = excludeStaticPath(),
"/a/mtcars.csv" = excludeStaticPath()
)
)
)
on.exit(s$stop())
exclude_subdir_index_file_content <- raw_file_content(test_path("apps/content/exclude/subdir/index.html"))
# Basic test
r <- fetch(local_url("/", s$getPort()))
expect_equal(r$status_code, 200)
expect_identical(r$content, index_file_content)
r <- fetch(local_url("/subdir", s$getPort()))
expect_equal(r$status_code, 200)
expect_identical(r$content, subdir_index_file_content)
r <- fetch(local_url("/exclude", s$getPort()))
expect_equal(r$status_code, 403)
r <- fetch(local_url("/exclude/index.html", s$getPort()))
expect_equal(r$status_code, 403)
r <- fetch(local_url("/exclude/subdir", s$getPort()))
expect_equal(r$status_code, 403)
r <- fetch(local_url("/exclude/subdir/index.html", s$getPort()))
expect_equal(r$status_code, 403)
# Include directories underneath excluded dir.
s$setStaticPath("exclude/include" = test_path("apps/content"))
r <- fetch(local_url("/exclude/include", s$getPort()))
expect_equal(r$status_code, 200)
expect_identical(r$content, index_file_content)
s$setStaticPath("exclude/subdir" = test_path("apps/content/exclude/subdir"))
r <- fetch(local_url("/exclude/subdir", s$getPort()))
expect_equal(r$status_code, 200)
expect_identical(r$content, exclude_subdir_index_file_content)
# A file that is not specifically excluded will use the C++ 404 path.
r <- fetch(local_url("/nonexistent.txt", s$getPort()))
expect_equal(r$status_code, 404)
# Fallthrough. Behavior should be unchanged except for non-existent files that
# are NOT in the excluded path.
s$setStaticPathOption(fallthrough = TRUE)
# Now, a file that is not specifically excluded will use the R 403 path
r <- fetch(local_url("/nonexistent.txt", s$getPort()))
expect_equal(r$status_code, 403)
s$setStaticPathOption(fallthrough = FALSE)
# Partial name matching ("subdi" was excluded) doesn't work.
r <- fetch(local_url("/subdir", s$getPort()))
expect_equal(r$status_code, 200)
expect_identical(r$content, subdir_index_file_content)
# Specific files
r <- fetch(local_url("/a/", s$getPort()))
expect_equal(r$status_code, 200)
r <- fetch(local_url("/a/mtcars.csv", s$getPort()))
expect_equal(r$status_code, 403)
# A file that is not specifically excluded will use the C++ 404 path.
r <- fetch(local_url("/file/nonexistent.txt", s$getPort()))
expect_equal(r$status_code, 404)
})
test_that("Header validation", {
s <- startServer("127.0.0.1", randomPort(),
list(
call = function(req) {
if (!identical(req$HTTP_TEST_VALIDATION, "aaa")) {
return(list(
status = 403,
headers = list("Test-Code-Path" = "R"),
body = "403 Forbidden\n"
))
}
return(list(
status = 200,
headers = list("Test-Code-Path" = "R"),
body = "200 OK\n"
))
},
staticPaths = list(
"/default" = staticPath(test_path("apps/content")),
# This path overrides validation
"/override" = staticPath(
test_path("apps/content"),
validation = c('"Test-Validation-1" == "bbb"')
),
# This path unsets validation
"/unset" = staticPath(
test_path("apps/content"),
validation = character()
),
# Fall through to R
"/fallthrough" = staticPath(
test_path("apps/content"),
fallthrough = TRUE
)
),
staticPathOptions = staticPathOptions(
headers = list("Test-Code-Path" = "C++"),
validation = c('"Test-Validation" == "aaa"')
)
)
)
on.exit(s$stop())
r <- fetch(local_url("/default", s$getPort()))
h <- parse_headers_list(r$headers)
expect_equal(r$status_code, 403)
# This header doesn't get set. Should it?
expect_false("test-code-path" %in% names(h))
expect_identical(rawToChar(r$content), "403 Forbidden\n")
r <- fetch(local_url("/default", s$getPort()),
handle_setheaders(new_handle(), "test-validation" = "aaa"))
h <- parse_headers_list(r$headers)
expect_equal(r$status_code, 200)
expect_identical(h$`test-code-path`, "C++")
expect_identical(r$content, index_file_content)
# Check case insensitive
r <- fetch(local_url("/default", s$getPort()),
handle_setheaders(new_handle(), "tesT-ValidatioN" = "aaa"))
expect_equal(r$status_code, 200)
r <- fetch(local_url("/unset", s$getPort()))
h <- parse_headers_list(r$headers)
expect_equal(r$status_code, 200)
expect_identical(h$`test-code-path`, "C++")
expect_identical(r$content, index_file_content)
# When fallthrough=TRUE, the header validation is still checked before falling
# through to the R code path.
r <- fetch(local_url("/fallthrough/missingfile", s$getPort()))
h <- parse_headers_list(r$headers)
expect_equal(r$status_code, 403)
# This header doesn't get set. Should it?
expect_false("test-code-path" %in% names(h))
expect_identical(rawToChar(r$content), "403 Forbidden\n")
r <- fetch(local_url("/fallthrough/missingfile", s$getPort()),
handle_setheaders(new_handle(), "test-validation" = "aaa"))
h <- parse_headers_list(r$headers)
expect_equal(r$status_code, 200)
expect_identical(h$`test-code-path`, "R")
expect_identical(rawToChar(r$content), "200 OK\n")
})
test_that("Dynamically changing paths", {
s <- startServer("127.0.0.1", randomPort(),
list(
call = function(req) {
list(
status = 500,
headers = list("Test-Code-Path" = "R"),
body = "500 Internal Server Error\n"
)
},
staticPaths = list(
"/static" = test_path("apps/content")
)
)
)
on.exit(s$stop())
r <- fetch(local_url("/static", s$getPort()))
expect_equal(r$status_code, 200)
expect_identical(r$content, index_file_content)
# Replace with different static path and options
s$setStaticPath(
"/static" = staticPath(
test_path("apps/content_1"),
indexhtml = FALSE
)
)
r <- fetch(local_url("/static", s$getPort()))
expect_equal(r$status_code, 404)
r <- fetch(local_url("/static/index.html", s$getPort()))
expect_equal(r$status_code, 200)
expect_identical(r$content, index_file_1_content)
# Remove static path
s$removeStaticPath("/static")
expect_equal(length(s$getStaticPaths()), 0)
r <- fetch(local_url("/static", s$getPort()))
expect_equal(r$status_code, 500)
h <- parse_headers_list(r$headers)
expect_identical(h$`test-code-path`, "R")
expect_identical(rawToChar(r$content), "500 Internal Server Error\n")
# Add static path
s$setStaticPath(
"/static_new" = test_path("apps/content")
)
r <- fetch(local_url("/static_new", s$getPort()))
expect_equal(r$status_code, 200)
expect_identical(r$content, index_file_content)
})
test_that("Dynamically changing options", {
s <- startServer("127.0.0.1", randomPort(),
list(
call = function(req) {
list(
status = 500,
headers = list("Test-Code-Path" = "R"),
body = "500 Internal Server Error\n"
)
},
staticPaths = list(
"/static" = test_path("apps/content")
)
)
)
on.exit(s$stop())
r <- fetch(local_url("/static", s$getPort()))
expect_equal(r$status_code, 200)
s$setStaticPathOption(indexhtml = FALSE)
r <- fetch(local_url("/static", s$getPort()))
expect_equal(r$status_code, 404)
s$setStaticPathOption(fallthrough = TRUE)
r <- fetch(local_url("/static", s$getPort()))
expect_equal(r$status_code, 500)
s$setStaticPathOption(
indexhtml = TRUE,
headers = list("Test-Headers" = "aaa"),
validation = c('"Test-Validation" == "aaa"')
)
r <- fetch(local_url("/static", s$getPort()))
expect_equal(r$status_code, 403)
r <- fetch(local_url("/static", s$getPort()),
handle_setheaders(new_handle(), "test-validation" = "aaa"))
h <- parse_headers_list(r$headers)
expect_equal(r$status_code, 200)
expect_identical(h$`test-headers`, "aaa")
# Unset some options
s$setStaticPathOption(
headers = list(),
validation = character()
)
r <- fetch(local_url("/static", s$getPort()))
h <- parse_headers_list(r$headers)
expect_equal(r$status_code, 200)
expect_false("test-headers" %in% h)
})
test_that("Escaped characters in paths", {
# Need to create files with weird names
static_dir <- tempfile("httpuv_test")
dir.create(static_dir)
# Use writeBin() instead of cat() because in Windows, cat() will convert "\n"
# to "\r\n".
writeBin(charToRaw("This is file content.\n"), file.path(static_dir, "file with space.txt"))
on.exit(unlink(static_dir, recursive = TRUE))
s <- startServer("127.0.0.1", randomPort(),
list(
call = function(req) {
list(
status = 500,
headers = list("Test-Code-Path" = "R"),
body = "500 Internal Server Error\n"
)
},
staticPaths = list(
"/static" = static_dir
)
)
)
on.exit(s$stop(), add = TRUE)
r <- fetch(local_url("/static/file%20with%20space.txt", s$getPort()))
expect_equal(r$status_code, 200)
expect_identical(rawToChar(r$content), "This is file content.\n")
})
test_that("Paths with ..", {
s <- startServer("127.0.0.1", randomPort(),
list(
call = function(req) {
list(
status = 404,
headers = list("Test-Code-Path" = "R"),
body = "404 Not Found\n"
)
},
staticPaths = list(
"/static" = test_path("apps/content")
)
)
)
on.exit(s$stop())
# Need to use http_request_con() instead of fetch() to send custom requests
# with "..".
res <- http_request_con("GET /", "127.0.0.1", s$getPort())
expect_identical(res[1], "HTTP/1.1 404 Not Found")
expect_true(any(grepl("^Test-Code-Path: R$", res, ignore.case = TRUE)))
res <- http_request_con("GET /static", "127.0.0.1", s$getPort())
expect_identical(res[1], "HTTP/1.1 200 OK")
# The presence of a ".." path segment results in a 400.
res <- http_request_con("GET /static/..", "127.0.0.1", s$getPort())
expect_identical(res[1], "HTTP/1.1 400 Bad Request")
res <- http_request_con("GET /static/../", "127.0.0.1", s$getPort())
expect_identical(res[1], "HTTP/1.1 400 Bad Request")
res <- http_request_con("GET /static/../static", "127.0.0.1", s$getPort())
expect_identical(res[1], "HTTP/1.1 400 Bad Request")
# ".." is valid as part of a path segment (but we'll get 404's since the files
# don't actually exist).
res <- http_request_con("GET /static/..foo", "127.0.0.1", s$getPort())
expect_identical(res[1], "HTTP/1.1 404 Not Found")
expect_false(any(grepl("^Test-Code-Path: R$", res, ignore.case = TRUE)))
res <- http_request_con("GET /static/foo..", "127.0.0.1", s$getPort())
expect_identical(res[1], "HTTP/1.1 404 Not Found")
expect_false(any(grepl("^Test-Code-Path: R$", res, ignore.case = TRUE)))
res <- http_request_con("GET /static/foo../", "127.0.0.1", s$getPort())
expect_identical(res[1], "HTTP/1.1 404 Not Found")
expect_false(any(grepl("^Test-Code-Path: R$", res, ignore.case = TRUE)))
})
test_that("Paths with backslash", {
s <- startServer("127.0.0.1", randomPort(),
list(
call = function(req) {
list(
status = 400,
headers = list("Test-Code-Path" = "R"),
body = "400 Bad Request\n"
)
},
staticPaths = list(
"/static" = test_path("apps/content")
)
)
)
on.exit(s$stop())
# Need to use http_request_con() instead of fetch() to send custom requests
# with "..".
# When a backslash is in path, should fall through to R code path.
# Raw backslash
res <- http_request_con("GET /static\\index.html", "127.0.0.1", s$getPort())
expect_identical(res[1], "HTTP/1.1 400 Bad Request")
expect_true(any(grepl("^Test-Code-Path: R$", res, ignore.case = TRUE)))
# Escaped backslash
res <- http_request_con("GET /static%5cindex.html", "127.0.0.1", s$getPort())
expect_identical(res[1], "HTTP/1.1 400 Bad Request")
expect_true(any(grepl("^Test-Code-Path: R$", res, ignore.case = TRUE)))
# Raw backslash with ..
res <- http_request_con("GET /static/..\\index.html", "127.0.0.1", s$getPort())
expect_identical(res[1], "HTTP/1.1 400 Bad Request")
expect_true(any(grepl("^Test-Code-Path: R$", res, ignore.case = TRUE)))
# Escaped backslash with ..
res <- http_request_con("GET /static/..%5cindex.html", "127.0.0.1", s$getPort())
expect_identical(res[1], "HTTP/1.1 400 Bad Request")
expect_true(any(grepl("^Test-Code-Path: R$", res, ignore.case = TRUE)))
})
test_that("HEAD, POST, PUT requests", {
s <- startServer("127.0.0.1", randomPort(),
list(
call = function(req) {
list(
status = 404,
headers = list("Test-Code-Path" = "R"),
body = "404 Not Found\n"
)
},
staticPaths = list(
"/static" = test_path("apps/content")
)
)
)
on.exit(s$stop())
# The GET results, for comparison to HEAD.
r_get <- fetch(local_url("/static", s$getPort()), gzip = FALSE)
h_get <- parse_headers_list(r_get$headers)
# HEAD is OK.
# Note the weird interface for a HEAD request:
# https://github.com/jeroen/curl/issues/24
r <- fetch(local_url("/static", s$getPort()), new_handle(nobody = TRUE), gzip = FALSE)
expect_equal(r$status_code, 200)
expect_true(length(r$content) == 0) # No message body for HEAD
h <- parse_headers_list(r$headers)
# Headers should match GET request, except for date.
expect_identical(h[setdiff(names(h), "date")], h_get[setdiff(names(h_get), "date")])
# POST and PUT are not OK
r <- fetch(local_url("/static", s$getPort()),
handle_setopt(new_handle(), customrequest = "POST"))
expect_equal(r$status_code, 400)
r <- fetch(local_url("/static", s$getPort()),
handle_setopt(new_handle(), customrequest = "PUT"))
expect_equal(r$status_code, 400)
})
test_that("Last-Modified and If-Modified-Since headers", {
s <- startServer("127.0.0.1", randomPort(),
list(
staticPaths = list(
"/" = staticPath(
test_path("apps/content"),
headers = list(
"ETag" = "abc",
"Cache-Control" = "max-age=12345",
"Other" = "xyz"
)
)
)
)
)
on.exit(s$stop())
# mtime of the target file, rounded down to nearest second.
file_mtime <- as.POSIXct(trunc(file.info(test_path("apps/content/mtcars.csv"))$mtime))
# First time retrieving: no Last-Modified header.
r <- fetch(local_url("/mtcars.csv", s$getPort()))
h <- parse_headers_list(r$headers)
http_mtime <- r$modified
expect_equal(file_mtime, http_mtime)
# Use the Last-Modified value in the If-Modified-Since header.
r1 <- fetch(local_url("/mtcars.csv", s$getPort()),
handle_setheaders(new_handle(),
"If-Modified-Since" = h$`last-modified`
)
)
expect_identical(r1$status_code, 304L)
expect_true(length(r1$content) == 0)
h1 <- parse_headers_list(r1$headers)
# A 304 response should contain only the following headers (and must contain
# them if the corresponding 200 response would have them):
# Cache-Control, Content-Location, Date, ETag, Expires, Vary
# https://httpstatuses.com/304
expect_identical(h[c("cache-control", "etag")], h1[c("cache-control", "etag")])
# The Date header differs from the previous response because the request was
# made at a different time. We just need to check that it's present.
expect_true("date" %in% names(h1))
# The mtime plus 1 second should result in a 304.
r1 <- fetch(local_url("/mtcars.csv", s$getPort()),
handle_setheaders(new_handle(),
"If-Modified-Since" = http_date_string(file_mtime + 1)
)
)
expect_identical(r1$status_code, 304L)
# Last-Modified header minus 1 second should result in a regular 200 response.
r1 <- fetch(local_url("/mtcars.csv", s$getPort()),
handle_setheaders(new_handle(),
"If-Modified-Since" = http_date_string(file_mtime - 1)
)
)
expect_identical(r1$status_code, 200L)
h1 <- parse_headers_list(r1$headers)
expect_identical(h[setdiff(names(h), "date")], h1[setdiff(names(h1), "date")])
# Malformed If-Modified-Since value should be ignored.
#
# First, a date far in the future should result in 304. Note that the 2038
# date is used here because on 32-bit Windows, dates that are beyond
# 2038-01-19 will overflow and wrap around, and this request will get a 200
# instead of 304. Other platforms seem not to have this limitation.
r1 <- fetch(local_url("/mtcars.csv", s$getPort()),
handle_setheaders(new_handle(),
"If-Modified-Since" = "Mon, 01 Jan 2038 12:00:00 GMT"
)
)
expect_identical(r1$status_code, 304L)
# Next, almost the same date, but slightly malformed, should result in 200.
r1 <- fetch(local_url("/mtcars.csv", s$getPort()),
handle_setheaders(new_handle(),
"If-Modified-Since" = "Mon, 01 Jan 2038 12:100:00 GMT"
)
)
expect_identical(r1$status_code, 200L)
})
test_that("Paths with non-ASCII characters", {
# Workaround for https://github.com/rstudio/httpuv/issues/264
# On Unix platforms that are using a non-UTF-8 locale, don't do these tests.
testthat::skip_if(
.Platform$OS.type == "unix" && !l10n_info()[["UTF-8"]],
"Skipping non-ASCII path tests on UTF-8 Unix system"
)
# "apps/fü", in UTF-8 encoding.
nonascii_path <- test_path("apps/f\U00FC")
dir.create(nonascii_path)
on.exit(unlink(nonascii_path, recursive = TRUE))
index_file_path <- file.path(nonascii_path, "index.html")
writeLines("Hello world!", index_file_path)
file_content <- raw_file_content(index_file_path)
s <- startServer("0.0.0.0", randomPort(),
list(
call = function(req) {
list(
status = 200L,
headers = list('Content-Type' = 'text/html'),
body = "R code path"
)
},
staticPaths = list(
"/f\U00FC" = nonascii_path,
"/foo" = nonascii_path
)
)
)
on.exit(s$stop(), add = TRUE)
# URL-encoded non-ASCII URL path, which maps to non-ASCII local path.
r <- fetch(local_url("/f%C3%BC", s$getPort()))
expect_identical(r$status_code, 200L)
# ASCII URL path, which maps to non-ASCII local path.
r <- fetch(local_url("/foo", s$getPort()))
expect_identical(r$status_code, 200L)
expect_identical(r$content, file_content)
})
|