thruster-rs / Thruster

A fast, middleware based, web framework written in Rust
MIT License
1.06k stars 47 forks source link

testing module seems to only have one `request` method #251

Closed Tronikelis closed 1 year ago

Tronikelis commented 1 year ago

Hey, I wanted to try writing simple tests.

The readme says:

let mut app = App::<Request, Ctx, ()>::new_basic();

...

app.get("/plaintext", m![plaintext]);

...

let result = testing::get(app, "/plaintext");

assert!(result.body == "Hello, World!");

But on thruster 1.3.3 with the hyper server the testing::get doesn't seem to get exported. Take a look at my example:

main.rs

use thruster::{m, middleware_fn, testing};
use thruster::{App, BasicContext as Ctx, HyperRequest};
use thruster::{MiddlewareNext, MiddlewareResult};

#[middleware_fn]
async fn plaintext(mut context: Ctx, _next: MiddlewareNext<Ctx>) -> MiddlewareResult<Ctx> {
    let val = "Hello, World!";
    context.body(val);
    Ok(context)
}

fn main() {
    let app = App::<HyperRequest, Ctx, ()>::new_basic().get("/plaintext", m![plaintext]);
    let result = testing::get(app, "/plaintext");
}

Cargo.toml:

[package]
name = "thruster-testing-module"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
thruster = { version = "1.3.3", features = ["hyper_server"] }
tokio = { version = "1.28.1", features = ["full"] }

cargo build:

error[E0425]: cannot find function `get` in module `testing`
  --> src\main.rs:14:27
   |
14 |     let result = testing::get(app, "/plaintext");
   |                           ^^^ not found in `testing`

It works without the hyper_server feature enabled though

trezm commented 1 year ago

That should be available in the Testing trait, if you try use thruster::{m, middleware_fn, testing, Testing};, does that fix the issue?

Tronikelis commented 1 year ago

Testing does not seem to exist for me:

error[E0432]: unresolved import `thruster::Testing`
 --> tests\main.rs:6:53
  |
6 |     HyperRequest, MiddlewareNext, MiddlewareResult, Testing,
  |                                                     ^^^^^^^
  |                                                     |
  |                                                     no `Testing` in the root
  |                                                     help: a similar name exists in the module: `testing`
trezm commented 1 year ago

sorry! Typo, it should be Testable. Check out the walkthrough website (it's a smidge out of date with the addition of contex_state) here.

Tronikelis commented 1 year ago

👍