From 5c3ec4c335a848671b3826ed8ea41ce64c03eb5f Mon Sep 17 00:00:00 2001 From: Trevor McMaster Date: Thu, 26 Oct 2023 15:13:08 -0600 Subject: [PATCH] Skip response body scripting (#170) * Bug fixes 2023-10-26 (#168) * Fixed issue with agent instead of controller acceptance * Fixed the errorfile validator since it can be an object * Fixed the acceptance tests if run after the agent tests * Fixed the start scripts to match the other controller/agent scripts * Cleaned up the static environment vars * Added new npm run acceptance:all - Acceptance:all will start both the agent and controller, and run the acceptance tests for both. You must manually Ctrl -C when finished * Split the skipBody try parameter into separate request/response skip (#169) * Split the skipBody try parameter into separate request/response skip - -k/--skip-response-body will only skip the response body - -K/--skip-request-body will only skip the request body - '-k -K' will skip BOTH request and response body * Fixed cargo fmt --- controller/acceptance/errorFile.spec.ts | 5 ++- controller/acceptance/test.spec.ts | 5 ++- controller/components/Layout/index.tsx | 2 +- controller/next.config.js | 27 +++++-------- guide/src/cli.md | 14 +++---- package.json | 12 +++--- pr.sh | 16 ++++---- src/bin/pewpew.rs | 54 ++++++++++++++++++++----- src/lib.rs | 13 +++--- 9 files changed, 91 insertions(+), 57 deletions(-) diff --git a/controller/acceptance/errorFile.spec.ts b/controller/acceptance/errorFile.spec.ts index b62b48c6..1edf4cfc 100644 --- a/controller/acceptance/errorFile.spec.ts +++ b/controller/acceptance/errorFile.spec.ts @@ -154,7 +154,10 @@ describe("ErrorFile API Integration", function () { } else { expect(res.status, "status").to.equal(200); expect(res.data, "body").to.not.equal(undefined); - expect(typeof res.data, "typeof res.data").to.equal("string"); + // If it's only a single line (that is json) this will be an object like + // {"type":"end","msg":"Test killed early by Ctrl-c"} + // expect(typeof res.data, "typeof res.data").to.equal("string"); + expect(["string", "object"].includes(typeof res.data), `["string", "object"].includes("${typeof res.data}")`).to.equal(true); done(); } } else { diff --git a/controller/acceptance/test.spec.ts b/controller/acceptance/test.spec.ts index 87e0b03f..ac604e93 100644 --- a/controller/acceptance/test.spec.ts +++ b/controller/acceptance/test.spec.ts @@ -1971,8 +1971,9 @@ describe("Test API Integration", () => { expect(Array.isArray(allTests.requestedTests), "requestedTests").to.equal(true); // Running should have at least one now expect(allTests.runningTests.length, "tests.runningTests.length: " + allTests.runningTests.length).to.be.greaterThan(0); - expect(allTests.recentTests.length, "tests.recentTests.length: " + allTests.recentTests.length).to.equal(0); - expect(allTests.requestedTests.length, "tests.requestedTests.length: " + allTests.requestedTests.length).to.be.greaterThanOrEqual(0); + // recent should be zero. But if the agent acceptance are run before this it will be 1 + // expect(allTests.recentTests.length, "tests.recentTests.length: " + allTests.recentTests.length).to.equal(0); + // expect(allTests.requestedTests.length, "tests.requestedTests.length: " + allTests.requestedTests.length).to.be.greaterThanOrEqual(0); }); it("GET /test?testId=validInS3 should respond 200 OK", (done: Mocha.Done) => { diff --git a/controller/components/Layout/index.tsx b/controller/components/Layout/index.tsx index 825c03b3..ab3dab0d 100644 --- a/controller/components/Layout/index.tsx +++ b/controller/components/Layout/index.tsx @@ -17,7 +17,7 @@ import { logout as authLogout } from "../../pages/api/util/authclient"; import getConfig from "next/config"; // Have to check for null on this since the tsc test compile it will be, but nextjs will have a publicRuntimeConfig -const publicRuntimeConfig: any = getConfig() && getConfig().publicRuntimeConfig ? getConfig().publicRuntimeConfig : {}; +const publicRuntimeConfig: any = getConfig() && getConfig().publicRuntimeConfig ? getConfig().publicRuntimeConfig : process.env; const HIDE_ENVIRONMENT: unknown = publicRuntimeConfig.HIDE_ENVIRONMENT; export type OtherControllers = Record Directory to store logs (if enabled with --loggers) - -k, --skipBody Skips request and reponse body from output (try command) + -k, --skip-response-body Skips reponse body from output (try command) + -K, --skip-request-body Skips request body from output (try command) -h, --help Prints help information ``` @@ -84,7 +82,9 @@ The `-l`, `--loggers` flag specifies that any loggers defined in the config file The `-d`, `--results-directory` parameter will store any log files (if the `--loggers` flag is used) in the specified directory. If the directory does not exist it is created. -The `-k`, `--skipBody` parameter ensures that during a Try run, the request and response bodies aren't displayed. This can be particularly useful for debugging requests or responses when the body is not crucial for the debugging process. +The `-k`, `--skip-response-body` parameter ensures that during a Try run, the response bodies aren't displayed. This can be particularly useful for debugging responses when the body is very long and not crucial for the debugging process. + +The `-K`, `--skip-request-body` parameter ensures that during a Try run, the request bodies aren't displayed. This can be particularly useful for debugging requests when the body is very long and not crucial for the debugging process.

In both the `run` and `try` subcommands a [config file](./config.md) is required. diff --git a/package.json b/package.json index 665bec46..bb74ef47 100644 --- a/package.json +++ b/package.json @@ -10,9 +10,9 @@ "./controller" ], "scripts": { - "start": "npm-run-all --parallel startagent startcontroller", - "startcontroller": "cd controller && npm run start", - "startagent": "cd agent && npm run start", + "start": "npm-run-all --parallel start:agent start:controller", + "start:controller": "cd controller && npm run start", + "start:agent": "cd agent && npm run start", "dev": "cd controller && npm run dev", "build": "npm run lint && npm run build:common && npm run build:agent && npm run build:controller", "storybook": "cd controller && npm run storybook", @@ -35,9 +35,11 @@ "test:common": "cd common && npm run test", "test:agent": "cd agent && npm run test", "test:controller": "cd controller && npm run test", - "acceptance": "npm run acceptance:agent && npm run acceptance:controller", + "acceptance:all": "npm-run-all --parallel start acceptance:sleep", + "acceptance:sleep": "sleep 30 && npm run acceptance", + "acceptance": "npm-run-all --serial acceptance:agent acceptance:controller", "acceptance:agent": "cd agent && npm run acceptance", - "acceptance:controller": "cd agent && npm run acceptance", + "acceptance:controller": "cd controller && npm run acceptance", "integration": "npm-run-all --serial lint integration:common integration:agent integration:controller testmerge", "integration:common": "cd common && npm run integration", "integration:agent": "cd agent && npm run integration", diff --git a/pr.sh b/pr.sh index 056ddb51..7cff7871 100755 --- a/pr.sh +++ b/pr.sh @@ -60,15 +60,13 @@ npm run linterror npm run build:react NODE_ENV=test npm test -# terminal 1 +# npm run testcleanup # npm run coverage -# grab screenshot -# cd controller/ && npm run testcleanup - -# terminal 2 -# cd controller/ && npm start -# terminal 3 -# cd controller/ && npm run acceptance -# grab screenshot +# echo grab screenshot + +# npm run testcleanup +# echo Hit Ctrl-C when acceptance tests finish +# npm acceptance:all +# echo grab screenshot cargo deny check --hide-inclusion-graph license sources advisories diff --git a/src/bin/pewpew.rs b/src/bin/pewpew.rs index a8083281..ef29bba4 100644 --- a/src/bin/pewpew.rs +++ b/src/bin/pewpew.rs @@ -151,15 +151,19 @@ mod args { /// Directory to store logs (if enabled with --loggers) #[arg(short = 'd', long = "results-directory", value_name = "DIRECTORY")] results_dir: Option, - /// Skips request and reponse body from output - #[arg(short = 'k', long = "skipBody")] - skip_body_on: bool, + /// Skips reponse body from output + #[arg(short = 'k', long = "skip-response-body")] + skip_response_body_on: bool, + /// Skips request body from output + #[arg(short = 'K', long = "skip-request-body")] + skip_request_body_on: bool, } impl From for TryConfig { fn from(value: TryConfigTmp) -> Self { let loggers_on = value.loggers_on; - let skip_body_on = value.skip_body_on; + let skip_response_body_on = value.skip_response_body_on; + let skip_request_body_on = value.skip_request_body_on; let results_dir = value.results_dir.filter(|_| loggers_on); if let Some(d) = &results_dir { create_dir_all(d).unwrap(); @@ -172,7 +176,8 @@ mod args { filters: value.filters, file: value.file, format: value.format, - skip_body_on, + skip_response_body_on, + skip_request_body_on, } } } @@ -420,7 +425,8 @@ mod tests { assert!(try_config.filters.is_none()); assert!(matches!(try_config.format, TryRunFormat::Human)); assert!(!try_config.loggers_on); - assert!(!try_config.skip_body_on); + assert!(!try_config.skip_response_body_on); + assert!(!try_config.skip_request_body_on); assert!(try_config.results_dir.is_none()); } @@ -437,6 +443,7 @@ mod tests { "_id=0", "-l", "-k", + "-K", "-o", STATS_FILE, YAML_FILE, @@ -460,7 +467,8 @@ mod tests { } assert!(matches!(try_config.format, TryRunFormat::Json)); assert!(try_config.loggers_on); - assert!(try_config.skip_body_on); + assert!(try_config.skip_response_body_on); + assert!(try_config.skip_request_body_on); assert!(try_config.results_dir.is_some()); assert_eq!(try_config.results_dir.unwrap().to_str().unwrap(), TEST_DIR); } @@ -477,7 +485,8 @@ mod tests { "--include", "_id=0", "--loggers", - "--skipBody", + "--skip-response-body", + "--skip-request-body", "--file", STATS_FILE, YAML_FILE, @@ -501,11 +510,38 @@ mod tests { } assert!(matches!(try_config.format, TryRunFormat::Json)); assert!(try_config.loggers_on); - assert!(try_config.skip_body_on); + assert!(try_config.skip_response_body_on); + assert!(try_config.skip_request_body_on); assert!(try_config.results_dir.is_some()); assert_eq!(try_config.results_dir.unwrap().to_str().unwrap(), TEST_DIR); } + #[test] + fn cli_try_skip_response_body() { + let cli_config = + args::try_parse_from(["myprog", TRY_COMMAND, "--skip-response-body", YAML_FILE]) + .unwrap(); + let ExecConfig::Try(try_config) = cli_config else { + panic!() + }; + assert_eq!(try_config.config_file.to_str().unwrap(), YAML_FILE); + assert!(try_config.skip_response_body_on); + assert!(!try_config.skip_request_body_on); + } + + #[test] + fn cli_try_request_body() { + let cli_config = + args::try_parse_from(["myprog", TRY_COMMAND, "--skip-request-body", YAML_FILE]) + .unwrap(); + let ExecConfig::Try(try_config) = cli_config else { + panic!() + }; + assert_eq!(try_config.config_file.to_str().unwrap(), YAML_FILE); + assert!(!try_config.skip_response_body_on); + assert!(try_config.skip_request_body_on); + } + #[test] fn cli_try_include() { let cli_config = args::try_parse_from([ diff --git a/src/lib.rs b/src/lib.rs index 6e8e31e4..e4d1530b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -337,9 +337,12 @@ pub struct TryConfig { /// Directory to store logs (if enabled with --loggers) #[arg(short = 'd', long = "results-directory", value_name = "DIRECTORY")] pub results_dir: Option, - /// Skips request and reponse body from output (try command) - #[arg(short = 's', long = "skipBody")] - pub skip_body_on: bool, + /// Skips reponse body from output + #[arg(short = 'k', long = "skip-response-body")] + pub skip_response_body_on: bool, + /// Skips request body from output + #[arg(short = 'K', long = "skip-request-body")] + pub skip_request_body_on: bool, } impl fmt::Display for TryConfig { @@ -881,14 +884,14 @@ fn create_try_run_future( debug!("create_try_run_future start"); // create a logger for the try run // request.headers only logs single Accept Headers due to JSON requirements. Use headers_all instead - let request_body_template = if try_config.skip_body_on { + let request_body_template = if try_config.skip_request_body_on { "" } else if matches!(try_config.format, TryRunFormat::Human) { "${request.body != '' ? request.body : ''}\n" } else { r#","body": "request.body""# }; - let response_body_template = if try_config.skip_body_on { + let response_body_template = if try_config.skip_response_body_on { "" } else if matches!(try_config.format, TryRunFormat::Human) { "${response.body != '' ? JSON.stringify(response.body) : ''}\n"