cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bimargul...@apache.org
Subject svn commit: r1208932 - in /cxf/trunk: rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/cors/CrossOriginOutputFilter.java systests/jaxrs/pom.xml systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/cors/BasicCrossOriginTest.java
Date Thu, 01 Dec 2011 02:22:11 GMT
Author: bimargulies
Date: Thu Dec  1 02:22:10 2011
New Revision: 1208932

URL: http://svn.apache.org/viewvc?rev=1208932&view=rev
Log:
CXF-3943: see the simplest possible test work, courtesy of the apache http client library.

Modified:
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/cors/CrossOriginOutputFilter.java
    cxf/trunk/systests/jaxrs/pom.xml
    cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/cors/BasicCrossOriginTest.java

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/cors/CrossOriginOutputFilter.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/cors/CrossOriginOutputFilter.java?rev=1208932&r1=1208931&r2=1208932&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/cors/CrossOriginOutputFilter.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/cors/CrossOriginOutputFilter.java
Thu Dec  1 02:22:10 2011
@@ -51,6 +51,12 @@ public class CrossOriginOutputFilter imp
         }
         return null;
     }
+    
+    private void addHeaders(ResponseBuilder rb, String key, List<String> vals) {
+        for (String v : vals) {
+            rb.header(key, v);
+        }
+    }
 
     public Response handleResponse(Message m, OperationResourceInfo ori, Response response)
{
         String op = (String)m.getExchange().get(CrossOriginOutputFilter.class.getName());
@@ -61,24 +67,24 @@ public class CrossOriginOutputFilter imp
         ResponseBuilder rbuilder = Response.fromResponse(response);
         if ("simple".equals(op)) {
             // 5.1.3: add Allow-Origin supplied from the input side, plus allow-credentials
as requested
-            rbuilder.header(CorsHeaderConstants.HEADER_AC_ALLOW_ORIGIN, originHeader);
+            addHeaders(rbuilder, CorsHeaderConstants.HEADER_AC_ALLOW_ORIGIN, originHeader);
             rbuilder.header(CorsHeaderConstants.HEADER_AC_ALLOW_CREDENTIALS, 
                                 Boolean.toString(allowCredentials));
             // 5.1.4 add allowed headers
             List<String> allowedHeaders 
                 = getHeadersFromInput(m, CorsHeaderConstants.HEADER_AC_ALLOW_METHODS);
             if (allowedHeaders != null) {
-                rbuilder.header(CorsHeaderConstants.HEADER_AC_ALLOW_METHODS, allowedHeaders);
+                addHeaders(rbuilder, CorsHeaderConstants.HEADER_AC_ALLOW_METHODS, allowedHeaders);
             }
             if (exposeHeaders.size() > 0) {
-                rbuilder.header(CorsHeaderConstants.HEADER_AC_EXPOSE_HEADERS, exposeHeaders);
+                addHeaders(rbuilder, CorsHeaderConstants.HEADER_AC_EXPOSE_HEADERS, exposeHeaders);
             }
             // if someone wants to clear the cache, we can't help them.
             return rbuilder.build();
         } else {
             // preflight
             // 5.2.7 add Allow-Origin supplied from the input side, plus allow-credentials
as requested
-            rbuilder.header(CorsHeaderConstants.HEADER_AC_ALLOW_ORIGIN, originHeader);
+            addHeaders(rbuilder, CorsHeaderConstants.HEADER_AC_ALLOW_ORIGIN, originHeader);
             rbuilder.header(CorsHeaderConstants.HEADER_AC_ALLOW_CREDENTIALS, 
                                 Boolean.toString(allowCredentials));
             // 5.2.8 max-age
@@ -89,13 +95,13 @@ public class CrossOriginOutputFilter imp
             /*
              * Currently, input side just lists the one requested method, and spec endorses
that.
              */
-            rbuilder.header(CorsHeaderConstants.HEADER_AC_ALLOW_METHODS, 
+            addHeaders(rbuilder, CorsHeaderConstants.HEADER_AC_ALLOW_METHODS, 
                             getHeadersFromInput(m, CorsHeaderConstants.HEADER_AC_ALLOW_METHODS));
             // 5.2.10 add allowed headers
             List<String> allowedHeaders 
                 = getHeadersFromInput(m, CorsHeaderConstants.HEADER_AC_ALLOW_HEADERS);
             if (allowedHeaders != null && allowedHeaders.size() > 0) {
-                rbuilder.header(CorsHeaderConstants.HEADER_AC_ALLOW_HEADERS, allowedHeaders);
+                addHeaders(rbuilder, CorsHeaderConstants.HEADER_AC_ALLOW_HEADERS, allowedHeaders);
             }
             return rbuilder.build();
             

Modified: cxf/trunk/systests/jaxrs/pom.xml
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/jaxrs/pom.xml?rev=1208932&r1=1208931&r2=1208932&view=diff
==============================================================================
--- cxf/trunk/systests/jaxrs/pom.xml (original)
+++ cxf/trunk/systests/jaxrs/pom.xml Thu Dec  1 02:22:10 2011
@@ -314,6 +314,12 @@
           <artifactId>jackson-core-asl</artifactId>
           <version>1.9.0</version>
         </dependency>
+        <dependency>
+         <groupId>org.apache.httpcomponents</groupId>
+         <artifactId>httpclient</artifactId>
+         <version>4.1.2</version>
+         <scope>test</scope>
+        </dependency>
     </dependencies>
     
     <build>

Modified: cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/cors/BasicCrossOriginTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/cors/BasicCrossOriginTest.java?rev=1208932&r1=1208931&r2=1208932&view=diff
==============================================================================
--- cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/cors/BasicCrossOriginTest.java
(original)
+++ cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/cors/BasicCrossOriginTest.java
Thu Dec  1 02:22:10 2011
@@ -19,18 +19,16 @@
 
 package org.apache.cxf.systest.jaxrs.cors;
 
-import java.io.InputStream;
-import java.util.List;
-
-import javax.ws.rs.core.MultivaluedMap;
-import javax.ws.rs.core.Response;
-
 import org.apache.cxf.helpers.IOUtils;
-import org.apache.cxf.interceptor.LoggingOutInterceptor;
-import org.apache.cxf.jaxrs.client.WebClient;
 import org.apache.cxf.jaxrs.cors.CorsHeaderConstants;
 import org.apache.cxf.systest.jaxrs.AbstractSpringServer;
 import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase;
+import org.apache.http.Header;
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpResponse;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.impl.client.DefaultHttpClient;
 
 import org.junit.BeforeClass;
 import org.junit.Ignore;
@@ -45,25 +43,21 @@ public class BasicCrossOriginTest extend
                    launchServer(SpringServer.class, true));
     }
     
-    @org.junit.Ignore
-    @SuppressWarnings("unchecked")
     @Test
     public void testSimpleGet() throws Exception {
         String origin = "http://localhost:" + PORT;
-        WebClient wc = WebClient.create(origin + "/");
-        WebClient.getConfig(wc).getOutInterceptors().add(new LoggingOutInterceptor());
-        // Since our WebClient doesn't know from Origin, we need to do this ourselves.
-        wc.header("Origin", origin);
-        Response r = wc.replacePath("/simpleGet/HelloThere").accept("text/plain").get();
-        assertEquals(Response.Status.OK.getStatusCode(), r.getStatus());
-        String echo = IOUtils.toString((InputStream)r.getEntity());
-        assertEquals("HelloThere", echo);
-        MultivaluedMap<String, Object> m = r.getMetadata();
-        Object acAllowed = m.get(CorsHeaderConstants.HEADER_AC_ALLOW_ORIGIN);
-        assertNotNull(acAllowed);
-        List<String> origins = (List<String>)acAllowed;
-        assertEquals(1, origins.size());
-        assertEquals(origin, origins.get(0));
+        HttpClient httpclient = new DefaultHttpClient();
+        HttpGet httpget = new HttpGet(origin + "/simpleGet/HelloThere");
+        httpget.addHeader("Origin", origin);
+        HttpResponse response = httpclient.execute(httpget);
+        HttpEntity entity = response.getEntity();
+        String e = IOUtils.toString(entity.getContent(), "utf-8");
+
+        assertEquals("HelloThere", e);
+        Header[] aaoHeaders = response.getHeaders(CorsHeaderConstants.HEADER_AC_ALLOW_ORIGIN);
+        assertNotNull(aaoHeaders);
+        assertEquals(1, aaoHeaders.length);
+        assertEquals("*", aaoHeaders[0].getValue());
     }
     
     @Ignore



Mime
View raw message