Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions src/main/java/com/pusher/client/util/BaseHttpAuthClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,19 +134,28 @@ protected String performAuthRequest() {
wr.flush();
wr.close();

// Read response
final InputStream is = connection.getInputStream();
final BufferedReader rd = new BufferedReader(new InputStreamReader(is));
String line;
// Read response. For error statuses, getInputStream() throws, so the
// body the server sent is read from the error stream instead.
final int responseHttpStatus = connection.getResponseCode();
final InputStream is = responseHttpStatus >= HttpURLConnection.HTTP_BAD_REQUEST
? connection.getErrorStream()
: connection.getInputStream();
final StringBuilder response = new StringBuilder();
while ((line = rd.readLine()) != null) {
response.append(line);
if (is != null) {
final BufferedReader rd = new BufferedReader(new InputStreamReader(is));
String line;
while ((line = rd.readLine()) != null) {
response.append(line);
}
rd.close();
}
rd.close();

final int responseHttpStatus = connection.getResponseCode();
if (responseHttpStatus != 200 && responseHttpStatus != 201) {
throw authFailureException(response.toString());
final String message = "Server returned HTTP response code: " + responseHttpStatus + " for URL: " + endPoint;
if (response.length() == 0) {
throw authFailureException(message);
}
throw authFailureException(message + ": " + response);
}

return response.toString();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
package com.pusher.client.util;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import com.pusher.client.AuthorizationFailureException;

import com.sun.net.httpserver.HttpServer;

import org.junit.Test;

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;

public class HttpChannelAuthorizerTest {

@Test(expected = IllegalArgumentException.class)
Expand All @@ -31,4 +41,47 @@ public void testNon200ResponseThrowsAuthorizationFailureException() {
final HttpChannelAuthorizer auth = new HttpChannelAuthorizer("https://127.0.0.1/no-way-this-is-a-valid-url");
auth.authorize("private-fish", "some socket id");
}

@Test
public void testErrorResponseBodyIsUsedAsAuthorizationFailureMessage() throws IOException {
final byte[] errorBody = "{\"error\":\"forbidden\"}".getBytes(StandardCharsets.UTF_8);
final HttpServer server = HttpServer.create(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), 0);
server.createContext("/auth", exchange -> {
exchange.sendResponseHeaders(403, errorBody.length);
try (OutputStream os = exchange.getResponseBody()) {
os.write(errorBody);
}
});
server.start();
final String url = "http://127.0.0.1:" + server.getAddress().getPort() + "/auth";
try {
final HttpChannelAuthorizer auth = new HttpChannelAuthorizer(url);
auth.authorize("private-fish", "some socket id");
fail("Expected AuthorizationFailureException");
} catch (final AuthorizationFailureException e) {
assertEquals("Server returned HTTP response code: 403 for URL: " + url + ": {\"error\":\"forbidden\"}", e.getMessage());
} finally {
server.stop(0);
}
}

@Test
public void testEmptyErrorResponseKeepsStatusAndUrlInAuthorizationFailureMessage() throws IOException {
final HttpServer server = HttpServer.create(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), 0);
server.createContext("/auth", exchange -> {
exchange.sendResponseHeaders(403, -1);
exchange.close();
});
server.start();
final String url = "http://127.0.0.1:" + server.getAddress().getPort() + "/auth";
try {
final HttpChannelAuthorizer auth = new HttpChannelAuthorizer(url);
auth.authorize("private-fish", "some socket id");
fail("Expected AuthorizationFailureException");
} catch (final AuthorizationFailureException e) {
assertEquals("Server returned HTTP response code: 403 for URL: " + url, e.getMessage());
} finally {
server.stop(0);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
package com.pusher.client.util;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import com.pusher.client.AuthenticationFailureException;

import com.sun.net.httpserver.HttpServer;

import org.junit.Test;

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;

public class HttpUserAuthenticatorTest {

@Test(expected = IllegalArgumentException.class)
Expand All @@ -31,4 +41,47 @@ public void testNon200ResponseThrowsAuthenticationFailtureException() {
final HttpUserAuthenticator auth = new HttpUserAuthenticator("https://127.0.0.1/no-way-this-is-a-valid-url");
auth.authenticate("some socket id");
}

@Test
public void testErrorResponseBodyIsUsedAsAuthenticationFailureMessage() throws IOException {
final byte[] errorBody = "{\"error\":\"forbidden\"}".getBytes(StandardCharsets.UTF_8);
final HttpServer server = HttpServer.create(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), 0);
server.createContext("/auth", exchange -> {
exchange.sendResponseHeaders(403, errorBody.length);
try (OutputStream os = exchange.getResponseBody()) {
os.write(errorBody);
}
});
server.start();
final String url = "http://127.0.0.1:" + server.getAddress().getPort() + "/auth";
try {
final HttpUserAuthenticator auth = new HttpUserAuthenticator(url);
auth.authenticate("some socket id");
fail("Expected AuthenticationFailureException");
} catch (final AuthenticationFailureException e) {
assertEquals("Server returned HTTP response code: 403 for URL: " + url + ": {\"error\":\"forbidden\"}", e.getMessage());
} finally {
server.stop(0);
}
}

@Test
public void testEmptyErrorResponseKeepsStatusAndUrlInAuthenticationFailureMessage() throws IOException {
final HttpServer server = HttpServer.create(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), 0);
server.createContext("/auth", exchange -> {
exchange.sendResponseHeaders(403, -1);
exchange.close();
});
server.start();
final String url = "http://127.0.0.1:" + server.getAddress().getPort() + "/auth";
try {
final HttpUserAuthenticator auth = new HttpUserAuthenticator(url);
auth.authenticate("some socket id");
fail("Expected AuthenticationFailureException");
} catch (final AuthenticationFailureException e) {
assertEquals("Server returned HTTP response code: 403 for URL: " + url, e.getMessage());
} finally {
server.stop(0);
}
}
}
Loading