| 1 |
library(testit) |
| 2 |
|
| 3 |
assert('html_doc() wraps content in an HTML document', { |
| 4 |
x = html_doc('<p>hi</p>', 'My Title') |
| 5 |
(any(grepl('<!DOCTYPE html>', x))) |
| 6 |
(any(grepl('<title>My Title</title>', x))) |
| 7 |
(any(grepl('<p>hi</p>', x))) |
| 8 |
}) |
| 9 |
|
| 10 |
assert('fileinfo_table() builds a directory listing table', { |
| 11 |
d = tempfile(); dir.create(d) |
| 12 |
on.exit(unlink(d, recursive = TRUE)) |
| 13 |
writeLines('a', file.path(d, 'a.txt')) |
| 14 |
dir.create(file.path(d, 'sub')) |
| 15 |
info = file.info(list.files(d, all.files = TRUE, full.names = TRUE)) |
| 16 |
tbl = fileinfo_table(info) |
| 17 |
(any(grepl('<table>', tbl))) |
| 18 |
(any(grepl('a.txt', tbl))) |
| 19 |
# directories get a trailing slash |
| 20 |
(any(grepl('sub/', tbl))) |
| 21 |
}) |
| 22 |
|
| 23 |
assert('resolve_path() classifies files, dirs, redirects, and 404s', { |
| 24 |
d = tempfile(); dir.create(d) |
| 25 |
on.exit(unlink(d, recursive = TRUE)) |
| 26 |
writeLines('hello', file.path(d, 'a.txt')) |
| 27 |
writeLines('<h1>Index</h1>', file.path(d, 'index.html')) |
| 28 |
dir.create(file.path(d, 'sub')) |
| 29 |
writeLines('sub-content', file.path(d, 'sub', 'b.txt')) |
| 30 |
|
| 31 |
in_dir(d, { |
| 32 |
# root with index.html |
| 33 |
r = resolve_path('.') |
| 34 |
(r$action %==% 'file') |
| 35 |
(basename(r$path) %==% 'index.html') |
| 36 |
|
| 37 |
# explicit file |
| 38 |
r = resolve_path('a.txt') |
| 39 |
(r$action %==% 'file') |
| 40 |
(basename(r$path) %==% 'a.txt') |
| 41 |
|
| 42 |
# directory without trailing slash → caller redirects |
| 43 |
r = resolve_path('sub') |
| 44 |
(r$action %==% 'add-slash') |
| 45 |
|
| 46 |
# directory listing (trailing slash, no index.html) |
| 47 |
r = resolve_path('sub/') |
| 48 |
(r$action %==% 'payload') |
| 49 |
(r$mime %==% 'text/html') |
| 50 |
(grepl('Index of', r$body)) |
| 51 |
(grepl('b.txt', r$body)) |
| 52 |
|
| 53 |
# missing file gives 404 payload |
| 54 |
r = resolve_path('missing.txt') |
| 55 |
(r$action %==% 'payload') |
| 56 |
(r$status %==% 404L) |
| 57 |
(grepl('Not found', r$body)) |
| 58 |
|
| 59 |
# favicon.ico falls back to R's built-in |
| 60 |
r = resolve_path('favicon.ico') |
| 61 |
(r$action %==% 'file') |
| 62 |
(basename(r$path) %==% 'favicon.ico') |
| 63 |
(r$mime %==% 'image/x-icon') |
| 64 |
|
| 65 |
# custom 404.html → caller redirects |
| 66 |
writeLines('<h1>404</h1>', '404.html') |
| 67 |
r = resolve_path('nope.html') |
| 68 |
(r$action %==% '404-redirect') |
| 69 |
}) |
| 70 |
}) |
| 71 |
|
| 72 |
assert('.resolve_path_to_httpd() maps to R httpd response shape', { |
| 73 |
d = tempfile(); dir.create(d) |
| 74 |
on.exit(unlink(d, recursive = TRUE)) |
| 75 |
writeLines('hi', file.path(d, 'a.txt')) |
| 76 |
dir.create(file.path(d, 'sub')) |
| 77 |
|
| 78 |
in_dir(d, { |
| 79 |
# file response |
| 80 |
r = .resolve_path_to_httpd('a.txt') |
| 81 |
(basename(r$file) %==% 'a.txt') |
| 82 |
|
| 83 |
# add-slash → 301 with Location header |
| 84 |
r = .resolve_path_to_httpd('sub') |
| 85 |
(r$`status code` %==% 301L) |
| 86 |
(grepl('^Location: /sub/$', r$header)) |
| 87 |
|
| 88 |
# 404 payload |
| 89 |
r = .resolve_path_to_httpd('missing.txt') |
| 90 |
(r$`status code` %==% 404L) |
| 91 |
(r$`content-type` %==% 'text/plain') |
| 92 |
|
| 93 |
# 404.html redirect |
| 94 |
writeLines('x', '404.html') |
| 95 |
r = .resolve_path_to_httpd('nope.html') |
| 96 |
(r$`status code` %==% 302L) |
| 97 |
(r$header %==% 'Location: /404.html') |
| 98 |
}) |
| 99 |
}) |