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:03:59 GMT
Repository: cxf
Updated Branches:
  refs/heads/master 45bc2219b -> de9c7ecd7


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

Branch: refs/heads/master
Commit: de9c7ecd7a4d205aa26d893889235468797e87bb
Parents: 45bc221
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:03:39 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/de9c7ecd/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/de9c7ecd/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();
+    }
 }


Mime
View raw message