cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject cxf git commit: [CXF-7174] Avoid NPE when content-type not set in http request, patch from Andy McCright applied, This closes #213
Date Mon, 12 Dec 2016 10:34:52 GMT
Repository: cxf
Updated Branches:
  refs/heads/3.0.x-fixes 1a1499e91 -> 8921296ff


[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/8921296f
Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/8921296f
Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/8921296f

Branch: refs/heads/3.0.x-fixes
Commit: 8921296ff8e6f3694460e0b3153cf3625e2357a7
Parents: 1a1499e
Author: Sergey Beryozkin <sberyozkin@gmail.com>
Authored: Mon Dec 12 10:03:39 2016 +0000
Committer: Sergey Beryozkin <sberyozkin@gmail.com>
Committed: Mon Dec 12 10:34:31 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/8921296f/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 dee9d61..5c8fa4d 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
@@ -359,7 +359,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/8921296f/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 e20a5e0..e26e5ff 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 tearDownClass() {
-        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
@@ -174,4 +163,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.<String>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.<String>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();
+    }
 }


Mime
View raw message