Code Monkey home page Code Monkey logo

axum-test-helper's Introduction

axum-test-helper

axum-test-helper exposes axum original TestClient, which is private to the axum crate

More information about this crate can be found in the crate documentation.

High level features

  • Provide an easy to use interface
  • Start a server in a different port for each call
  • Deal with JSON, text and files response/requests

Usage example

Add this crate as a dev-dependency:

[dev-dependencies]
axum-test-helper = "0.*" # alternatively specify the version as "0.3.0"

Use the TestClient on your own Router:

use axum::Router;
use axum::http::StatusCode;
use axum_test_helper::TestClient;

// you can replace this Router with your own app
let app = Router::new().route("/", get(|| async {}));

// initiate the TestClient with the previous declared Router
let client = TestClient::new(app);
let res = client.get("/").send().await;
assert_eq!(res.status(), StatusCode::OK);

You can find examples like this in the example directory.

See the crate documentation for way more examples.

Disable trace

By default axum-test-helper print trace like Listening on 127.0.0.1:36457. You can disable trace with axum-test-helper = { version = "0.*", default-features = false, features = ["withouttrace"] }.

Contributing

Before submitting a pull request or after pulling from the main repository, ensure all tests pass:

# Run axum-test-helper tests
cargo test

# Test the hello-world example project
(cd examples/hello-world && cargo test)

License

This project is licensed under the MIT license.

axum-test-helper's People

Contributors

apps4uco avatar audunska avatar cosmichorrordev avatar jokler avatar jorgecarleitao avatar miasofteam avatar ngerakines avatar perobertson avatar quodlibetor avatar renancloudwalk avatar sunng87 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

axum-test-helper's Issues

Testing WebSockets?

Any chance TestClient could work to test the axum setup with websocket? For example, how to setup a test for:

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    let addr = SocketAddr::from(([0, 0, 0, 0], 3000));
    axum::Server::bind(&addr)
        .serve(app().into_make_service_with_connect_info::<SocketAddr>())
        .await?;
    Ok(())
}

fn app() -> Router {
    Router::new().route("/ws", get(handler))
}

async fn handler(ws: WebSocketUpgrade) -> impl IntoResponse {
    ws.on_upgrade(callback)
}

async fn callback(mut socket: WebSocket) {
    while let Some(msg) = socket.recv().await {
        if let Ok(msg) = msg {
            if let Ok(msg) = msg.to_text() {
                handle_message(msg);
            }
        }
    }
}

fn handle_message(msg: &str) {
    println!("{msg}");
}

Feature Request: Cookie support in TestClient

For applications which use a cookie based login mechanism it would be helpful if TestClient had built-in cookie support.

This should be relatively straightforward to implement, basically enable the cookie feature on the reqwest dependency and add a cookie_store(true) to the client builder.

If the additional dependencies are considered a problem this could probably also be enabled by a feature.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.