Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

logs http request parts always in the same order respectively to order in configuration parameter #151

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import static com.google.common.collect.Lists.newArrayList;
import static com.google.common.collect.Sets.intersection;
import static com.google.common.collect.Sets.newHashSet;
import static io.restassured.filter.log.LogDetail.*;
import static lv.ctco.cukes.core.CukesOptions.*;
import static org.slf4j.LoggerFactory.getLogger;

Expand Down Expand Up @@ -76,31 +77,21 @@ public void beforeRequest(RequestSpecification requestSpecification) {
final RequestLogSpecification logSpec = filterableRequestSpecification.log();
final List<LogDetail> logDetails = parseLogDetails(world.get(LOGGING_REQUEST_INCLUDES, DEFAULT_REQUEST_INCLUDES));

details:
for (LogDetail detail : logDetails) {
switch (detail) {
case ALL:
logSpec.all();
break details;
case BODY:
logSpec.body();
break;
case COOKIES:
logSpec.cookies();
break;
case HEADERS:
logSpec.headers();
break;
case METHOD:
logSpec.method();
break;
case PARAMS:
logSpec.parameters();
break;
case URI:
logSpec.uri();
break;
}
if (logDetails.contains(ALL)) {
logSpec.all();
} else {
if (logDetails.contains(METHOD))
logSpec.method();
if (logDetails.contains(URI))
logSpec.uri();
if (logDetails.contains(HEADERS))
logSpec.headers();
if (logDetails.contains(COOKIES))
logSpec.cookies();
if (logDetails.contains(PARAMS))
logSpec.parameters();
if (logDetails.contains(BODY))
logSpec.body();
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package lv.ctco.cukes.http.logging;

import io.restassured.RestAssured;
import io.restassured.config.LogConfig;
import io.restassured.specification.RequestSpecification;
import lv.ctco.cukes.core.internal.context.GlobalWorldFacade;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.runners.MockitoJUnitRunner;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.util.stream.Stream;

import static io.restassured.config.RestAssuredConfig.newConfig;
import static lv.ctco.cukes.core.CukesOptions.LOGGING_REQUEST_INCLUDES;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class HttpLoggingPluginTest {

private static final String EXPECTED_RESULT = "Request method:\tGET\n" +
"Request URI:\thttp://google.com/?q=hi\n" +
"Headers:\t\tAccept=*/*\n" +
"\t\t\t\tContent-Type=application/json; charset=UTF-8\n" +
"Request params:\tq=hi\n" +
"Query params:\t<none>\n" +
"Form params:\t<none>\n" +
"Path params:\t<none>\n" +
"Multiparts:\t\t<none>\n" +
"Body:\n" +
"{\n" +
" \"param\": \"value\"\n" +
"}\n";

@InjectMocks
@Spy
private HttpLoggingPlugin plugin;

@Mock
private GlobalWorldFacade facade;

private PrintStream logStream;
private ByteArrayOutputStream out;

@Before
public void setUp() throws Exception {
out = new ByteArrayOutputStream();
logStream = new PrintStream(out, true, "UTF-8");
}

@Test
public void fixedOutputOrderOfComponents() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can it be reworked to parametrized test?

RequestSpecification request = RestAssured.given()
.config(newConfig().logConfig(new LogConfig(logStream, true)))
.baseUri("http://google.com")
.body("{\"param\":\"value\"}")
.param("q", "hi")
.header("Content-Type", "application/json");


String[] loggingIncludes = {
"body,params,uri,method,headers",
"params,uri,body,method,headers",
"body,method,headers,params,uri,",
"method,headers,body,params,uri"
};
Stream.of(loggingIncludes)
.forEach(
includes -> {
when(facade.get(LOGGING_REQUEST_INCLUDES, "")).thenReturn(includes);

plugin.beforeRequest(request);

request.get();

String result = null;
try {
result = out.toString("UTF-8");
} catch (UnsupportedEncodingException e) {
fail(e.toString());
}
assertThat(result, is(EXPECTED_RESULT));
out.reset();
}
);
}
}