@Test public void testPreemptiveBasicAuthFailsIfNoCredentials() throws Exception { try { feed = HttpFeed.builder() .entity(entity) .baseUrl(new URL("http://shouldNeverBeCalled.org")) .preemptiveBasicAuth(true) .poll(new HttpPollConfig<Integer>(SENSOR_INT) .period(100) .onSuccess(HttpValueFunctions.responseCode()) .onException(Functions.constant(-1))) .build(); Asserts.shouldHaveFailedPreviously(); } catch (IllegalArgumentException e) { Asserts.expectedFailureContains(e, "Must not enable preemptiveBasicAuth when there are no credentials"); } }
@Test public void testPollsAndParsesHttpErrorResponseLocal() throws Exception { int unboundPort = Networking.nextAvailablePort(10000); feed = HttpFeed.builder() .entity(entity) .baseUri("http://localhost:" + unboundPort + "/path/should/not/exist") .poll(new HttpPollConfig<String>(SENSOR_STRING) .onSuccess(Functions.constant("success")) .onFailure(Functions.constant("failure")) .onException(Functions.constant("error"))) .build(); assertSensorEventually(SENSOR_STRING, "error", TIMEOUT_MS); }
@Test public void testFailsIfUsernameNull() throws Exception { try { feed = HttpFeed.builder() .entity(entity) .baseUrl(new URL("http://shouldNeverBeCalled.org")) .credentials(null, "Pa55w0rd") .poll(new HttpPollConfig<Integer>(SENSOR_INT) .period(100) .onSuccess(HttpValueFunctions.responseCode()) .onException(Functions.constant(-1))) .build(); Asserts.shouldHaveFailedPreviously(); } catch (IllegalArgumentException e) { Asserts.expectedFailureContainsIgnoreCase(e, "may not be null"); } }
@Test public void testPreemptiveBasicAuthFailsIfUserContainsColon() throws Exception { try { feed = HttpFeed.builder() .entity(entity) .baseUrl(new URL("http://shouldNeverBeCalled.org")) .credentials("userWith:colon", "Pa55w0rd") .preemptiveBasicAuth(true) .poll(new HttpPollConfig<Integer>(SENSOR_INT) .period(100) .onSuccess(HttpValueFunctions.responseCode()) .onException(Functions.constant(-1))) .build(); Asserts.shouldHaveFailedPreviously(); } catch (IllegalArgumentException e) { Asserts.expectedFailureContains(e, "must not contain colon"); } }
protected void runPreemptiveBasicAuth(String username, String password) throws Exception { feed = HttpFeed.builder() .entity(entity) .baseUrl(server.getUrl("/")) .credentials(username, password) .preemptiveBasicAuth(true) .poll(new HttpPollConfig<Integer>(SENSOR_INT) .period(100) .onSuccess(HttpValueFunctions.responseCode()) .onException(Functions.constant(-1))) .build(); EntityAsserts.assertAttributeEqualsEventually(entity, SENSOR_INT, 200); RecordedRequest req = server.takeRequest(); String headerVal = req.getHeader("Authorization"); String expectedVal = getBasicAuthHeaderVal(username, password); assertEquals(headerVal, expectedVal); }
@Test public void testSetsConnectionTimeoutWhenServerDisconnects() throws Exception { if (server != null) server.shutdown(); server = BetterMockWebServer.newInstanceLocalhost(); for (int i = 0; i < 100; i++) { server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.DISCONNECT_AT_START)); } server.play(); baseUrl = server.getUrl("/"); feed = HttpFeed.builder() .entity(entity) .baseUrl(baseUrl) .poll(new HttpPollConfig<Integer>(SENSOR_INT) .period(100) .connectionTimeout(Duration.TEN_SECONDS) .socketTimeout(Duration.TEN_SECONDS) .onSuccess(HttpValueFunctions.responseCode()) .onException(Functions.constant(-1))) .build(); assertSensorEventually(SENSOR_INT, -1, TIMEOUT_MS); }
.onException(Functions.constant(-1))) .build();
@Test public void testUsesExceptionHandlerOn4xxAndNoFailureHandler() throws Exception { if (server != null) server.shutdown(); server = BetterMockWebServer.newInstanceLocalhost(); for (int i = 0; i < 100; i++) { server.enqueue(new MockResponse() .setResponseCode(401) .setBody("Unauthorised")); } server.play(); feed = HttpFeed.builder() .entity(entity) .baseUrl(server.getUrl("/")) .poll(new HttpPollConfig<Integer>(SENSOR_INT) .period(100) .onSuccess(HttpValueFunctions.responseCode()) .onException(Functions.constant(-1))) .build(); assertSensorEventually(SENSOR_INT, -1, TIMEOUT_MS); server.shutdown(); }
@Test(groups = {"Integration"}) public void testPollWithInvalidCredentialsFails() throws Exception { httpService = new HttpService(PortRanges.fromString("9000+")) .basicAuthentication("brooklyn", "hunter2") .start(); feed = HttpFeed.builder() .entity(entity) .baseUri(httpService.getUrl()) .credentials("brooklyn", "9876543210") .poll(new HttpPollConfig<Integer>(SENSOR_INT) .period(100) .onSuccess(HttpValueFunctions.responseCode()) .onFailure(HttpValueFunctions.responseCode())) .poll(new HttpPollConfig<String>(SENSOR_STRING) .period(100) .onSuccess(HttpValueFunctions.stringContentsFunction()) .onException(Functions.constant("Failed!"))) .build(); EntityAsserts.assertAttributeEqualsEventually(entity, SENSOR_INT, 401); Asserts.succeedsEventually(new Runnable() { @Override public void run() { String val = entity.getAttribute(SENSOR_STRING); assertTrue(val != null && val.equals("Failed!"), "val=" + val); } }); } }