forked from vert-x3/vertx-web
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added tests for combinations and fixes.
Signed-off-by: Paulo Lopes <[email protected]>
- Loading branch information
Showing
3 changed files
with
75 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
vertx-web/src/test/java/io/vertx/ext/web/handler/ChainAuthMixHandlerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package io.vertx.ext.web.handler; | ||
|
||
import io.vertx.core.AsyncResult; | ||
import io.vertx.core.Future; | ||
import io.vertx.core.Handler; | ||
import io.vertx.core.http.HttpMethod; | ||
import io.vertx.core.json.JsonObject; | ||
import io.vertx.ext.auth.User; | ||
import io.vertx.ext.web.RoutingContext; | ||
import io.vertx.ext.web.WebTestBase; | ||
import io.vertx.ext.web.handler.impl.AuthenticationHandlerImpl; | ||
import io.vertx.ext.web.handler.impl.HttpStatusException; | ||
import io.vertx.ext.web.sstore.LocalSessionStore; | ||
import org.junit.Test; | ||
|
||
public class ChainAuthMixHandlerTest extends WebTestBase { | ||
|
||
private AuthenticationHandler success = new AuthenticationHandlerImpl((authInfo, resultHandler) -> resultHandler.handle(Future.succeededFuture(User.create(new JsonObject())))) { | ||
@Override | ||
public void parseCredentials(RoutingContext context, Handler<AsyncResult<JsonObject>> handler) { | ||
handler.handle(Future.succeededFuture(new JsonObject())); | ||
} | ||
}; | ||
|
||
private AuthenticationHandler failure = new AuthenticationHandlerImpl((authInfo, resultHandler) -> resultHandler.handle(Future.failedFuture("Oops!"))) { | ||
@Override | ||
public void parseCredentials(RoutingContext context, Handler<AsyncResult<JsonObject>> handler) { | ||
handler.handle(Future.failedFuture(new HttpStatusException(401))); | ||
} | ||
}; | ||
|
||
@Test | ||
public void testFailOrFailOrSuccess() throws Exception { | ||
|
||
// (failure OR (failure OR success)) | ||
ChainAuthHandler chain = | ||
ChainAuthHandler.any() | ||
.add(failure) | ||
.add( | ||
ChainAuthHandler.any() | ||
.add(failure) | ||
.add(success) | ||
); | ||
|
||
|
||
router.route().handler(SessionHandler.create(LocalSessionStore.create(vertx))); | ||
router.route().handler(chain); | ||
router.route().handler(ctx -> ctx.response().end()); | ||
|
||
testRequest(HttpMethod.GET, "/", 200, "OK"); | ||
} | ||
|
||
@Test | ||
public void testFailOrSuccessAndFail() throws Exception { | ||
|
||
// (failure OR (sucess AND failure)) | ||
ChainAuthHandler chain = | ||
ChainAuthHandler.any() | ||
.add(failure) | ||
.add( | ||
ChainAuthHandler.all() | ||
.add(success) | ||
.add(failure) | ||
); | ||
|
||
|
||
router.route().handler(SessionHandler.create(LocalSessionStore.create(vertx))); | ||
router.route().handler(chain); | ||
router.route().handler(ctx -> ctx.response().end()); | ||
|
||
testRequest(HttpMethod.GET, "/", 401, "Unauthorized"); | ||
} | ||
} |