Skip to content

Commit 38b6746

Browse files
committed
HtmlUnitRequestBuilder detects form encoding type
Issue: SPR-14916
1 parent cb50f6b commit 38b6746

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/HtmlUnitRequestBuilder.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import javax.servlet.http.HttpSession;
3636

3737
import com.gargoylesoftware.htmlunit.CookieManager;
38+
import com.gargoylesoftware.htmlunit.FormEncodingType;
3839
import com.gargoylesoftware.htmlunit.WebClient;
3940
import com.gargoylesoftware.htmlunit.WebRequest;
4041
import com.gargoylesoftware.htmlunit.util.NameValuePair;
@@ -234,6 +235,12 @@ private void content(MockHttpServletRequest request, String charset) {
234235

235236
private void contentType(MockHttpServletRequest request) {
236237
String contentType = header("Content-Type");
238+
if (contentType == null) {
239+
FormEncodingType encodingType = this.webRequest.getEncodingType();
240+
if (encodingType != null) {
241+
contentType = encodingType.getName();
242+
}
243+
}
237244
request.setContentType(contentType != null ? contentType : MediaType.ALL_VALUE);
238245
}
239246

spring-test/src/test/java/org/springframework/test/web/servlet/htmlunit/HtmlUnitRequestBuilderTests.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -27,6 +27,7 @@
2727
import javax.servlet.http.Cookie;
2828
import javax.servlet.http.HttpSession;
2929

30+
import com.gargoylesoftware.htmlunit.FormEncodingType;
3031
import org.apache.commons.io.IOUtils;
3132
import org.apache.http.auth.UsernamePasswordCredentials;
3233

@@ -150,6 +151,18 @@ public void buildRequestContentType() {
150151
assertThat(actualRequest.getHeader("Content-Type"), equalTo(contentType));
151152
}
152153

154+
@Test // SPR-14916
155+
public void buildRequestContentTypeWithFormSubmission() {
156+
webRequest.setEncodingType(FormEncodingType.URL_ENCODED);
157+
158+
MockHttpServletRequest actualRequest = requestBuilder.buildRequest(servletContext);
159+
160+
assertThat(actualRequest.getContentType(), equalTo("application/x-www-form-urlencoded"));
161+
assertThat(actualRequest.getHeader("Content-Type"),
162+
equalTo("application/x-www-form-urlencoded;charset=ISO-8859-1"));
163+
}
164+
165+
153166
@Test
154167
public void buildRequestContextPathUsesFirstSegmentByDefault() {
155168
String contextPath = requestBuilder.buildRequest(servletContext).getContextPath();

0 commit comments

Comments
 (0)