/*
* 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.clerezza.triaxrs.blackbox;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.Map;
import javax.ws.rs.GET;
import javax.ws.rs.HttpMethod;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import org.apache.clerezza.triaxrs.JaxRsHandler;
import org.apache.clerezza.triaxrs.mock.RequestImpl;
import org.apache.clerezza.triaxrs.mock.RequestURIImpl;
import org.apache.clerezza.triaxrs.mock.ResponseImpl;
import org.apache.clerezza.triaxrs.testutils.HandlerCreator;
import org.junit.Assert;
import org.junit.Test;
import org.wymiwyg.wrhapi.HeaderName;
import org.wymiwyg.wrhapi.Method;
import org.wymiwyg.wrhapi.RequestURI;
public class AutoGeneratedOptionsTest {
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@HttpMethod("PROPFIND")
private @interface PROPFIND {
}
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@HttpMethod("FOO")
private @interface TEST {
}
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@HttpMethod("BAR")
private @interface TEST2 {
}
@Path("/")
public static class MyResource {
@PROPFIND
@Produces("*/*")
public void propfind() {
}
@POST
public void postIt() {
}
@TEST
public void fooIt() {
}
@GET
@Path("sub")
public void sub() {
}
}
@Path("/somepath")
public static class MyResource2 {
@TEST2
@Produces("*/*")
public void propfind() {
}
}
@Test
public void testResponseToOptionsRequest() throws Exception {
JaxRsHandler handler = HandlerCreator.getHandler(MyResource.class, MyResource2.class);
RequestURIImpl uri = new RequestURIImpl();
RequestImpl request = new RequestImpl();
ResponseImpl response = new ResponseImpl();
uri.setPath("/");
request.setRequestURI(uri);
request.setMethod(Method.OPTIONS);
handler.handle(request, response);
Map<HeaderName,String[]> headers = response.getHeaders();
String[] allowHeader = headers.get(HeaderName.ALLOW);
Assert.assertNotNull(allowHeader);
String allow = "";
for(String st : allowHeader){
allow += st;
}
Assert.assertTrue(allow.contains("PROPFIND"));
Assert.assertFalse(allow.contains("GET"));
Assert.assertTrue(allow.contains("POST"));
Assert.assertTrue(allow.contains("FOO"));
}
@Test
public void testResponseToOptionsRequestOnSubresource() throws Exception {
JaxRsHandler handler = HandlerCreator.getHandler(MyResource.class);
RequestURIImpl uri = new RequestURIImpl();
RequestImpl request = new RequestImpl();
ResponseImpl response = new ResponseImpl();
uri.setPath("/sub");
request.setRequestURI(uri);
request.setMethod(Method.OPTIONS);
handler.handle(request, response);
Map<HeaderName,String[]> headers = response.getHeaders();
String[] allowHeader = headers.get(HeaderName.ALLOW);
Assert.assertNotNull(allowHeader);
String allow = "";
for(String st : allowHeader){
allow += st;
}
Assert.assertFalse(allow.contains("PROPFIND"));
Assert.assertTrue(allow.contains("GET"));
Assert.assertFalse(allow.contains("POST"));
Assert.assertFalse(allow.contains("FOO"));
}
@Test
public void testResponseToOptionsRequestWithNoResource() throws Exception {
JaxRsHandler handler = HandlerCreator.getHandler(MyResource.class, MyResource2.class);
RequestURIImpl uri = new RequestURIImpl();
RequestImpl request = new RequestImpl();
ResponseImpl response = new ResponseImpl();
uri.setType(RequestURI.Type.NO_RESOURCE);
request.setRequestURI(uri);
request.setMethod(Method.OPTIONS);
handler.handle(request, response);
Map<HeaderName,String[]> headers = response.getHeaders();
String[] allowHeader = headers.get(HeaderName.ALLOW);
Assert.assertNotNull(allowHeader);
String allow = "";
for(String st : allowHeader){
allow += st;
}
Assert.assertTrue(allow.contains("PROPFIND"));
Assert.assertTrue(allow.contains("GET"));
Assert.assertTrue(allow.contains("POST"));
Assert.assertTrue(allow.contains("FOO"));
Assert.assertTrue(allow.contains("BAR"));
}
}