Rename WhoisServer and WhoisHttpServer to actions

These servlets are converted to actions during daggerization. Calling them servers are misleading.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=190942237
This commit is contained in:
jianglai 2018-03-29 09:40:44 -07:00
parent 038a2679c7
commit c40eda3235
21 changed files with 170 additions and 185 deletions

View file

@ -852,7 +852,7 @@ public final class RegistryConfig {
/** /**
* Amount of time public HTTP proxies are permitted to cache our WHOIS responses. * Amount of time public HTTP proxies are permitted to cache our WHOIS responses.
* *
* @see google.registry.whois.WhoisHttpServer * @see google.registry.whois.WhoisHttpAction
*/ */
@Provides @Provides
@Config("whoisHttpExpires") @Config("whoisHttpExpires")

View file

@ -42,9 +42,9 @@ import google.registry.ui.server.registrar.RegistrarPaymentAction;
import google.registry.ui.server.registrar.RegistrarPaymentSetupAction; import google.registry.ui.server.registrar.RegistrarPaymentSetupAction;
import google.registry.ui.server.registrar.RegistrarPremiumPriceAckAction; import google.registry.ui.server.registrar.RegistrarPremiumPriceAckAction;
import google.registry.ui.server.registrar.RegistrarSettingsAction; import google.registry.ui.server.registrar.RegistrarSettingsAction;
import google.registry.whois.WhoisHttpServer; import google.registry.whois.WhoisAction;
import google.registry.whois.WhoisHttpAction;
import google.registry.whois.WhoisModule; import google.registry.whois.WhoisModule;
import google.registry.whois.WhoisServer;
/** Dagger component with per-request lifetime for "default" App Engine module. */ /** Dagger component with per-request lifetime for "default" App Engine module. */
@RequestScope @RequestScope
@ -77,8 +77,10 @@ interface FrontendRequestComponent {
RdapIpAction rdapDefaultAction(); RdapIpAction rdapDefaultAction();
RdapNameserverAction rdapNameserverAction(); RdapNameserverAction rdapNameserverAction();
RdapNameserverSearchAction rdapNameserverSearchAction(); RdapNameserverSearchAction rdapNameserverSearchAction();
WhoisHttpServer whoisHttpServer();
WhoisServer whoisServer(); WhoisHttpAction whoisHttpAction();
WhoisAction whoisAction();
@Subcomponent.Builder @Subcomponent.Builder
abstract class Builder implements RequestComponentBuilder<FrontendRequestComponent> { abstract class Builder implements RequestComponentBuilder<FrontendRequestComponent> {

View file

@ -41,20 +41,16 @@ import org.joda.time.DateTime;
* <p>All commands and responses conform to the WHOIS spec as defined in RFC 3912. Commands must be * <p>All commands and responses conform to the WHOIS spec as defined in RFC 3912. Commands must be
* sent via an HTTP POST in the request body. * sent via an HTTP POST in the request body.
* *
* <p>This servlet is meant to serve as a low level interface for the proxy app which forwards us * <p>This action is meant to serve as a low level interface for the proxy app which forwards us
* requests received on port 43. However this interface is technically higher level because it sends * requests received on port 43. However this interface is technically higher level because it sends
* back proper HTTP error codes such as 200, 400, 500, etc. These are discarded by the proxy because * back proper HTTP error codes such as 200, 400, 500, etc. These are discarded by the proxy because
* WHOIS specifies no manner for differentiating successful and erroneous requests. * WHOIS specifies no manner for differentiating successful and erroneous requests.
* *
* @see WhoisHttpServer * @see WhoisHttpAction
* @see <a href="http://www.ietf.org/rfc/rfc3912.txt">RFC 3912: WHOIS Protocol Specification</a> * @see <a href="http://www.ietf.org/rfc/rfc3912.txt">RFC 3912: WHOIS Protocol Specification</a>
*/ */
@Action( @Action(path = "/_dr/whois", method = POST, auth = Auth.AUTH_INTERNAL_OR_ADMIN)
path = "/_dr/whois", public class WhoisAction implements Runnable {
method = POST,
auth = Auth.AUTH_INTERNAL_OR_ADMIN
)
public class WhoisServer implements Runnable {
private static final FormattingLogger logger = FormattingLogger.getLoggerForCallerClass(); private static final FormattingLogger logger = FormattingLogger.getLoggerForCallerClass();
@ -75,7 +71,9 @@ public class WhoisServer implements Runnable {
@Inject @Config("whoisDisclaimer") String disclaimer; @Inject @Config("whoisDisclaimer") String disclaimer;
@Inject WhoisMetric.Builder metricBuilder; @Inject WhoisMetric.Builder metricBuilder;
@Inject WhoisMetrics whoisMetrics; @Inject WhoisMetrics whoisMetrics;
@Inject WhoisServer() {}
@Inject
WhoisAction() {}
@Override @Override
public void run() { public void run() {

View file

@ -48,12 +48,12 @@ import org.joda.time.Duration;
/** /**
* Human-Friendly HTTP WHOIS API * Human-Friendly HTTP WHOIS API
* *
* <p>This API uses easy to understand paths rather than {@link WhoisServer} which requires a POST * <p>This API uses easy to understand paths rather than {@link WhoisAction} which requires a POST
* request containing a WHOIS command. Because the typical WHOIS command is along the lines of * request containing a WHOIS command. Because the typical WHOIS command is along the lines of
* {@code "domain google.lol"} or the equivalent {@code "google.lol}, this servlet is just going to * {@code "domain google.lol"} or the equivalent {@code "google.lol}, this action is just going to
* replace the slashes with spaces and let {@link WhoisReader} figure out what to do. * replace the slashes with spaces and let {@link WhoisReader} figure out what to do.
* *
* <p>This servlet accepts requests from any origin. * <p>This action accepts requests from any origin.
* *
* <p>You can send AJAX requests to our WHOIS API from your <em>very own</em> website using the * <p>You can send AJAX requests to our WHOIS API from your <em>very own</em> website using the
* following embed code: * following embed code:
@ -93,14 +93,10 @@ import org.joda.time.Duration;
* </script> * </script>
* </pre> * </pre>
* *
* @see WhoisServer * @see WhoisAction
*/ */
@Action( @Action(path = WhoisHttpAction.PATH, isPrefix = true, auth = Auth.AUTH_PUBLIC_ANONYMOUS)
path = WhoisHttpServer.PATH, public final class WhoisHttpAction implements Runnable {
isPrefix = true,
auth = Auth.AUTH_PUBLIC_ANONYMOUS
)
public final class WhoisHttpServer implements Runnable {
public static final String PATH = "/whois/"; public static final String PATH = "/whois/";
@ -142,7 +138,9 @@ public final class WhoisHttpServer implements Runnable {
@Inject @RequestPath String requestPath; @Inject @RequestPath String requestPath;
@Inject WhoisMetric.Builder metricBuilder; @Inject WhoisMetric.Builder metricBuilder;
@Inject WhoisMetrics whoisMetrics; @Inject WhoisMetrics whoisMetrics;
@Inject WhoisHttpServer() {}
@Inject
WhoisHttpAction() {}
@Override @Override
public void run() { public void run() {

View file

@ -31,7 +31,7 @@ import org.joda.time.DateTime;
/** /**
* Instrumentation for WHOIS requests. * Instrumentation for WHOIS requests.
* *
* @see WhoisServer * @see WhoisAction
*/ */
public class WhoisMetrics { public class WhoisMetrics {

View file

@ -1,6 +1,6 @@
PATH CLASS METHODS OK AUTH_METHODS MIN USER_POLICY PATH CLASS METHODS OK AUTH_METHODS MIN USER_POLICY
/_dr/epp EppTlsAction POST n INTERNAL,API APP ADMIN /_dr/epp EppTlsAction POST n INTERNAL,API APP ADMIN
/_dr/whois WhoisServer POST n INTERNAL,API APP ADMIN /_dr/whois WhoisAction POST n INTERNAL,API APP ADMIN
/check CheckApiAction GET n INTERNAL NONE PUBLIC /check CheckApiAction GET n INTERNAL NONE PUBLIC
/rdap/autnum/(*) RdapAutnumAction GET,HEAD n INTERNAL NONE PUBLIC /rdap/autnum/(*) RdapAutnumAction GET,HEAD n INTERNAL NONE PUBLIC
/rdap/domain/(*) RdapDomainAction GET,HEAD n INTERNAL,API,LEGACY NONE PUBLIC /rdap/domain/(*) RdapDomainAction GET,HEAD n INTERNAL,API,LEGACY NONE PUBLIC
@ -17,4 +17,4 @@ PATH CLASS METHODS OK AUTH_MET
/registrar-premium-price-ack RegistrarPremiumPriceAckAction POST n API,LEGACY USER PUBLIC /registrar-premium-price-ack RegistrarPremiumPriceAckAction POST n API,LEGACY USER PUBLIC
/registrar-settings RegistrarSettingsAction POST n API,LEGACY USER PUBLIC /registrar-settings RegistrarSettingsAction POST n API,LEGACY USER PUBLIC
/registrar-xhr EppConsoleAction POST n API,LEGACY USER PUBLIC /registrar-xhr EppConsoleAction POST n API,LEGACY USER PUBLIC
/whois/(*) WhoisHttpServer GET n INTERNAL NONE PUBLIC /whois/(*) WhoisHttpAction GET n INTERNAL NONE PUBLIC

View file

@ -60,9 +60,9 @@ import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.junit.runners.JUnit4; import org.junit.runners.JUnit4;
/** Unit tests for {@link WhoisServer}. */ /** Unit tests for {@link WhoisAction}. */
@RunWith(JUnit4.class) @RunWith(JUnit4.class)
public class WhoisServerTest { public class WhoisActionTest {
@Rule public final AppEngineRule appEngine = AppEngineRule.builder().withDatastore().build(); @Rule public final AppEngineRule appEngine = AppEngineRule.builder().withDatastore().build();
@Rule public final InjectRule inject = new InjectRule(); @Rule public final InjectRule inject = new InjectRule();
@ -70,18 +70,18 @@ public class WhoisServerTest {
private final FakeResponse response = new FakeResponse(); private final FakeResponse response = new FakeResponse();
private final FakeClock clock = new FakeClock(DateTime.parse("2009-06-29T20:13:00Z")); private final FakeClock clock = new FakeClock(DateTime.parse("2009-06-29T20:13:00Z"));
private WhoisServer newWhoisServer(String input) { private WhoisAction newWhoisAction(String input) {
WhoisServer whoisServer = new WhoisServer(); WhoisAction whoisAction = new WhoisAction();
whoisServer.clock = clock; whoisAction.clock = clock;
whoisServer.input = new StringReader(input); whoisAction.input = new StringReader(input);
whoisServer.response = response; whoisAction.response = response;
whoisServer.whoisReader = new WhoisReader(new WhoisCommandFactory()); whoisAction.whoisReader = new WhoisReader(new WhoisCommandFactory());
whoisServer.whoisMetrics = new WhoisMetrics(); whoisAction.whoisMetrics = new WhoisMetrics();
whoisServer.metricBuilder = WhoisMetric.builderForRequest(clock); whoisAction.metricBuilder = WhoisMetric.builderForRequest(clock);
whoisServer.disclaimer = whoisAction.disclaimer =
"Doodle Disclaimer\nI exist so that carriage return\nin disclaimer can be tested."; "Doodle Disclaimer\nI exist so that carriage return\nin disclaimer can be tested.";
whoisServer.retrier = new Retrier(new FakeSleeper(clock), 3); whoisAction.retrier = new Retrier(new FakeSleeper(clock), 3);
return whoisServer; return whoisAction;
} }
@Before @Before
@ -92,9 +92,9 @@ public class WhoisServerTest {
@Test @Test
public void testRun_badRequest_stillSends200() throws Exception { public void testRun_badRequest_stillSends200() throws Exception {
newWhoisServer("\r\n").run(); newWhoisAction("\r\n").run();
assertThat(response.getStatus()).isEqualTo(200); assertThat(response.getStatus()).isEqualTo(200);
assertThat(response.getPayload()).isEqualTo(loadFile("whois_server_no_command.txt")); assertThat(response.getPayload()).isEqualTo(loadFile("whois_action_no_command.txt"));
} }
@Test @Test
@ -110,9 +110,9 @@ public class WhoisServerTest {
persistResource(makeHostResource("ns2.cat.lol", "bad:f00d:cafe::15:beef")), persistResource(makeHostResource("ns2.cat.lol", "bad:f00d:cafe::15:beef")),
registrar)); registrar));
persistSimpleResources(makeRegistrarContacts(registrar)); persistSimpleResources(makeRegistrarContacts(registrar));
newWhoisServer("domain cat.lol\r\n").run(); newWhoisAction("domain cat.lol\r\n").run();
assertThat(response.getStatus()).isEqualTo(200); assertThat(response.getStatus()).isEqualTo(200);
assertThat(response.getPayload()).isEqualTo(loadFile("whois_server_domain.txt")); assertThat(response.getPayload()).isEqualTo(loadFile("whois_action_domain.txt"));
} }
@Test @Test
@ -128,9 +128,9 @@ public class WhoisServerTest {
persistResource(makeHostResource("ns2.cat.みんな", "bad:f00d:cafe::15:beef")), persistResource(makeHostResource("ns2.cat.みんな", "bad:f00d:cafe::15:beef")),
registrar)); registrar));
persistSimpleResources(makeRegistrarContacts(registrar)); persistSimpleResources(makeRegistrarContacts(registrar));
newWhoisServer("domain cat.みんな\r\n").run(); newWhoisAction("domain cat.みんな\r\n").run();
assertThat(response.getStatus()).isEqualTo(200); assertThat(response.getStatus()).isEqualTo(200);
assertThat(response.getPayload()).isEqualTo(loadFile("whois_server_idn_punycode.txt")); assertThat(response.getPayload()).isEqualTo(loadFile("whois_action_idn_punycode.txt"));
} }
@Test @Test
@ -146,17 +146,16 @@ public class WhoisServerTest {
persistResource(makeHostResource("ns2.cat.みんな", "bad:f00d:cafe::15:beef")), persistResource(makeHostResource("ns2.cat.みんな", "bad:f00d:cafe::15:beef")),
registrar)); registrar));
persistSimpleResources(makeRegistrarContacts(registrar)); persistSimpleResources(makeRegistrarContacts(registrar));
newWhoisServer("domain cat.xn--q9jyb4c\r\n").run(); newWhoisAction("domain cat.xn--q9jyb4c\r\n").run();
assertThat(response.getStatus()).isEqualTo(200); assertThat(response.getStatus()).isEqualTo(200);
assertThat(response.getPayload()).isEqualTo(loadFile("whois_server_idn_punycode.txt")); assertThat(response.getPayload()).isEqualTo(loadFile("whois_action_idn_punycode.txt"));
} }
@Test @Test
public void testRun_domainNotFound_returns200OkAndPlainTextResponse() throws Exception { public void testRun_domainNotFound_returns200OkAndPlainTextResponse() throws Exception {
newWhoisServer("domain cat.lol\r\n").run(); newWhoisAction("domain cat.lol\r\n").run();
assertThat(response.getStatus()).isEqualTo(200); assertThat(response.getStatus()).isEqualTo(200);
assertThat(response.getPayload()) assertThat(response.getPayload()).isEqualTo(loadFile("whois_action_domain_not_found.txt"));
.isEqualTo(loadFile("whois_server_domain_not_found.txt"));
} }
// todo (b/27378695): reenable or delete this test // todo (b/27378695): reenable or delete this test
@ -175,10 +174,9 @@ public class WhoisServerTest {
persistResource(makeHostResource("ns2.cat.lol", "bad:f00d:cafe::15:beef")), persistResource(makeHostResource("ns2.cat.lol", "bad:f00d:cafe::15:beef")),
registrar)); registrar));
persistSimpleResources(makeRegistrarContacts(registrar)); persistSimpleResources(makeRegistrarContacts(registrar));
newWhoisServer("domain cat.lol\r\n").run(); newWhoisAction("domain cat.lol\r\n").run();
assertThat(response.getStatus()).isEqualTo(200); assertThat(response.getStatus()).isEqualTo(200);
assertThat(response.getPayload()) assertThat(response.getPayload()).isEqualTo(loadFile("whois_action_domain_not_found.txt"));
.isEqualTo(loadFile("whois_server_domain_not_found.txt"));
} }
@Test @Test
@ -197,10 +195,9 @@ public class WhoisServerTest {
(registrar = makeRegistrar("example", "Example Registrar", ACTIVE)))) (registrar = makeRegistrar("example", "Example Registrar", ACTIVE))))
.asBuilder().setDeletionTime(clock.nowUtc().minusDays(1)).build()); .asBuilder().setDeletionTime(clock.nowUtc().minusDays(1)).build());
persistSimpleResources(makeRegistrarContacts(registrar)); persistSimpleResources(makeRegistrarContacts(registrar));
newWhoisServer("domain cat.lol\r\n").run(); newWhoisAction("domain cat.lol\r\n").run();
assertThat(response.getStatus()).isEqualTo(200); assertThat(response.getStatus()).isEqualTo(200);
assertThat(response.getPayload()) assertThat(response.getPayload()).isEqualTo(loadFile("whois_action_domain_not_found.txt"));
.isEqualTo(loadFile("whois_server_domain_not_found.txt"));
} }
/** /**
@ -238,7 +235,7 @@ public class WhoisServerTest {
.setCreationTimeForTest(clock.nowUtc()).build()); .setCreationTimeForTest(clock.nowUtc()).build());
persistSimpleResources(makeRegistrarContacts(registrar)); persistSimpleResources(makeRegistrarContacts(registrar));
assertThat(domain1.getRepoId()).isNotEqualTo(domain2.getRepoId()); assertThat(domain1.getRepoId()).isNotEqualTo(domain2.getRepoId());
newWhoisServer("domain cat.lol\r\n").run(); newWhoisAction("domain cat.lol\r\n").run();
assertThat(response.getStatus()).isEqualTo(200); assertThat(response.getStatus()).isEqualTo(200);
assertThat(response.getPayload()).contains("Dr. Pablo"); assertThat(response.getPayload()).contains("Dr. Pablo");
assertThat(response.getPayload()).contains("ns1.google.lol"); assertThat(response.getPayload()).contains("ns1.google.lol");
@ -247,15 +244,15 @@ public class WhoisServerTest {
@Test @Test
public void testRun_nameserverQuery_works() throws Exception { public void testRun_nameserverQuery_works() throws Exception {
persistResource(makeHostResource("ns1.cat.lol", "1.2.3.4")); persistResource(makeHostResource("ns1.cat.lol", "1.2.3.4"));
newWhoisServer("nameserver ns1.cat.lol\r\n").run(); newWhoisAction("nameserver ns1.cat.lol\r\n").run();
assertThat(response.getStatus()).isEqualTo(200); assertThat(response.getStatus()).isEqualTo(200);
assertThat(response.getPayload()).isEqualTo(loadFile("whois_server_nameserver.txt")); assertThat(response.getPayload()).isEqualTo(loadFile("whois_action_nameserver.txt"));
} }
@Test @Test
public void testRun_ipv6_displaysInCollapsedReadableFormat() throws Exception { public void testRun_ipv6_displaysInCollapsedReadableFormat() throws Exception {
persistResource(makeHostResource("ns1.cat.lol", "bad:f00d:cafe::15:beef")); persistResource(makeHostResource("ns1.cat.lol", "bad:f00d:cafe::15:beef"));
newWhoisServer("nameserver ns1.cat.lol\r\n").run(); newWhoisAction("nameserver ns1.cat.lol\r\n").run();
assertThat(response.getStatus()).isEqualTo(200); assertThat(response.getStatus()).isEqualTo(200);
assertThat(response.getPayload()).contains("ns1.cat.lol"); assertThat(response.getPayload()).contains("ns1.cat.lol");
// The most important thing this tests is that the outputted address is compressed! // The most important thing this tests is that the outputted address is compressed!
@ -266,7 +263,7 @@ public class WhoisServerTest {
@Test @Test
public void testRun_idnNameserver_works() throws Exception { public void testRun_idnNameserver_works() throws Exception {
persistResource(makeHostResource("ns1.cat.みんな", "1.2.3.4")); persistResource(makeHostResource("ns1.cat.みんな", "1.2.3.4"));
newWhoisServer("nameserver ns1.cat.みんな\r\n").run(); newWhoisAction("nameserver ns1.cat.みんな\r\n").run();
assertThat(response.getStatus()).isEqualTo(200); assertThat(response.getStatus()).isEqualTo(200);
assertThat(response.getPayload()).contains("ns1.cat.xn--q9jyb4c"); assertThat(response.getPayload()).contains("ns1.cat.xn--q9jyb4c");
assertThat(response.getPayload()).contains("1.2.3.4"); assertThat(response.getPayload()).contains("1.2.3.4");
@ -275,7 +272,7 @@ public class WhoisServerTest {
@Test @Test
public void testRun_punycodeNameserver_works() throws Exception { public void testRun_punycodeNameserver_works() throws Exception {
persistResource(makeHostResource("ns1.cat.みんな", "1.2.3.4")); persistResource(makeHostResource("ns1.cat.みんな", "1.2.3.4"));
newWhoisServer("nameserver ns1.cat.xn--q9jyb4c\r\n").run(); newWhoisAction("nameserver ns1.cat.xn--q9jyb4c\r\n").run();
assertThat(response.getStatus()).isEqualTo(200); assertThat(response.getStatus()).isEqualTo(200);
assertThat(response.getPayload()).contains("ns1.cat.xn--q9jyb4c"); assertThat(response.getPayload()).contains("ns1.cat.xn--q9jyb4c");
assertThat(response.getPayload()).contains("1.2.3.4"); assertThat(response.getPayload()).contains("1.2.3.4");
@ -284,10 +281,9 @@ public class WhoisServerTest {
@Test @Test
public void testRun_nameserverNotFound_returns200AndText() throws Exception { public void testRun_nameserverNotFound_returns200AndText() throws Exception {
persistResource(makeHostResource("ns1.cat.lol", "1.2.3.4")); persistResource(makeHostResource("ns1.cat.lol", "1.2.3.4"));
newWhoisServer("nameserver ns1.cat.lulz\r\n").run(); newWhoisAction("nameserver ns1.cat.lulz\r\n").run();
assertThat(response.getStatus()).isEqualTo(200); assertThat(response.getStatus()).isEqualTo(200);
assertThat(response.getPayload()) assertThat(response.getPayload()).isEqualTo(loadFile("whois_action_nameserver_not_found.txt"));
.isEqualTo(loadFile("whois_server_nameserver_not_found.txt"));
} }
@Test @Test
@ -295,16 +291,15 @@ public class WhoisServerTest {
persistResource( persistResource(
makeHostResource("ns1.cat.lol", "1.2.3.4").asBuilder() makeHostResource("ns1.cat.lol", "1.2.3.4").asBuilder()
.setDeletionTime(clock.nowUtc().minusDays(1)).build()); .setDeletionTime(clock.nowUtc().minusDays(1)).build());
newWhoisServer("nameserver ns1.cat.lol\r\n").run(); newWhoisAction("nameserver ns1.cat.lol\r\n").run();
assertThat(response.getStatus()).isEqualTo(200); assertThat(response.getStatus()).isEqualTo(200);
assertThat(response.getPayload()) assertThat(response.getPayload()).isEqualTo(loadFile("whois_action_nameserver_not_found.txt"));
.isEqualTo(loadFile("whois_server_nameserver_not_found.txt"));
} }
@Test @Test
public void testRun_ipNameserverLookup_works() throws Exception { public void testRun_ipNameserverLookup_works() throws Exception {
persistResource(makeHostResource("ns1.cat.lol", "1.2.3.4")); persistResource(makeHostResource("ns1.cat.lol", "1.2.3.4"));
newWhoisServer("nameserver 1.2.3.4").run(); newWhoisAction("nameserver 1.2.3.4").run();
assertThat(response.getStatus()).isEqualTo(200); assertThat(response.getStatus()).isEqualTo(200);
assertThat(response.getPayload()).contains("ns1.cat.lol"); assertThat(response.getPayload()).contains("ns1.cat.lol");
} }
@ -313,7 +308,7 @@ public class WhoisServerTest {
public void testRun_ipMapsToMultipleNameservers_theyAllGetReturned() throws Exception { public void testRun_ipMapsToMultipleNameservers_theyAllGetReturned() throws Exception {
persistResource(makeHostResource("ns1.cat.lol", "1.2.3.4")); persistResource(makeHostResource("ns1.cat.lol", "1.2.3.4"));
persistResource(makeHostResource("ns2.cat.lol", "1.2.3.4")); persistResource(makeHostResource("ns2.cat.lol", "1.2.3.4"));
newWhoisServer("nameserver 1.2.3.4").run(); newWhoisAction("nameserver 1.2.3.4").run();
assertThat(response.getStatus()).isEqualTo(200); assertThat(response.getStatus()).isEqualTo(200);
assertThat(response.getPayload()).contains("ns1.cat.lol"); assertThat(response.getPayload()).contains("ns1.cat.lol");
assertThat(response.getPayload()).contains("ns2.cat.lol"); assertThat(response.getPayload()).contains("ns2.cat.lol");
@ -324,7 +319,7 @@ public class WhoisServerTest {
persistResource(makeHostResource("ns1.cat.lol", "1.2.3.4")); persistResource(makeHostResource("ns1.cat.lol", "1.2.3.4"));
persistResource( persistResource(
makeHostResource("ns1.cat.xn--q9jyb4c", "1.2.3.4")); makeHostResource("ns1.cat.xn--q9jyb4c", "1.2.3.4"));
newWhoisServer("nameserver 1.2.3.4").run(); newWhoisAction("nameserver 1.2.3.4").run();
assertThat(response.getStatus()).isEqualTo(200); assertThat(response.getStatus()).isEqualTo(200);
assertThat(response.getPayload()).contains("ns1.cat.lol"); assertThat(response.getPayload()).contains("ns1.cat.lol");
assertThat(response.getPayload()).contains("ns1.cat.xn--q9jyb4c"); assertThat(response.getPayload()).contains("ns1.cat.xn--q9jyb4c");
@ -332,28 +327,27 @@ public class WhoisServerTest {
@Test @Test
public void testRun_ipNameserverEntityDoesNotExist_returns200NotFound() throws Exception { public void testRun_ipNameserverEntityDoesNotExist_returns200NotFound() throws Exception {
newWhoisServer("nameserver feed:a:bee::acab\r\n").run(); newWhoisAction("nameserver feed:a:bee::acab\r\n").run();
assertThat(response.getStatus()).isEqualTo(200); assertThat(response.getStatus()).isEqualTo(200);
assertThat(response.getPayload()).isEqualTo(loadFile("whois_server_ip_not_found.txt")); assertThat(response.getPayload()).isEqualTo(loadFile("whois_action_ip_not_found.txt"));
} }
@Test @Test
public void testRun_ipMapsToNameserverUnderNonAuthoritativeTld_notFound() throws Exception { public void testRun_ipMapsToNameserverUnderNonAuthoritativeTld_notFound() throws Exception {
assertThat(getTlds()).doesNotContain("com"); assertThat(getTlds()).doesNotContain("com");
persistResource(makeHostResource("ns1.google.com", "1.2.3.4")); persistResource(makeHostResource("ns1.google.com", "1.2.3.4"));
newWhoisServer("nameserver 1.2.3.4").run(); newWhoisAction("nameserver 1.2.3.4").run();
assertThat(response.getStatus()).isEqualTo(200); assertThat(response.getStatus()).isEqualTo(200);
assertThat(response.getPayload()).isEqualTo(loadFile("whois_server_ip_not_found.txt")); assertThat(response.getPayload()).isEqualTo(loadFile("whois_action_ip_not_found.txt"));
} }
@Test @Test
public void testRun_nameserverUnderNonAuthoritativeTld_notFound() throws Exception { public void testRun_nameserverUnderNonAuthoritativeTld_notFound() throws Exception {
assertThat(getTlds()).doesNotContain("com"); assertThat(getTlds()).doesNotContain("com");
persistResource(makeHostResource("ns1.google.com", "1.2.3.4")); persistResource(makeHostResource("ns1.google.com", "1.2.3.4"));
newWhoisServer("nameserver ns1.google.com").run(); newWhoisAction("nameserver ns1.google.com").run();
assertThat(response.getStatus()).isEqualTo(200); assertThat(response.getStatus()).isEqualTo(200);
assertThat(response.getPayload()) assertThat(response.getPayload()).isEqualTo(loadFile("whois_action_nameserver_not_found.txt"));
.isEqualTo(loadFile("whois_server_nameserver_not_found.txt"));
} }
// todo (b/27378695): reenable or delete this test // todo (b/27378695): reenable or delete this test
@ -362,10 +356,9 @@ public class WhoisServerTest {
public void testRun_nameserverInTestTld_notFound() throws Exception { public void testRun_nameserverInTestTld_notFound() throws Exception {
persistResource(Registry.get("lol").asBuilder().setTldType(Registry.TldType.TEST).build()); persistResource(Registry.get("lol").asBuilder().setTldType(Registry.TldType.TEST).build());
persistResource(makeHostResource("ns1.cat.lol", "1.2.3.4")); persistResource(makeHostResource("ns1.cat.lol", "1.2.3.4"));
newWhoisServer("nameserver ns1.cat.lol").run(); newWhoisAction("nameserver ns1.cat.lol").run();
assertThat(response.getStatus()).isEqualTo(200); assertThat(response.getStatus()).isEqualTo(200);
assertThat(response.getPayload()) assertThat(response.getPayload()).isEqualTo(loadFile("whois_action_nameserver_not_found.txt"));
.isEqualTo(loadFile("whois_server_nameserver_not_found.txt"));
} }
@Test @Test
@ -374,9 +367,9 @@ public class WhoisServerTest {
makeRegistrar("example", "Example Registrar, Inc.", ACTIVE)); makeRegistrar("example", "Example Registrar, Inc.", ACTIVE));
persistSimpleResources(makeRegistrarContacts(registrar)); persistSimpleResources(makeRegistrarContacts(registrar));
// Notice the partial search without "inc". // Notice the partial search without "inc".
newWhoisServer("registrar example registrar").run(); newWhoisAction("registrar example registrar").run();
assertThat(response.getStatus()).isEqualTo(200); assertThat(response.getStatus()).isEqualTo(200);
assertThat(response.getPayload()).isEqualTo(loadFile("whois_server_registrar.txt")); assertThat(response.getPayload()).isEqualTo(loadFile("whois_action_registrar.txt"));
} }
@Test @Test
@ -390,9 +383,9 @@ public class WhoisServerTest {
.build()); .build());
persistSimpleResources(makeRegistrarContacts(registrar)); persistSimpleResources(makeRegistrarContacts(registrar));
// Notice the partial search without "inc". // Notice the partial search without "inc".
newWhoisServer("registrar example registrar").run(); newWhoisAction("registrar example registrar").run();
assertThat(response.getStatus()).isEqualTo(200); assertThat(response.getStatus()).isEqualTo(200);
assertThat(response.getPayload()).isEqualTo(loadFile("whois_server_registrar.txt")); assertThat(response.getPayload()).isEqualTo(loadFile("whois_action_registrar.txt"));
} }
@Test @Test
@ -400,10 +393,9 @@ public class WhoisServerTest {
Registrar registrar = persistResource( Registrar registrar = persistResource(
makeRegistrar("example", "Example Registrar, Inc.", Registrar.State.PENDING)); makeRegistrar("example", "Example Registrar, Inc.", Registrar.State.PENDING));
persistSimpleResources(makeRegistrarContacts(registrar)); persistSimpleResources(makeRegistrarContacts(registrar));
newWhoisServer("registrar Example Registrar, Inc.").run(); newWhoisAction("registrar Example Registrar, Inc.").run();
assertThat(response.getStatus()).isEqualTo(200); assertThat(response.getStatus()).isEqualTo(200);
assertThat(response.getPayload()) assertThat(response.getPayload()).isEqualTo(loadFile("whois_action_registrar_not_found.txt"));
.isEqualTo(loadFile("whois_server_registrar_not_found.txt"));
} }
@Test @Test
@ -415,10 +407,9 @@ public class WhoisServerTest {
.setType(Registrar.Type.TEST) .setType(Registrar.Type.TEST)
.build()); .build());
persistSimpleResources(makeRegistrarContacts(registrar)); persistSimpleResources(makeRegistrarContacts(registrar));
newWhoisServer("registrar Example Registrar, Inc.").run(); newWhoisAction("registrar Example Registrar, Inc.").run();
assertThat(response.getStatus()).isEqualTo(200); assertThat(response.getStatus()).isEqualTo(200);
assertThat(response.getPayload()) assertThat(response.getPayload()).isEqualTo(loadFile("whois_action_registrar_not_found.txt"));
.isEqualTo(loadFile("whois_server_registrar_not_found.txt"));
} }
@Test @Test
@ -434,7 +425,7 @@ public class WhoisServerTest {
registrar)); registrar));
persistSimpleResources(makeRegistrarContacts(registrar)); persistSimpleResources(makeRegistrarContacts(registrar));
newWhoisServer("domain cat.1.test\r\n").run(); newWhoisAction("domain cat.1.test\r\n").run();
assertThat(response.getStatus()).isEqualTo(200); assertThat(response.getStatus()).isEqualTo(200);
assertThat(response.getPayload()).contains("Domain Name: cat.1.test\r\n"); assertThat(response.getPayload()).contains("Domain Name: cat.1.test\r\n");
} }
@ -442,7 +433,7 @@ public class WhoisServerTest {
@Test @Test
public void testRun_hostnameWithMultilevelTld_isStillConsideredHostname() throws Exception { public void testRun_hostnameWithMultilevelTld_isStillConsideredHostname() throws Exception {
persistResource(makeHostResource("ns1.cat.1.test", "1.2.3.4")); persistResource(makeHostResource("ns1.cat.1.test", "1.2.3.4"));
newWhoisServer("nameserver ns1.cat.1.test\r\n").run(); newWhoisAction("nameserver ns1.cat.1.test\r\n").run();
assertThat(response.getStatus()).isEqualTo(200); assertThat(response.getStatus()).isEqualTo(200);
assertThat(response.getPayload()).contains("ns1.cat.1.test"); assertThat(response.getPayload()).contains("ns1.cat.1.test");
assertThat(response.getPayload()).contains("1.2.3.4"); assertThat(response.getPayload()).contains("1.2.3.4");
@ -452,57 +443,57 @@ public class WhoisServerTest {
public void testRun_metricsLoggedForSuccessfulCommand() throws Exception { public void testRun_metricsLoggedForSuccessfulCommand() throws Exception {
persistResource(makeHostResource("ns1.cat.lol", "1.2.3.4")); persistResource(makeHostResource("ns1.cat.lol", "1.2.3.4"));
persistResource(makeHostResource("ns2.cat.lol", "1.2.3.4")); persistResource(makeHostResource("ns2.cat.lol", "1.2.3.4"));
WhoisServer server = newWhoisServer("nameserver 1.2.3.4"); WhoisAction action = newWhoisAction("nameserver 1.2.3.4");
server.whoisMetrics = mock(WhoisMetrics.class); action.whoisMetrics = mock(WhoisMetrics.class);
server.run(); action.run();
WhoisMetric expected = WhoisMetric expected =
WhoisMetric.builderForRequest(clock) WhoisMetric.builderForRequest(clock)
.setCommandName("NameserverLookupByIp") .setCommandName("NameserverLookupByIp")
.setNumResults(2) .setNumResults(2)
.setStatus(SC_OK) .setStatus(SC_OK)
.build(); .build();
verify(server.whoisMetrics).recordWhoisMetric(eq(expected)); verify(action.whoisMetrics).recordWhoisMetric(eq(expected));
} }
@Test @Test
public void testRun_metricsLoggedForUnsuccessfulCommand() throws Exception { public void testRun_metricsLoggedForUnsuccessfulCommand() throws Exception {
WhoisServer server = newWhoisServer("domain cat.lol\r\n"); WhoisAction action = newWhoisAction("domain cat.lol\r\n");
server.whoisMetrics = mock(WhoisMetrics.class); action.whoisMetrics = mock(WhoisMetrics.class);
server.run(); action.run();
WhoisMetric expected = WhoisMetric expected =
WhoisMetric.builderForRequest(clock) WhoisMetric.builderForRequest(clock)
.setCommandName("DomainLookup") .setCommandName("DomainLookup")
.setNumResults(0) .setNumResults(0)
.setStatus(SC_NOT_FOUND) .setStatus(SC_NOT_FOUND)
.build(); .build();
verify(server.whoisMetrics).recordWhoisMetric(eq(expected)); verify(action.whoisMetrics).recordWhoisMetric(eq(expected));
} }
@Test @Test
public void testRun_metricsLoggedForInternalServerError() throws Exception { public void testRun_metricsLoggedForInternalServerError() throws Exception {
persistResource(makeHostResource("ns1.cat.lol", "1.2.3.4")); persistResource(makeHostResource("ns1.cat.lol", "1.2.3.4"));
WhoisServer server = newWhoisServer("ns1.cat.lol"); WhoisAction action = newWhoisAction("ns1.cat.lol");
server.whoisReader = mock(WhoisReader.class); action.whoisReader = mock(WhoisReader.class);
when(server.whoisReader.readCommand(any(Reader.class), any(DateTime.class))) when(action.whoisReader.readCommand(any(Reader.class), any(DateTime.class)))
.thenThrow(new IOException("missing cat interface")); .thenThrow(new IOException("missing cat interface"));
server.whoisMetrics = mock(WhoisMetrics.class); action.whoisMetrics = mock(WhoisMetrics.class);
server.run(); action.run();
WhoisMetric expected = WhoisMetric expected =
WhoisMetric.builderForRequest(clock) WhoisMetric.builderForRequest(clock)
.setNumResults(0) .setNumResults(0)
.setStatus(SC_INTERNAL_SERVER_ERROR) .setStatus(SC_INTERNAL_SERVER_ERROR)
.build(); .build();
verify(server.whoisMetrics).recordWhoisMetric(eq(expected)); verify(action.whoisMetrics).recordWhoisMetric(eq(expected));
assertThat(response.getPayload()).isEqualTo("Internal Server Error"); assertThat(response.getPayload()).isEqualTo("Internal Server Error");
} }
@Test @Test
public void testRun_retryOnTransientFailure() throws Exception { public void testRun_retryOnTransientFailure() throws Exception {
persistResource(makeHostResource("ns1.cat.lol", "1.2.3.4")); persistResource(makeHostResource("ns1.cat.lol", "1.2.3.4"));
WhoisServer server = newWhoisServer("ns1.cat.lol"); WhoisAction action = newWhoisAction("ns1.cat.lol");
WhoisResponse expectedResponse = WhoisResponse expectedResponse =
server.whoisReader.readCommand(server.input, clock.nowUtc()).executeQuery(clock.nowUtc()); action.whoisReader.readCommand(action.input, clock.nowUtc()).executeQuery(clock.nowUtc());
WhoisReader mockReader = mock(WhoisReader.class); WhoisReader mockReader = mock(WhoisReader.class);
WhoisCommand mockCommand = mock(WhoisCommand.class); WhoisCommand mockCommand = mock(WhoisCommand.class);
@ -512,8 +503,8 @@ public class WhoisServerTest {
.thenThrow(new DatastoreTimeoutException("Expected transient exception #2")) .thenThrow(new DatastoreTimeoutException("Expected transient exception #2"))
.thenReturn(expectedResponse); .thenReturn(expectedResponse);
server.whoisReader = mockReader; action.whoisReader = mockReader;
server.run(); action.run();
assertThat(response.getPayload()).isEqualTo(loadFile("whois_server_nameserver.txt")); assertThat(response.getPayload()).isEqualTo(loadFile("whois_action_nameserver.txt"));
} }
} }

View file

@ -57,13 +57,13 @@ import org.junit.runner.RunWith;
import org.junit.runners.JUnit4; import org.junit.runners.JUnit4;
/** /**
* Unit tests for {@link WhoisHttpServer}. * Unit tests for {@link WhoisHttpAction}.
* *
* <p>This class should be limited to testing the HTTP interface, as the bulk of the WHOIS testing * <p>This class should be limited to testing the HTTP interface, as the bulk of the WHOIS testing
* can be found in {@link WhoisServerTest}. * can be found in {@link WhoisActionTest}.
*/ */
@RunWith(JUnit4.class) @RunWith(JUnit4.class)
public class WhoisHttpServerTest { public class WhoisHttpActionTest {
@Rule public final AppEngineRule appEngine = AppEngineRule.builder().withDatastore().build(); @Rule public final AppEngineRule appEngine = AppEngineRule.builder().withDatastore().build();
@Rule public final InjectRule inject = new InjectRule(); @Rule public final InjectRule inject = new InjectRule();
@ -71,18 +71,18 @@ public class WhoisHttpServerTest {
private final FakeResponse response = new FakeResponse(); private final FakeResponse response = new FakeResponse();
private final FakeClock clock = new FakeClock(DateTime.parse("2009-06-29T20:13:00Z")); private final FakeClock clock = new FakeClock(DateTime.parse("2009-06-29T20:13:00Z"));
private WhoisHttpServer newWhoisHttpServer(String pathInfo) { private WhoisHttpAction newWhoisHttpAction(String pathInfo) {
WhoisHttpServer whoisServer = new WhoisHttpServer(); WhoisHttpAction whoisAction = new WhoisHttpAction();
whoisServer.clock = clock; whoisAction.clock = clock;
whoisServer.expires = Duration.standardHours(1); whoisAction.expires = Duration.standardHours(1);
whoisServer.requestPath = WhoisHttpServer.PATH + pathInfo; whoisAction.requestPath = WhoisHttpAction.PATH + pathInfo;
whoisServer.response = response; whoisAction.response = response;
whoisServer.whoisReader = new WhoisReader(new WhoisCommandFactory()); whoisAction.whoisReader = new WhoisReader(new WhoisCommandFactory());
whoisServer.whoisMetrics = new WhoisMetrics(); whoisAction.whoisMetrics = new WhoisMetrics();
whoisServer.metricBuilder = WhoisMetric.builderForRequest(clock); whoisAction.metricBuilder = WhoisMetric.builderForRequest(clock);
whoisServer.disclaimer = whoisAction.disclaimer =
"Doodle Disclaimer\nI exist so that carriage return\nin disclaimer can be tested."; "Doodle Disclaimer\nI exist so that carriage return\nin disclaimer can be tested.";
return whoisServer; return whoisAction;
} }
@Before @Before
@ -99,28 +99,26 @@ public class WhoisHttpServerTest {
@Test @Test
public void testRun_emptyQuery_returns400BadRequestWithPlainTextOutput() throws Exception { public void testRun_emptyQuery_returns400BadRequestWithPlainTextOutput() throws Exception {
newWhoisHttpServer("").run(); newWhoisHttpAction("").run();
assertThat(response.getStatus()).isEqualTo(400); assertThat(response.getStatus()).isEqualTo(400);
assertThat(response.getContentType()).isEqualTo(PLAIN_TEXT_UTF_8); assertThat(response.getContentType()).isEqualTo(PLAIN_TEXT_UTF_8);
assertThat(response.getPayload()).isEqualTo(loadFile("whois_server_no_command.txt")); assertThat(response.getPayload()).isEqualTo(loadFile("whois_action_no_command.txt"));
} }
@Test @Test
public void testRun_badUrlEncoding_returns400BadRequestWithPlainTextOutput() throws Exception { public void testRun_badUrlEncoding_returns400BadRequestWithPlainTextOutput() throws Exception {
newWhoisHttpServer("nic.%u307F%u3093%u306A").run(); newWhoisHttpAction("nic.%u307F%u3093%u306A").run();
assertThat(response.getStatus()).isEqualTo(400); assertThat(response.getStatus()).isEqualTo(400);
assertThat(response.getContentType()).isEqualTo(PLAIN_TEXT_UTF_8); assertThat(response.getContentType()).isEqualTo(PLAIN_TEXT_UTF_8);
assertThat(response.getPayload()) assertThat(response.getPayload()).isEqualTo(loadFile("whois_action_malformed_path.txt"));
.isEqualTo(loadFile("whois_server_malformed_path.txt"));
} }
@Test @Test
public void testRun_domainNotFound_returns404StatusAndPlainTextResponse() throws Exception { public void testRun_domainNotFound_returns404StatusAndPlainTextResponse() throws Exception {
newWhoisHttpServer("/domain/cat.lol").run(); newWhoisHttpAction("/domain/cat.lol").run();
assertThat(response.getStatus()).isEqualTo(404); assertThat(response.getStatus()).isEqualTo(404);
assertThat(response.getContentType()).isEqualTo(PLAIN_TEXT_UTF_8); assertThat(response.getContentType()).isEqualTo(PLAIN_TEXT_UTF_8);
assertThat(response.getPayload()) assertThat(response.getPayload()).isEqualTo(loadFile("whois_action_domain_not_found.txt"));
.isEqualTo(loadFile("whois_server_domain_not_found.txt"));
} }
// todo (b/27378695): reenable or delete this test // todo (b/27378695): reenable or delete this test
@ -139,11 +137,10 @@ public class WhoisHttpServerTest {
persistResource(makeHostResource("ns2.cat.lol", "bad:f00d:cafe::15:beef")), persistResource(makeHostResource("ns2.cat.lol", "bad:f00d:cafe::15:beef")),
registrar)); registrar));
persistSimpleResources(makeRegistrarContacts(registrar)); persistSimpleResources(makeRegistrarContacts(registrar));
newWhoisHttpServer("/domain/cat.lol").run(); newWhoisHttpAction("/domain/cat.lol").run();
assertThat(response.getStatus()).isEqualTo(404); assertThat(response.getStatus()).isEqualTo(404);
assertThat(response.getContentType()).isEqualTo(PLAIN_TEXT_UTF_8); assertThat(response.getContentType()).isEqualTo(PLAIN_TEXT_UTF_8);
assertThat(response.getPayload()) assertThat(response.getPayload()).isEqualTo(loadFile("whois_action_domain_not_found.txt"));
.isEqualTo(loadFile("whois_server_domain_not_found.txt"));
} }
@Test @Test
@ -159,9 +156,9 @@ public class WhoisHttpServerTest {
persistResource(makeHostResource("ns2.cat.みんな", "bad:f00d:cafe::15:beef")), persistResource(makeHostResource("ns2.cat.みんな", "bad:f00d:cafe::15:beef")),
registrar)); registrar));
persistSimpleResources(makeRegistrarContacts(registrar)); persistSimpleResources(makeRegistrarContacts(registrar));
newWhoisHttpServer("/domain/cat.みんな").run(); newWhoisHttpAction("/domain/cat.みんな").run();
assertThat(response.getStatus()).isEqualTo(200); assertThat(response.getStatus()).isEqualTo(200);
assertThat(response.getPayload()).isEqualTo(loadFile("whois_server_idn_utf8.txt")); assertThat(response.getPayload()).isEqualTo(loadFile("whois_action_idn_utf8.txt"));
} }
@Test @Test
@ -176,7 +173,7 @@ public class WhoisHttpServerTest {
persistResource(makeHostResource("ns1.cat.みんな", "1.2.3.4")), persistResource(makeHostResource("ns1.cat.みんな", "1.2.3.4")),
persistResource(makeHostResource("ns2.cat.みんな", "bad:f00d:cafe::15:beef")), persistResource(makeHostResource("ns2.cat.みんな", "bad:f00d:cafe::15:beef")),
persistResource(makeRegistrar("example", "Example Registrar", Registrar.State.ACTIVE)))); persistResource(makeRegistrar("example", "Example Registrar", Registrar.State.ACTIVE))));
newWhoisHttpServer("/domain/cat.みんな").run(); newWhoisHttpAction("/domain/cat.みんな").run();
assertThat(response.getPayload()).contains("Eric Schmidt"); assertThat(response.getPayload()).contains("Eric Schmidt");
} }
@ -190,7 +187,7 @@ public class WhoisHttpServerTest {
persistResource(makeHostResource("ns1.cat.みんな", "1.2.3.4")), persistResource(makeHostResource("ns1.cat.みんな", "1.2.3.4")),
persistResource(makeHostResource("ns2.cat.みんな", "bad:f00d:cafe::15:beef")), persistResource(makeHostResource("ns2.cat.みんな", "bad:f00d:cafe::15:beef")),
persistResource(makeRegistrar("example", "Example Registrar", Registrar.State.ACTIVE)))); persistResource(makeRegistrar("example", "Example Registrar", Registrar.State.ACTIVE))));
newWhoisHttpServer("cat.みんな").run(); newWhoisHttpAction("cat.みんな").run();
assertThat(response.getStatus()).isEqualTo(200); assertThat(response.getStatus()).isEqualTo(200);
assertThat(response.getPayload()).contains("Domain Name: cat.みんな\r\n"); assertThat(response.getPayload()).contains("Domain Name: cat.みんな\r\n");
} }
@ -205,14 +202,14 @@ public class WhoisHttpServerTest {
persistResource(makeHostResource("ns1.cat.みんな", "1.2.3.4")), persistResource(makeHostResource("ns1.cat.みんな", "1.2.3.4")),
persistResource(makeHostResource("ns2.cat.みんな", "bad:f00d:cafe::15:beef")), persistResource(makeHostResource("ns2.cat.みんな", "bad:f00d:cafe::15:beef")),
persistResource(makeRegistrar("example", "Example Registrar", Registrar.State.ACTIVE)))); persistResource(makeRegistrar("example", "Example Registrar", Registrar.State.ACTIVE))));
newWhoisHttpServer("cat.みんな").run(); newWhoisHttpAction("cat.みんな").run();
assertThat(response.getPayload()).doesNotContain("<script>"); assertThat(response.getPayload()).doesNotContain("<script>");
} }
@Test @Test
public void testRun_hostnameOnly_works() throws Exception { public void testRun_hostnameOnly_works() throws Exception {
persistResource(makeHostResource("ns1.cat.みんな", "1.2.3.4")); persistResource(makeHostResource("ns1.cat.みんな", "1.2.3.4"));
newWhoisHttpServer("ns1.cat.みんな").run(); newWhoisHttpAction("ns1.cat.みんな").run();
assertThat(response.getPayload()).contains("Server Name: ns1.cat.みんな\r\n"); assertThat(response.getPayload()).contains("Server Name: ns1.cat.みんな\r\n");
} }
@ -229,15 +226,15 @@ public class WhoisHttpServerTest {
persistResource(makeHostResource("ns2.cat.みんな", "bad:f00d:cafe::15:beef")), persistResource(makeHostResource("ns2.cat.みんな", "bad:f00d:cafe::15:beef")),
registrar)); registrar));
persistSimpleResources(makeRegistrarContacts(registrar)); persistSimpleResources(makeRegistrarContacts(registrar));
newWhoisHttpServer("/domain/cat.xn--q9jyb4c").run(); newWhoisHttpAction("/domain/cat.xn--q9jyb4c").run();
assertThat(response.getPayload()).isEqualTo(loadFile("whois_server_idn_utf8.txt")); assertThat(response.getPayload()).isEqualTo(loadFile("whois_action_idn_utf8.txt"));
} }
@Test @Test
public void testRun_nameserverQuery_works() throws Exception { public void testRun_nameserverQuery_works() throws Exception {
persistResource(makeHostResource("ns1.cat.lol", "1.2.3.4")); persistResource(makeHostResource("ns1.cat.lol", "1.2.3.4"));
newWhoisHttpServer("/nameserver/ns1.cat.lol").run(); newWhoisHttpAction("/nameserver/ns1.cat.lol").run();
assertThat(response.getPayload()).isEqualTo(loadFile("whois_server_nameserver.txt")); assertThat(response.getPayload()).isEqualTo(loadFile("whois_action_nameserver.txt"));
} }
// todo (b/27378695): reenable or delete this test // todo (b/27378695): reenable or delete this test
@ -245,15 +242,15 @@ public class WhoisHttpServerTest {
@Test @Test
public void testRun_nameserverQueryInTestTld_notFound() throws Exception { public void testRun_nameserverQueryInTestTld_notFound() throws Exception {
persistResource(makeHostResource("ns1.cat.lol", "1.2.3.4")); persistResource(makeHostResource("ns1.cat.lol", "1.2.3.4"));
newWhoisHttpServer("/nameserver/ns1.cat.lol").run(); newWhoisHttpAction("/nameserver/ns1.cat.lol").run();
assertThat(response.getPayload()).isEqualTo(loadFile("whois_server_nameserver.txt")); assertThat(response.getPayload()).isEqualTo(loadFile("whois_action_nameserver.txt"));
} }
@Test @Test
public void testRun_lastUpdateTimestamp_isPresentInResponse() throws Exception { public void testRun_lastUpdateTimestamp_isPresentInResponse() throws Exception {
clock.setTo(DateTime.parse("2020-07-12T23:52:43Z")); clock.setTo(DateTime.parse("2020-07-12T23:52:43Z"));
persistResource(makeHostResource("ns1.cat.lol", "1.2.3.4")); persistResource(makeHostResource("ns1.cat.lol", "1.2.3.4"));
newWhoisHttpServer("/nameserver/ns1.cat.lol").run(); newWhoisHttpAction("/nameserver/ns1.cat.lol").run();
assertThat(response.getPayload()) assertThat(response.getPayload())
.contains(">>> Last update of WHOIS database: 2020-07-12T23:52:43Z <<<"); .contains(">>> Last update of WHOIS database: 2020-07-12T23:52:43Z <<<");
} }
@ -261,7 +258,7 @@ public class WhoisHttpServerTest {
@Test @Test
public void testRun_nameserverQueryIdn_works() throws Exception { public void testRun_nameserverQueryIdn_works() throws Exception {
persistResource(makeHostResource("ns1.cat.みんな", "1.2.3.4")); persistResource(makeHostResource("ns1.cat.みんな", "1.2.3.4"));
newWhoisHttpServer("/nameserver/ns1.cat.みんな").run(); newWhoisHttpAction("/nameserver/ns1.cat.みんな").run();
assertThat(response.getPayload()).contains("ns1.cat.みんな"); assertThat(response.getPayload()).contains("ns1.cat.みんな");
assertThat(response.getPayload()).contains("1.2.3.4"); assertThat(response.getPayload()).contains("1.2.3.4");
} }
@ -269,7 +266,7 @@ public class WhoisHttpServerTest {
@Test @Test
public void testRun_nameserverQueryPunycode_works() throws Exception { public void testRun_nameserverQueryPunycode_works() throws Exception {
persistResource(makeHostResource("ns1.cat.みんな", "1.2.3.4")); persistResource(makeHostResource("ns1.cat.みんな", "1.2.3.4"));
newWhoisHttpServer("/nameserver/ns1.cat.xn--q9jyb4c").run(); newWhoisHttpAction("/nameserver/ns1.cat.xn--q9jyb4c").run();
assertThat(response.getPayload()).contains("ns1.cat.みんな"); assertThat(response.getPayload()).contains("ns1.cat.みんな");
assertThat(response.getPayload()).contains("1.2.3.4"); assertThat(response.getPayload()).contains("1.2.3.4");
} }
@ -277,7 +274,7 @@ public class WhoisHttpServerTest {
@Test @Test
public void testRun_trailingSlashInPath_getsIgnored() throws Exception { public void testRun_trailingSlashInPath_getsIgnored() throws Exception {
persistResource(makeHostResource("ns1.cat.みんな", "1.2.3.4")); persistResource(makeHostResource("ns1.cat.みんな", "1.2.3.4"));
newWhoisHttpServer("/nameserver/ns1.cat.xn--q9jyb4c/").run(); newWhoisHttpAction("/nameserver/ns1.cat.xn--q9jyb4c/").run();
assertThat(response.getPayload()).contains("ns1.cat.みんな"); assertThat(response.getPayload()).contains("ns1.cat.みんな");
assertThat(response.getPayload()).contains("1.2.3.4"); assertThat(response.getPayload()).contains("1.2.3.4");
} }
@ -292,7 +289,7 @@ public class WhoisHttpServerTest {
persistResource(makeHostResource("ns1.cat.lol", "1.2.3.4")), persistResource(makeHostResource("ns1.cat.lol", "1.2.3.4")),
persistResource(makeHostResource("ns2.cat.lol", "bad:f00d:cafe::15:beef")), persistResource(makeHostResource("ns2.cat.lol", "bad:f00d:cafe::15:beef")),
persistResource(makeRegistrar("example", "Example Registrar", Registrar.State.ACTIVE)))); persistResource(makeRegistrar("example", "Example Registrar", Registrar.State.ACTIVE))));
newWhoisHttpServer("/domain/cat.LOL").run(); newWhoisHttpAction("/domain/cat.LOL").run();
assertThat(response.getPayload()).contains("Domain Name: cat.lol\r\n"); assertThat(response.getPayload()).contains("Domain Name: cat.lol\r\n");
} }
@ -307,7 +304,7 @@ public class WhoisHttpServerTest {
persistResource(makeHostResource("ns2.cat.lol", "bad:f00d:cafe::15:beef")), persistResource(makeHostResource("ns2.cat.lol", "bad:f00d:cafe::15:beef")),
persistResource(makeRegistrar("example", "Example Registrar", Registrar.State.ACTIVE)))); persistResource(makeRegistrar("example", "Example Registrar", Registrar.State.ACTIVE))));
// python -c "print ''.join('%' + hex(ord(c))[2:] for c in 'cat.lol')" // python -c "print ''.join('%' + hex(ord(c))[2:] for c in 'cat.lol')"
newWhoisHttpServer("/domain/%63%61%74%2e%6c%6f%6c").run(); newWhoisHttpAction("/domain/%63%61%74%2e%6c%6f%6c").run();
assertThat(response.getPayload()).contains("Domain Name: cat.lol\r\n"); assertThat(response.getPayload()).contains("Domain Name: cat.lol\r\n");
} }
@ -317,9 +314,9 @@ public class WhoisHttpServerTest {
makeRegistrar("example", "Example Registrar, Inc.", Registrar.State.ACTIVE)); makeRegistrar("example", "Example Registrar, Inc.", Registrar.State.ACTIVE));
persistSimpleResources(makeRegistrarContacts(registrar)); persistSimpleResources(makeRegistrarContacts(registrar));
// Notice the partial search without "inc". // Notice the partial search without "inc".
newWhoisHttpServer("/registrar/Example%20Registrar").run(); newWhoisHttpAction("/registrar/Example%20Registrar").run();
assertThat(response.getStatus()).isEqualTo(200); assertThat(response.getStatus()).isEqualTo(200);
assertThat(response.getPayload()).isEqualTo(loadFile("whois_server_registrar.txt")); assertThat(response.getPayload()).isEqualTo(loadFile("whois_action_registrar.txt"));
} }
@Test @Test
@ -327,10 +324,9 @@ public class WhoisHttpServerTest {
Registrar registrar = persistResource( Registrar registrar = persistResource(
makeRegistrar("example", "Example Registrar, Inc.", Registrar.State.PENDING)); makeRegistrar("example", "Example Registrar, Inc.", Registrar.State.PENDING));
persistSimpleResources(makeRegistrarContacts(registrar)); persistSimpleResources(makeRegistrarContacts(registrar));
newWhoisHttpServer("/registrar/Example%20Registrar,%20Inc.").run(); newWhoisHttpAction("/registrar/Example%20Registrar,%20Inc.").run();
assertThat(response.getStatus()).isEqualTo(404); assertThat(response.getStatus()).isEqualTo(404);
assertThat(response.getPayload()) assertThat(response.getPayload()).isEqualTo(loadFile("whois_action_registrar_not_found.txt"));
.isEqualTo(loadFile("whois_server_registrar_not_found.txt"));
} }
@Test @Test
@ -339,53 +335,52 @@ public class WhoisHttpServerTest {
makeRegistrar("example", "Example Registrar, Inc.", Registrar.State.ACTIVE) makeRegistrar("example", "Example Registrar, Inc.", Registrar.State.ACTIVE)
.asBuilder().setType(Registrar.Type.TEST).setIanaIdentifier(null).build()); .asBuilder().setType(Registrar.Type.TEST).setIanaIdentifier(null).build());
persistSimpleResources(makeRegistrarContacts(registrar)); persistSimpleResources(makeRegistrarContacts(registrar));
newWhoisHttpServer("/registrar/Example%20Registrar,%20Inc.").run(); newWhoisHttpAction("/registrar/Example%20Registrar,%20Inc.").run();
assertThat(response.getStatus()).isEqualTo(404); assertThat(response.getStatus()).isEqualTo(404);
assertThat(response.getPayload()) assertThat(response.getPayload()).isEqualTo(loadFile("whois_action_registrar_not_found.txt"));
.isEqualTo(loadFile("whois_server_registrar_not_found.txt"));
} }
@Test @Test
public void testRun_metricsLoggedForSuccessfulCommand() throws Exception { public void testRun_metricsLoggedForSuccessfulCommand() throws Exception {
persistResource(makeHostResource("ns1.cat.lol", "1.2.3.4")); persistResource(makeHostResource("ns1.cat.lol", "1.2.3.4"));
WhoisHttpServer server = newWhoisHttpServer("/nameserver/ns1.cat.lol"); WhoisHttpAction action = newWhoisHttpAction("/nameserver/ns1.cat.lol");
server.whoisMetrics = mock(WhoisMetrics.class); action.whoisMetrics = mock(WhoisMetrics.class);
server.run(); action.run();
WhoisMetric expected = WhoisMetric expected =
WhoisMetric.builderForRequest(clock) WhoisMetric.builderForRequest(clock)
.setCommandName("NameserverLookupByHost") .setCommandName("NameserverLookupByHost")
.setNumResults(1) .setNumResults(1)
.setStatus(SC_OK) .setStatus(SC_OK)
.build(); .build();
verify(server.whoisMetrics).recordWhoisMetric(eq(expected)); verify(action.whoisMetrics).recordWhoisMetric(eq(expected));
} }
@Test @Test
public void testRun_metricsLoggedForUnsuccessfulCommand() throws Exception { public void testRun_metricsLoggedForUnsuccessfulCommand() throws Exception {
WhoisHttpServer server = newWhoisHttpServer("nic.%u307F%u3093%u306A"); WhoisHttpAction action = newWhoisHttpAction("nic.%u307F%u3093%u306A");
server.whoisMetrics = mock(WhoisMetrics.class); action.whoisMetrics = mock(WhoisMetrics.class);
server.run(); action.run();
WhoisMetric expected = WhoisMetric expected =
WhoisMetric.builderForRequest(clock).setNumResults(0).setStatus(SC_BAD_REQUEST).build(); WhoisMetric.builderForRequest(clock).setNumResults(0).setStatus(SC_BAD_REQUEST).build();
verify(server.whoisMetrics).recordWhoisMetric(eq(expected)); verify(action.whoisMetrics).recordWhoisMetric(eq(expected));
} }
@Test @Test
public void testRun_metricsLoggedForInternalServerError() throws Exception { public void testRun_metricsLoggedForInternalServerError() throws Exception {
persistResource(makeHostResource("ns1.cat.lol", "1.2.3.4")); persistResource(makeHostResource("ns1.cat.lol", "1.2.3.4"));
WhoisHttpServer server = newWhoisHttpServer("ns1.cat.lol"); WhoisHttpAction action = newWhoisHttpAction("ns1.cat.lol");
server.whoisReader = mock(WhoisReader.class); action.whoisReader = mock(WhoisReader.class);
when(server.whoisReader.readCommand(any(Reader.class), any(DateTime.class))) when(action.whoisReader.readCommand(any(Reader.class), any(DateTime.class)))
.thenThrow(new IOException("missing cat interface")); .thenThrow(new IOException("missing cat interface"));
server.whoisMetrics = mock(WhoisMetrics.class); action.whoisMetrics = mock(WhoisMetrics.class);
RuntimeException thrown = assertThrows(RuntimeException.class, server::run); RuntimeException thrown = assertThrows(RuntimeException.class, action::run);
assertThat(thrown).hasCauseThat().hasMessageThat().isEqualTo("missing cat interface"); assertThat(thrown).hasCauseThat().hasMessageThat().isEqualTo("missing cat interface");
WhoisMetric expected = WhoisMetric expected =
WhoisMetric.builderForRequest(clock) WhoisMetric.builderForRequest(clock)
.setNumResults(0) .setNumResults(0)
.setStatus(SC_INTERNAL_SERVER_ERROR) .setStatus(SC_INTERNAL_SERVER_ERROR)
.build(); .build();
verify(server.whoisMetrics).recordWhoisMetric(eq(expected)); verify(action.whoisMetrics).recordWhoisMetric(eq(expected));
} }
} }

