Skip to content

Commit 12acc5f

Browse files
author
Igor Polevoy
committed
#228 Add OPTIONS HTTP Method
1 parent 7a0711b commit 12acc5f

File tree

5 files changed

+94
-3
lines changed

5 files changed

+94
-3
lines changed

activeweb-testing/src/main/java/org/javalite/activeweb/RequestBuilder.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,16 @@ public void delete(String actionName) {
320320
submitRequest(actionName, HttpMethod.DELETE);
321321
}
322322

323+
/**
324+
* Simulate HTTP OPTIONS call to an action of controller.
325+
*
326+
* @param actionName name of action as on a URL - not CamelCase.
327+
*/
328+
public void options(String actionName) {
329+
realAction = actionName;
330+
submitRequest(actionName, HttpMethod.OPTIONS);
331+
}
332+
323333

324334
private void submitRequest(String actionName, HttpMethod method) {
325335

@@ -361,7 +371,7 @@ private void submitRequest(String actionName, HttpMethod method) {
361371
addCookiesInternal(request);
362372

363373
//this is to fake the PUT and DELETE methods, just like a browser
364-
if(!(method.equals(HttpMethod.GET) || method.equals(HttpMethod.POST))){
374+
if(method.equals(HttpMethod.PUT)){
365375
request.setParameter("_method", method.toString());
366376
request.setMethod("POST");
367377
}else{
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package app.controllers;
2+
3+
import org.javalite.activeweb.AppController;
4+
import org.javalite.activeweb.annotations.OPTIONS;
5+
6+
/**
7+
* @author Igor Polevoy on 6/19/15.
8+
*/
9+
public class OptionsController extends AppController {
10+
11+
@OPTIONS
12+
public void index(){
13+
respond("ok");
14+
}
15+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
Copyright 2009-2014 Igor Polevoy
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
package org.javalite.activeweb;
17+
18+
19+
import org.junit.Test;
20+
21+
22+
/**
23+
* @author Igor Polevoy
24+
*/
25+
public class OptionsMethodSpec extends IntegrationSpec{
26+
27+
28+
@Test
29+
public void shouldRouteToAppropriateDeleteMethod(){
30+
controller("options").integrateViews().options("index");
31+
a(responseContent()).shouldBeEqual("ok");
32+
}
33+
}

activeweb/src/main/java/org/javalite/activeweb/HttpMethod.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
* @author Igor Polevoy
2525
*/
2626
public enum HttpMethod {
27-
GET, POST, PUT, DELETE, HEAD;
27+
GET, POST, PUT, DELETE, HEAD, OPTIONS;
2828

2929
/**
3030
* Detects a method from annotation
@@ -43,8 +43,10 @@ public static HttpMethod method(Annotation annotation){
4343
return DELETE;
4444
}else if (annotation instanceof HEAD) {
4545
return HEAD;
46+
}else if (annotation instanceof OPTIONS) {
47+
return OPTIONS;
4648
}else{
47-
throw new IllegalArgumentException("allowable annotations: @GET, @POST, @PUT, @DELETE, @HEAD, all from 'activeweb.annotations' package.");
49+
throw new IllegalArgumentException("Allowed annotations can be found in 'org.javalite.activeweb.annotations' package.");
4850
}
4951
}
5052

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
Copyright 2009-2014 Igor Polevoy
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
package org.javalite.activeweb.annotations;
17+
18+
import java.lang.annotation.ElementType;
19+
import java.lang.annotation.Retention;
20+
import java.lang.annotation.RetentionPolicy;
21+
import java.lang.annotation.Target;
22+
23+
/**
24+
* Mark an action of a controller with this annotation to receive an HTTP OPTIONS request.
25+
*
26+
* @author Igor Polevoy
27+
*/
28+
29+
@Retention(RetentionPolicy.RUNTIME)
30+
@Target(ElementType.METHOD)
31+
public @interface OPTIONS {}

0 commit comments

Comments
 (0)