Repository: cxf Updated Branches: refs/heads/3.1.x-fixes f32dcab58 -> 718892116
[CXF-7174] Avoid NPE when content-type not set in http request, patch from Andy McCright applied, This closes #213 Project: http://git-wip-us.apache.org/repos/asf/cxf/repo Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/71889211 Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/71889211 Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/71889211 Branch: refs/heads/3.1.x-fixes Commit: 7188921166444e861adf9b133708cef7c01d4b49 Parents: f32dcab Author: Sergey Beryozkin <sberyoz...@gmail.com> Authored: Mon Dec 12 10:03:39 2016 +0000 Committer: Sergey Beryozkin <sberyoz...@gmail.com> Committed: Mon Dec 12 10:04:43 2016 +0000 ---------------------------------------------------------------------- .../org/apache/cxf/transport/http/Headers.java | 2 +- .../apache/cxf/transport/http/HeadersTest.java | 60 +++++++++++++++----- 2 files changed, 48 insertions(+), 14 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cxf/blob/71889211/rt/transports/http/src/main/java/org/apache/cxf/transport/http/Headers.java ---------------------------------------------------------------------- diff --git a/rt/transports/http/src/main/java/org/apache/cxf/transport/http/Headers.java b/rt/transports/http/src/main/java/org/apache/cxf/transport/http/Headers.java index 3d58112..8aecbd3 100644 --- a/rt/transports/http/src/main/java/org/apache/cxf/transport/http/Headers.java +++ b/rt/transports/http/src/main/java/org/apache/cxf/transport/http/Headers.java @@ -373,7 +373,7 @@ public class Headers { public String determineContentType() { String ct = null; List<Object> ctList = CastUtils.cast(headers.get(Message.CONTENT_TYPE)); - if (ctList != null && ctList.size() == 1) { + if (ctList != null && ctList.size() == 1 && ctList.get(0) != null) { ct = ctList.get(0).toString(); } else { ct = (String)message.get(Message.CONTENT_TYPE); http://git-wip-us.apache.org/repos/asf/cxf/blob/71889211/rt/transports/http/src/test/java/org/apache/cxf/transport/http/HeadersTest.java ---------------------------------------------------------------------- diff --git a/rt/transports/http/src/test/java/org/apache/cxf/transport/http/HeadersTest.java b/rt/transports/http/src/test/java/org/apache/cxf/transport/http/HeadersTest.java index bf5cf84..bcb4228 100755 --- a/rt/transports/http/src/test/java/org/apache/cxf/transport/http/HeadersTest.java +++ b/rt/transports/http/src/test/java/org/apache/cxf/transport/http/HeadersTest.java @@ -36,29 +36,17 @@ import org.apache.cxf.message.MessageImpl; import org.easymock.EasyMock; import org.easymock.IMocksControl; -import org.junit.AfterClass; import org.junit.Assert; -import org.junit.BeforeClass; import org.junit.Test; /** * */ public class HeadersTest extends Assert { - private static IMocksControl control; - - @BeforeClass - public static void setUpClass() { - control = EasyMock.createNiceControl(); - } - - @AfterClass - public static void tearDown() { - control.verify(); - } @Test public void setHeadersTest() throws Exception { + IMocksControl control = EasyMock.createNiceControl(); String[] headerNames = {"Content-Type", "authorization", "soapAction"}; String[] headerValues = {"text/xml", "Basic Zm9vOmJhcg==", "foo"}; Map<String, List<String>> inmap = new HashMap<String, List<String>>(); @@ -102,6 +90,7 @@ public class HeadersTest extends Assert { assertEquals("unexpected header", protocolHeaders.get("SOAPACTION").get(0), headerValues[2]); assertEquals("unexpected header", protocolHeaders.get("soapAction").get(0), headerValues[2]); + control.verify(); } @Test @@ -173,4 +162,49 @@ public class HeadersTest extends Assert { Headers.logProtocolHeaders(logger, Level.INFO, headerMap, false); } + + @Test + public void nullContentTypeTest() { + IMocksControl control = EasyMock.createNiceControl(); + + Message message = new MessageImpl(); + + // first check - content-type==null in message, nothing specified in request + // expect that determineContentType will return the default value of text/xml + message.put(Message.CONTENT_TYPE, null); + Headers headers = new Headers(message); + assertEquals("Unexpected content-type determined - expected text/xml", "text/xml", + headers.determineContentType()); + + // second check - null specified in request, valid content-type specified in message + // expect that determineContentType returns the content-type specified in the message + HttpServletRequest req = control.createMock(HttpServletRequest.class); + EasyMock.expect(req.getHeaderNames()).andReturn(Collections.emptyEnumeration()); + EasyMock.expect(req.getContentType()).andReturn(null).anyTimes(); + control.replay(); + message = new MessageImpl(); + message.put(Message.CONTENT_TYPE, "application/json"); + headers = new Headers(message); + headers.copyFromRequest(req); + assertEquals("Unexpected content-type determined - expected application/json", "application/json", + headers.determineContentType()); + + control.verify(); + + // third check - content-type==null in message, null in request + // expect that determineContentType returns the default value of text/xml + control = EasyMock.createNiceControl(); + req = control.createMock(HttpServletRequest.class); + EasyMock.expect(req.getHeaderNames()).andReturn(Collections.emptyEnumeration()); + EasyMock.expect(req.getContentType()).andReturn(null).anyTimes(); + control.replay(); + message = new MessageImpl(); + message.put(Message.CONTENT_TYPE, null); + headers = new Headers(message); + headers.copyFromRequest(req); + assertEquals("Unexpected content-type determined - expected text/xml", "text/xml", + headers.determineContentType()); + + control.verify(); + } }