Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package org.eclipse.jetty.ee10.servlet;

import java.security.Principal;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;

Expand All @@ -40,6 +41,7 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.awaitility.Awaitility.await;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
Expand Down Expand Up @@ -96,6 +98,7 @@ public void testWriteOnDeferredAuthentication() throws Exception
AtomicReference<Boolean> authenticatedRef = new AtomicReference<>();
AtomicReference<Principal> userPrincipalRef = new AtomicReference<>();
AtomicReference<Throwable> servletErrorRef = new AtomicReference<>();
AtomicInteger serviceCount = new AtomicInteger();
startServer(new LoginAuthenticator()
{
@Override
Expand Down Expand Up @@ -145,12 +148,17 @@ protected void service(HttpServletRequest req, HttpServletResponse resp)
{
servletErrorRef.set(t);
}
finally
{
serviceCount.incrementAndGet();
}
}
});

// Authenticator is invoked twice, first time for deferred auth on getUserPrincipal() which tries to write and fails,
// the second time for authenticate() which returns false and sends a 401 response.
String response = _connector.getResponse("GET /public/foo HTTP/1.0\r\n\r\n");
await().atMost(5, TimeUnit.SECONDS).until(() -> serviceCount.get() == 1);
assertThat(response, containsString("401 Unauthorized"));
assertThat(response, containsString("this is a challenge"));
assertThat(authenticatorCount.get(), equalTo(2));
Expand All @@ -161,6 +169,7 @@ protected void service(HttpServletRequest req, HttpServletResponse resp)
// Authenticator is invoked twice, first time for deferred auth on getUserPrincipal() which tries to write and fails,
// the second time for authenticate() which returns true and sends a 200 response.
response = _connector.getResponse("GET /public/foo?authenticate HTTP/1.0\r\n\r\n");
await().atMost(5, TimeUnit.SECONDS).until(() -> serviceCount.get() == 2);
assertThat(response, containsString("200 OK"));
assertThat(response, containsString("success"));
assertThat(authenticatorCount.get(), equalTo(4));
Expand Down
Loading