Skip to content

Commit

Permalink
headers - Introduced new @RequestHeader annotation, considered as an …
Browse files Browse the repository at this point in the history
…alias for @param(kind=REQUEST_HEADER)
  • Loading branch information
fcamblor committed Sep 1, 2017
1 parent c61b4db commit 43be8b6
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 9 deletions.
Expand Up @@ -197,10 +197,18 @@ private void buildResourceMethodParams(ResourceMethodAnnotation annotation, Reso
Set<String> pathParamNamesToMatch = Sets.newHashSet(resourceMethod.pathParamNames);
for (VariableElement p : annotation.methodElem.getParameters()) {
Param param = p.getAnnotation(Param.class);
RequestHeader reqHeaderAnn = p.getAnnotation(RequestHeader.class);
String paramName = p.getSimpleName().toString();
String reqParamName = p.getSimpleName().toString();
ResourceMethodParameterKind parameterKind = null;
if (param != null) {
String reqParamName;
if (param == null) {
if(reqHeaderAnn != null) {
reqParamName = reqHeaderAnn.value().length() != 0?reqHeaderAnn.value():paramName;
parameterKind = ResourceMethodParameterKind.REQUEST_HEADER;
} else {
reqParamName = paramName;
}
} else {
reqParamName = param.value().length() == 0 ? paramName : param.value();
if (param.kind() != Param.Kind.DEFAULT) {
parameterKind = ResourceMethodParameterKind.valueOf(param.kind().name());
Expand Down
5 changes: 5 additions & 0 deletions restx-core/src/main/java/restx/annotations/RequestHeader.java
@@ -0,0 +1,5 @@
package restx.annotations;

public @interface RequestHeader {
String value() default "";
}
Expand Up @@ -130,7 +130,7 @@ public DateTime[] optionalArrayedJodaDatesParams(Optional<DateTime[]> params, Op
}

@GET("/params/headers")
public String headerParams(@Param(value = "X-A", kind = Param.Kind.REQUEST_HEADER) String a, @Param(value = "X-B", kind = Param.Kind.REQUEST_HEADER) Optional<DateTime> b) {
return "a=" + a + " b=" + b.orNull();
public String headerParams(@Param(value = "X-A", kind = Param.Kind.REQUEST_HEADER) String a, @RequestHeader("X-B") Optional<DateTime> b, @RequestHeader Optional<String> Date) {
return "a=" + a + " b=" + b.orNull() + " date=" + String.valueOf(Date.orNull());
}
}
Expand Up @@ -30,26 +30,29 @@ public void should_return_header_params() throws Exception {
HttpRequest httpRequest = server.client().authenticatedAs("admin")
.GET("/api/params/headers")
.header("X-A", "aaa")
.header("X-B", "2017-09-01T09:07:46Z");
.header("X-B", "2017-09-01T09:07:46Z")
.header("Date", "Tue, 15 Nov 1994 08:12:31 GMT");

assertThat(httpRequest.code()).isEqualTo(200);
assertThat(httpRequest.body().trim()).isEqualTo("a=aaa b=2017-09-01T09:07:46.000Z");
assertThat(httpRequest.body().trim()).isEqualTo("a=aaa b=2017-09-01T09:07:46.000Z date=Tue, 15 Nov 1994 08:12:31 GMT");
}

@Test
public void should_return_header_params_with_absent_value() throws Exception {
HttpRequest httpRequest = server.client().authenticatedAs("admin")
.GET("/api/params/headers")
.header("X-A", "aaa");
.header("X-A", "aaa")
.header("Date", "Tue, 15 Nov 1994 08:12:31 GMT");

assertThat(httpRequest.code()).isEqualTo(200);
assertThat(httpRequest.body().trim()).isEqualTo("a=aaa b=null");
assertThat(httpRequest.body().trim()).isEqualTo("a=aaa b=null date=Tue, 15 Nov 1994 08:12:31 GMT");
}

@Test
public void should_return_header_params_with_missing_required_value() throws Exception {
HttpRequest httpRequest = server.client().authenticatedAs("admin")
.GET("/api/params/headers");
.GET("/api/params/headers")
.header("Date", "Tue, 15 Nov 1994 08:12:31 GMT");

assertThat(httpRequest.code()).isEqualTo(500);
}
Expand Down

0 comments on commit 43be8b6

Please sign in to comment.