mirror of
https://github.com/google/nomulus.git
synced 2025-05-14 16:37:13 +02:00
Refactor RequestHandler to handle request component construction
This refactors RequestHandler so that it handles the construction of the request component itself, rather than being handed a pre-built request component instance constructed by the invoking servlet. The motivation for this change is so that RequestHandler can be extended in future CLs to compute authentication results, and can provide those results as an available binding in the constructed request component. An alternative approach could have been to compute the authentication results within RequestModule itself, but I think it's clearer to keep business logic like that outside of Dagger providers. This CL makes the following individual changes: - Adds request component builders, which implement a RequestComponentBuilder interface so they can all be manipulated by RequestHandler - Instead of obtaining request components via factory methods on the global components, one now can have global-scoped bindings just inject the request component builders (which requires adding a module to each global component declaring the subcomponent). This follows the recommended approach here: http://google.github.io/dagger/subcomponents.html - Instead of exposing request components on the global component interface, we now expose module-specific subclasses of RequestHandler that @Inject the appropriate request component builder's provider and pass it to the superclass (note that inheritance isn't strictly necessary here but saves boilerplate) - RequestHandler now takes the Provider<RequestComponentBuilder> and builds the component itself using its own fresh RequestModule instance. This provides some nice encapsulation but is mainly needed for adding a RequestAuthModule in future work. - RequestHandler also takes UserService now, which can be provided via Dagger by the subclass. Longer-term that will go away in favor of instead providing AuthStrategy instances, some of which will use UserService internally. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=138815648
This commit is contained in:
parent
5a8ef4f0d6
commit
f742ac8056
18 changed files with 261 additions and 66 deletions
|
@ -28,6 +28,7 @@ import com.google.common.testing.NullPointerTester;
|
|||
import google.registry.request.HttpException.ServiceUnavailableException;
|
||||
import google.registry.testing.AppEngineRule;
|
||||
import google.registry.testing.InjectRule;
|
||||
import google.registry.testing.Providers;
|
||||
import google.registry.testing.UserInfo;
|
||||
import java.io.PrintWriter;
|
||||
import java.io.StringWriter;
|
||||
|
@ -125,6 +126,14 @@ public final class RequestHandlerTest {
|
|||
}
|
||||
}
|
||||
|
||||
/** Fake Builder for the fake component above to satisfy RequestHandler expectations. */
|
||||
public abstract static class Builder implements RequestComponentBuilder<Component, Builder> {
|
||||
@Override
|
||||
public Builder requestModule(RequestModule requestModule) {
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
@Mock
|
||||
private HttpServletRequest req;
|
||||
|
||||
|
@ -148,11 +157,21 @@ public final class RequestHandlerTest {
|
|||
|
||||
private final Component component = new Component();
|
||||
private final StringWriter httpOutput = new StringWriter();
|
||||
private final RequestHandler<Component> handler = RequestHandler.create(Component.class);
|
||||
private RequestHandler<Component, Builder> handler;
|
||||
|
||||
@Before
|
||||
public void before() throws Exception {
|
||||
inject.setStaticField(RequestHandler.class, "userService", userService);
|
||||
// Initialize here, not inline, so that we pick up the mocked UserService.
|
||||
handler = RequestHandler.createForTest(
|
||||
Component.class,
|
||||
Providers.of(new Builder() {
|
||||
@Override
|
||||
public Component build() {
|
||||
// Use a fake Builder that returns the single component instance that uses the mocks.
|
||||
return component;
|
||||
}
|
||||
}),
|
||||
userService);
|
||||
when(rsp.getWriter()).thenReturn(new PrintWriter(httpOutput));
|
||||
}
|
||||
|
||||
|
@ -165,7 +184,7 @@ public final class RequestHandlerTest {
|
|||
public void testHandleRequest_normalRequest_works() throws Exception {
|
||||
when(req.getMethod()).thenReturn("GET");
|
||||
when(req.getRequestURI()).thenReturn("/bumblebee");
|
||||
handler.handleRequest(req, rsp, component);
|
||||
handler.handleRequest(req, rsp);
|
||||
verifyZeroInteractions(rsp);
|
||||
verify(bumblebeeTask).run();
|
||||
}
|
||||
|
@ -174,7 +193,7 @@ public final class RequestHandlerTest {
|
|||
public void testHandleRequest_multipleMethodMappings_works() throws Exception {
|
||||
when(req.getMethod()).thenReturn("POST");
|
||||
when(req.getRequestURI()).thenReturn("/bumblebee");
|
||||
handler.handleRequest(req, rsp, component);
|
||||
handler.handleRequest(req, rsp);
|
||||
verify(bumblebeeTask).run();
|
||||
}
|
||||
|
||||
|
@ -182,7 +201,7 @@ public final class RequestHandlerTest {
|
|||
public void testHandleRequest_prefixEnabled_subpathsWork() throws Exception {
|
||||
when(req.getMethod()).thenReturn("GET");
|
||||
when(req.getRequestURI()).thenReturn("/bumblebee/hive");
|
||||
handler.handleRequest(req, rsp, component);
|
||||
handler.handleRequest(req, rsp);
|
||||
verify(bumblebeeTask).run();
|
||||
}
|
||||
|
||||
|
@ -190,7 +209,7 @@ public final class RequestHandlerTest {
|
|||
public void testHandleRequest_taskHasAutoPrintOk_printsOk() throws Exception {
|
||||
when(req.getMethod()).thenReturn("POST");
|
||||
when(req.getRequestURI()).thenReturn("/sloth");
|
||||
handler.handleRequest(req, rsp, component);
|
||||
handler.handleRequest(req, rsp);
|
||||
verify(slothTask).run();
|
||||
verify(rsp).setContentType("text/plain; charset=utf-8");
|
||||
verify(rsp).getWriter();
|
||||
|
@ -201,7 +220,7 @@ public final class RequestHandlerTest {
|
|||
public void testHandleRequest_prefixDisabled_subpathsReturn404NotFound() throws Exception {
|
||||
when(req.getMethod()).thenReturn("POST");
|
||||
when(req.getRequestURI()).thenReturn("/sloth/nest");
|
||||
handler.handleRequest(req, rsp, component);
|
||||
handler.handleRequest(req, rsp);
|
||||
verify(rsp).sendError(404);
|
||||
}
|
||||
|
||||
|
@ -209,7 +228,7 @@ public final class RequestHandlerTest {
|
|||
public void testHandleRequest_taskThrowsHttpException_getsHandledByHandler() throws Exception {
|
||||
when(req.getMethod()).thenReturn("GET");
|
||||
when(req.getRequestURI()).thenReturn("/fail");
|
||||
handler.handleRequest(req, rsp, component);
|
||||
handler.handleRequest(req, rsp);
|
||||
verify(rsp).sendError(503, "Set sail for fail");
|
||||
}
|
||||
|
||||
|
@ -219,7 +238,7 @@ public final class RequestHandlerTest {
|
|||
throws Exception {
|
||||
when(req.getMethod()).thenReturn("GET");
|
||||
when(req.getRequestURI()).thenReturn("/failAtConstruction");
|
||||
handler.handleRequest(req, rsp, component);
|
||||
handler.handleRequest(req, rsp);
|
||||
verify(rsp).sendError(503, "Fail at construction");
|
||||
}
|
||||
|
||||
|
@ -227,7 +246,7 @@ public final class RequestHandlerTest {
|
|||
public void testHandleRequest_notFound_returns404NotFound() throws Exception {
|
||||
when(req.getMethod()).thenReturn("GET");
|
||||
when(req.getRequestURI()).thenReturn("/bogus");
|
||||
handler.handleRequest(req, rsp, component);
|
||||
handler.handleRequest(req, rsp);
|
||||
verify(rsp).sendError(404);
|
||||
}
|
||||
|
||||
|
@ -235,7 +254,7 @@ public final class RequestHandlerTest {
|
|||
public void testHandleRequest_methodNotAllowed_returns405MethodNotAllowed() throws Exception {
|
||||
when(req.getMethod()).thenReturn("POST");
|
||||
when(req.getRequestURI()).thenReturn("/fail");
|
||||
handler.handleRequest(req, rsp, component);
|
||||
handler.handleRequest(req, rsp);
|
||||
verify(rsp).sendError(405);
|
||||
}
|
||||
|
||||
|
@ -243,7 +262,7 @@ public final class RequestHandlerTest {
|
|||
public void testHandleRequest_insaneMethod_returns405MethodNotAllowed() throws Exception {
|
||||
when(req.getMethod()).thenReturn("FIREAWAY");
|
||||
when(req.getRequestURI()).thenReturn("/fail");
|
||||
handler.handleRequest(req, rsp, component);
|
||||
handler.handleRequest(req, rsp);
|
||||
verify(rsp).sendError(405);
|
||||
}
|
||||
|
||||
|
@ -253,7 +272,7 @@ public final class RequestHandlerTest {
|
|||
public void testHandleRequest_lowercaseMethod_notRecognized() throws Exception {
|
||||
when(req.getMethod()).thenReturn("get");
|
||||
when(req.getRequestURI()).thenReturn("/bumblebee");
|
||||
handler.handleRequest(req, rsp, component);
|
||||
handler.handleRequest(req, rsp);
|
||||
verify(rsp).sendError(405);
|
||||
}
|
||||
|
||||
|
@ -268,7 +287,7 @@ public final class RequestHandlerTest {
|
|||
public void testXsrfProtection_noTokenProvided_returns403Forbidden() throws Exception {
|
||||
when(req.getMethod()).thenReturn("POST");
|
||||
when(req.getRequestURI()).thenReturn("/safe-sloth");
|
||||
handler.handleRequest(req, rsp, component);
|
||||
handler.handleRequest(req, rsp);
|
||||
verify(rsp).sendError(403, "Invalid X-CSRF-Token");
|
||||
}
|
||||
|
||||
|
@ -277,7 +296,7 @@ public final class RequestHandlerTest {
|
|||
when(req.getMethod()).thenReturn("POST");
|
||||
when(req.getHeader("X-CSRF-Token")).thenReturn(generateToken("vampire"));
|
||||
when(req.getRequestURI()).thenReturn("/safe-sloth");
|
||||
handler.handleRequest(req, rsp, component);
|
||||
handler.handleRequest(req, rsp);
|
||||
verify(safeSlothTask).run();
|
||||
}
|
||||
|
||||
|
@ -286,7 +305,7 @@ public final class RequestHandlerTest {
|
|||
when(req.getMethod()).thenReturn("POST");
|
||||
when(req.getHeader("X-CSRF-Token")).thenReturn(generateToken("blood"));
|
||||
when(req.getRequestURI()).thenReturn("/safe-sloth");
|
||||
handler.handleRequest(req, rsp, component);
|
||||
handler.handleRequest(req, rsp);
|
||||
verify(rsp).sendError(403, "Invalid X-CSRF-Token");
|
||||
}
|
||||
|
||||
|
@ -294,7 +313,7 @@ public final class RequestHandlerTest {
|
|||
public void testXsrfProtection_GETMethodWithoutToken_doesntCheckToken() throws Exception {
|
||||
when(req.getMethod()).thenReturn("GET");
|
||||
when(req.getRequestURI()).thenReturn("/safe-sloth");
|
||||
handler.handleRequest(req, rsp, component);
|
||||
handler.handleRequest(req, rsp);
|
||||
verify(safeSlothTask).run();
|
||||
}
|
||||
|
||||
|
@ -304,7 +323,7 @@ public final class RequestHandlerTest {
|
|||
when(userService.createLoginURL("/users-only")).thenReturn("/login");
|
||||
when(req.getMethod()).thenReturn("GET");
|
||||
when(req.getRequestURI()).thenReturn("/users-only");
|
||||
handler.handleRequest(req, rsp, component);
|
||||
handler.handleRequest(req, rsp);
|
||||
verify(rsp).setStatus(302);
|
||||
verify(rsp).setHeader("Location", "/login");
|
||||
}
|
||||
|
@ -314,7 +333,7 @@ public final class RequestHandlerTest {
|
|||
when(userService.isUserLoggedIn()).thenReturn(true);
|
||||
when(req.getMethod()).thenReturn("GET");
|
||||
when(req.getRequestURI()).thenReturn("/users-only");
|
||||
handler.handleRequest(req, rsp, component);
|
||||
handler.handleRequest(req, rsp);
|
||||
verify(usersOnlyAction).run();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue