public Updater(String path, Consumer<Http1FilterConfig> setter) { this.path = path; this.setter = setter; config = new Http1DeterministicRuleEngineConfig(); unmarshaller = new ThriftUnmarshaller(); }
@Test public void testWriteHttp1DeterministicRuleEngineConfig() throws Exception { try (TestingServer server = new TestingServer()) { server.start(); Http1DeterministicRuleEngineConfig config = new Http1DeterministicRuleEngineConfig(); HashMultimap<String, String> headers = HashMultimap.create(); headers.put("User-Agent", "Bad-actor: 1.0"); Http1DeterministicRuleEngineConfig.Rule bad = new Http1DeterministicRuleEngineConfig.Rule( HttpMethod.GET, "/path/to/failure", HttpVersion.HTTP_1_0, headers); Http1DeterministicRuleEngineConfig.Rule good = new Http1DeterministicRuleEngineConfig.Rule(null, null, null, null); config.blacklistRule(bad); config.whitelistRule(good); ThriftMarshaller marshaller = new ThriftMarshaller(); RetryPolicy retryPolicy = new RetryOneTime(1); try (CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), retryPolicy)) { client.start(); String path = "/some/path/to/nodes/http1Rules"; ZooKeeperWriteProvider provider = new ZooKeeperWriteProvider(marshaller, client); provider.write(path, config); byte[] data = client.getData().forPath(path); ThriftUnmarshaller unmarshaller = new ThriftUnmarshaller(); Http1DeterministicRuleEngineConfig read = new Http1DeterministicRuleEngineConfig(); unmarshaller.unmarshall(read, data); assertEquals(config, read); } } }
@Test public void testUpdater() throws UnknownHostException { ThriftMarshaller marshaller = new ThriftMarshaller(); Http1DeterministicRuleEngineConfig rules = new Http1DeterministicRuleEngineConfig(); HashMultimap<String, String> headers = HashMultimap.create(); headers.put("User-Agent", "Bad-actor: 1.0"); Http1DeterministicRuleEngineConfig.Rule bad = new Http1DeterministicRuleEngineConfig.Rule( HttpMethod.GET, "/path/to/failure", HttpVersion.HTTP_1_0, headers); rules.blacklistRule(bad); Http1FilterConfig.Updater updater = new Http1FilterConfig.Updater("path", this::setHttp1FilterConfig); updater.update(marshaller.marshall(rules)); Http1FilterConfig expected = new Http1FilterConfig(rules.getBlacklistRules()); assertEquals(expected, config); }