From 5ba0b037031c3b1afc89a419d432c5f1d8748aa2 Mon Sep 17 00:00:00 2001
From: Matthias Piepkorn <mpiepk@gmail.com>
Date: Sun, 05 Feb 2017 11:18:26 +0000
Subject: [PATCH] Add test for ContentTypeHelper, fix format parameter handling

---
 src/main/java/org/keycloak/protocol/cas/utils/ContentTypeHelper.java |    3 ++-
 src/test/java/org/keycloak/protocol/cas/ContentTypeHelperTest.java   |   45 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 47 insertions(+), 1 deletions(-)

diff --git a/src/main/java/org/keycloak/protocol/cas/utils/ContentTypeHelper.java b/src/main/java/org/keycloak/protocol/cas/utils/ContentTypeHelper.java
index e842ef0..0e13ca6 100644
--- a/src/main/java/org/keycloak/protocol/cas/utils/ContentTypeHelper.java
+++ b/src/main/java/org/keycloak/protocol/cas/utils/ContentTypeHelper.java
@@ -19,7 +19,8 @@
     public MediaType selectResponseType() {
         String format = uriInfo.getQueryParameters().getFirst(CASLoginProtocol.FORMAT_PARAM);
         if (format != null && !format.isEmpty()) {
-            request.getMutableHeaders().add(HttpHeaders.ACCEPT, "application/" + format);
+            //if parameter is set, it overrides all header values (see spec section 2.5.1)
+            request.getMutableHeaders().putSingle(HttpHeaders.ACCEPT, "application/" + format.toLowerCase());
         }
         Variant variant = restRequest.selectVariant(Variant.mediaTypes(MediaType.APPLICATION_XML_TYPE, MediaType.APPLICATION_JSON_TYPE).build());
         return variant == null ? MediaType.APPLICATION_XML_TYPE : variant.getMediaType();
diff --git a/src/test/java/org/keycloak/protocol/cas/ContentTypeHelperTest.java b/src/test/java/org/keycloak/protocol/cas/ContentTypeHelperTest.java
new file mode 100644
index 0000000..c08ab47
--- /dev/null
+++ b/src/test/java/org/keycloak/protocol/cas/ContentTypeHelperTest.java
@@ -0,0 +1,45 @@
+package org.keycloak.protocol.cas;
+
+import org.jboss.resteasy.mock.MockHttpRequest;
+import org.jboss.resteasy.mock.MockHttpResponse;
+import org.jboss.resteasy.specimpl.RequestImpl;
+import org.junit.Test;
+import org.keycloak.protocol.cas.utils.ContentTypeHelper;
+
+import javax.ws.rs.core.HttpHeaders;
+import javax.ws.rs.core.MediaType;
+
+import static org.junit.Assert.assertEquals;
+
+public class ContentTypeHelperTest {
+    @Test
+    public void test() throws Exception {
+        assertEquals(MediaType.APPLICATION_XML_TYPE, get("http://example.com/", null).selectResponseType());
+        assertEquals(MediaType.APPLICATION_JSON_TYPE, get("http://example.com/?format=json", null).selectResponseType());
+        assertEquals(MediaType.APPLICATION_XML_TYPE, get("http://example.com/?format=xml", null).selectResponseType());
+        assertEquals(MediaType.APPLICATION_JSON_TYPE, get("http://example.com/?format=JSON", null).selectResponseType());
+        assertEquals(MediaType.APPLICATION_XML_TYPE, get("http://example.com/?format=XML", null).selectResponseType());
+
+        assertEquals(MediaType.APPLICATION_XML_TYPE, get("http://example.com/", MediaType.APPLICATION_XML).selectResponseType());
+        assertEquals(MediaType.APPLICATION_JSON_TYPE, get("http://example.com/?format=json", MediaType.APPLICATION_XML).selectResponseType());
+        assertEquals(MediaType.APPLICATION_XML_TYPE, get("http://example.com/?format=xml", MediaType.APPLICATION_XML).selectResponseType());
+
+        assertEquals(MediaType.APPLICATION_JSON_TYPE, get("http://example.com/", MediaType.APPLICATION_JSON).selectResponseType());
+        assertEquals(MediaType.APPLICATION_JSON_TYPE, get("http://example.com/?format=json", MediaType.APPLICATION_JSON).selectResponseType());
+        assertEquals(MediaType.APPLICATION_XML_TYPE, get("http://example.com/?format=xml", MediaType.APPLICATION_JSON).selectResponseType());
+
+        assertEquals(MediaType.APPLICATION_XML_TYPE, get("http://example.com/", MediaType.TEXT_PLAIN).selectResponseType());
+    }
+
+    private ContentTypeHelper get(String uri, String acceptHeader) throws Exception {
+        MockHttpRequest req = MockHttpRequest.get(uri);
+        MockHttpResponse res = new MockHttpResponse();
+        RequestImpl restReq = new RequestImpl(req, res);
+
+        if (acceptHeader != null) {
+            req = req.header(HttpHeaders.ACCEPT, acceptHeader);
+        }
+
+        return new ContentTypeHelper(req, restReq, req.getUri());
+    }
+}

--
Gitblit v1.9.1