Github user geomacy commented on a diff in the pull request:
https://github.com/apache/brooklyn-server/pull/790#discussion_r131346980
--- Diff:
core/src/test/java/org/apache/brooklyn/feed/http/HttpFeedTest.java ---
@@ -360,7 +364,90 @@ public void testPollsMultiClearsOnSubsequentFailure()
throws Exception {
server.shutdown();
}
+
+ @Test
+ public void testPreemptiveBasicAuth() throws Exception {
+ final String username = "brooklyn";
+ final String password = "hunter2";
+
+ 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 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");
+ }
+ }
+ // Expected behaviour of o.a.http.client is that it first sends the
request without credentials,
+ // and then when given a challenge for basic-auth it re-sends the
request with the basic-auth header.
+ @Test
+ public void testNonPreemptiveBasicAuth() throws Exception {
+ final String username = "brooklyn";
+ final String password = "hunter2";
+
+ if (server != null) server.shutdown();
+ server = BetterMockWebServer.newInstanceLocalhost();
+ server.enqueue(new MockResponse()
+ .setResponseCode(401)
+ .addHeader("WWW-Authenticate", "Basic"));
+ server.enqueue(new MockResponse()
+ .setResponseCode(200)
+ .setBody("Hello World"));
+ server.play();
+
+ feed = HttpFeed.builder()
+ .entity(entity)
+ .baseUrl(server.getUrl("/"))
+ .credentials(username, password)
+ .poll(new HttpPollConfig<Integer>(SENSOR_INT)
+ .period(Duration.ONE_MINUTE) // so only dealing
with first request
+ .onSuccess(HttpValueFunctions.responseCode())
+ .onException(Functions.constant(-1)))
+ .build();
+
+ EntityAsserts.assertAttributeEqualsEventually(entity, SENSOR_INT,
200);
+ RecordedRequest req = server.takeRequest();
+ assertEquals(req.getHeader("Authorization"), null);
+
+ RecordedRequest req2 = server.takeRequest();
+ String headerVal = req2.getHeader("Authorization");
+ String expected = getBasicAuthHeaderVal(username, password);
+ assertEquals(headerVal, expected);
+ }
+
+ public static String getBasicAuthHeaderVal(String username, String
password) {
+ String toencode = username + (password == null ? "" :
":"+password);
--- End diff --
same comment about not dropping the `:`
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---