inst/plumber/test/01_test_apps.R

#### ------------------
#
# /apps
#
#### ------------------

# 1. GET an app by app_name
res <- httr::GET(
  paste0(url_, "/apps"),
  auth,
  query = list(
    app_name = app_name_
  ),
  encode = "json"
)

res_content <- jsonlite::fromJSON(
  httr::content(res, "text", encoding = "UTF-8")
)

status_out <- httr::status_code(res)

# check the status code
if (status_out == 200L) print("PASS") else stop("FAIL")
# app does not yet exist, so content should be an empty list
if (length(res_content) == 0) print("PASS") else stop("FAIL")


# 2. create an app
res <- httr::POST(
  paste0(url_, "/apps"),
  auth,
  query = list(
    app_name = app_name_
  ),
  encode = "json"
)

res_content <- jsonlite::fromJSON(
  httr::content(res, "text", encoding = "UTF-8")
)

# 3. GET an app by app_name
res <- httr::GET(
  paste0(url_, "/apps"),
  auth,
  query = list(
    app_name = app_name_
  ),
  encode = "json"
)

app_dat <- jsonlite::fromJSON(
  httr::content(res, "text", encoding = "UTF-8")
)

status_out <- httr::status_code(res)

# TRUE: expect status code == 200
if (status_out == 200) print("PASS") else stop("FAIL")
# should be a 5 column data frame for the app
if (length(app_dat) == 5 && nrow(app_dat) == 1) print("PASS") else stop("FAIL")

# 4. GET an app by app_uid
res <- httr::GET(
  paste0(url_, "/apps"),
  auth,
  query = list(
    app_uid = app_dat$uid
  ),
  encode = "json"
)

res_content <- jsonlite::fromJSON(
  httr::content(res, "text", encoding = "UTF-8")
)

status_out <- httr::status_code(res)

# TRUE: expect status code == 200
if (status_out == 200) print("PASS") else stop("FAIL")
# should be a 5 column data frame for the app
if (length(res_content) == 5 && nrow(res_content) == 1) print("PASS") else stop("FAIL")

# 5. get all apps
res <- httr::GET(
  paste0(url_, "/apps"),
  auth,
  encode = "json"
)

apps_dat <- jsonlite::fromJSON(
  httr::content(res, "text", encoding = "UTF-8")
)

status_out <- httr::status_code(res)
# TRUE: expect status code == 200
if (status_out == 200) print("PASS") else stop("FAIL")



# 6. DELETE the newly created app
res <- httr::DELETE(
  paste0(url_, "/apps"),
  auth,
  query = list(
    app_uid = app_dat$uid
  ),
  encode = "json"
)

res_content <- jsonlite::fromJSON(
  httr::content(res, "text", encoding = "UTF-8")
)

status_out <- httr::status_code(res)
# TRUE: expect status code == 200
if (status_out == 200) print("PASS") else stop("FAIL")



# create the test app again.  This app will be used in the next tests.
res <- httr::POST(
  paste0(url_, "/apps"),
  auth,
  query = list(
    app_name = app_name_
  ),
  encode = "json"
)

res_content <- jsonlite::fromJSON(
  httr::content(res, "text", encoding = "UTF-8")
)

status_out <- httr::status_code(res)
# TRUE: expect status code == 200
if (status_out == 200) print("PASS") else stop("FAIL")
Tychobra/polishedapi documentation built on July 19, 2020, 11:41 p.m.