-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: set charset for request content
- Loading branch information
Showing
2 changed files
with
132 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
...http/src/test/java/org/citrusframework/http/interceptor/LoggingClientInterceptorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package org.citrusframework.http.interceptor; | ||
|
||
import org.mockito.Mockito; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.http.HttpRequest; | ||
import org.testng.annotations.BeforeMethod; | ||
import org.testng.annotations.Test; | ||
|
||
import java.lang.reflect.Method; | ||
import java.net.URI; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.Mockito.when; | ||
|
||
public class LoggingClientInterceptorTest { | ||
private LoggingClientInterceptor loggingClientInterceptor; | ||
|
||
@BeforeMethod | ||
public void setUp() { | ||
loggingClientInterceptor = new LoggingClientInterceptor(); | ||
} | ||
|
||
@Test | ||
public void testGetRequestContentWithCharset() throws Exception { | ||
HttpRequest request = Mockito.mock(HttpRequest.class); | ||
HttpHeaders headers = Mockito.mock(HttpHeaders.class); | ||
|
||
when(request.getMethod()).thenReturn(HttpMethod.valueOf("POST")); | ||
when(request.getURI()).thenReturn(new URI("http://übelexample.com")); | ||
when(request.getHeaders()).thenReturn(headers); | ||
when(headers.getFirst(HttpHeaders.CONTENT_TYPE)).thenReturn("application/json; charset=UTF-8"); | ||
|
||
String body = "test body"; | ||
|
||
Method method = LoggingClientInterceptor.class.getDeclaredMethod("getRequestContent", HttpRequest.class, String.class); | ||
method.setAccessible(true); | ||
String result = (String) method.invoke(loggingClientInterceptor, request, body); | ||
|
||
assertThat(result).isEqualToNormalizingNewlines(""" | ||
POST http://übelexample.com | ||
test body"""); | ||
} | ||
|
||
@Test | ||
public void testGetRequestContentWithoutCharset() throws Exception { | ||
HttpRequest request = Mockito.mock(HttpRequest.class); | ||
HttpHeaders headers = Mockito.mock(HttpHeaders.class); | ||
|
||
when(request.getMethod()).thenReturn(HttpMethod.valueOf("GET")); | ||
when(request.getURI()).thenReturn(new URI("http://example.com")); | ||
when(request.getHeaders()).thenReturn(headers); | ||
when(headers.getFirst(HttpHeaders.CONTENT_TYPE)).thenReturn("application/json"); | ||
|
||
String body = "test body"; | ||
|
||
Method method = LoggingClientInterceptor.class.getDeclaredMethod("getRequestContent", HttpRequest.class, String.class); | ||
method.setAccessible(true); | ||
String result = (String) method.invoke(loggingClientInterceptor, request, body); | ||
|
||
assertThat(result).isEqualToNormalizingNewlines(""" | ||
GET http://example.com | ||
test body"""); | ||
} | ||
|
||
@Test | ||
public void testGetRequestContentWithNullContentType() throws Exception { | ||
HttpRequest request = Mockito.mock(HttpRequest.class); | ||
HttpHeaders headers = Mockito.mock(HttpHeaders.class); | ||
|
||
when(request.getMethod()).thenReturn(HttpMethod.valueOf("GET")); | ||
when(request.getURI()).thenReturn(new URI("http://example.com")); | ||
when(request.getHeaders()).thenReturn(headers); | ||
when(headers.getFirst(HttpHeaders.CONTENT_TYPE)).thenReturn(null); | ||
|
||
String body = "test body"; | ||
|
||
Method method = LoggingClientInterceptor.class.getDeclaredMethod("getRequestContent", HttpRequest.class, String.class); | ||
method.setAccessible(true); | ||
String result = (String) method.invoke(loggingClientInterceptor, request, body); | ||
|
||
assertThat(result).isEqualToNormalizingNewlines(""" | ||
GET http://example.com | ||
test body"""); | ||
} | ||
|
||
@Test | ||
public void testGetRequestContentWithEmptyStringContentType() throws Exception { | ||
HttpRequest request = Mockito.mock(HttpRequest.class); | ||
HttpHeaders headers = Mockito.mock(HttpHeaders.class); | ||
|
||
when(request.getMethod()).thenReturn(HttpMethod.valueOf("GET")); | ||
when(request.getURI()).thenReturn(new URI("http://example.com")); | ||
when(request.getHeaders()).thenReturn(headers); | ||
when(headers.getFirst(HttpHeaders.CONTENT_TYPE)).thenReturn(""); | ||
|
||
String body = "test body"; | ||
|
||
Method method = LoggingClientInterceptor.class.getDeclaredMethod("getRequestContent", HttpRequest.class, String.class); | ||
method.setAccessible(true); | ||
String result = (String) method.invoke(loggingClientInterceptor, request, body); | ||
|
||
assertThat(result).isEqualToNormalizingNewlines(""" | ||
GET http://example.com | ||
test body"""); | ||
} | ||
} |