Skip to content

Commit

Permalink
Skip response body scripting (#170)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
tkmcmaster committed Oct 26, 2023
1 parent a813382 commit 5c3ec4c
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 57 deletions.
5 changes: 4 additions & 1 deletion controller/acceptance/errorFile.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 3 additions & 2 deletions controller/acceptance/test.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
2 changes: 1 addition & 1 deletion controller/components/Layout/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, {
Expand Down
27 changes: 9 additions & 18 deletions controller/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,35 +121,26 @@ const nextConfig = {
HIDE_ENVIRONMENT: process.env.HIDE_ENVIRONMENT,
},
// https://github.com/vercel/next.js/discussions/11493#discussioncomment-14606
env: { // These are sent to the client and the server and are set at build time
// @ts-ignore
LoggingLevel: process.env.LoggingLevel,
env: { // These are sent to the client and the server and are set at build time for static pages
LoggingLevel: process.env.LoggingLevel || "", // Only checks if debug
// @ts-ignore
APPLICATION_NAME: process.env.APPLICATION_NAME,
// @ts-ignore
SYSTEM_NAME: process.env.SYSTEM_NAME,
// @ts-ignore
FS_SITE: process.env.FS_SITE,
FS_SITE: process.env.FS_SITE, // Used by auth client/Layout
// @ts-ignore
BASE_PATH: process.env.BASE_PATH,
BASE_PATH: process.env.BASE_PATH, // client utils/Layout
// @ts-ignore
ASSET_PREFIX: process.env.ASSET_PREFIX,
ASSET_PREFIX: process.env.ASSET_PREFIX, // client utils/Layout
// @ts-ignore
TEST_STATUS_REFRESH_DELAY: process.env.TEST_STATUS_REFRESH_DELAY,
HIDE_ENVIRONMENT: process.env.HIDE_ENVIRONMENT, // Used by Layout
// @ts-ignore
TEST_ERRORS_MAX_DISPLAYED: process.env.TEST_ERRORS_MAX_DISPLAYED,
AUTH_MODE: process.env.AUTH_MODE, // Used by auth client/Layout
// @ts-ignore
TEST_ERRORS_MAX_LINE_LENGTH: process.env.TEST_ERRORS_MAX_LINE_LENGTH,
AUTH_COOKIE_NAME: process.env.AUTH_COOKIE_NAME, // Used by auth client/Layout
// @ts-ignore
REDIRECT_TO_S3: process.env.REDIRECT_TO_S3,
// @ts-ignore
UNZIP_S3_FILES: process.env.UNZIP_S3_FILES,
// @ts-ignore
AUTH_MODE: process.env.AUTH_MODE,
// @ts-ignore
AUTH_COOKIE_NAME: process.env.AUTH_COOKIE_NAME,
// @ts-ignore
AUTH_HEADER_NAME: process.env.AUTH_HEADER_NAME,
AUTH_HEADER_NAME: process.env.AUTH_HEADER_NAME, // Used by auth client/Layout
}
};

Expand Down
14 changes: 7 additions & 7 deletions guide/src/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,8 @@ Commands:
help Print this message or the help of the given subcommand(s)
Options:
-h, --help Prints help information
-V, --version Prints version information
-l, --loggers Enable loggers defined in the config file
-d, --results-directory Directory to store logs (if enabled with --loggers)
-k, --skipBody Skips request and reponse body from output (try command)
-h, --help Prints help information
-V, --version Prints version information
```

As signified in the above help output, there are two subcommands `run` and `try`.
Expand Down Expand Up @@ -72,7 +69,8 @@ Options:
endpoint matching the filter is included in the test
-l, --loggers Enable loggers defined in the config file
-d, --results-directory <DIRECTORY> 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
```

Expand All @@ -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.
<br/><br/>

In both the `run` and `try` subcommands a [config file](./config.md) is required.
Expand Down
12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
16 changes: 7 additions & 9 deletions pr.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
54 changes: 45 additions & 9 deletions src/bin/pewpew.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PathBuf>,
/// 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<TryConfigTmp> 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();
Expand All @@ -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,
}
}
}
Expand Down Expand Up @@ -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());
}

Expand All @@ -437,6 +443,7 @@ mod tests {
"_id=0",
"-l",
"-k",
"-K",
"-o",
STATS_FILE,
YAML_FILE,
Expand All @@ -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);
}
Expand All @@ -477,7 +485,8 @@ mod tests {
"--include",
"_id=0",
"--loggers",
"--skipBody",
"--skip-response-body",
"--skip-request-body",
"--file",
STATS_FILE,
YAML_FILE,
Expand All @@ -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([
Expand Down
13 changes: 8 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PathBuf>,
/// 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 {
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 5c3ec4c

Please sign in to comment.