viniciusgerevini / http-test-server

Programatically create resources and pre-defined responses for tests
Apache License 2.0
10 stars 3 forks source link

Is the URI Regex actualy regex? #13

Open OmentaElvis opened 1 month ago

OmentaElvis commented 1 month ago

I needed some complex path handling. I created this regex to handle my dynamic paths.

let server = TestServer::new()?;
let metadata = server
                .create_resource("^/(?<group_id>.*)/(?<artifact_id>.*)/maven-metadata.xml$");

I tested the regex externally and it works as expected but the create_resource one panics with:

regex parse error:
    ^/(
      ^
error: unclosed group

What i expected was to obtain the params by.

let group_id = param.path.get("group_id").unwrap().replace('/', ".");
let artifact_id = param.path.get("artifact_id").unwrap();

so the uri: /com/example/my-module/maven-metadata.xml

should produce: group_id : com.example artifact_id: my-module

OmentaElvis commented 1 month ago

I did some digging on the source code and found out this line defines a regex that is used to replace all existence of '?' followed by any character after that. This effectively treats all my regex after the first '?' as a beginning of query parameters.

    let query_regex = Regex::new(r"\?.*").unwrap();
    // then
    let pattern = query_regex.replace(uri, "");

Should we like add an escape mechanism to signify to the create_uri_regex to leave the argument as is since we already provided our own regex? Or maybe clarify on the documentation that only certain symbols will pass the uri parameters parsing and other valid regex may not execute as expected.

I would suggest a different create_resource function that accept regex as is without modifying it or a public set_uri_regex for Resource.