Skip to content

Commit

Permalink
reimplement the url_check() function
Browse files Browse the repository at this point in the history
  • Loading branch information
Karim-Mane committed Jan 25, 2024
1 parent 6a990e2 commit 85faa71
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 50 deletions.
35 changes: 8 additions & 27 deletions R/readepi_helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -133,36 +133,17 @@ fingertips_get_args <- function(args_list =
#'
#' @param base_url the URL to the HIS of interest
#'
#' @returns throws an error if the domain of the provided URL is not valid,
#' (invisibly) TRUE
#'
#' @keywords internal
#'
url_check <- function(base_url) {
checkmate::assert_character(base_url, any.missing = FALSE, len = 1L,
null.ok = FALSE)
if (grepl("^https?://", base_url)) {
base_url <- gsub("^https?://", "", base_url)
}

# split the URL parts
url_parts <- unlist(strsplit(base_url, "/", fixed = TRUE))
# without the protocol, lets look for the sub-domain, second-level domain
# and the top-level domain
domain_structure_is_correct <- url_check_domain_structure(url_parts[[1L]])
stopifnot("Incorrect domain name in provided URL!" =
domain_structure_is_correct)
return(domain_structure_is_correct)
}

#' Check whether the domain structure of the URL is correct
#'
#' @param url_parts a character string with the domain from the URL
#'
#' @keywords internal
url_check_domain_structure <- function(url_parts) {
domain_structure_is_correct <- TRUE
url_domains <- unlist(strsplit(url_parts, ".",
fixed = TRUE))
if (length(url_domains) < 2L) {
domain_structure_is_correct <- FALSE
}
domain_structure_is_correct
regex <- "^(https?://)?(www\\.)?([a-z0-9]([a-z0-9]|(\\-[a-z0-9]))*\\.)+[a-z]+$" # nolint: line_length_linter
domain <- strsplit(gsub("^(https?://)?(www\\.)?", "", base_url),
"/")[[c(1L, 1L)]]
stopifnot("Incorrect domain name in provided URL!" = grepl(regex, domain))
return(invisible(TRUE))
}
41 changes: 18 additions & 23 deletions tests/testthat/test-readepi_helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -207,44 +207,39 @@ test_that("url_check works as expected", {
})

test_that("url_check works as expected", {
res <- url_check(base_url = "http://www.karim.sn")
res <- url_check(base_url = "stackoverflow.com")
expect_true(res)
})

test_that("url_check works as expected", {
res <- url_check(base_url = "www.karim.sn")
res <- url_check(base_url = "stackoverflow.com/questions/ask")
expect_true(res)
})

test_that("url_check works as expected", {
res <- url_check(base_url = "karim.sn")
res <- url_check(base_url = "https://stackoverflow.com/questions/ask")
expect_true(res)
})

test_that("fingertips_get_args fails as expected", {
test_that("url_check works as expected", {
res <- url_check(base_url = "http://www.amazon.com/Computers-Internet-Books/b/ref=bhp_bb0309A_comint2?ie=UTF8&node=5&pf_rd_m=ATVPDKIKX0DER&pf_rd_s=browse&pf_rd_r=0AH7GM29WF81Q72VPFDH&pf_rd_t=101&pf_rd_p=1273387142&pf_rd_i=283155") # nolint: line_length_linter
expect_true(res)
})

test_that("url_check fails as expected", {
expect_error(
url_check("ftp://www.karim.sn"),
regexp = cat("URL must start with 'http://' or 'https://'.")
url_check("google"),
regexp = cat("URL must be in the form of 'https://test.com' or
'https://www.test.com' or 'test.com'.")
)

expect_error(
url_check("this_should_not_work"),
url_check("http://www"),
regexp = cat("URL must be in the form of 'https://test.com' or
'https://www.test.com' or 'test.com'.")
)
expect_error(
url_check("http://www..des"),
regexp = cat("URL must be in the form of 'https://test.com' or
'https://www.test.com' or 'test.com'.")
)
})

test_that("url_check_domain_structure works as expected", {
res <- url_check_domain_structure("www.karim.sn")
expect_true(res)
})

test_that("url_check_domain_structure works as expected", {
res <- url_check_domain_structure("karim.sn")
expect_true(res)
})

test_that("url_check_domain_structure works as expected", {
res <- url_check_domain_structure("this_should_not_work")
expect_false(res)
})

0 comments on commit 85faa71

Please sign in to comment.