Skip to content

06. Request Builder

JakeHoward edited this page Jul 16, 2015 · 1 revision

Building new requests from scratch

When testing web applications it's handy to have a request builder that let's you create different kinds of requests. Utterlyidle provides com.googlecode.utterlyidle.RequestBuilder for that purpose.

Request request = new RequestBuilder("POST", "/home").
                cookie("cookieName", cookie("cookieValue")).
                form("formParam", "formParamValue").
                header("CustomHeader", "customHeaderValue").
                accepting("text/plain").
                query("queryParam", "queryParamValue").
                build();

Generates the following request (generated from request.toString()):

POST /home?queryParam=queryParamValue HTTP/1.1
Cookie: cookieName="cookieValue"; 
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
CustomHeader: customHeaderValue
Accept: text/plain
Content-Length: 24

formParam=formParamValue

Please note that Content-Type and Content-Length headers were added automatically because we declared a form parameter.

Modifying original request

If you want to modify original request, you should pass it as an argument to the RequestBuilder and call appropriate methods as shown in the code below:

Request originalRequest = ...;
Request clonedRequest = new RequestBuilder(originalRequest)
                                        .removeHeaders("Header1")
                                        .removeQuery("QueryParam1")
                                        .replaceHeader("Header2", "NewValue")
                                        .replaceQuery("QueryParam2", "NewValue")
                                        .build();

Clone this wiki locally