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

chore(volo-http): add mock transport service for client #506

Merged
merged 1 commit into from
Sep 30, 2024

Conversation

yukiiiteru
Copy link
Member

@yukiiiteru yukiiiteru commented Sep 30, 2024

Motivation

Support full-link testing.

Solution

This PR adds a mock transport service for client, which can replace the default transport and test functions without network. It can respond an empty response with a given status code or a client-side service.

In addition, the MockTransport also supports server-side service, e.g., Router, and an example has been also updated:

    #[tokio::test]
    async fn client_call_router() {
        let router: Router = Router::new().route("/get", get(|| async { HELLO_WORLD }));
        let client = ClientBuilder::new().mock(MockTransport::server_service(router));
        {
            let ret = client.get("/get").unwrap().send().await.unwrap().into_string().await.unwrap();
            assert_eq!(ret, HELLO_WORLD);
        }
        {
            let ret = client.get("http://127.0.0.1/get").unwrap().send().await.unwrap().into_string().await.unwrap();
            assert_eq!(ret, HELLO_WORLD);
        }
        {
            let resp = client.get("/").unwrap().send().await.unwrap();
            assert_eq!(resp.status(), StatusCode::NOT_FOUND);
        }
        {
            let resp = client.post("/get").unwrap().send().await.unwrap();
            assert_eq!(resp.status(), StatusCode::METHOD_NOT_ALLOWED);
        }
    }

This commit adds a mock transport service for client, which can replace
the default transport and test functions without network.

Signed-off-by: Yu Li <liyu.yukiteru@bytedance.com>
@yukiiiteru yukiiiteru requested review from a team as code owners September 30, 2024 08:09
@yukiiiteru yukiiiteru merged commit 9fe1be7 into cloudwego:main Sep 30, 2024
7 of 15 checks passed
@yukiiiteru yukiiiteru deleted the chore/test-helpers branch October 8, 2024 07:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants