parent
75ac7de5cd
commit
879bedf205
1
pom.xml
1
pom.xml
|
@ -208,6 +208,7 @@
|
||||||
<module>spring-zuul</module>
|
<module>spring-zuul</module>
|
||||||
<module>spring-reactor</module>
|
<module>spring-reactor</module>
|
||||||
<module>spring-vertx</module>
|
<module>spring-vertx</module>
|
||||||
|
<module>spring-rest-logging</module>
|
||||||
|
|
||||||
<module>testing</module>
|
<module>testing</module>
|
||||||
<module>testng</module>
|
<module>testng</module>
|
||||||
|
|
|
@ -0,0 +1,38 @@
|
||||||
|
package com.baeldung.rest.log.util;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.StringWriter;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
|
||||||
|
import org.apache.commons.io.IOUtils;
|
||||||
|
import org.springframework.web.util.ContentCachingRequestWrapper;
|
||||||
|
import org.springframework.web.util.WebUtils;
|
||||||
|
|
||||||
|
public class RequestLoggingUtil {
|
||||||
|
|
||||||
|
public static String getStringFromInputStream(InputStream is) {
|
||||||
|
StringWriter writer = new StringWriter();
|
||||||
|
String encoding = "UTF-8";
|
||||||
|
try {
|
||||||
|
IOUtils.copy(is, writer, encoding);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return writer.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String readPayload(final HttpServletRequest request) throws IOException {
|
||||||
|
String payloadData = null;
|
||||||
|
ContentCachingRequestWrapper contentCachingRequestWrapper = WebUtils.getNativeRequest(request, ContentCachingRequestWrapper.class);
|
||||||
|
if (null != contentCachingRequestWrapper) {
|
||||||
|
byte[] buf = contentCachingRequestWrapper.getContentAsByteArray();
|
||||||
|
if (buf.length > 0) {
|
||||||
|
payloadData = new String(buf, 0, buf.length, contentCachingRequestWrapper.getCharacterEncoding());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return payloadData;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -15,14 +15,14 @@ public class TestTaxiFareController {
|
||||||
private static final String URL = "http://localhost:" + 9090 + "/rest-log/taxifare/";
|
private static final String URL = "http://localhost:" + 9090 + "/rest-log/taxifare/";
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void given_taxi_fare_get() {
|
public void givenRequest_thenfetchTaxiFareRateCard() {
|
||||||
TestRestTemplate testRestTemplate = new TestRestTemplate();
|
TestRestTemplate testRestTemplate = new TestRestTemplate();
|
||||||
ResponseEntity<String> response = testRestTemplate.getForEntity(URL + "get/", String.class);
|
ResponseEntity<String> response = testRestTemplate.getForEntity(URL + "get/", String.class);
|
||||||
assertThat(response.getStatusCode(), equalTo(HttpStatus.OK));
|
assertThat(response.getStatusCode(), equalTo(HttpStatus.OK));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void given_taxi_ride_get_fare() {
|
public void givenTaxiRide_thenGetCalculatedFare() {
|
||||||
TestRestTemplate testRestTemplate = new TestRestTemplate();
|
TestRestTemplate testRestTemplate = new TestRestTemplate();
|
||||||
TaxiRide taxiRide = new TaxiRide(true,10l);
|
TaxiRide taxiRide = new TaxiRide(true,10l);
|
||||||
String fare = testRestTemplate.postForObject(URL + "calculate/", taxiRide,String.class);
|
String fare = testRestTemplate.postForObject(URL + "calculate/", taxiRide,String.class);
|
||||||
|
|
Loading…
Reference in New Issue