actix / actix/actix-web

Add support to be able to test the route tree using `match_pattern` and `match_name`.

未关闭
#3,286 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
主要语言
Rust
星标
24.8k
派生
1.9k
平均合并
23 小时 10 分钟
30 天内合并 PR
26

描述

So currently the test requests does not seem to implement the match functions - `match_pattern`/`match_name`.
Consider the below app having two services:
```rust
#[get("/hello/{name}", name = "greet")]
async fn greet(name: web::Path) -> impl Responder {
format!("Hello {name}!")
}

#[get("/hello/me", name = "greet_me")]
async fn greet_me() -> impl Responder {
format!("Hi Developer!")
}
```
And a test like this:
```rust
#[actix_web::test]
async fn test_index_get() {
let _app = test::init_service(App::new().service(greet).service(greet_me)).await;
let req = test::TestRequest::get()
.uri("/hello/john")
.insert_header(ContentType::plaintext())
.to_srv_request();

println!("{:?}", req.match_pattern());
println!("{:?}", req.match_name());
assert!(true);
}
```

## Expected Behavior

`match_pattern` should print the matching route url/path, in this case: "/hello/{name}"
`match_name` should print the given name to the service, in this case: "greet"

## Current Behavior

Both `match_pattern` and `match_name` returns None.

## Context

We can use these functions to test if the user has registered the services in the correct order as he/she intended and the user requests are in fact routing to correct handlers.

Currently, if user had registered the `greet` service before `greet_me` service and a user sends the request to `/hello/me` it will send `"Hello me!`` whereas it should have returned `"Hi Developer!".
That's what we wanted to test, Additional Context: https://github.com/qdrant/qdrant/issues/3547.

## Your Environment

- Rust Version: rustc 1.75.0 (82e1608df 2023-12-21)
- Actix Web Version: 4

贡献指南

打开贡献指南

调研方向

Trace how TestRequest::to_srv_request() obtains route information after init_service, then inspect the implementations of match_pattern and match_name. Add coverage for the greet and greet_me routing example, verifying the matched pattern and service name are returned instead of None.

由索引模型根据 Issue 内容生成。

评估

技术栈
rust
领域
backend, testing
Issue 类型
缺陷
难度
3/5
预计耗时
1-2 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。