This is an automated email from the ASF dual-hosted git repository. jamesbognar pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/juneau.git
The following commit(s) were added to refs/heads/master by this push: new 90f0596 Tests. 90f0596 is described below commit 90f059647ce2a5431ca3e77e795e5f9b68964202 Author: JamesBognar <jamesbog...@apache.org> AuthorDate: Sun May 13 13:50:13 2018 -0400 Tests. --- .../apache/juneau/rest/test/HeadersResource.java | 245 -------- .../java/org/apache/juneau/rest/test/Root.java | 1 - .../org/apache/juneau/rest/test/HeadersTest.java | 262 --------- .../org/apache/juneau/rest/test/_TestSuite.java | 1 - .../juneau/rest/mock/MockServletRequest.java | 318 +++++++++-- .../apache/juneau/rest/headers/HeadersTest.java | 623 +++++++++++++++++++++ 6 files changed, 896 insertions(+), 554 deletions(-) diff --git a/juneau-microservice/juneau-microservice-test/src/main/java/org/apache/juneau/rest/test/HeadersResource.java b/juneau-microservice/juneau-microservice-test/src/main/java/org/apache/juneau/rest/test/HeadersResource.java deleted file mode 100644 index e64b506..0000000 --- a/juneau-microservice/juneau-microservice-test/src/main/java/org/apache/juneau/rest/test/HeadersResource.java +++ /dev/null @@ -1,245 +0,0 @@ -// *************************************************************************************************************************** -// * 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.juneau.rest.test; - -import static org.apache.juneau.http.HttpMethodName.*; - -import org.apache.juneau.*; -import org.apache.juneau.encoders.*; -import org.apache.juneau.http.*; -import org.apache.juneau.plaintext.*; -import org.apache.juneau.rest.*; -import org.apache.juneau.rest.annotation.*; - -/** - * JUnit automated testcase resource. - */ -@RestResource( - path="/testHeaders", - serializers=HeadersResource.PlainTextAnythingSerializer.class, - parsers=HeadersResource.PlainTextAnythingParser.class, - encoders=HeadersResource.IdentityAnythingEncoder.class, - paramResolvers=HeadersResource.CustomHeaderParam.class -) -public class HeadersResource extends RestServlet { - private static final long serialVersionUID = 1L; - - //==================================================================================================== - // HTTP 1.1 headers - //==================================================================================================== - - @RestMethod(name=GET, path="/accept") - public String accept(Accept accept) { - return accept.toString(); - } - @RestMethod(name=GET, path="/acceptCharset") - public String acceptCharset(AcceptCharset acceptCharset) { - return acceptCharset.toString(); - } - @RestMethod(name=GET, path="/acceptEncoding") - public String acceptEncoding(AcceptEncoding acceptEncoding) { - return acceptEncoding.toString(); - } - @RestMethod(name=GET, path="/acceptLanguage") - public String acceptLanguage(AcceptLanguage acceptLanguage) { - return acceptLanguage.toString(); - } - @RestMethod(name=GET, path="/authorization") - public String authorization(Authorization authorization) { - return authorization.toString(); - } - @RestMethod(name=GET, path="/cacheControl") - public String cacheControl(CacheControl cacheControl) { - return cacheControl.toString(); - } - @RestMethod(name=GET, path="/connection") - public String connection(Connection connection) { - return connection.toString(); - } - @RestMethod(name=GET, path="/contentLength") - public String contentLength(ContentLength contentLength) { - return contentLength.toString(); - } - @RestMethod(name=GET, path="/contentType") - public String contentType(ContentType contentType) { - return contentType.toString(); - } - @RestMethod(name=GET, path="/date") - public String date(org.apache.juneau.http.Date date) { - return date.toString(); - } - @RestMethod(name=GET, path="/expect") - public String expect(Expect expect) { - return expect.toString(); - } - @RestMethod(name=GET, path="/from") - public String from(From from) { - return from.toString(); - } - @RestMethod(name=GET, path="/host") - public String host(Host host) { - return host.toString(); - } - @RestMethod(name=GET, path="/ifMatch") - public String IfMatch(IfMatch ifMatch) { - return ifMatch.toString(); - } - @RestMethod(name=GET, path="/ifModifiedSince") - public String ifModifiedSince(IfModifiedSince ifModifiedSince) { - return ifModifiedSince.toString(); - } - @RestMethod(name=GET, path="/ifNoneMatch") - public String ifNoneMatch(IfNoneMatch ifNoneMatch) { - return ifNoneMatch.toString(); - } - @RestMethod(name=GET, path="/ifRange") - public String ifRange(IfRange ifRange) { - return ifRange.toString(); - } - @RestMethod(name=GET, path="/ifUnmodifiedSince") - public String ifUnmodifiedSince(IfUnmodifiedSince ifUnmodifiedSince) { - return ifUnmodifiedSince.toString(); - } - @RestMethod(name=GET, path="/maxForwards") - public String maxForwards(MaxForwards maxForwards) { - return maxForwards.toString(); - } - @RestMethod(name=GET, path="/pragma") - public String pragma(Pragma pragma) { - return pragma.toString(); - } - @RestMethod(name=GET, path="/proxyAuthorization") - public String proxyAuthorization(ProxyAuthorization proxyAuthorization) { - return proxyAuthorization.toString(); - } - @RestMethod(name=GET, path="/range") - public String range(Range range) { - return range.toString(); - } - @RestMethod(name=GET, path="/referer") - public String referer(Referer referer) { - return referer.toString(); - } - @RestMethod(name=GET, path="/te") - public String te(TE te) { - return te.toString(); - } - @RestMethod(name=GET, path="/upgrade") - public String upgrade(Upgrade upgrade) { - return upgrade.toString(); - } - @RestMethod(name=GET, path="/userAgent") - public String userAgent(UserAgent userAgent) { - return userAgent.toString(); - } - @RestMethod(name=GET, path="/warning") - public String warning(Warning warning) { - return warning.toString(); - } - @RestMethod(name=GET, path="/customHeader") - public String customHeader(CustomHeader customHeader) { - return customHeader.toString(); - } - - public static class CustomHeaderParam extends RestMethodParam { - public CustomHeaderParam() { - super(RestParamType.HEADER, "Custom", CustomHeader.class); - } - @Override - public Object resolve(RestRequest req, RestResponse res) throws Exception { - return new CustomHeader(req.getHeader("Custom")); - } - } - - public static class CustomHeader { - public String value; - public CustomHeader(String value) { - this.value = value; - } - @Override - public String toString() { - return value; - } - } - - public static class PlainTextAnythingSerializer extends PlainTextSerializer { - public PlainTextAnythingSerializer(PropertyStore ps) { - super(ps, "text/plain", "*/*"); - } - } - - public static class PlainTextAnythingParser extends PlainTextParser { - public PlainTextAnythingParser(PropertyStore ps) { - super(ps, "*/*"); - } - } - - public static class IdentityAnythingEncoder extends IdentityEncoder { - @Override /* ConfigEncoder */ - public String[] getCodings() { - return new String[]{"*"}; - } - } - - //==================================================================================================== - // Default values. - //==================================================================================================== - - @RestMethod(name=GET, path="/defaultRequestHeaders", defaultRequestHeaders={"H1:1","H2=2"," H3 : 3 "}) - public ObjectMap defaultRequestHeaders(RequestHeaders headers) { - return new ObjectMap() - .append("h1", headers.getString("H1")) - .append("h2", headers.getString("H2")) - .append("h3", headers.getString("H3")); - } - - @RestMethod(name=GET, path="/defaultRequestHeadersCaseInsensitive", defaultRequestHeaders={"H1:1","H2=2"," H3 : 3 "}) - public ObjectMap defaultRequestHeadersCaseInsensitive(RequestHeaders headers) { - return new ObjectMap() - .append("h1", headers.getString("h1")) - .append("h2", headers.getString("h2")) - .append("h3", headers.getString("h3")); - } - - @RestMethod(name=GET, path="/annotatedHeaders") - public ObjectMap annotatedHeaders(@Header("H1") String h1, @Header("H2") String h2, @Header("H3") String h3) { - return new ObjectMap() - .append("h1", h1) - .append("h2", h2) - .append("h3", h3); - } - - @RestMethod(name=GET, path="/annotatedHeadersCaseInsensitive") - public ObjectMap annotatedHeadersCaseInsensitive(@Header("h1") String h1, @Header("h2") String h2, @Header("h3") String h3) { - return new ObjectMap() - .append("h1", h1) - .append("h2", h2) - .append("h3", h3); - } - - @RestMethod(name=GET, path="/annotatedHeadersDefault") - public ObjectMap annotatedHeadersDefault(@Header(value="h1",_default="1") String h1, @Header(value="h2",_default="2") String h2, @Header(value="h3",_default="3") String h3) { - return new ObjectMap() - .append("h1", h1) - .append("h2", h2) - .append("h3", h3); - } - - @RestMethod(name=GET, path="/annotatedAndDefaultHeaders", defaultRequestHeaders={"H1:1","H2=2"," H3 : 3 "}) - public ObjectMap annotatedAndDefaultHeaders(@Header(value="h1",_default="4") String h1, @Header(value="h2",_default="5") String h2, @Header(value="h3",_default="6") String h3) { - return new ObjectMap() - .append("h1", h1) - .append("h2", h2) - .append("h3", h3); - } -} diff --git a/juneau-microservice/juneau-microservice-test/src/main/java/org/apache/juneau/rest/test/Root.java b/juneau-microservice/juneau-microservice-test/src/main/java/org/apache/juneau/rest/test/Root.java index 2298c0c..ab68256 100644 --- a/juneau-microservice/juneau-microservice-test/src/main/java/org/apache/juneau/rest/test/Root.java +++ b/juneau-microservice/juneau-microservice-test/src/main/java/org/apache/juneau/rest/test/Root.java @@ -31,7 +31,6 @@ import org.apache.juneau.rest.test.client.*; ConfigResource.class, TransformsResource.class, FormDataResource.class, - HeadersResource.class, HtmlDocResource.class, HtmlDocLinksResource.class, InheritanceResource.TestEncoders.class, diff --git a/juneau-microservice/juneau-microservice-test/src/test/java/org/apache/juneau/rest/test/HeadersTest.java b/juneau-microservice/juneau-microservice-test/src/test/java/org/apache/juneau/rest/test/HeadersTest.java deleted file mode 100644 index 2001415..0000000 --- a/juneau-microservice/juneau-microservice-test/src/test/java/org/apache/juneau/rest/test/HeadersTest.java +++ /dev/null @@ -1,262 +0,0 @@ -// *************************************************************************************************************************** -// * 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.juneau.rest.test; - -import static org.apache.juneau.rest.testutils.TestUtils.*; -import static org.junit.Assert.*; - -import org.apache.juneau.*; -import org.apache.juneau.rest.client.*; -import org.junit.*; - -public class HeadersTest extends RestTestcase { - - RestClient client = TestMicroservice.DEFAULT_CLIENT; - - private static String URL = "/testHeaders"; - - //==================================================================================================== - // Basic tests - //==================================================================================================== - - @Test - public void accept() throws Exception { - assertEquals("text/foo", client.doGet(URL + "/accept").accept("text/foo").getResponseAsString()); - assertEquals("text/foo+bar", client.doGet(URL + "/accept").accept("text/foo+bar").getResponseAsString()); - assertEquals("text/*", client.doGet(URL + "/accept").accept("text/*").getResponseAsString()); - assertEquals("*/foo", client.doGet(URL + "/accept").accept("*/foo").getResponseAsString()); - - assertEquals("text/foo", client.doGet(URL + "/accept").accept("text/foo;q=1.0").getResponseAsString()); - assertEquals("text/foo;q=0.9", client.doGet(URL + "/accept").accept("text/foo;q=0.9").getResponseAsString()); - assertEquals("text/foo;x=X;q=0.9;y=Y", client.doGet(URL + "/accept").accept("text/foo;x=X;q=0.9;y=Y").getResponseAsString()); - - assertEquals("text/foo", client.doGet(URL + "/accept").query("Accept", "text/foo").getResponseAsString()); - } - - @Test - public void acceptCharset() throws Exception { - assertEquals("UTF-8", client.doGet(URL + "/acceptCharset").acceptCharset("UTF-8").getResponseAsString()); - assertEquals("UTF-8", client.doGet(URL + "/acceptCharset").query("Accept-Charset", "UTF-8").getResponseAsString()); - } - - @Test - public void acceptEncoding() throws Exception { - assertEquals("foo", client.doGet(URL + "/acceptEncoding").acceptEncoding("foo").getResponseAsString()); - assertEquals("*", client.doGet(URL + "/acceptEncoding").acceptEncoding("*").getResponseAsString()); - assertEquals("*", client.doGet(URL + "/acceptEncoding").query("Accept-Encoding", "*").getResponseAsString()); - } - - @Test - public void acceptLanguage() throws Exception { - assertEquals("foo", client.doGet(URL + "/acceptLanguage").acceptLanguage("foo").getResponseAsString()); - assertEquals("foo", client.doGet(URL + "/acceptLanguage").query("Accept-Language", "foo").getResponseAsString()); - } - - @Test - public void authorization() throws Exception { - assertEquals("foo", client.doGet(URL + "/authorization").authorization("foo").getResponseAsString()); - assertEquals("foo", client.doGet(URL + "/authorization").query("Authorization", "foo").getResponseAsString()); - } - - @Test - public void cacheControl() throws Exception { - assertEquals("foo", client.doGet(URL + "/cacheControl").cacheControl("foo").getResponseAsString()); - assertEquals("foo", client.doGet(URL + "/cacheControl").query("Cache-Control", "foo").getResponseAsString()); - } - - @Test - public void connection() throws Exception { - assertEquals("foo", client.doGet(URL + "/connection").connection("foo").getResponseAsString()); - assertEquals("foo", client.doGet(URL + "/connection").query("Connection", "foo").getResponseAsString()); - } - - @Test - public void contentLength() throws Exception { - assertEquals("0", client.doGet(URL + "/contentLength").contentLength(0).getResponseAsString()); - assertEquals("0", client.doGet(URL + "/contentLength").query("Content-Length", 0).getResponseAsString()); - } - - @Test - public void contentType() throws Exception { - assertEquals("text/foo", client.doGet(URL + "/contentType").contentType("text/foo").getResponseAsString()); - assertEquals("text/foo", client.doGet(URL + "/contentType").query("Content-Type", "text/foo").getResponseAsString()); - } - - @Test - public void date() throws Exception { - assertEquals("foo", client.doGet(URL + "/date").date("foo").getResponseAsString()); - assertEquals("foo", client.doGet(URL + "/date").query("Date", "foo").getResponseAsString()); - } - - @Test - public void expect() throws Exception { - // This seems to blow up Jetty - //assertEquals("100-continue", client.doGet(URL + "/expect").expect("100-continue").getResponseAsString()); - //assertEquals("100-continue", client.doGet(URL + "/expect").query("Expect", "100-continue").getResponseAsString()); - } - - @Test - public void from() throws Exception { - assertEquals("foo", client.doGet(URL + "/from").from("foo").getResponseAsString()); - assertEquals("foo", client.doGet(URL + "/from").query("From", "foo").getResponseAsString()); - } - - @Test - public void host() throws Exception { - assertTrue(client.doGet(URL + "/host").host("localhost").getResponseAsString().startsWith("localhost")); - assertTrue(client.doGet(URL + "/host").query("Host", "localhost").getResponseAsString().startsWith("localhost")); - } - - @Test - public void ifMatch() throws Exception { - RestClient client = TestMicroservice.DEFAULT_CLIENT; - assertEquals("\"foo\"", client.doGet(URL + "/ifMatch").ifMatch("foo").getResponseAsString()); - assertEquals("\"foo\"", client.doGet(URL + "/ifMatch").ifMatch("\"foo\"").getResponseAsString()); - assertEquals("W/\"foo\"", client.doGet(URL + "/ifMatch").ifMatch("W/\"foo\"").getResponseAsString()); - assertEquals("W/\"foo\", \"bar\"", client.doGet(URL + "/ifMatch").ifMatch("W/\"foo\",\"bar\"").getResponseAsString()); - assertEquals("\"foo\"", client.doGet(URL + "/ifMatch").query("If-Match", "foo").getResponseAsString()); - } - - @Test - public void ifModifiedSince() throws Exception { - assertEquals("foo", client.doGet(URL + "/ifModifiedSince").ifModifiedSince("foo").getResponseAsString()); - assertEquals("foo", client.doGet(URL + "/ifModifiedSince").query("If-Modified-Since", "foo").getResponseAsString()); - } - - @Test - public void ifNoneMatch() throws Exception { - assertEquals("\"foo\"", client.doGet(URL + "/ifNoneMatch").ifNoneMatch("foo").getResponseAsString()); - assertEquals("\"foo\"", client.doGet(URL + "/ifNoneMatch").ifNoneMatch("\"foo\"").getResponseAsString()); - assertEquals("W/\"foo\"", client.doGet(URL + "/ifNoneMatch").ifNoneMatch("W/\"foo\"").getResponseAsString()); - assertEquals("W/\"foo\", \"bar\"", client.doGet(URL + "/ifNoneMatch").ifNoneMatch("W/\"foo\",\"bar\"").getResponseAsString()); - assertEquals("\"foo\"", client.doGet(URL + "/ifNoneMatch").query("If-None-Match", "foo").getResponseAsString()); - } - - @Test - public void ifRange() throws Exception { - assertEquals("foo", client.doGet(URL + "/ifRange").ifRange("foo").getResponseAsString()); - assertEquals("foo", client.doGet(URL + "/ifRange").query("If-Range", "foo").getResponseAsString()); - } - - @Test - public void ifUnmodifiedSince() throws Exception { - assertEquals("foo", client.doGet(URL + "/ifUnmodifiedSince").ifUnmodifiedSince("foo").getResponseAsString()); - assertEquals("foo", client.doGet(URL + "/ifUnmodifiedSince").query("If-Unmodified-Since", "foo").getResponseAsString()); - } - - @Test - public void maxForwards() throws Exception { - assertEquals("123", client.doGet(URL + "/maxForwards").maxForwards(123).getResponseAsString()); - assertEquals("123", client.doGet(URL + "/maxForwards").query("Max-Forwards", 123).getResponseAsString()); - } - - @Test - public void pragma() throws Exception { - assertEquals("foo", client.doGet(URL + "/pragma").pragma("foo").getResponseAsString()); - assertEquals("foo", client.doGet(URL + "/pragma").query("Pragma", "foo").getResponseAsString()); - } - - @Test - public void proxyAuthorization() throws Exception { - assertEquals("foo", client.doGet(URL + "/proxyAuthorization").proxyAuthorization("foo").getResponseAsString()); - assertEquals("foo", client.doGet(URL + "/proxyAuthorization").query("Proxy-Authorization", "foo").getResponseAsString()); - } - - @Test - public void range() throws Exception { - assertEquals("foo", client.doGet(URL + "/range").range("foo").getResponseAsString()); - assertEquals("foo", client.doGet(URL + "/range").query("Range", "foo").getResponseAsString()); - } - - @Test - public void referer() throws Exception { - assertEquals("foo", client.doGet(URL + "/referer").referer("foo").getResponseAsString()); - assertEquals("foo", client.doGet(URL + "/referer").query("Referer", "foo").getResponseAsString()); - } - - @Test - public void te() throws Exception { - assertEquals("foo", client.doGet(URL + "/te").te("foo").getResponseAsString()); - assertEquals("foo", client.doGet(URL + "/te").query("TE", "foo").getResponseAsString()); - } - -// @Test -// public void upgrade() throws Exception { -// assertEquals("foo", client.doGet(URL + "/upgrade").upgrade("foo").getResponseAsString()); -// assertEquals("foo", client.doGet(URL + "/upgrade").query("Upgrade", "foo").getResponseAsString()); -// } -// - @Test - public void userAgent() throws Exception { - assertEquals("foo", client.doGet(URL + "/userAgent").userAgent("foo").getResponseAsString()); - assertEquals("foo", client.doGet(URL + "/userAgent").query("User-Agent", "foo").getResponseAsString()); - } - - @Test - public void warning() throws Exception { - assertEquals("foo", client.doGet(URL + "/warning").warning("foo").getResponseAsString()); - assertEquals("foo", client.doGet(URL + "/warning").query("Warning", "foo").getResponseAsString()); - } - - @Test - public void customHeader() throws Exception { - assertEquals("foo", client.doGet(URL + "/customHeader").header("Custom", "foo").getResponseAsString()); - assertEquals("foo", client.doGet(URL + "/customHeader").query("Custom", "foo").getResponseAsString()); - } - - //==================================================================================================== - // Default values. - //==================================================================================================== - - @Test - public void defaultRequestHeaders() throws Exception { - assertObjectEquals("{h1:'1',h2:'2',h3:'3'}", client.doGet(URL + "/defaultRequestHeaders").getResponse(ObjectMap.class)); - assertObjectEquals("{h1:'4',h2:'5',h3:'6'}", client.doGet(URL + "/defaultRequestHeaders").header("H1",4).header("H2",5).header("H3",6).getResponse(ObjectMap.class)); - assertObjectEquals("{h1:'4',h2:'5',h3:'6'}", client.doGet(URL + "/defaultRequestHeaders").header("h1",4).header("h2",5).header("h3",6).getResponse(ObjectMap.class)); - } - - @Test - public void defaultRequestHeadersCaseInsensitive() throws Exception { - assertObjectEquals("{h1:'1',h2:'2',h3:'3'}", client.doGet(URL + "/defaultRequestHeadersCaseInsensitive").getResponse(ObjectMap.class)); - assertObjectEquals("{h1:'4',h2:'5',h3:'6'}", client.doGet(URL + "/defaultRequestHeadersCaseInsensitive").header("H1",4).header("H2",5).header("H3",6).getResponse(ObjectMap.class)); - assertObjectEquals("{h1:'4',h2:'5',h3:'6'}", client.doGet(URL + "/defaultRequestHeadersCaseInsensitive").header("h1",4).header("h2",5).header("h3",6).getResponse(ObjectMap.class)); - } - - @Test - public void annotatedHeaders() throws Exception { - assertObjectEquals("{h1:null,h2:null,h3:null}", client.doGet(URL + "/annotatedHeaders").getResponse(ObjectMap.class)); - assertObjectEquals("{h1:'4',h2:'5',h3:'6'}", client.doGet(URL + "/annotatedHeaders").header("H1",4).header("H2",5).header("H3",6).getResponse(ObjectMap.class)); - assertObjectEquals("{h1:'4',h2:'5',h3:'6'}", client.doGet(URL + "/annotatedHeaders").header("h1",4).header("h2",5).header("h3",6).getResponse(ObjectMap.class)); - } - - @Test - public void annotatedHeadersCaseInsensitive() throws Exception { - assertObjectEquals("{h1:null,h2:null,h3:null}", client.doGet(URL + "/annotatedHeadersCaseInsensitive").getResponse(ObjectMap.class)); - assertObjectEquals("{h1:'4',h2:'5',h3:'6'}", client.doGet(URL + "/annotatedHeadersCaseInsensitive").header("H1",4).header("H2",5).header("H3",6).getResponse(ObjectMap.class)); - assertObjectEquals("{h1:'4',h2:'5',h3:'6'}", client.doGet(URL + "/annotatedHeadersCaseInsensitive").header("h1",4).header("h2",5).header("h3",6).getResponse(ObjectMap.class)); - } - - @Test - public void annotatedHeadersDefault() throws Exception { - assertObjectEquals("{h1:'1',h2:'2',h3:'3'}", client.doGet(URL + "/annotatedHeadersDefault").getResponse(ObjectMap.class)); - assertObjectEquals("{h1:'4',h2:'5',h3:'6'}", client.doGet(URL + "/annotatedHeadersDefault").header("H1",4).header("H2",5).header("H3",6).getResponse(ObjectMap.class)); - assertObjectEquals("{h1:'4',h2:'5',h3:'6'}", client.doGet(URL + "/annotatedHeadersDefault").header("h1",4).header("h2",5).header("h3",6).getResponse(ObjectMap.class)); - } - - @Test - public void annotatedAndDefaultHeaders() throws Exception { - assertObjectEquals("{h1:'4',h2:'5',h3:'6'}", client.doGet(URL + "/annotatedAndDefaultHeaders").getResponse(ObjectMap.class)); - assertObjectEquals("{h1:'7',h2:'8',h3:'9'}", client.doGet(URL + "/annotatedAndDefaultHeaders").header("H1",7).header("H2",8).header("H3",9).getResponse(ObjectMap.class)); - assertObjectEquals("{h1:'7',h2:'8',h3:'9'}", client.doGet(URL + "/annotatedAndDefaultHeaders").header("h1",7).header("h2",8).header("h3",9).getResponse(ObjectMap.class)); - } -} diff --git a/juneau-microservice/juneau-microservice-test/src/test/java/org/apache/juneau/rest/test/_TestSuite.java b/juneau-microservice/juneau-microservice-test/src/test/java/org/apache/juneau/rest/test/_TestSuite.java index a6cc00f..5a04bdf 100644 --- a/juneau-microservice/juneau-microservice-test/src/test/java/org/apache/juneau/rest/test/_TestSuite.java +++ b/juneau-microservice/juneau-microservice-test/src/test/java/org/apache/juneau/rest/test/_TestSuite.java @@ -29,7 +29,6 @@ import org.junit.runners.Suite.*; ClientFuturesTest.class, ConfigTest.class, FormDataTest.class, - HeadersTest.class, HtmlDocTest.class, HtmlDocLinksTest.class, InheritanceTest.class, diff --git a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/mock/MockServletRequest.java b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/mock/MockServletRequest.java index 65a8281..c4d9041 100644 --- a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/mock/MockServletRequest.java +++ b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/mock/MockServletRequest.java @@ -264,8 +264,8 @@ public class MockServletRequest implements HttpServletRequest { * @param value Header value. * @return This object (for method chaining). */ - public MockServletRequest header(String name, String value) { - this.headerMap.put(name, new String[] {value}); + public MockServletRequest header(String name, Object value) { + this.headerMap.put(name, new String[] {asString(value)}); return this; } @@ -996,34 +996,62 @@ public class MockServletRequest implements HttpServletRequest { return null; } + //================================================================================================================= + // Convenience methods - query and form data + //================================================================================================================= + /** - * Specifies the <code>Content-Type</code> header value on the request. + * Adds a form data entry to this request. * - * @param value The new value. + * @param key + * @param value * @return This object (for method chaining). */ - public MockServletRequest contentType(String value) { - return header("Content-Type", value); + public MockServletRequest formData(String key, Object value) { + if (formDataMap == null) + formDataMap = new LinkedHashMap<>(); + String s = asString(value); + String[] existing = formDataMap.get(key); + if (existing == null) + existing = new String[]{s}; + else + existing = new AList<>().appendAll(Arrays.asList(existing)).append(s).toArray(new String[0]); + formDataMap.put(key, existing); + return this; } /** - * Specifies the <code>Accept</code> header value on the request. + * Adds a query data entry to this request. * - * @param value The new value. + * @param key + * @param value * @return This object (for method chaining). */ - public MockServletRequest accept(String value) { - return header("Accept", value); + public MockServletRequest query(String key, Object value) { + if (queryData == null) + queryData = new LinkedHashMap<>(); + String s = asString(value); + String[] existing = queryData.get(key); + if (existing == null) + existing = new String[]{s}; + else + existing = new AList<>().appendAll(Arrays.asList(existing)).append(s).toArray(new String[0]); + queryData.put(key, existing); + return this; } + //================================================================================================================= + // Convenience methods - headers + //================================================================================================================= + /** - * Specifies the <code>Accept-Language</code> header value on the request. + * Specifies the <code>Accept</code> header value on the request. * * @param value The new value. * @return This object (for method chaining). */ - public MockServletRequest acceptLanguage(String value) { - return header("Accept-Language", value); + public MockServletRequest accept(Object value) { + return header("Accept", value); } /** @@ -1032,7 +1060,7 @@ public class MockServletRequest implements HttpServletRequest { * @param value The new value. * @return This object (for method chaining). */ - public MockServletRequest acceptCharset(String value) { + public MockServletRequest acceptCharset(Object value) { return header("Accept-Charset", value); } @@ -1042,67 +1070,267 @@ public class MockServletRequest implements HttpServletRequest { * @param value The new value. * @return This object (for method chaining). */ - public MockServletRequest acceptEncoding(String value) { + public MockServletRequest acceptEncoding(Object value) { return header("Accept-Encoding", value); } /** + * Specifies the <code>Accept-Language</code> header value on the request. + * + * @param value The new value. + * @return This object (for method chaining). + */ + public MockServletRequest acceptLanguage(Object value) { + return header("Accept-Language", value); + } + + /** + * Specifies the <code>Authorization</code> header value on the request. + * + * @param value The new value for the header. + * @return This object (for method chaining). + */ + public MockServletRequest authorization(Object value) { + return header("Authorization", value); + } + + /** + * Specifies the <code>Cache-Control</code> header value on the request. + * + * @param value The new value for the header. + * @return This object (for method chaining). + */ + public MockServletRequest cacheControl(Object value) { + return header("Cache-Control", value); + } + + /** * Specifies the <code>X-Client-Version</code> header value on the request. * * @param value The new value. * @return This object (for method chaining). */ - public MockServletRequest clientVersion(String value) { + public MockServletRequest clientVersion(Object value) { return header("X-Client-Version", value); } /** + * Specifies the <code>Connection</code> header value on the request. + * + * @param value The new value for the header. + * @return This object (for method chaining). + */ + public MockServletRequest connection(Object value) { + return header("Connection", value); + } + + /** * Specifies the <code>Content-Encoding</code> header value on the request. * * @param value The new value. * @return This object (for method chaining). */ - public MockServletRequest contentEncoding(String value) { + public MockServletRequest contentEncoding(Object value) { return header("Content-Encoding", value); } /** - * Adds a form data entry to this request. + * Specifies the <code>Content-Length</code> header value on the request. * - * @param key - * @param value + * @param value The new value for the header. * @return This object (for method chaining). */ - public MockServletRequest formData(String key, Object value) { - if (formDataMap == null) - formDataMap = new LinkedHashMap<>(); - String s = asString(value); - String[] existing = formDataMap.get(key); - if (existing == null) - existing = new String[]{s}; - else - existing = new AList<>().appendAll(Arrays.asList(existing)).append(s).toArray(new String[0]); - formDataMap.put(key, existing); - return this; + public MockServletRequest contentLength(Object value) { + return header("Content-Length", value); } /** - * Adds a query data entry to this request. + * Specifies the <code>Content-Type</code> header value on the request. * - * @param key - * @param value + * @param value The new value. * @return This object (for method chaining). */ - public MockServletRequest query(String key, Object value) { - if (queryData == null) - queryData = new LinkedHashMap<>(); - String s = asString(value); - String[] existing = queryData.get(key); - if (existing == null) - existing = new String[]{s}; - else - existing = new AList<>().appendAll(Arrays.asList(existing)).append(s).toArray(new String[0]); - queryData.put(key, existing); - return this; + public MockServletRequest contentType(Object value) { + return header("Content-Type", value); + } + + /** + * Specifies the <code>Date</code> header value on the request. + * + * @param value The new value for the header. + * @return This object (for method chaining). + */ + public MockServletRequest date(Object value) { + return header("Date", value); + } + + /** + * Specifies the <code>Expect</code> header value on the request. + * + * @param value The new value for the header. + * @return This object (for method chaining). + */ + public MockServletRequest expect(Object value) { + return header("Expect", value); + } + + /** + * Specifies the <code>From</code> header value on the request. + * + * @param value The new value for the header. + * @return This object (for method chaining). + */ + public MockServletRequest from(Object value) { + return header("From", value); + } + + /** + * Specifies the <code>Host</code> header value on the request. + * + * @param value The new value for the header. + * @return This object (for method chaining). + */ + public MockServletRequest host(Object value) { + return header("Host", value); + } + + /** + * Specifies the <code>If-Match</code> header value on the request. + * + * @param value The new value for the header. + * @return This object (for method chaining). + */ + public MockServletRequest ifMatch(Object value) { + return header("If-Match", value); + } + + /** + * Specifies the <code>If-Modified-Since</code> header value on the request. + * + * @param value The new value for the header. + * @return This object (for method chaining). + */ + public MockServletRequest ifModifiedSince(Object value) { + return header("If-Modified-Since", value); + } + + /** + * Specifies the <code>If-None-Match</code> header value on the request. + * + * @param value The new value for the header. + * @return This object (for method chaining). + */ + public MockServletRequest ifNoneMatch(Object value) { + return header("If-None-Match", value); + } + + /** + * Specifies the <code>If-Range</code> header value on the request. + * + * @param value The new value for the header. + * @return This object (for method chaining). + */ + public MockServletRequest ifRange(Object value) { + return header("If-Range", value); + } + + /** + * Specifies the <code>If-Unmodified-Since</code> header value on the request. + * + * @param value The new value for the header. + * @return This object (for method chaining). + */ + public MockServletRequest ifUnmodifiedSince(Object value) { + return header("If-Unmodified-Since", value); + } + + /** + * Specifies the <code>Max-Forwards</code> header value on the request. + * + * @param value The new value for the header. + * @return This object (for method chaining). + */ + public MockServletRequest maxForwards(Object value) { + return header("Max-Forwards", value); + } + + /** + * Specifies the <code>Pragma</code> header value on the request. + * + * @param value The new value for the header. + * @return This object (for method chaining). + */ + public MockServletRequest pragma(Object value) { + return header("Pragma", value); + } + + /** + * Specifies the <code>Proxy-Authorization</code> header value on the request. + * + * @param value The new value for the header. + * @return This object (for method chaining). + */ + public MockServletRequest proxyAuthorization(Object value) { + return header("Proxy-Authorization", value); + } + + /** + * Specifies the <code>Range</code> header value on the request. + * + * @param value The new value for the header. + * @return This object (for method chaining). + */ + public MockServletRequest range(Object value) { + return header("Range", value); + } + + /** + * Specifies the <code>Referer</code> header value on the request. + * + * @param value The new value for the header. + * @return This object (for method chaining). + */ + public MockServletRequest referer(Object value) { + return header("Referer", value); + } + + /** + * Specifies the <code>TE</code> header value on the request. + * + * @param value The new value for the header. + * @return This object (for method chaining). + */ + public MockServletRequest te(Object value) { + return header("TE", value); + } + + /** + * Specifies the <code>Upgrade</code> header value on the request. + * + * @param value The new value for the header. + * @return This object (for method chaining). + */ + public MockServletRequest upgrade(Object value) { + return header("Upgrade", value); + } + + /** + * Specifies the <code>User-Agent</code> header value on the request. + * + * @param value The new value for the header. + * @return This object (for method chaining). + */ + public MockServletRequest userAgent(Object value) { + return header("User-Agent", value); + } + + /** + * Specifies the <code>Warning</code> header value on the request. + * + * @param value The new value for the header. + * @return This object (for method chaining). + */ + public MockServletRequest warning(Object value) { + return header("Warning", value); } } diff --git a/juneau-rest/juneau-rest-server/src/test/java/org/apache/juneau/rest/headers/HeadersTest.java b/juneau-rest/juneau-rest-server/src/test/java/org/apache/juneau/rest/headers/HeadersTest.java new file mode 100644 index 0000000..28cc9b2 --- /dev/null +++ b/juneau-rest/juneau-rest-server/src/test/java/org/apache/juneau/rest/headers/HeadersTest.java @@ -0,0 +1,623 @@ +// *************************************************************************************************************************** +// * 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.juneau.rest.headers; + +import static org.apache.juneau.http.HttpMethodName.*; + +import org.apache.juneau.*; +import org.apache.juneau.encoders.*; +import org.apache.juneau.http.*; +import org.apache.juneau.plaintext.*; +import org.apache.juneau.rest.*; +import org.apache.juneau.rest.annotation.*; +import org.apache.juneau.rest.mock.*; +import org.junit.*; +import org.junit.runners.*; + +/** + * Tests various aspects of headers in general. + */ +@SuppressWarnings({"javadoc"}) +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +public class HeadersTest { + + //==================================================================================================== + // HTTP 1.1 headers + //==================================================================================================== + + @RestResource( + serializers=AnythingSerializer.class, + parsers=AnythingParser.class, + encoders=AnythingEncoder.class + ) + public static class A { + + @RestMethod(name=GET, path="/accept") + public String accept(Accept accept) { + return accept.toString(); + } + @RestMethod(name=GET, path="/acceptCharset") + public String acceptCharset(AcceptCharset acceptCharset) { + return acceptCharset.toString(); + } + @RestMethod(name=GET, path="/acceptEncoding") + public String acceptEncoding(AcceptEncoding acceptEncoding) { + return acceptEncoding.toString(); + } + @RestMethod(name=GET, path="/acceptLanguage") + public String acceptLanguage(AcceptLanguage acceptLanguage) { + return acceptLanguage.toString(); + } + @RestMethod(name=GET, path="/authorization") + public String authorization(Authorization authorization) { + return authorization.toString(); + } + @RestMethod(name=GET, path="/cacheControl") + public String cacheControl(CacheControl cacheControl) { + return cacheControl.toString(); + } + @RestMethod(name=GET, path="/connection") + public String connection(Connection connection) { + return connection.toString(); + } + @RestMethod(name=GET, path="/contentLength") + public String contentLength(ContentLength contentLength) { + return contentLength.toString(); + } + @RestMethod(name=GET, path="/contentType") + public String contentType(ContentType contentType) { + return contentType.toString(); + } + @RestMethod(name=GET, path="/date") + public String date(org.apache.juneau.http.Date date) { + return date.toString(); + } + @RestMethod(name=GET, path="/expect") + public String expect(Expect expect) { + return expect.toString(); + } + @RestMethod(name=GET, path="/from") + public String from(From from) { + return from.toString(); + } + @RestMethod(name=GET, path="/host") + public String host(Host host) { + return host.toString(); + } + @RestMethod(name=GET, path="/ifMatch") + public String IfMatch(IfMatch ifMatch) { + return ifMatch.toString(); + } + @RestMethod(name=GET, path="/ifModifiedSince") + public String ifModifiedSince(IfModifiedSince ifModifiedSince) { + return ifModifiedSince.toString(); + } + @RestMethod(name=GET, path="/ifNoneMatch") + public String ifNoneMatch(IfNoneMatch ifNoneMatch) { + return ifNoneMatch.toString(); + } + @RestMethod(name=GET, path="/ifRange") + public String ifRange(IfRange ifRange) { + return ifRange.toString(); + } + @RestMethod(name=GET, path="/ifUnmodifiedSince") + public String ifUnmodifiedSince(IfUnmodifiedSince ifUnmodifiedSince) { + return ifUnmodifiedSince.toString(); + } + @RestMethod(name=GET, path="/maxForwards") + public String maxForwards(MaxForwards maxForwards) { + return maxForwards.toString(); + } + @RestMethod(name=GET, path="/pragma") + public String pragma(Pragma pragma) { + return pragma.toString(); + } + @RestMethod(name=GET, path="/proxyAuthorization") + public String proxyAuthorization(ProxyAuthorization proxyAuthorization) { + return proxyAuthorization.toString(); + } + @RestMethod(name=GET, path="/range") + public String range(Range range) { + return range.toString(); + } + @RestMethod(name=GET, path="/referer") + public String referer(Referer referer) { + return referer.toString(); + } + @RestMethod(name=GET, path="/te") + public String te(TE te) { + return te.toString(); + } + @RestMethod(name=GET, path="/upgrade") + public String upgrade(Upgrade upgrade) { + return upgrade.toString(); + } + @RestMethod(name=GET, path="/userAgent") + public String userAgent(UserAgent userAgent) { + return userAgent.toString(); + } + @RestMethod(name=GET, path="/warning") + public String warning(Warning warning) { + return warning.toString(); + } + } + private static MockRest a = MockRest.create(A.class); + + public static class AnythingSerializer extends PlainTextSerializer { + public AnythingSerializer(PropertyStore ps) { + super(ps, "text/plain", "*/*"); + } + } + + public static class AnythingParser extends PlainTextParser { + public AnythingParser(PropertyStore ps) { + super(ps, "*/*"); + } + } + + public static class AnythingEncoder extends IdentityEncoder { + @Override /* ConfigEncoder */ + public String[] getCodings() { + return new String[]{"*"}; + } + } + + @Test + public void a01a_accept() throws Exception { + a.request("GET", "/accept").accept("text/foo").execute().assertBody("text/foo"); + a.request("GET", "/accept").accept("text/foo+bar").execute().assertBody("text/foo+bar"); + a.request("GET", "/accept").accept("text/*").execute().assertBody("text/*"); + a.request("GET", "/accept").accept("*/foo").execute().assertBody("*/foo"); + } + @Test + public void a01b_accept_qValues() throws Exception { + a.request("GET", "/accept").accept("text/foo;q=1.0").execute().assertBody("text/foo"); + a.request("GET", "/accept").accept("text/foo;q=0.9").execute().assertBody("text/foo;q=0.9"); + a.request("GET", "/accept").accept("text/foo;x=X;q=0.9;y=Y").execute().assertBody("text/foo;x=X;q=0.9;y=Y"); + } + @Test + public void a01b_accept_query() throws Exception { + a.request("GET", "/accept?Accept=text/foo").execute().assertBody("text/foo"); + } + @Test + public void a02a_acceptCharset() throws Exception { + a.request("GET", "/acceptCharset").acceptCharset("UTF-8").execute().assertBody("UTF-8"); + } + @Test + public void a02b_acceptCharset_query() throws Exception { + a.request("GET", "/acceptCharset?Accept-Charset=UTF-8").execute().assertBody("UTF-8"); + } + @Test + public void a03a_acceptEncoding() throws Exception { + a.request("GET", "/acceptEncoding").acceptEncoding("foo").execute().assertBody("foo"); + a.request("GET", "/acceptEncoding").acceptEncoding("*").execute().assertBody("*"); + } + @Test + public void a03b_acceptEncoding_query() throws Exception { + a.request("GET", "/acceptEncoding?Accept-Encoding=*").execute().assertBody("*"); + } + @Test + public void a04a_acceptLanguage() throws Exception { + a.request("GET", "/acceptLanguage").acceptLanguage("foo").execute().assertBody("foo"); + } + @Test + public void a04b_acceptLanguage_query() throws Exception { + a.request("GET", "/acceptLanguage?Accept-Language=foo").acceptLanguage("foo").execute().assertBody("foo"); + } + @Test + public void a05a_authorization() throws Exception { + a.request("GET", "/authorization").authorization("foo").execute().assertBody("foo"); + } + @Test + public void a05b_authorization_query() throws Exception { + a.request("GET", "/authorization?Authorization=foo").execute().assertBody("foo"); + } + @Test + public void a06a_cacheControl() throws Exception { + a.request("GET", "/cacheControl").cacheControl("foo").execute().assertBody("foo"); + } + @Test + public void a06b_cacheControl_query() throws Exception { + a.request("GET", "/cacheControl?Cache-Control=foo").execute().assertBody("foo"); + } + @Test + public void a07a_connection() throws Exception { + a.request("GET", "/connection").connection("foo").execute().assertBody("foo"); + } + @Test + public void a07b_connection_query() throws Exception { + a.request("GET", "/connection?Connection=foo").execute().assertBody("foo"); + } + @Test + public void a08a_contentLength() throws Exception { + a.request("GET", "/contentLength").contentLength(0).execute().assertBody("0"); + } + @Test + public void a08b_contentLength_query() throws Exception { + a.request("GET", "/contentLength?Content-Length=0").execute().assertBody("0"); + } + @Test + public void a09a_contentType() throws Exception { + a.request("GET", "/contentType").contentType("text/foo").execute().assertBody("text/foo"); + } + @Test + public void a09b_contentType_query() throws Exception { + a.request("GET", "/contentType?Content-Type=text/foo").execute().assertBody("text/foo"); + } + @Test + public void a10a_date() throws Exception { + a.request("GET", "/date").date("foo").execute().assertBody("foo"); + } + @Test + public void a10b_date_query() throws Exception { + a.request("GET", "/date?Date=foo").execute().assertBody("foo"); + } + @Test + public void a11a_expect() throws Exception { + a.request("GET", "/expect").expect("100-continue").execute().assertBody("100-continue"); + } + @Test + public void a11b_expect_query() throws Exception { + a.request("GET", "/expect?Expect=100-continue").execute().assertBody("100-continue"); + } + @Test + public void a12a_from() throws Exception { + a.request("GET", "/from").from("foo").execute().assertBody("foo"); + } + public void a12b_from_query() throws Exception { + a.request("GET", "/from?From=foo").execute().assertBody("foo"); + } + @Test + public void a13a_host() throws Exception { + a.request("GET", "/host").host("localhost").execute().assertBody("localhost"); + } + @Test + public void a13b_host_query() throws Exception { + a.request("GET", "/host?Host=localhost").execute().assertBody("localhost"); + } + @Test + public void a14a_ifMatch() throws Exception { + a.request("GET", "/ifMatch").ifMatch("foo").execute().assertBody("\"foo\""); + a.request("GET", "/ifMatch").ifMatch("\"foo\"").execute().assertBody("\"foo\""); + a.request("GET", "/ifMatch").ifMatch("W/\"foo\"").execute().assertBody("W/\"foo\""); + a.request("GET", "/ifMatch").ifMatch("W/\"foo\",\"bar\"").execute().assertBody("W/\"foo\", \"bar\""); + } + @Test + public void a14b_ifMatch_query() throws Exception { + a.request("GET", "/ifMatch?If-Match=foo").execute().assertBody("\"foo\""); + } + @Test + public void a15a_ifModifiedSince() throws Exception { + a.request("GET", "/ifModifiedSince").ifModifiedSince("foo").execute().assertBody("foo"); + } + @Test + public void a15b_ifModifiedSince_query() throws Exception { + a.request("GET", "/ifModifiedSince?If-Modified-Since=foo").execute().assertBody("foo"); + } + @Test + public void a16a_ifNoneMatch() throws Exception { + a.request("GET", "/ifNoneMatch").ifNoneMatch("foo").execute().assertBody("\"foo\""); + a.request("GET", "/ifNoneMatch").ifNoneMatch("\"foo\"").execute().assertBody("\"foo\""); + a.request("GET", "/ifNoneMatch").ifNoneMatch("W/\"foo\"").execute().assertBody("W/\"foo\""); + a.request("GET", "/ifNoneMatch").ifNoneMatch("W/\"foo\",\"bar\"").execute().assertBody("W/\"foo\", \"bar\""); + } + @Test + public void a16b_ifNoneMatch_query() throws Exception { + a.request("GET", "/ifNoneMatch?If-None-Match=foo").execute().assertBody("\"foo\""); + } + @Test + public void a17a_ifRange() throws Exception { + a.request("GET", "/ifRange").ifRange("foo").execute().assertBody("foo"); + } + @Test + public void a17b_ifRange_query() throws Exception { + a.request("GET", "/ifRange?If-Range=foo").execute().assertBody("foo"); + } + @Test + public void a18a_ifUnmodifiedSince() throws Exception { + a.request("GET", "/ifUnmodifiedSince").ifUnmodifiedSince("foo").execute().assertBody("foo"); + } + @Test + public void a18b_ifUnmodifiedSince_query() throws Exception { + a.request("GET", "/ifUnmodifiedSince?If-Unmodified-Since=foo").execute().assertBody("foo"); + } + @Test + public void a19a_maxForwards() throws Exception { + a.request("GET", "/maxForwards").maxForwards(123).execute().assertBody("123"); + } + @Test + public void a19b_maxForwards_query() throws Exception { + a.request("GET", "/maxForwards?Max-Forwards=123").execute().assertBody("123"); + } + @Test + public void a20a_pragma() throws Exception { + a.request("GET", "/pragma").pragma("foo").execute().assertBody("foo"); + } + @Test + public void a20b_pragma_query() throws Exception { + a.request("GET", "/pragma?Pragma=foo").execute().assertBody("foo"); + } + @Test + public void a21a_proxyAuthorization() throws Exception { + a.request("GET", "/proxyAuthorization").proxyAuthorization("foo").execute().assertBody("foo"); + } + @Test + public void a21b_proxyAuthorization_query() throws Exception { + a.request("GET", "/proxyAuthorization?Proxy-Authorization=foo").execute().assertBody("foo"); + } + @Test + public void a22a_range() throws Exception { + a.request("GET", "/range").range("foo").execute().assertBody("foo"); + } + @Test + public void a22b_range_query() throws Exception { + a.request("GET", "/range?Range=foo").execute().assertBody("foo"); + } + @Test + public void a23a_referer() throws Exception { + a.request("GET", "/referer").referer("foo").execute().assertBody("foo"); + } + @Test + public void a23b_referer_query() throws Exception { + a.request("GET", "/referer?Referer=foo").execute().assertBody("foo"); + } + @Test + public void a24a_te() throws Exception { + a.request("GET", "/te").te("foo").execute().assertBody("foo"); + } + @Test + public void a24b_te_query() throws Exception { + a.request("GET", "/te?TE=foo").execute().assertBody("foo"); + } + @Test + public void a25a_upgrade() throws Exception { + a.request("GET", "/upgrade").upgrade("foo").execute().assertBody("foo"); + } + @Test + public void a25b_upgrade_query() throws Exception { + a.request("GET", "/upgrade?Upgrade=foo").execute().assertBody("foo"); + } + @Test + public void a26a_userAgent() throws Exception { + a.request("GET", "/userAgent").userAgent("foo").execute().assertBody("foo"); + } + @Test + public void a26b_userAgent_query() throws Exception { + a.request("GET", "/userAgent?User-Agent=foo").execute().assertBody("foo"); + } + @Test + public void a27a_warning() throws Exception { + a.request("GET", "/warning").warning("foo").execute().assertBody("foo"); + } + @Test + public void a27b_warning_query() throws Exception { + a.request("GET", "/warning?Warning=foo").execute().assertBody("foo"); + } + + //==================================================================================================== + // Custom header. + //==================================================================================================== + + @RestResource(paramResolvers=CustomHeaderParam.class) + public static class B { + @RestMethod(name=GET) + public String customHeader(CustomHeader customHeader) { + return customHeader.toString(); + } + } + static MockRest b = MockRest.create(B.class); + + public static class CustomHeaderParam extends RestMethodParam { + public CustomHeaderParam() { + super(RestParamType.HEADER, "Custom", CustomHeader.class); + } + @Override + public Object resolve(RestRequest req, RestResponse res) throws Exception { + return new CustomHeader(req.getHeader("Custom")); + } + } + + public static class CustomHeader { + public String value; + public CustomHeader(String value) { + this.value = value; + } + @Override + public String toString() { + return value; + } + } + + @Test + public void b01a_customHeader() throws Exception { + b.request("GET", "/").header("Custom", "foo").execute().assertBody("foo"); + } + @Test + public void b01b_customHeader_query() throws Exception { + b.request("GET", "?Custom=foo").execute().assertBody("foo"); + } + + //==================================================================================================== + // Default values - Default request headers + //==================================================================================================== + + @RestResource + public static class C { + @RestMethod(name=GET, defaultRequestHeaders={"H1:1","H2=2"," H3 : 3 "}) + public ObjectMap c(RequestHeaders headers) { + return new ObjectMap() + .append("h1", headers.getString("H1")) + .append("h2", headers.getString("H2")) + .append("h3", headers.getString("H3")); + } + } + static MockRest c = MockRest.create(C.class); + + @Test + public void c01_defaultRequestHeaders_default() throws Exception { + c.request("GET", "/").execute().assertBody("{h1:'1',h2:'2',h3:'3'}"); + } + @Test + public void c02_defaultRequestHeaders_override() throws Exception { + c.request("GET", "/").header("H1",4).header("H2",5).header("H3",6).execute().assertBody("{h1:'4',h2:'5',h3:'6'}"); + } + @Test + public void c03_defaultRequestHeaders_override_caseInsensitive() throws Exception { + c.request("GET", "/").header("h1",4).header("h2",5).header("h3",6).execute().assertBody("{h1:'4',h2:'5',h3:'6'}"); + } + + //==================================================================================================== + // Default values - Default request headers, case-insensitive matching + //==================================================================================================== + + @RestResource + public static class D { + @RestMethod(name=GET, defaultRequestHeaders={"H1:1","H2=2"," H3 : 3 "}) + public ObjectMap d(RequestHeaders headers) { + return new ObjectMap() + .append("h1", headers.getString("h1")) + .append("h2", headers.getString("h2")) + .append("h3", headers.getString("h3")); + } + } + static MockRest d = MockRest.create(D.class); + + @Test + public void d01_defaultRequestHeadersCaseInsensitive_default() throws Exception { + d.request("GET", "/").execute().assertBody("{h1:'1',h2:'2',h3:'3'}"); + } + @Test + public void d02_defaultRequestHeadersCaseInsensitive_override() throws Exception { + d.request("GET", "/").header("H1",4).header("H2",5).header("H3",6).execute().assertBody("{h1:'4',h2:'5',h3:'6'}"); + } + @Test + public void d03_defaultRequestHeadersCaseInsensitive_override_caseInsensitive() throws Exception { + d.request("GET", "/").header("h1",4).header("h2",5).header("h3",6).execute().assertBody("{h1:'4',h2:'5',h3:'6'}"); + } + + //==================================================================================================== + // Default values - Annotated headers. + //==================================================================================================== + + @RestResource + public static class E { + @RestMethod(name=GET) + public ObjectMap e(@Header("H1") String h1, @Header("H2") String h2, @Header("H3") String h3) { + return new ObjectMap() + .append("h1", h1) + .append("h2", h2) + .append("h3", h3); + } + } + static MockRest e = MockRest.create(E.class); + + @Test + public void e01_annotatedHeaders_default() throws Exception { + e.request("GET", "/").execute().assertBody("{h1:null,h2:null,h3:null}"); + } + @Test + public void e02_annotatedHeaders_override() throws Exception { + e.request("GET", "/").header("H1",4).header("H2",5).header("H3",6).execute().assertBody("{h1:'4',h2:'5',h3:'6'}"); + } + @Test + public void e03_annotatedHeaders_override_caseInsensitive() throws Exception { + e.request("GET", "/").header("h1",4).header("h2",5).header("h3",6).execute().assertBody("{h1:'4',h2:'5',h3:'6'}"); + } + + //==================================================================================================== + // Default values - Annotated headers, case-insensitive matching. + //==================================================================================================== + + @RestResource + public static class F { + @RestMethod(name=GET) + public ObjectMap f(@Header("h1") String h1, @Header("h2") String h2, @Header("h3") String h3) { + return new ObjectMap() + .append("h1", h1) + .append("h2", h2) + .append("h3", h3); + } + } + static MockRest f = MockRest.create(F.class); + + @Test + public void f01_annotatedHeadersCaseInsensitive_default() throws Exception { + f.request("GET", "/").execute().assertBody("{h1:null,h2:null,h3:null}"); + } + @Test + public void f02_annotatedHeadersCaseInsensitive_override() throws Exception { + f.request("GET", "/").header("H1",4).header("H2",5).header("H3",6).execute().assertBody("{h1:'4',h2:'5',h3:'6'}"); + } + @Test + public void f03_annotatedHeadersCaseInsensitive_override_caseInsensitive() throws Exception { + f.request("GET", "/").header("h1",4).header("h2",5).header("h3",6).execute().assertBody("{h1:'4',h2:'5',h3:'6'}"); + } + + //==================================================================================================== + // Default values - Annotated headers with default values. + //==================================================================================================== + + @RestResource + public static class G { + @RestMethod(name=GET) + public ObjectMap g(@Header(value="h1",_default="1") String h1, @Header(value="h2",_default="2") String h2, @Header(value="h3",_default="3") String h3) { + return new ObjectMap() + .append("h1", h1) + .append("h2", h2) + .append("h3", h3); + } + } + static MockRest g = MockRest.create(G.class); + + @Test + public void g01_annotatedHeadersDefault_default() throws Exception { + g.request("GET", "/").execute().assertBody("{h1:'1',h2:'2',h3:'3'}"); + } + @Test + public void g02_annotatedHeadersDefault_override() throws Exception { + g.request("GET", "/").header("H1",4).header("H2",5).header("H3",6).execute().assertBody("{h1:'4',h2:'5',h3:'6'}"); + } + @Test + public void g03_annotatedHeadersDefault_override_caseInsensitive() throws Exception { + g.request("GET", "/").header("h1",4).header("h2",5).header("h3",6).execute().assertBody("{h1:'4',h2:'5',h3:'6'}"); + } + + //==================================================================================================== + // Default values - Annotated headers with default values and default request headers. + //==================================================================================================== + + @RestResource + public static class H { + @RestMethod(name=GET, defaultRequestHeaders={"H1:1","H2=2"," H3 : 3 "}) + public ObjectMap h(@Header(value="h1",_default="4") String h1, @Header(value="h2",_default="5") String h2, @Header(value="h3",_default="6") String h3) { + return new ObjectMap() + .append("h1", h1) + .append("h2", h2) + .append("h3", h3); + } + } + static MockRest h = MockRest.create(H.class); + + @Test + public void h01_annotatedAndDefaultHeaders_default() throws Exception { + h.request("GET", "/").execute().assertBody("{h1:'4',h2:'5',h3:'6'}"); + } + @Test + public void h02_annotatedAndDefaultHeaders_override() throws Exception { + h.request("GET", "/").header("H1",7).header("H2",8).header("H3",9).execute().assertBody("{h1:'7',h2:'8',h3:'9'}"); + } + @Test + public void h03_annotatedAndDefaultHeaders_override_caseInsensitive() throws Exception { + h.request("GET", "/").header("h1",7).header("h2",8).header("h3",9).execute().assertBody("{h1:'7',h2:'8',h3:'9'}"); + } +} -- To stop receiving notification emails like this one, please contact jamesbog...@apache.org.