Skip to content

Commit

Permalink
Minor API change and a test for the HTTP handler roles setup.
Browse files Browse the repository at this point in the history
  • Loading branch information
nmihajlovski committed Mar 13, 2016
1 parent bef1aec commit 1e33584
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 5 deletions.
Expand Up @@ -49,7 +49,7 @@ public interface RouteConfig {


Set<String> roles(); Set<String> roles();


RouteOptions roles(Set<String> roles); RouteOptions roles(String... roles);


HttpWrapper[] wrappers(); HttpWrapper[] wrappers();


Expand Down
Expand Up @@ -108,7 +108,7 @@ public Set<String> roles() {
} }


@Override @Override
public RouteOptions roles(Set<String> roles) { public RouteOptions roles(String... roles) {
Coll.assign(this.roles, roles); Coll.assign(this.roles, roles);
return this; return this;
} }
Expand All @@ -131,7 +131,7 @@ public RouteOptions copy() {
copy.view(view()); copy.view(view());
copy.mvc(mvc()); copy.mvc(mvc());
copy.transactionMode(transactionMode()); copy.transactionMode(transactionMode());
copy.roles(roles()); copy.roles(roles.toArray(new String[roles.size()]));
copy.wrap(wrappers()); copy.wrap(wrappers());


return copy; return copy;
Expand Down
Expand Up @@ -29,7 +29,6 @@
import org.rapidoid.http.RouteOptions; import org.rapidoid.http.RouteOptions;
import org.rapidoid.http.handler.HttpHandlers; import org.rapidoid.http.handler.HttpHandlers;
import org.rapidoid.lambda.*; import org.rapidoid.lambda.*;
import org.rapidoid.u.U;


import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.Collection; import java.util.Collection;
Expand Down Expand Up @@ -324,7 +323,7 @@ public OnRoute wrap(HttpWrapper... wrappers) {
} }


public OnRoute roles(String... roles) { public OnRoute roles(String... roles) {
options.roles(U.set(roles)); options.roles(roles);
return this; return this;
} }


Expand Down
@@ -0,0 +1,43 @@
package org.rapidoid.http;

/*
* #%L
* rapidoid-integration-tests
* %%
* Copyright (C) 2014 - 2016 Nikolche Mihajlovski and contributors
* %%
* Licensed 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.
* #L%
*/

import org.junit.Test;
import org.rapidoid.annotation.Authors;
import org.rapidoid.annotation.Since;
import org.rapidoid.setup.On;

@Authors("Nikolche Mihajlovski")
@Since("5.1.0")
public class HttpRolesTest extends HttpTestCommons {

@Test
public void testRoles() {
On.defaults().roles("aa", "bb");

On.get("/a").json(() -> "ok");
On.get("/ok").roles().json(() -> "ok");

onlyGet("/a");
onlyGet("/ok");
}

}
@@ -0,0 +1,8 @@
HTTP/1.1 403 Forbidden
Connection: keep-alive
Server: Rapidoid
Date: XXXXX GMT
Content-Type: application/json; charset=utf-8
Content-Length: 85

{"error":"The user doesn't have the required roles!","code":403,"status":"Forbidden"}
@@ -0,0 +1,8 @@
HTTP/1.1 200 OK
Connection: keep-alive
Server: Rapidoid
Date: XXXXX GMT
Content-Type: application/json; charset=utf-8
Content-Length: 4

"ok"

0 comments on commit 1e33584

Please sign in to comment.