Reputation: 2101
I have the following step:
@Given("Request specifications are set with base uri {string}")
public void setRequestsSpec(String baseUri){
requestSpecification = new RequestSpecBuilder()
.setBaseUri(baseUri)
.addFilter(new ResponseLoggingFilter())//log request and response for better debugging. You can also only log if a requests fails.
.addFilter(new RequestLoggingFilter())
.addFilter(new RcAllureFilter())
.build();
Then I have:
@When("^Azure Login Request Executed$")
public void azureLoginExecuted() {
response =
given() //Add x-www-form-urlencoded body params:
.spec(testContext().getRequestSpec())
.formParam(GRANT_TYPE_KEY, GRANT_TYPE_VALUE)
.formParam(AUTO_TEAM_CLIENT_ID_KEY, AUTO_TEAM_CLIENT_ID_VALUE)
.formParam(AUTO_TEAM_CLIENT_SECRET_KEY, AUTO_TEAM_CLIENT_SECRET_VALUE)
.formParam(RESOURCE_KEY, RESOURCE_VALUE)
.when()
.post(AUTO_TEAM_TENANT_ID + RESOURCE); //Send the request along with the resource
setAuthorizationToken();
}
How can I extract the request's details like URI, headers and parameters from it? I cannot find a class from which I can extract the request details. In RequestSpecification class, I hardly can find any getter functions in this class. I need this values in order to build a formatted log message. Is there another way?
Upvotes: 0
Views: 790
Reputation: 478
if you are trying to get the details from requestspecification, then you can use like this .
RequestSpecification spec = new RequestSpecBuilder().setContentType(ContentType.JSON).addHeader("h1", "h2")
.build();
QueryableRequestSpecification queryable = SpecificationQuerier.query(spec);
System.out.println(" Content is " + queryable.getContentType());
System.out.println(" Header is " + queryable.getHeaders().getValue("h1"));
But in your scenario, you want request details too. so , best way would be to use a requestlogging filter, which accepts a PrintStream (which in turn can work With ByteArrayOutPutStream which can convert to a String ) . Basic idea is, to use RequestLoggingFilter with a PRintStream and then use any code to save PrintStream to a String. You can usr StringWriter too.
RequestSpecification spec = new RequestSpecBuilder().build();
StringWriter requestWriter = new StringWriter();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream printStream = new PrintStream(baos);
Response response = given().spec(spec).contentType(ContentType.JSON)
.filter(new RequestLoggingFilter(printStream)).get("https://jsonplaceholder.typicode.com/todos/1");
printStream.flush();
System.out.println(baos);
Upvotes: 2