Skip to content

Commit

Permalink
add feedback from pull request 33 #24
Browse files Browse the repository at this point in the history
  • Loading branch information
salvafern committed Jan 26, 2022
1 parent 5e65cd1 commit c472ab5
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 17 deletions.
6 changes: 4 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ Suggests:
dplyr,
mapview,
testthis,
webmockr
webmockr,
withr
RoxygenNote: 7.1.1
SystemRequirements: C++11, GDAL (>= 2.0.1), GEOS (>= 3.4.0),
PROJ (>= 4.8.0)
Expand All @@ -53,7 +54,8 @@ Imports:
janitor,
curl,
jsonlite,
httr
httr,
utils
URL: https://github.com/EMODnet/EMODnetWFS
BugReports: https://github.com/EMODnet/EMODnetWFS/issues
VignetteBuilder: knitr
Expand Down
20 changes: 15 additions & 5 deletions R/client.R
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,14 @@ perform_http_request <- function(service_url){
usethis::ui_oops("WFS client creation failed.")
usethis::ui_info("Service: {usethis::ui_value(service_url)}")

if(!curl::has_internet()){
has_internet <- function() {
if (nzchar(Sys.getenv("NO_INTERNET_TEST_EMODNET"))) {
return(FALSE)
}
curl::has_internet()
}

if(!has_internet()){
usethis::ui_info("Reason: There is no internet connection")
return(NULL)
}
Expand All @@ -84,15 +91,18 @@ perform_http_request <- function(service_url){
# If fails returns FALSE, if there is internet and HTTP status is successful then return TRUE
check_service <- function(request){

# Raise the GetCapabilities throws a bad HTTP status
if(is.null(request)){
return(NULL)
}

if(httr::http_error(request)){
usethis::ui_info("HTTP Status: {crayon::red(httr::http_status(request)$message)}")
usethis::ui_line()

# Check if monitor tool is up, and then ask if wants to browse the app
if(interactive() & !is.null(curl::nslookup("monitor.emodnet.eu", error = FALSE))){
is_monitor_up <- !is.null(curl::nslookup("monitor.emodnet.eu", error = FALSE))
if(interactive() & is_monitor_up){
if(usethis::ui_yeah("Browse the EMODnet OGC monitor?")){
browseURL("https://monitor.emodnet.eu/resources?lang=en&resource_type=OGC:WFS")
utils::browseURL("https://monitor.emodnet.eu/resources?lang=en&resource_type=OGC:WFS")
}
}

Expand Down
31 changes: 21 additions & 10 deletions tests/testthat/test-client.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ test_that("Default connection works", {
c("WFSClient", "OWSClient", "OGCAbstractObject", "R6"))
expect_equal(wfs$getUrl(),
"https://ows.emodnet-seabedhabitats.eu/geoserver/emodnet_open_maplibrary/wfs")
})
})

test_that("Specified connection works", {
wfs <- emodnet_init_wfs_client(service = "bathymetry")
Expand All @@ -15,16 +15,14 @@ test_that("Specified connection works", {
})

test_that("Services down handled", {

test_url <- "https://demo.geo-solutions.it/geoserver/ows?request=GetCapabilities"

webmockr::httr_mock()

z <- webmockr::stub_request('get', uri = test_url) %>%
webmockr::wi_th(headers = list('Accept' = 'application/json, text/xml, application/xml, */*'))
test_url <- "https://demo.geo-solutions.it/geoserver/ows?request=GetCapabilities"

webmockr::to_return(z, status = 500)
webmockr::to_return(z, status = 200)
webmockr::stub_request('get', uri = test_url) %>%
webmockr::wi_th(headers = list('Accept' = 'application/json, text/xml, application/xml, */*')) %>%
webmockr::to_return(status = 500) %>%
webmockr::to_return(status = 200)

req_fail <- httr::GET(test_url)
req_success <- httr::GET(test_url)
Expand All @@ -34,9 +32,22 @@ test_that("Services down handled", {
expect_false(httr::http_error(req_success))

# Test check_service behavior
expect_null(EMODnetWFS:::check_service(req_fail))
expect_error(EMODnetWFS:::check_service(req_success))
expect_null(check_service(req_fail))
expect_error(check_service(req_success))

webmockr::disable()
})

test_that("No internet challenge", {
withr::local_envvar(list(NO_INTERNET_TEST_EMODNET = "bla"))

test_url <- "https://demo.geo-solutions.it/geoserver/ows?"

req_no_internet <- perform_http_request(test_url)

expect_null(req_no_internet)
expect_null(check_service(req_no_internet))
})



0 comments on commit c472ab5

Please sign in to comment.