View file

@ -55,28 +55,28 @@ public final class WhoisInjectionTest {
} }
@Test @Test
public void testWhoisServer_injectsAndWorks() throws Exception { public void testWhoisAction_injectsAndWorks() throws Exception {
createTld("lol"); createTld("lol");
persistResource(makeHostResource("ns1.cat.lol", "1.2.3.4")); persistResource(makeHostResource("ns1.cat.lol", "1.2.3.4"));
when(req.getReader()).thenReturn(new BufferedReader(new StringReader("ns1.cat.lol\r\n"))); when(req.getReader()).thenReturn(new BufferedReader(new StringReader("ns1.cat.lol\r\n")));
DaggerWhoisTestComponent.builder() DaggerWhoisTestComponent.builder()
.requestModule(new RequestModule(req, rsp)) .requestModule(new RequestModule(req, rsp))
.build() .build()
.whoisServer() .whoisAction()
.run(); .run();
verify(rsp).setStatus(200); verify(rsp).setStatus(200);
assertThat(httpOutput.toString()).contains("ns1.cat.lol"); assertThat(httpOutput.toString()).contains("ns1.cat.lol");
} }
@Test @Test
public void testWhoisHttpServer_injectsAndWorks() throws Exception { public void testWhoisHttpAction_injectsAndWorks() throws Exception {
createTld("lol"); createTld("lol");
persistResource(makeHostResource("ns1.cat.lol", "1.2.3.4")); persistResource(makeHostResource("ns1.cat.lol", "1.2.3.4"));
when(req.getRequestURI()).thenReturn("/whois/ns1.cat.lol"); when(req.getRequestURI()).thenReturn("/whois/ns1.cat.lol");
DaggerWhoisTestComponent.builder() DaggerWhoisTestComponent.builder()
.requestModule(new RequestModule(req, rsp)) .requestModule(new RequestModule(req, rsp))
.build() .build()
.whoisHttpServer() .whoisHttpAction()
.run(); .run();
verify(rsp).setStatus(200); verify(rsp).setStatus(200);
assertThat(httpOutput.toString()).contains("ns1.cat.lol"); assertThat(httpOutput.toString()).contains("ns1.cat.lol");

View file

@ -30,6 +30,7 @@ import javax.inject.Singleton;
WhoisModule.class, WhoisModule.class,
}) })
interface WhoisTestComponent { interface WhoisTestComponent {
WhoisHttpServer whoisHttpServer(); WhoisHttpAction whoisHttpAction();
WhoisServer whoisServer();
WhoisAction whoisAction();
} }