Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

introduce GooseRequest and builder pattern #373

Merged
merged 8 commits into from
Nov 2, 2021
Merged
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
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
# Changelog

## 0.14.2-dev
## 0.15.0-dev
- [#372](https://github.com/tag1consulting/goose/pull/372) de-deduplicate documentation, favoring [The Goose Book](https://book.goose.rs)
- [#373](https://github.com/tag1consulting/goose/pull/373) **API change**: introduce `GooseRequest` and `GooseRequestBuilder` for more flexibility when making requests
o remove `GooseUser::post_named`, `GooseUser::head_named`, `GooseUser::delete_named`, `GooseUser::goose_get`, `GooseUser::goose_put`, `GooseUser::goose_head`, `GooseUser::goose_put`, `GooseUser::goose_patch`, `GooseUser::goose_delete`, and `GooseUser::goose_send`
o adds or modifies helpers `GooseUser::get`, `GooseUser::get_named`, `GooseUser::post`, `GooseUser::post_form`, `GooseUser::post_json`, `GooseUser::head`, and `GooseUser::delete`
o replaces `GooseUser::goose_send` with `GooseUser::request` which accepts a `GooseRequest` object
o fixes [#370] (see `GooseRequestBuilder::expect_status_code`)

## 0.14.1 October 13, 2021
- [#364](https://github.com/tag1consulting/goose/pull/364) add link from the [Developer Documentation](https://docs.rs/goose) to [The Git Book](https://book.goose.rs)
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "goose"
version = "0.14.2-dev"
version = "0.15.0-dev"
authors = ["Jeremy Andrews <jeremy@tag1consulting.com>"]
edition = "2018"
description = "A load testing framework inspired by Locust."
Expand Down
6 changes: 2 additions & 4 deletions examples/drupal_memcache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,7 @@ async fn drupal_memcache_login(user: &mut GooseUser) -> GooseTaskResult {
("form_id", "user_login"),
("op", "Log+in"),
];
let request_builder = user.goose_post("/user")?;
let _goose = user.goose_send(request_builder.form(&params), None).await;
let _goose = user.post_form("/user", &params).await?;
// @TODO: verify that we actually logged in.
}
Err(e) => {
Expand Down Expand Up @@ -302,8 +301,7 @@ async fn drupal_memcache_post_comment(user: &mut GooseUser) -> GooseTaskResult {
];

// Post the comment.
let request_builder = user.goose_post(&comment_path)?;
let mut goose = user.goose_send(request_builder.form(&params), None).await?;
let mut goose = user.post_form(&comment_path, &params).await?;

// Verify that the comment posted.
match goose.response {
Expand Down
4 changes: 1 addition & 3 deletions examples/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,8 @@ async fn main() -> Result<(), GooseError> {
/// on_start task when registering it above. This means it only runs one time
/// per user, when the user thread first starts.
async fn website_login(user: &mut GooseUser) -> GooseTaskResult {
let request_builder = user.goose_post("/login")?;
// https://docs.rs/reqwest/*/reqwest/blocking/struct.RequestBuilder.html#method.form
let params = [("username", "test_user"), ("password", "")];
let _goose = user.goose_send(request_builder.form(&params), None).await?;
let _goose = user.post_form("/login", &params).await?;

Ok(())
}
Expand Down
24 changes: 17 additions & 7 deletions examples/simple_with_session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,9 @@ async fn main() -> Result<(), GooseError> {
/// on_start task when registering it above. This means it only runs one time
/// per user, when the user thread first starts.
async fn website_signup(user: &mut GooseUser) -> GooseTaskResult {
let request_builder = user.goose_post("/signup")?;
// https://docs.rs/reqwest/*/reqwest/blocking/struct.RequestBuilder.html#method.form
let params = [("username", "test_user"), ("password", "")];
let response = user
.goose_send(request_builder.form(&params), None)
.post_form("/signup", &params)
.await?
.response?
.json::<AuthenticationResponse>()
Expand All @@ -74,11 +72,23 @@ async fn website_signup(user: &mut GooseUser) -> GooseTaskResult {

/// A very simple task that simply loads the front page.
async fn authenticated_index(user: &mut GooseUser) -> GooseTaskResult {
// This will panic if the session is missing or if the session is not of the right type
// use `get_session_data` to handle missing session
// This will panic if the session is missing or if the session is not of the right type.
// Use `get_session_data` to handle a missing session.
let session = user.get_session_data_unchecked::<Session>();
let request = user.goose_get("/")?.bearer_auth(&session.jwt_token);
let _goose = user.goose_send(request, None).await?;

// Create a Reqwest RequestBuilder object and configure bearer authentication when making
// a GET request for the index.
let reqwest_request_builder = user
.get_request_builder(&GooseMethod::Get, "/")?
.bearer_auth(&session.jwt_token);

// Add the manually created RequestBuilder and build a GooseRequest object.
let goose_request = GooseRequest::builder()
.set_request_builder(reqwest_request_builder)
.build();

// Make the actual request.
user.request(goose_request).await?;

Ok(())
}
9 changes: 4 additions & 5 deletions examples/umami/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ pub async fn log_in(user: &mut GooseUser) -> GooseTaskResult {
("form_id", &"user_login_form".to_string()),
("op", &"Log+in".to_string()),
];
let request_builder = user.goose_post("/en/user/login")?;
logged_in_user = user.goose_send(request_builder.form(&params), None).await?;
logged_in_user = user.post_form("/en/user/login", &params).await?;

// A successful log in is redirected.
if !logged_in_user.request.redirected {
Expand Down Expand Up @@ -169,9 +168,9 @@ pub async fn edit_article(user: &mut GooseUser) -> GooseTaskResult {
("form_id", &"node_article_edit_form".to_string()),
("op", &"Save (this translation)".to_string()),
];
let request_builder =
user.goose_post(&format!("/en/node/{}/edit", article.unwrap().nid))?;
saved_article = user.goose_send(request_builder.form(&params), None).await?;
saved_article = user
.post_form(&format!("/en/node/{}/edit", article.unwrap().nid), &params)
.await?;

// A successful node save is redirected.
if !saved_article.request.redirected {
Expand Down
6 changes: 2 additions & 4 deletions examples/umami/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -565,8 +565,7 @@ pub async fn anonymous_contact_form(user: &mut GooseUser, english: bool) -> Goos
("form_id", "contact_message_feedback_form"),
("op", "Send+message"),
];
let request_builder = user.goose_post(contact_form_url)?;
contact_form = user.goose_send(request_builder.form(&params), None).await?;
contact_form = user.post_form(contact_form_url, &params).await?;
}
Err(e) => {
return user.set_failure(
Expand Down Expand Up @@ -694,8 +693,7 @@ pub async fn search(user: &mut GooseUser, english: bool) -> GooseTaskResult {
("form_id", "search_form"),
("op", "Search"),
];
let request_builder = user.goose_post(search_form_url)?;
search_form = user.goose_send(request_builder.form(&params), None).await?;
search_form = user.post_form(search_form_url, &params).await?;

// A successful search is redirected.
if !search_form.request.redirected {
Expand Down
2 changes: 1 addition & 1 deletion src/docs/goose-book/src/config/rustls.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ By default Reqwest (and therefore Goose) uses the system-native transport layer

```toml
[dependencies]
goose = { version = "^0.14", default-features = false, features = ["rustls-tls"] }
goose = { version = "^0.15", default-features = false, features = ["rustls-tls"] }
```
2 changes: 1 addition & 1 deletion src/docs/goose-book/src/controller/telnet.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
goose> ?
goose 0.14.1 controller commands:
goose 0.15.0 controller commands:
help (?) this help
exit (quit) exit controller
start start an idle load test
Expand Down
9 changes: 5 additions & 4 deletions src/docs/goose-book/src/getting-started/creating.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ This creates a new directory named `loadtest/` containing `loadtest/Cargo.toml`

```toml
[dependencies]
goose = "^0.14"
goose = "^0.15"
tokio = "^1.12"
```

Expand All @@ -21,9 +21,9 @@ At this point it's possible to compile all dependencies, though the resulting bi
```bash
$ cargo run
Updating crates.io index
Downloaded goose v0.14.1
Downloaded goose v0.15.0
...
Compiling goose v0.14.1
Compiling goose v0.15.0
Compiling loadtest v0.1.0 (/home/jandrews/devel/rust/loadtest)
Finished dev [unoptimized + debuginfo] target(s) in 52.97s
Running `target/debug/loadtest`
Expand All @@ -41,7 +41,8 @@ use goose::prelude::*;
> ```rust
> use crate::config::{GooseDefault, GooseDefaultType};
> use crate::goose::{
> GooseTask, GooseTaskError, GooseTaskFunction, GooseTaskResult, GooseTaskSet, GooseUser,
> GooseMethod, GooseRequest, GooseTask, GooseTaskError, GooseTaskFunction, GooseTaskResult,
> GooseTaskSet, GooseUser,
> };
> use crate::metrics::{GooseCoordinatedOmissionMitigation, GooseMetrics};
> use crate::{task, taskset, GooseAttack, GooseError, GooseScheduler};
Expand Down
Loading