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

Issue #929: fix set charset for request content #1247

Merged
merged 1 commit into from
Nov 6, 2024
Merged
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 @@ -32,12 +32,13 @@
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map.Entry;

import static java.lang.String.join;
import static java.lang.System.lineSeparator;
import static java.nio.charset.StandardCharsets.UTF_8;

/**
* Simple logging interceptor writes Http request and response messages to the console.
Expand Down Expand Up @@ -120,6 +121,24 @@ public boolean hasMessageListeners() {
* @return
*/
private String getRequestContent(HttpRequest request, String body) {
String contentType = request.getHeaders().getFirst(HttpHeaders.CONTENT_TYPE);
if (contentType != null) {
String[] contentTypeParts = contentType.split(";");
for (String contentTypePart : contentTypeParts) {
if(contentTypePart.startsWith("charset=") && !contentTypePart.endsWith("charset=")) {
String charset = contentTypePart.split("=")[1];
phos-web marked this conversation as resolved.
Show resolved Hide resolved
try {
body = new String(body.getBytes(), charset);
} catch (UnsupportedEncodingException e) {
body = new String(body.getBytes(), UTF_8);
}
}
break;
}
} else {
body = new String(body.getBytes(), UTF_8);
}

StringBuilder builder = new StringBuilder();

builder.append(request.getMethod());
Expand Down Expand Up @@ -226,7 +245,7 @@ public String getBodyContent() throws IOException {
getBody();
}

return new String(body, StandardCharsets.UTF_8);
return new String(body, UTF_8);
}

@Override
Expand Down
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);
phos-web marked this conversation as resolved.
Show resolved Hide resolved

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""");
}
}
Loading