Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: include request method and URL into HttpResponseException message #1002

Merged
merged 2 commits into from Mar 16, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -284,6 +284,13 @@ public static StringBuilder computeMessageBuffer(HttpResponse response) {
}
builder.append(statusMessage);
}
HttpRequest request = response.getRequest();
if (request != null) {
if (builder.length() > 0) {
builder.append('\n');
}
builder.append("Request URL: ").append(request.getUrl());
}
return builder;
}
}
Expand Up @@ -14,19 +14,23 @@

package com.google.api.client.http;

import static com.google.api.client.testing.http.HttpTesting.SIMPLE_GENERIC_URL;
import static com.google.api.client.util.StringUtils.LINE_SEPARATOR;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertThrows;

import com.google.api.client.http.HttpResponseException.Builder;
import com.google.api.client.testing.http.HttpTesting;
import com.google.api.client.testing.http.MockHttpTransport;
import com.google.api.client.testing.http.MockLowLevelHttpRequest;
import com.google.api.client.testing.http.MockLowLevelHttpResponse;
import com.google.api.client.util.StringUtils;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import junit.framework.TestCase;
import org.junit.function.ThrowingRunnable;

/**
* Tests {@link HttpResponseException}.
Expand All @@ -37,12 +41,11 @@ public class HttpResponseExceptionTest extends TestCase {

public void testConstructor() throws Exception {
HttpTransport transport = new MockHttpTransport();
HttpRequest request =
transport.createRequestFactory().buildGetRequest(HttpTesting.SIMPLE_GENERIC_URL);
HttpRequest request = transport.createRequestFactory().buildGetRequest(SIMPLE_GENERIC_URL);
HttpResponse response = request.execute();
HttpHeaders headers = response.getHeaders();
HttpResponseException e = new HttpResponseException(response);
medb marked this conversation as resolved.
Show resolved Hide resolved
assertEquals("200", e.getMessage());
assertThat(e).hasMessageThat().isEqualTo("200\nRequest URL: " + SIMPLE_GENERIC_URL);
assertNull(e.getContent());
assertEquals(200, e.getStatusCode());
assertNull(e.getStatusMessage());
Expand Down Expand Up @@ -83,8 +86,7 @@ public LowLevelHttpResponse execute() throws IOException {
};
}
};
HttpRequest request =
transport.createRequestFactory().buildGetRequest(HttpTesting.SIMPLE_GENERIC_URL);
HttpRequest request = transport.createRequestFactory().buildGetRequest(SIMPLE_GENERIC_URL);
HttpResponse response = request.execute();
HttpResponseException e = new HttpResponseException(response);
assertEquals("OK", e.getStatusMessage());
Expand All @@ -105,14 +107,18 @@ public LowLevelHttpResponse execute() throws IOException {
};
}
};
HttpRequest request =
transport.createRequestFactory().buildGetRequest(HttpTesting.SIMPLE_GENERIC_URL);
try {
request.execute();
fail();
} catch (HttpResponseException e) {
assertEquals("", e.getMessage());
}
final HttpRequest request =
transport.createRequestFactory().buildGetRequest(SIMPLE_GENERIC_URL);
HttpResponseException responseException =
assertThrows(
HttpResponseException.class,
new ThrowingRunnable() {
@Override
public void run() throws Throwable {
request.execute();
}
});
assertThat(responseException).hasMessageThat().isEqualTo("Request URL: " + SIMPLE_GENERIC_URL);
}

public void testConstructor_messageButNoStatusCode() throws Exception {
Expand All @@ -131,14 +137,20 @@ public LowLevelHttpResponse execute() throws IOException {
};
}
};
HttpRequest request =
transport.createRequestFactory().buildGetRequest(HttpTesting.SIMPLE_GENERIC_URL);
try {
request.execute();
fail();
} catch (HttpResponseException e) {
assertEquals("Foo", e.getMessage());
}
final HttpRequest request =
transport.createRequestFactory().buildGetRequest(SIMPLE_GENERIC_URL);
HttpResponseException responseException =
assertThrows(
HttpResponseException.class,
new ThrowingRunnable() {
@Override
public void run() throws Throwable {
request.execute();
}
});
assertThat(responseException)
.hasMessageThat()
.isEqualTo("Foo\nRequest URL: " + SIMPLE_GENERIC_URL);
}

public void testComputeMessage() throws Exception {
Expand All @@ -156,10 +168,10 @@ public LowLevelHttpResponse execute() throws IOException {
};
}
};
HttpRequest request =
transport.createRequestFactory().buildGetRequest(HttpTesting.SIMPLE_GENERIC_URL);
HttpRequest request = transport.createRequestFactory().buildGetRequest(SIMPLE_GENERIC_URL);
HttpResponse response = request.execute();
assertEquals("200 Foo", HttpResponseException.computeMessageBuffer(response).toString());
assertThat(HttpResponseException.computeMessageBuffer(response).toString())
.isEqualTo("200 Foo\nRequest URL: " + SIMPLE_GENERIC_URL);
}

public void testThrown() throws Exception {
Expand All @@ -179,15 +191,25 @@ public LowLevelHttpResponse execute() throws IOException {
};
}
};
HttpRequest request =
transport.createRequestFactory().buildGetRequest(HttpTesting.SIMPLE_GENERIC_URL);
try {
request.execute();
fail();
} catch (HttpResponseException e) {
assertEquals(
"404 Not Found" + StringUtils.LINE_SEPARATOR + "Unable to find resource", e.getMessage());
}
final HttpRequest request =
transport.createRequestFactory().buildGetRequest(SIMPLE_GENERIC_URL);
HttpResponseException responseException =
assertThrows(
HttpResponseException.class,
new ThrowingRunnable() {
@Override
public void run() throws Throwable {
request.execute();
}
});

assertThat(responseException)
.hasMessageThat()
.isEqualTo(
"404 Not Found\nRequest URL: "
+ SIMPLE_GENERIC_URL
+ LINE_SEPARATOR
+ "Unable to find resource");
}

public void testInvalidCharset() throws Exception {
Expand All @@ -208,14 +230,21 @@ public LowLevelHttpResponse execute() throws IOException {
};
}
};
HttpRequest request =
transport.createRequestFactory().buildGetRequest(HttpTesting.SIMPLE_GENERIC_URL);
try {
request.execute();
fail();
} catch (HttpResponseException e) {
assertEquals("404 Not Found", e.getMessage());
}
final HttpRequest request =
transport.createRequestFactory().buildGetRequest(SIMPLE_GENERIC_URL);
HttpResponseException responseException =
assertThrows(
HttpResponseException.class,
new ThrowingRunnable() {
@Override
public void run() throws Throwable {
request.execute();
}
});

assertThat(responseException)
.hasMessageThat()
.isEqualTo("404 Not Found\nRequest URL: " + SIMPLE_GENERIC_URL);
}

public void testUnsupportedCharset() throws Exception {
Expand All @@ -236,20 +265,25 @@ public LowLevelHttpResponse execute() throws IOException {
};
}
};
HttpRequest request =
transport.createRequestFactory().buildGetRequest(HttpTesting.SIMPLE_GENERIC_URL);
try {
request.execute();
fail();
} catch (HttpResponseException e) {
assertEquals("404 Not Found", e.getMessage());
}
final HttpRequest request =
transport.createRequestFactory().buildGetRequest(SIMPLE_GENERIC_URL);
HttpResponseException responseException =
assertThrows(
HttpResponseException.class,
new ThrowingRunnable() {
@Override
public void run() throws Throwable {
request.execute();
}
});
assertThat(responseException)
.hasMessageThat()
.isEqualTo("404 Not Found\nRequest URL: " + SIMPLE_GENERIC_URL);
}

public void testSerialization() throws Exception {
HttpTransport transport = new MockHttpTransport();
HttpRequest request =
transport.createRequestFactory().buildGetRequest(HttpTesting.SIMPLE_GENERIC_URL);
HttpRequest request = transport.createRequestFactory().buildGetRequest(SIMPLE_GENERIC_URL);
HttpResponse response = request.execute();
HttpResponseException e = new HttpResponseException(response);
ByteArrayOutputStream out = new ByteArrayOutputStream();
Expand Down