lantunes / fixd

An HTTP Server Fixture for Testing HTTP Clients and mocking web services, and a Java Micro Web Framework
http://bigtesting.org/fixd
Apache License 2.0
41 stars 6 forks source link

Handle Splat (Wildcard) Path Parameters #5

Closed lantunes closed 10 years ago

lantunes commented 10 years ago

The server should be able to handle splat, or wildcard, path parameters. The following is a basic test that demonstrates the functionality:

server.handle(Method.GET, "/*")
          .with(200, "text/plain", "[request.path]");

Response resp = new AsyncHttpClient()
                      .prepareGet("http://localhost:8080/1")
                      .execute().get();
assertEquals("/1", resp.getResponseBody().trim());

resp = new AsyncHttpClient()
           .prepareGet("http://localhost:8080/2")
           .execute().get();
assertEquals("/2", resp.getResponseBody().trim());

In the test above, the path "/*" indicates that all requests with any path must be handled by the specified handling logic.

Multiple splat parameters must be supported, as the following test demonstrates:

server.handle(Method.GET, "/say/*/to/*")
          .with(200, "text/plain", "[request.path]");

Response resp = new AsyncHttpClient()
                           .prepareGet("http://localhost:8080/hello")
                           .execute().get();
assertEquals(404, resp.getStatusCode());

resp = new AsyncHttpClient()
           .prepareGet("http://localhost:8080/say/hello/to/world")
           .execute().get();
assertEquals("/say/hello/to/world", resp.getResponseBody().trim());

resp = new AsyncHttpClient()
           .prepareGet("http://localhost:8080/say/bye/to/Tim")
           .execute().get();
assertEquals("/say/bye/to/Tim", resp.getResponseBody().trim());

Finally, the values of the splat parameters should be available in the request properties to the user, as the other path parameters are. Perhaps a method such as splat() can return an array of the parameters, in the order in which they are declared and captured on the path.