| 1 |
test_that("Basic URL parser",{ |
| 2 |
url <- 'https://jerry:[email protected]:888/foo/bar?test=123#bla' |
| 3 |
out <- curl_parse_url(url) |
| 4 |
expect_equal(out$url, url) |
| 5 |
expect_equal(out$scheme, 'https') |
| 6 |
expect_equal(out$host, 'google.com') |
| 7 |
expect_equal(out$port, '888') |
| 8 |
expect_equal(out$path, '/foo/bar') |
| 9 |
expect_equal(out$params, c(test="123")) |
| 10 |
expect_equal(out$fragment, 'bla') |
| 11 |
expect_equal(out$user, 'jerry') |
| 12 |
expect_equal(out$password, 'secret') |
| 13 |
expect_equal(curl_modify_url(out), url) |
| 14 |
|
| 15 |
expect_equal(curl_modify_url(out), url) |
| 16 |
out$url <- NULL |
| 17 |
expect_equal(curl_modify_url(out), url) |
| 18 |
}) |
| 19 |
|
| 20 |
test_that("Relative links need a baseurl",{ |
| 21 |
base <- 'https://jerry:[email protected]:888/foo/bar?x=1#nothing' |
| 22 |
out1 <- curl_parse_url("/test1", base) |
| 23 |
out2 <- curl_parse_url("./test2", base) |
| 24 |
out3 <- curl_parse_url("#bla", base) |
| 25 |
expect_equal(out1$url, "https://jerry:[email protected]:888/test1") |
| 26 |
expect_equal(out2$url, "https://jerry:[email protected]:888/foo/test2") |
| 27 |
expect_equal(out3$url, "https://jerry:[email protected]:888/foo/bar?x=1#bla") |
| 28 |
expect_error(curl_parse_url("/test1")) |
| 29 |
expect_error(curl_parse_url("./test2")) |
| 30 |
}) |
| 31 |
|
| 32 |
test_that("Consistent URL encoding", { |
| 33 |
url1 <- "https://ja.wikipedia.org/wiki/\u5bff\u53f8" |
| 34 |
url2 <- "https://ja.wikipedia.org/wiki/%E5%AF%BF%E5%8F%B8" |
| 35 |
out1 <- curl_parse_url(url1) |
| 36 |
out2 <- curl_parse_url(url2) |
| 37 |
out3 <- curl_parse_url(url1, decode = FALSE) |
| 38 |
out4 <- curl_parse_url(url2, decode = FALSE) |
| 39 |
expect_equal(out1$url, url2) |
| 40 |
expect_equal(out1$path, sub("^.*/wiki", "/wiki", url1)) |
| 41 |
expect_equal(out2$url, url2) |
| 42 |
expect_equal(out2$path, sub("^.*/wiki", "/wiki", url1)) |
| 43 |
expect_equal(out3$url, url2) |
| 44 |
expect_equal(out3$path, sub("^.*/wiki", "/wiki", url2)) |
| 45 |
expect_equal(out4$url, url2) |
| 46 |
expect_equal(out4$path, sub("^.*/wiki", "/wiki", url2)) |
| 47 |
|
| 48 |
expect_equal(curl_modify_url(out1), url2) |
| 49 |
expect_equal(curl_modify_url(out2), url2) |
| 50 |
out1$url <- NULL |
| 51 |
out2$url <- NULL |
| 52 |
expect_equal(curl_modify_url(out1), url2) |
| 53 |
expect_equal(curl_modify_url(out2), url2) |
| 54 |
}) |
| 55 |
|
| 56 |
test_that("IPv6 address is understood", { |
| 57 |
out <- curl_parse_url('http://[2001:db8::1]:8080') |
| 58 |
expect_equal(out$host, '[2001:db8::1]') |
| 59 |
expect_equal(out$port, '8080') |
| 60 |
}) |
| 61 |
|
| 62 |
test_that("Decoding parameters", { |
| 63 |
url <- 'https://www.test.com/bla?tv=tom%26jerry&math=1%2B1+%3D+2&empty=' |
| 64 |
out <- curl_parse_url(url) |
| 65 |
expect_equal(out$params, c(tv = "tom&jerry", math = "1+1 = 2", empty = "")) |
| 66 |
expect_equal(curl_modify_url(out), url) |
| 67 |
out$url <- NULL |
| 68 |
expect_equal(curl_modify_url(out), url) |
| 69 |
expect_equal(curl_modify_url(out, query = '', path = ''), 'https://www.test.com/') |
| 70 |
}) |
| 71 |
|
| 72 |
test_that("Using a default scheme", { |
| 73 |
expect_error(curl_parse_url('yolo'), 'parse') |
| 74 |
out <- curl_parse_url('yolo', default_scheme = TRUE) |
| 75 |
expect_equal(out$url, 'https://yolo/') |
| 76 |
}) |