Skip to content

Commit 101f6fd

Browse files
author
mokhan
committed
Comment incorporation
Missing file commit
1 parent 75ac7de commit 101f6fd

2 files changed

Lines changed: 40 additions & 2 deletions

File tree

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.baeldung.rest.log.util;
2+
3+
import java.io.IOException;
4+
import java.io.InputStream;
5+
import java.io.StringWriter;
6+
7+
import javax.servlet.http.HttpServletRequest;
8+
9+
import org.apache.commons.io.IOUtils;
10+
import org.springframework.web.util.ContentCachingRequestWrapper;
11+
import org.springframework.web.util.WebUtils;
12+
13+
public class RequestLoggingUtil {
14+
15+
public static String getStringFromInputStream(InputStream is) {
16+
StringWriter writer = new StringWriter();
17+
String encoding = "UTF-8";
18+
try {
19+
IOUtils.copy(is, writer, encoding);
20+
} catch (IOException e) {
21+
e.printStackTrace();
22+
}
23+
return writer.toString();
24+
}
25+
26+
public static String readPayload(final HttpServletRequest request) throws IOException {
27+
String payloadData = null;
28+
ContentCachingRequestWrapper contentCachingRequestWrapper = WebUtils.getNativeRequest(request, ContentCachingRequestWrapper.class);
29+
if (null != contentCachingRequestWrapper) {
30+
byte[] buf = contentCachingRequestWrapper.getContentAsByteArray();
31+
if (buf.length > 0) {
32+
payloadData = new String(buf, 0, buf.length, contentCachingRequestWrapper.getCharacterEncoding());
33+
}
34+
}
35+
return payloadData;
36+
}
37+
38+
}

‎spring-rest-logging/src/test/java/com/baeldung/rest/log/controller/TestTaxiFareController.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ public class TestTaxiFareController {
1515
private static final String URL = "http://localhost:" + 9090 + "/rest-log/taxifare/";
1616

1717
@Test
18-
public void given_taxi_fare_get() {
18+
public void givenRequest_thenfetchTaxiFareRateCard() {
1919
TestRestTemplate testRestTemplate = new TestRestTemplate();
2020
ResponseEntity<String> response = testRestTemplate.getForEntity(URL + "get/", String.class);
2121
assertThat(response.getStatusCode(), equalTo(HttpStatus.OK));
2222
}
2323

2424
@Test
25-
public void given_taxi_ride_get_fare() {
25+
public void givenTaxiRide_thenGetCalculatedFare() {
2626
TestRestTemplate testRestTemplate = new TestRestTemplate();
2727
TaxiRide taxiRide = new TaxiRide(true,10l);
2828
String fare = testRestTemplate.postForObject(URL + "calculate/", taxiRide,String.class);

0 commit comments

Comments
 (0)