Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions R/loadtest.R
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,29 @@ parse_url <- function(url){
list(protocol = protocol, domain = domain, path = path, port = port)
}

#' Encode HTML/XML entities
#'
#' Jemeter's documentation in XML which means special characters like &, <, >
#' need to be encoded as HTML entities. This function does that without dependencies.
#'
#' @param stringified_body the request body as a single string
#'
#' @return a string
#'
#' @examples
#' encode_html_entities('{"title":"this & that"}')
encode_html_entities <- function(stringified_body) {
body_noamp <- gsub("&", "&amp;", stringified_body)
body_noquot <- gsub("\"", "&quot;", body_noamp)
body_nogt <- gsub(">", "&gt;", body_noquot)
body_nolt <- gsub("<", "&lt;", body_nogt)
body_noapos <- gsub("\'", "&apos;", body_nolt)

encoded_body <- gsub("\"", "&quot;", body_noapos)

encoded_body
}

#' Run a load test of an HTTP request
#'
#' This is the core function of the package, which creates many HTTP requests using Apache JMeter.
Expand Down Expand Up @@ -170,9 +193,11 @@ loadtest <- function(url,
if(!is.null(body)){

if(encode=="json"){
request_body <- gsub("\"", "&quot;", jsonlite::toJSON(body,auto_unbox=TRUE))
json_body <- jsonlite::toJSON(body,auto_unbox=TRUE)
request_body <- encode_html_entities(json_body)

} else if(encode=="raw"){
request_body <- gsub("\"", "&quot;", body)
request_body <- request_body <- encode_html_entities(body)
} else {
stop("'encode' value not yet supported")
}
Expand Down
21 changes: 21 additions & 0 deletions man/encode_html_entities.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions tests/testthat/test_loadtest.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,14 @@ test_that("loadtest works with more method/headers/body", {
expect_equal(nrow(results), threads*loops, label = "Table had invalid number of rows")
expect_true(all(results$request_status=="Success"),label = "Some requests failed")
})

test_that("encode_html_entities works", {
test_body <- list(title = "this & that",
body = "!@#$%^&*()-=+~`?/>.<,±§'|:;{]{}äüãçöß]",
userId = 1)
json_body <- jsonlite::toJSON(test_body, auto_unbox = TRUE)
expect_equal(
as.character(encode_html_entities(json_body)),
"{&quot;title&quot;:&quot;this &amp; that&quot;,&quot;body&quot;:&quot;!@#$%^&amp;*()-=+~`?/&gt;.&lt;,±§&apos;|:;{]{}äüãçöß]&quot;,&quot;userId&quot;:1}"
)
})