Skip to content

Commit bb0a408

Browse files
author
Yaniv Inbar
committed
NetHttpResponse.getStatusCode() should return 0 not -1
http://codereview.appspot.com/5728066/
1 parent 7b6e081 commit bb0a408

File tree

7 files changed

+159
-13
lines changed

7 files changed

+159
-13
lines changed

google-http-client/src/main/java/com/google/api/client/http/HttpResponse.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ public boolean isSuccessStatusCode() {
318318
}
319319

320320
/**
321-
* Returns the HTTP status code.
321+
* Returns the HTTP status code or {@code 0} for none.
322322
*
323323
* @since 1.5
324324
*/

google-http-client/src/main/java/com/google/api/client/http/HttpResponseException.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public boolean isSuccessStatusCode() {
8383
}
8484

8585
/**
86-
* Returns the HTTP status code.
86+
* Returns the HTTP status code or {@code 0} for none.
8787
*
8888
* @since 1.7
8989
*/
@@ -135,10 +135,16 @@ private static String computeMessageWithContent(HttpResponse response) {
135135
*/
136136
public static StringBuilder computeMessageBuffer(HttpResponse response) {
137137
StringBuilder builder = new StringBuilder();
138-
builder.append(response.getStatusCode());
138+
int statusCode = response.getStatusCode();
139+
if (statusCode != 0) {
140+
builder.append(statusCode);
141+
}
139142
String statusMessage = response.getStatusMessage();
140143
if (statusMessage != null) {
141-
builder.append(' ').append(statusMessage);
144+
if (statusCode != 0) {
145+
builder.append(' ');
146+
}
147+
builder.append(statusMessage);
142148
}
143149
return builder;
144150
}

google-http-client/src/main/java/com/google/api/client/http/javanet/NetHttpResponse.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ final class NetHttpResponse extends LowLevelHttpResponse {
3434

3535
NetHttpResponse(HttpURLConnection connection) throws IOException {
3636
this.connection = connection;
37-
responseCode = connection.getResponseCode();
37+
int responseCode = connection.getResponseCode();
38+
this.responseCode = responseCode == -1 ? 0 : responseCode;
3839
responseMessage = connection.getResponseMessage();
3940
List<String> headerNames = this.headerNames;
4041
List<String> headerValues = this.headerValues;

google-http-client/src/main/java/com/google/api/client/testing/http/MockLowLevelHttpResponse.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ public class MockLowLevelHttpResponse extends LowLevelHttpResponse {
5050
private String reasonPhrase;
5151

5252
/** List of header names of HTTP response (empty array list by default). */
53-
private ArrayList<String> headerNames = new ArrayList<String>();
53+
private List<String> headerNames = new ArrayList<String>();
5454

5555
/** List of header values of HTTP response (empty array list by default). */
56-
private ArrayList<String> headerValues = new ArrayList<String>();
56+
private List<String> headerValues = new ArrayList<String>();
5757

5858
/** Content encoding or {@code null} for none. */
5959
private String contentEncoding;
@@ -154,8 +154,7 @@ public final List<String> getHeaderNames() {
154154
*
155155
* @since 1.5
156156
*/
157-
public MockLowLevelHttpResponse setHeaderNames(ArrayList<String> headerNames) {
158-
// TODO(yanivi): change parameter to a List in 1.6
157+
public MockLowLevelHttpResponse setHeaderNames(List<String> headerNames) {
159158
this.headerNames = Preconditions.checkNotNull(headerNames);
160159
return this;
161160
}
@@ -178,8 +177,7 @@ public final List<String> getHeaderValues() {
178177
*
179178
* @since 1.5
180179
*/
181-
public MockLowLevelHttpResponse setHeaderValues(ArrayList<String> headerValues) {
182-
// TODO(yanivi): change parameter to a List in 1.6
180+
public MockLowLevelHttpResponse setHeaderValues(List<String> headerValues) {
183181
this.headerValues = Preconditions.checkNotNull(headerValues);
184182
return this;
185183
}

google-http-client/src/test/java/com/google/api/client/http/HttpResponseExceptionTest.java

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,55 @@ public void testConstructorWithMessage() throws IOException {
5454
assertEquals("foo", e.getMessage());
5555
}
5656

57+
public void testConstructor_noStatusCode() throws IOException {
58+
HttpTransport transport = new MockHttpTransport() {
59+
@Override
60+
public LowLevelHttpRequest buildGetRequest(String url) throws IOException {
61+
return new MockLowLevelHttpRequest() {
62+
@Override
63+
public LowLevelHttpResponse execute() throws IOException {
64+
MockLowLevelHttpResponse result = new MockLowLevelHttpResponse();
65+
result.setStatusCode(0);
66+
return result;
67+
}
68+
};
69+
}
70+
};
71+
HttpRequest request =
72+
transport.createRequestFactory().buildGetRequest(HttpTesting.SIMPLE_GENERIC_URL);
73+
try {
74+
request.execute();
75+
fail();
76+
} catch (HttpResponseException e) {
77+
assertEquals("", e.getMessage());
78+
}
79+
}
80+
81+
public void testConstructor_messageButNoStatusCode() throws IOException {
82+
HttpTransport transport = new MockHttpTransport() {
83+
@Override
84+
public LowLevelHttpRequest buildGetRequest(String url) throws IOException {
85+
return new MockLowLevelHttpRequest() {
86+
@Override
87+
public LowLevelHttpResponse execute() throws IOException {
88+
MockLowLevelHttpResponse result = new MockLowLevelHttpResponse();
89+
result.setStatusCode(0);
90+
result.setReasonPhrase("Foo");
91+
return result;
92+
}
93+
};
94+
}
95+
};
96+
HttpRequest request =
97+
transport.createRequestFactory().buildGetRequest(HttpTesting.SIMPLE_GENERIC_URL);
98+
try {
99+
request.execute();
100+
fail();
101+
} catch (HttpResponseException e) {
102+
assertEquals("Foo", e.getMessage());
103+
}
104+
}
105+
57106
@SuppressWarnings("deprecation")
58107
public void testComputeMessage() throws IOException {
59108
HttpTransport transport = new MockHttpTransport() {
@@ -97,8 +146,8 @@ public LowLevelHttpResponse execute() throws IOException {
97146
request.execute();
98147
fail();
99148
} catch (HttpResponseException e) {
100-
assertEquals("404 Not Found" + Strings.LINE_SEPARATOR + "Unable to find resource",
101-
e.getMessage());
149+
assertEquals(
150+
"404 Not Found" + Strings.LINE_SEPARATOR + "Unable to find resource", e.getMessage());
102151
}
103152
}
104153

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright (c) 2012 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
15+
package com.google.api.client.http.javanet;
16+
17+
import junit.framework.TestCase;
18+
19+
import java.io.IOException;
20+
import java.net.HttpURLConnection;
21+
22+
/**
23+
* Tests {@link NetHttpResponse}.
24+
*
25+
* @author Yaniv Inbar
26+
*/
27+
public class NetHttpResponseTest extends TestCase {
28+
29+
static class MockHttpURLConnection extends HttpURLConnection {
30+
31+
private final int responseCode;
32+
33+
protected MockHttpURLConnection(int responseCode) {
34+
super(null);
35+
this.responseCode = responseCode;
36+
}
37+
38+
@Override
39+
public void disconnect() {
40+
}
41+
42+
@Override
43+
public boolean usingProxy() {
44+
return false;
45+
}
46+
47+
@Override
48+
public void connect() throws IOException {
49+
}
50+
51+
@Override
52+
public int getResponseCode() throws IOException {
53+
return responseCode;
54+
}
55+
56+
}
57+
58+
public void testGetStatusCode() throws IOException {
59+
assertEquals(0, new NetHttpResponse(new MockHttpURLConnection(-1)).getStatusCode());
60+
assertEquals(200, new NetHttpResponse(new MockHttpURLConnection(200)).getStatusCode());
61+
}
62+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright (c) 2012 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
15+
package com.google.api.client.http.javanet;
16+
17+
import junit.framework.TestCase;
18+
19+
/**
20+
* Tests {@link NetHttpTransport}.
21+
*
22+
* @author Yaniv Inbar
23+
*/
24+
public class NetHttpTransportTest extends TestCase {
25+
26+
public void testSupportsHead() {
27+
NetHttpTransport transport = new NetHttpTransport();
28+
assertTrue(transport.supportsHead());
29+
}
30+
}

0 commit comments

Comments
 (0)