From b4aa870a3dcf56edf20329972a4bf54cc72f673d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Santiago?= Date: Fri, 3 Apr 2020 09:18:12 +0200 Subject: [PATCH] encode html entitites Fixes https://github.com/tmobile/loadtest/issues/10 --- R/loadtest.R | 29 +++++++++++++++++++++++++++-- man/encode_html_entities.Rd | 21 +++++++++++++++++++++ tests/testthat/test_loadtest.R | 11 +++++++++++ 3 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 man/encode_html_entities.Rd diff --git a/R/loadtest.R b/R/loadtest.R index 1b7a2b0..96a265d 100644 --- a/R/loadtest.R +++ b/R/loadtest.R @@ -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("&", "&", stringified_body) + body_noquot <- gsub("\"", """, body_noamp) + body_nogt <- gsub(">", ">", body_noquot) + body_nolt <- gsub("<", "<", body_nogt) + body_noapos <- gsub("\'", "'", body_nolt) + + encoded_body <- gsub("\"", """, 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. @@ -170,9 +193,11 @@ loadtest <- function(url, if(!is.null(body)){ if(encode=="json"){ - request_body <- gsub("\"", """, 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("\"", """, body) + request_body <- request_body <- encode_html_entities(body) } else { stop("'encode' value not yet supported") } diff --git a/man/encode_html_entities.Rd b/man/encode_html_entities.Rd new file mode 100644 index 0000000..f4c7f36 --- /dev/null +++ b/man/encode_html_entities.Rd @@ -0,0 +1,21 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/loadtest.R +\name{encode_html_entities} +\alias{encode_html_entities} +\title{Encode HTML/XML entities} +\usage{ +encode_html_entities(stringified_body) +} +\arguments{ +\item{stringified_body}{the request body as a single string} +} +\value{ +a string +} +\description{ +Jemeter's documentation in XML which means special characters like &, <, > +need to be encoded as HTML entities. This function does that without dependencies. +} +\examples{ +encode_html_entities('{"title":"this & that"}') +} diff --git a/tests/testthat/test_loadtest.R b/tests/testthat/test_loadtest.R index cdd868c..28d9718 100644 --- a/tests/testthat/test_loadtest.R +++ b/tests/testthat/test_loadtest.R @@ -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)), + "{"title":"this & that","body":"!@#$%^&*()-=+~`?/>.<,±§'|:;{]{}äüãçöß]","userId":1}" + ) +})