R/plugin-test.R

#' @title Testing Plugin
#'
#' @description
#' Provide a set of utilities to test requests going through the application
#' logic, for profiling or evaluation.
#'
#' @param path      (str) path to be hit by the request
#' @param router    (str) route name
#' @param attach    (str) whether to use the request, message, or header stack
#' @param url       (str) URL of the request
#' @param method    (str) HTTP method
#' @param message   (str) message glue string
#' @param binary    (flg) is the message a binary message?
#' @param base_path (str) root; stripped out of the path when handling requests
#' @param content   (str) content of the request
#' @param headers   (lst) list of request headers of the test request
#' @param trust     (flg) indicates whether request should be trusted
#' @param close     (flg) if this is not a route, then close the connection?
#' @param ...       (arg) extra arguments
#'
#' @export
TestPlugin <- R6Class(

  classname = "Plugin",
  cloneable = FALSE,
  inherit   = DispatchPlugin,

  public = list(

    #' @description Create a new `TestPlugin`
    initialize = function() {
      super$initialize("test", NULL)
    },

    #' @description Simulate an HTTP header request
    #' @param ... (arg) additional arguments to [`fiery::fake_request()`]
    header = function(path      = "/",
                      url       = NULL,
                      method    = "get",
                      router    = NULL,
                      base_path = "",
                      content   = "",
                      headers   = list(),
                      trust     = TRUE,
                      ...) {

      assert_string(path)
      self$fire$build_routers()
      private$dispatcher(
        router  = router,
        attach  = "header",
        request = private$fake_request(path, url, method, base_path,
                                       content, headers, trust, ...),
        binary  = FALSE,
        message = NULL,
        close   = FALSE
      )()

    },

    #' @description Simulate an HTTP request
    #' @param ... (arg) additional arguments to [`fiery::fake_request()`]
    request = function(path      = "/",
                       url       = NULL,
                       method    = "get",
                       router    = NULL,
                       base_path = "",
                       content   = "",
                       headers   = list(),
                       trust     = TRUE,
                       ...) {

      assert_string(path)
      self$fire$build_routers()
      private$dispatcher(
        router  = router,
        attach  = "request",
        request = private$fake_request(path, url, method, base_path,
                                       content, headers, trust, ...),
        binary  = FALSE,
        message = NULL,
        close   = FALSE
      )()

    },

    #' @description Simulate a websocket message
    #' @param message (str) message to be sent
    message = function(message,
                            ...,
                            binary    = FALSE,
                            path      = "/",
                            url       = NULL,
                            method    = "get",
                            router    = NULL,
                            base_path = "",
                            content   = "",
                            headers   = list(),
                            trust     = TRUE,
                            close     = TRUE) {

      assert_string(path)
      self$fire$build_routers()
      private$dispatcher(
        router  = router,
        attach  = "message",
        request = private$fake_request(path, url, method, base_path,
                                       content, headers, trust, ...),
        binary  = binary,
        message = message,
        close   = close
      )()

    }

  )
)
tjpalanca/webtools documentation built on Dec. 23, 2021, 11 a.m.