You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by db...@apache.org on 2012/01/25 18:15:56 UTC
svn commit: r1235832 - in
/openejb/trunk/openejb/arquillian-tomee/arquillian-tomee-tests/src/test/java/org/apache/openejb/arquillian/tests/jaxrs:
./ httpheaders/
Author: dblevins
Date: Wed Jan 25 17:15:55 2012
New Revision: 1235832
URL: http://svn.apache.org/viewvc?rev=1235832&view=rev
Log:
Tests for HTTP Header params
TOMEE-125
Added:
openejb/trunk/openejb/arquillian-tomee/arquillian-tomee-tests/src/test/java/org/apache/openejb/arquillian/tests/jaxrs/httpheaders/
openejb/trunk/openejb/arquillian-tomee/arquillian-tomee-tests/src/test/java/org/apache/openejb/arquillian/tests/jaxrs/httpheaders/Application.java
openejb/trunk/openejb/arquillian-tomee/arquillian-tomee-tests/src/test/java/org/apache/openejb/arquillian/tests/jaxrs/httpheaders/HttpHeadersMethodsResource.java
openejb/trunk/openejb/arquillian-tomee/arquillian-tomee-tests/src/test/java/org/apache/openejb/arquillian/tests/jaxrs/httpheaders/JAXRSHttpHeadersTest.java
Modified:
openejb/trunk/openejb/arquillian-tomee/arquillian-tomee-tests/src/test/java/org/apache/openejb/arquillian/tests/jaxrs/JaxrsTest.java
Modified: openejb/trunk/openejb/arquillian-tomee/arquillian-tomee-tests/src/test/java/org/apache/openejb/arquillian/tests/jaxrs/JaxrsTest.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/arquillian-tomee/arquillian-tomee-tests/src/test/java/org/apache/openejb/arquillian/tests/jaxrs/JaxrsTest.java?rev=1235832&r1=1235831&r2=1235832&view=diff
==============================================================================
--- openejb/trunk/openejb/arquillian-tomee/arquillian-tomee-tests/src/test/java/org/apache/openejb/arquillian/tests/jaxrs/JaxrsTest.java (original)
+++ openejb/trunk/openejb/arquillian-tomee/arquillian-tomee-tests/src/test/java/org/apache/openejb/arquillian/tests/jaxrs/JaxrsTest.java Wed Jan 25 17:15:55 2012
@@ -19,12 +19,43 @@ package org.apache.openejb.arquillian.te
import org.apache.ziplock.IO;
import java.io.IOException;
+import java.net.HttpURLConnection;
import java.net.URL;
+import java.util.HashMap;
+import java.util.Map;
/**
* @version $Rev$ $Date$
*/
public class JaxrsTest {
+
+ protected Map<String, String> headers(String... h) throws IOException {
+ Map<String, String> map = new HashMap<String, String>();
+
+ for (int i = 0; i < h.length - 1;) {
+ String key = h[i++];
+ String value = h[i++];
+ map.put(key, value);
+ }
+
+ return map;
+ }
+
+ protected String get(Map<String, String> headers, String path) throws IOException {
+
+ if (path.startsWith("/")) path = path.substring(1);
+ final String port = System.getProperty("tomee.http.port", "11080");
+ final String url = String.format("http://localhost:%s/%s/%s", port, this.getClass().getSimpleName(), path);
+
+ final URL url1 = new URL(url);
+ HttpURLConnection connection = (HttpURLConnection) url1.openConnection();
+ for (Map.Entry<String, String> header : headers.entrySet()) {
+ connection.setRequestProperty(header.getKey(), header.getValue());
+ }
+
+ return IO.slurp(connection.getInputStream());
+ }
+
protected String get(String path) throws IOException {
if (path.startsWith("/")) path = path.substring(1);
final String port = System.getProperty("tomee.http.port", "11080");
Added: openejb/trunk/openejb/arquillian-tomee/arquillian-tomee-tests/src/test/java/org/apache/openejb/arquillian/tests/jaxrs/httpheaders/Application.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/arquillian-tomee/arquillian-tomee-tests/src/test/java/org/apache/openejb/arquillian/tests/jaxrs/httpheaders/Application.java?rev=1235832&view=auto
==============================================================================
--- openejb/trunk/openejb/arquillian-tomee/arquillian-tomee-tests/src/test/java/org/apache/openejb/arquillian/tests/jaxrs/httpheaders/Application.java (added)
+++ openejb/trunk/openejb/arquillian-tomee/arquillian-tomee-tests/src/test/java/org/apache/openejb/arquillian/tests/jaxrs/httpheaders/Application.java Wed Jan 25 17:15:55 2012
@@ -0,0 +1,37 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.openejb.arquillian.tests.jaxrs.httpheaders;
+
+import java.util.HashSet;
+import java.util.Set;
+
+/**
+ * Application to test the HTTP headers
+ */
+public class Application extends javax.ws.rs.core.Application {
+
+ @Override
+ public Set<Class<?>> getClasses() {
+ Set<Class<?>> clazzes = new HashSet<Class<?>>();
+ clazzes.add(HttpHeadersMethodsResource.class);
+ return clazzes;
+ }
+
+}
Added: openejb/trunk/openejb/arquillian-tomee/arquillian-tomee-tests/src/test/java/org/apache/openejb/arquillian/tests/jaxrs/httpheaders/HttpHeadersMethodsResource.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/arquillian-tomee/arquillian-tomee-tests/src/test/java/org/apache/openejb/arquillian/tests/jaxrs/httpheaders/HttpHeadersMethodsResource.java?rev=1235832&view=auto
==============================================================================
--- openejb/trunk/openejb/arquillian-tomee/arquillian-tomee-tests/src/test/java/org/apache/openejb/arquillian/tests/jaxrs/httpheaders/HttpHeadersMethodsResource.java (added)
+++ openejb/trunk/openejb/arquillian-tomee/arquillian-tomee-tests/src/test/java/org/apache/openejb/arquillian/tests/jaxrs/httpheaders/HttpHeadersMethodsResource.java Wed Jan 25 17:15:55 2012
@@ -0,0 +1,142 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.openejb.arquillian.tests.jaxrs.httpheaders;
+
+import javax.ws.rs.GET;
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+import javax.ws.rs.QueryParam;
+import javax.ws.rs.core.Context;
+import javax.ws.rs.core.Cookie;
+import javax.ws.rs.core.HttpHeaders;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.MultivaluedMap;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+
+@Path("context/httpheaders")
+public class HttpHeadersMethodsResource {
+
+ @Context
+ private HttpHeaders headersfield;
+
+ @GET
+ @Path("acceptablelanguages")
+ public String getLanguages() {
+ List<Locale> languages = headersfield.getAcceptableLanguages();
+ StringBuilder sb = new StringBuilder("acceptablelanguages:");
+ for (Locale l : languages) {
+ sb.append(l.getLanguage() + ":");
+ }
+ return sb.toString();
+ }
+
+ @GET
+ @Path("acceptablemediatypes")
+ public String getAcceptableMediaTypes() {
+ List<MediaType> mediatypes = headersfield.getAcceptableMediaTypes();
+ StringBuilder sb = new StringBuilder("acceptablemediatypes:");
+ for (MediaType mt : mediatypes) {
+ sb.append(mt.getType() + "/" + mt.getSubtype() + ":");
+ }
+ return sb.toString();
+ }
+
+ @POST
+ @Path("requestmediatype")
+ public String getRequestMediaType() {
+ MediaType mt = headersfield.getMediaType();
+ StringBuilder sb = new StringBuilder("mediatype:");
+ if (mt != null) {
+ sb.append(mt.getType() + "/" + mt.getSubtype() + ":");
+ } else {
+ sb.append("null:");
+ }
+ return sb.toString();
+ }
+
+ @POST
+ @Path("language")
+ public String getLanguage() {
+ Locale l = headersfield.getLanguage();
+ StringBuilder sb = new StringBuilder("language:");
+ if (l != null) {
+ sb.append(l.getLanguage() + ":");
+ } else {
+ sb.append("null:");
+ }
+ return sb.toString();
+ }
+
+ @POST
+ @Path("cookies")
+ public String getCookies() {
+ Map<String, Cookie> cookies = headersfield.getCookies();
+ StringBuilder sb = new StringBuilder("cookies:");
+ if (cookies == null) {
+ sb.append("null:");
+ } else {
+ List<String> cookieNames = new ArrayList<String>(cookies.keySet());
+ Collections.sort(cookieNames);
+ for (String c : cookieNames) {
+ sb.append(c + "=" + cookies.get(c).getValue() + ":");
+ }
+ }
+ return sb.toString();
+ }
+
+ @GET
+ public String getHeader(@Context HttpHeaders headers, @QueryParam("name") String headerName) {
+ try {
+ List<String> values = headers.getRequestHeader(headerName);
+ if (values == null) {
+ return "requestheader:null:";
+ } else {
+ values = new ArrayList<String>(values);
+ }
+ Collections.sort(values);
+ return "requestheader:" + values.toString();
+ } catch (IllegalArgumentException e) {
+ return "requestheader:illegalarg:";
+ }
+ }
+
+ @GET
+ @Path("/requestheaders")
+ public String getHeaders(@Context HttpHeaders headers, @QueryParam("name") String headerName) {
+ MultivaluedMap<String, String> requestHeaders = headers.getRequestHeaders();
+ List<String> keys = new ArrayList<String>(requestHeaders.keySet());
+ Collections.sort(keys);
+ StringBuilder sb = new StringBuilder("requestheaders:");
+ for (String k : keys) {
+ sb.append(k + "=");
+ List<String> values = requestHeaders.get(k);
+ if (values != null) {
+ values = new ArrayList<String>(values);
+ Collections.sort(values);
+ sb.append(values + ":");
+ }
+ }
+ return sb.toString();
+ }
+}
Added: openejb/trunk/openejb/arquillian-tomee/arquillian-tomee-tests/src/test/java/org/apache/openejb/arquillian/tests/jaxrs/httpheaders/JAXRSHttpHeadersTest.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/arquillian-tomee/arquillian-tomee-tests/src/test/java/org/apache/openejb/arquillian/tests/jaxrs/httpheaders/JAXRSHttpHeadersTest.java?rev=1235832&view=auto
==============================================================================
--- openejb/trunk/openejb/arquillian-tomee/arquillian-tomee-tests/src/test/java/org/apache/openejb/arquillian/tests/jaxrs/httpheaders/JAXRSHttpHeadersTest.java (added)
+++ openejb/trunk/openejb/arquillian-tomee/arquillian-tomee-tests/src/test/java/org/apache/openejb/arquillian/tests/jaxrs/httpheaders/JAXRSHttpHeadersTest.java Wed Jan 25 17:15:55 2012
@@ -0,0 +1,140 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.openejb.arquillian.tests.jaxrs.httpheaders;
+
+import org.apache.openejb.arquillian.tests.jaxrs.JaxrsTest;
+import org.apache.ziplock.WebModule;
+import org.jboss.arquillian.container.test.api.Deployment;
+import org.jboss.arquillian.junit.Arquillian;
+import org.jboss.shrinkwrap.api.spec.WebArchive;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import java.io.IOException;
+import java.util.Map;
+
+import static junit.framework.Assert.assertEquals;
+import static junit.framework.Assert.assertTrue;
+
+/**
+ * Tests the {@link javax.ws.rs.core.HttpHeaders} methods.
+ */
+@RunWith(Arquillian.class)
+public class JAXRSHttpHeadersTest extends JaxrsTest {
+
+ @Deployment(testable = false)
+ public static WebArchive archive() {
+ return new WebModule(JAXRSHttpHeadersTest.class).getArchive();
+ }
+
+ /**
+ * Tests {@link javax.ws.rs.core.HttpHeaders#getAcceptableLanguages()} that if given no
+ * acceptable languages, that it will return the server default locale back.
+ *
+ * @throws java.io.IOException
+ */
+ @Test
+ public void testAcceptableLanguagesNoneGiven() throws IOException {
+ final String response = get("/context/httpheaders/acceptablelanguages");
+ assertEquals("acceptablelanguages:", response);
+ }
+
+ /**
+ * Tests {@link javax.ws.rs.core.HttpHeaders#getAcceptableLanguages()} that if given a
+ * language, it will be the only language in the list.
+ *
+ * @throws java.io.IOException
+ */
+ @Test
+ public void testAcceptableLanguagesOneGiven() throws IOException {
+ final Map<String, String> headers = headers("Accept-Language", "de");
+ final String actual = get(headers, "/context/httpheaders/acceptablelanguages");
+ assertEquals("acceptablelanguages:de:", actual);
+ }
+
+
+ /**
+ * Tests {@link javax.ws.rs.core.HttpHeaders#getAcceptableLanguages()} that if given multiple
+ * languages, all will be returned in the list.
+ *
+ * @throws IOException
+ */
+ public void testAcceptableLanguagesManyGiven() throws IOException {
+ final Map<String, String> headers = headers("Accept-Language", "de, en, zh");
+ final String responseBody = get(headers, "/context/httpheaders/acceptablelanguages");
+ assertEquals("acceptablelanguages:de:", responseBody);
+ assertTrue(responseBody, responseBody.startsWith("acceptablelanguages:"));
+ assertTrue(responseBody, responseBody.contains(":de:"));
+ assertTrue(responseBody, responseBody.contains(":en:"));
+ assertTrue(responseBody, responseBody.contains(":zh:"));
+ }
+
+ /**
+ * Tests {@link javax.ws.rs.core.HttpHeaders#getAcceptableLanguages()} that if given multiple
+ * languages, all will be returned in the list sorted by their quality
+ * value.
+ *
+ * @throws IOException
+ */
+ public void testAcceptableLanguagesManyGivenQSort() throws IOException {
+ final Map<String, String> headers = headers("Accept-Language", "de;q=0.6, en;q=0.8, zh;q=0.7");
+ final String responseBody = get(headers, "/context/httpheaders/acceptablelanguages");
+ assertEquals("acceptablelanguages:de:", responseBody);
+ }
+
+ /**
+ * Tests {@link javax.ws.rs.core.HttpHeaders#getAcceptableMediaTypes()} that if given no
+ * Accept header, wildcard/wildcard is returned.
+ *
+ * @throws IOException
+ */
+ public void testAcceptableMediaTypesNoneGiven() throws IOException {
+ final String responseBody = get("/context/httpheaders/acceptablemediatypes");
+ assertEquals("acceptablemediatypes:*/*:", responseBody);
+ }
+
+ /**
+ * Tests {@link javax.ws.rs.core.HttpHeaders#getAcceptableMediaTypes()} that if given a
+ * single Accept header value, it is returned.
+ *
+ * @throws IOException
+ */
+ public void testAcceptableMediaTypesOneGiven() throws IOException {
+ final Map<String, String> headers = headers("Accept", "text/plain");
+ final String responseBody = get(headers, "/context/httpheaders/acceptablemediatypes");
+
+ assertEquals("acceptablemediatypes:text/plain:", responseBody);
+ // TODO assert assertEquals("text/plain", getResponseHeader("Content-Type").getValue());
+ }
+
+ /**
+ * Tests {@link javax.ws.rs.core.HttpHeaders#getAcceptableMediaTypes()} that if given
+ * multiple Accept header values, the values are sorted by q-value.
+ *
+ * @throws IOException
+ */
+ public void testAcceptableMediaTypesManyGiven() throws IOException {
+ final Map<String, String> headers = headers("Accept", "text/plain;q=1.0,*/*;q=0.6, application/json;q=0.7,text/xml;q=0.8");
+ final String responseBody = get(headers, "/context/httpheaders/acceptablemediatypes");
+
+ assertEquals("acceptablemediatypes:text/plain:text/xml:application/json:*/*:", responseBody);
+ //TODO assertEquals("text/plain;q=1.0", getMethod.getResponseHeader("Content-Type").getValue());
+ }
+
+}