14
14
15
15
package com .google .api .client .http ;
16
16
17
+ import static com .google .api .client .testing .http .HttpTesting .SIMPLE_GENERIC_URL ;
18
+ import static com .google .api .client .util .StringUtils .LINE_SEPARATOR ;
19
+ import static com .google .common .truth .Truth .assertThat ;
20
+ import static org .junit .Assert .assertThrows ;
21
+
17
22
import com .google .api .client .http .HttpResponseException .Builder ;
18
- import com .google .api .client .testing .http .HttpTesting ;
19
23
import com .google .api .client .testing .http .MockHttpTransport ;
20
24
import com .google .api .client .testing .http .MockLowLevelHttpRequest ;
21
25
import com .google .api .client .testing .http .MockLowLevelHttpResponse ;
22
- import com .google .api .client .util .StringUtils ;
23
26
import java .io .ByteArrayInputStream ;
24
27
import java .io .ByteArrayOutputStream ;
25
28
import java .io .IOException ;
26
29
import java .io .ObjectInputStream ;
27
30
import java .io .ObjectOutput ;
28
31
import java .io .ObjectOutputStream ;
29
32
import junit .framework .TestCase ;
33
+ import org .junit .function .ThrowingRunnable ;
30
34
31
35
/**
32
36
* Tests {@link HttpResponseException}.
@@ -37,16 +41,15 @@ public class HttpResponseExceptionTest extends TestCase {
37
41
38
42
public void testConstructor () throws Exception {
39
43
HttpTransport transport = new MockHttpTransport ();
40
- HttpRequest request =
41
- transport .createRequestFactory ().buildGetRequest (HttpTesting .SIMPLE_GENERIC_URL );
44
+ HttpRequest request = transport .createRequestFactory ().buildGetRequest (SIMPLE_GENERIC_URL );
42
45
HttpResponse response = request .execute ();
43
46
HttpHeaders headers = response .getHeaders ();
44
- HttpResponseException e = new HttpResponseException (response );
45
- assertEquals ( "200" , e . getMessage () );
46
- assertNull (e .getContent ());
47
- assertEquals (200 , e .getStatusCode ());
48
- assertNull (e .getStatusMessage ());
49
- assertTrue (headers == e .getHeaders ());
47
+ HttpResponseException responseException = new HttpResponseException (response );
48
+ assertThat ( responseException ). hasMessageThat (). isEqualTo ( "200\n GET " + SIMPLE_GENERIC_URL );
49
+ assertNull (responseException .getContent ());
50
+ assertEquals (200 , responseException .getStatusCode ());
51
+ assertNull (responseException .getStatusMessage ());
52
+ assertTrue (headers == responseException .getHeaders ());
50
53
}
51
54
52
55
public void testBuilder () throws Exception {
@@ -83,11 +86,10 @@ public LowLevelHttpResponse execute() throws IOException {
83
86
};
84
87
}
85
88
};
86
- HttpRequest request =
87
- transport .createRequestFactory ().buildGetRequest (HttpTesting .SIMPLE_GENERIC_URL );
89
+ HttpRequest request = transport .createRequestFactory ().buildGetRequest (SIMPLE_GENERIC_URL );
88
90
HttpResponse response = request .execute ();
89
- HttpResponseException e = new HttpResponseException (response );
90
- assertEquals ("OK" , e .getStatusMessage ());
91
+ HttpResponseException responseException = new HttpResponseException (response );
92
+ assertEquals ("OK" , responseException .getStatusMessage ());
91
93
}
92
94
93
95
public void testConstructor_noStatusCode () throws Exception {
@@ -105,14 +107,18 @@ public LowLevelHttpResponse execute() throws IOException {
105
107
};
106
108
}
107
109
};
108
- HttpRequest request =
109
- transport .createRequestFactory ().buildGetRequest (HttpTesting .SIMPLE_GENERIC_URL );
110
- try {
111
- request .execute ();
112
- fail ();
113
- } catch (HttpResponseException e ) {
114
- assertEquals ("" , e .getMessage ());
115
- }
110
+ final HttpRequest request =
111
+ transport .createRequestFactory ().buildGetRequest (SIMPLE_GENERIC_URL );
112
+ HttpResponseException responseException =
113
+ assertThrows (
114
+ HttpResponseException .class ,
115
+ new ThrowingRunnable () {
116
+ @ Override
117
+ public void run () throws Throwable {
118
+ request .execute ();
119
+ }
120
+ });
121
+ assertThat (responseException ).hasMessageThat ().isEqualTo ("GET " + SIMPLE_GENERIC_URL );
116
122
}
117
123
118
124
public void testConstructor_messageButNoStatusCode () throws Exception {
@@ -131,14 +137,18 @@ public LowLevelHttpResponse execute() throws IOException {
131
137
};
132
138
}
133
139
};
134
- HttpRequest request =
135
- transport .createRequestFactory ().buildGetRequest (HttpTesting .SIMPLE_GENERIC_URL );
136
- try {
137
- request .execute ();
138
- fail ();
139
- } catch (HttpResponseException e ) {
140
- assertEquals ("Foo" , e .getMessage ());
141
- }
140
+ final HttpRequest request =
141
+ transport .createRequestFactory ().buildGetRequest (SIMPLE_GENERIC_URL );
142
+ HttpResponseException responseException =
143
+ assertThrows (
144
+ HttpResponseException .class ,
145
+ new ThrowingRunnable () {
146
+ @ Override
147
+ public void run () throws Throwable {
148
+ request .execute ();
149
+ }
150
+ });
151
+ assertThat (responseException ).hasMessageThat ().isEqualTo ("Foo\n GET " + SIMPLE_GENERIC_URL );
142
152
}
143
153
144
154
public void testComputeMessage () throws Exception {
@@ -156,10 +166,10 @@ public LowLevelHttpResponse execute() throws IOException {
156
166
};
157
167
}
158
168
};
159
- HttpRequest request =
160
- transport .createRequestFactory ().buildGetRequest (HttpTesting .SIMPLE_GENERIC_URL );
169
+ HttpRequest request = transport .createRequestFactory ().buildGetRequest (SIMPLE_GENERIC_URL );
161
170
HttpResponse response = request .execute ();
162
- assertEquals ("200 Foo" , HttpResponseException .computeMessageBuffer (response ).toString ());
171
+ assertThat (HttpResponseException .computeMessageBuffer (response ).toString ())
172
+ .isEqualTo ("200 Foo\n GET " + SIMPLE_GENERIC_URL );
163
173
}
164
174
165
175
public void testThrown () throws Exception {
@@ -179,15 +189,25 @@ public LowLevelHttpResponse execute() throws IOException {
179
189
};
180
190
}
181
191
};
182
- HttpRequest request =
183
- transport .createRequestFactory ().buildGetRequest (HttpTesting .SIMPLE_GENERIC_URL );
184
- try {
185
- request .execute ();
186
- fail ();
187
- } catch (HttpResponseException e ) {
188
- assertEquals (
189
- "404 Not Found" + StringUtils .LINE_SEPARATOR + "Unable to find resource" , e .getMessage ());
190
- }
192
+ final HttpRequest request =
193
+ transport .createRequestFactory ().buildGetRequest (SIMPLE_GENERIC_URL );
194
+ HttpResponseException responseException =
195
+ assertThrows (
196
+ HttpResponseException .class ,
197
+ new ThrowingRunnable () {
198
+ @ Override
199
+ public void run () throws Throwable {
200
+ request .execute ();
201
+ }
202
+ });
203
+
204
+ assertThat (responseException )
205
+ .hasMessageThat ()
206
+ .isEqualTo (
207
+ "404 Not Found\n GET "
208
+ + SIMPLE_GENERIC_URL
209
+ + LINE_SEPARATOR
210
+ + "Unable to find resource" );
191
211
}
192
212
193
213
public void testInvalidCharset () throws Exception {
@@ -208,14 +228,21 @@ public LowLevelHttpResponse execute() throws IOException {
208
228
};
209
229
}
210
230
};
211
- HttpRequest request =
212
- transport .createRequestFactory ().buildGetRequest (HttpTesting .SIMPLE_GENERIC_URL );
213
- try {
214
- request .execute ();
215
- fail ();
216
- } catch (HttpResponseException e ) {
217
- assertEquals ("404 Not Found" , e .getMessage ());
218
- }
231
+ final HttpRequest request =
232
+ transport .createRequestFactory ().buildGetRequest (SIMPLE_GENERIC_URL );
233
+ HttpResponseException responseException =
234
+ assertThrows (
235
+ HttpResponseException .class ,
236
+ new ThrowingRunnable () {
237
+ @ Override
238
+ public void run () throws Throwable {
239
+ request .execute ();
240
+ }
241
+ });
242
+
243
+ assertThat (responseException )
244
+ .hasMessageThat ()
245
+ .isEqualTo ("404 Not Found\n GET " + SIMPLE_GENERIC_URL );
219
246
}
220
247
221
248
public void testUnsupportedCharset () throws Exception {
@@ -236,30 +263,35 @@ public LowLevelHttpResponse execute() throws IOException {
236
263
};
237
264
}
238
265
};
239
- HttpRequest request =
240
- transport .createRequestFactory ().buildGetRequest (HttpTesting .SIMPLE_GENERIC_URL );
241
- try {
242
- request .execute ();
243
- fail ();
244
- } catch (HttpResponseException e ) {
245
- assertEquals ("404 Not Found" , e .getMessage ());
246
- }
266
+ final HttpRequest request =
267
+ transport .createRequestFactory ().buildGetRequest (SIMPLE_GENERIC_URL );
268
+ HttpResponseException responseException =
269
+ assertThrows (
270
+ HttpResponseException .class ,
271
+ new ThrowingRunnable () {
272
+ @ Override
273
+ public void run () throws Throwable {
274
+ request .execute ();
275
+ }
276
+ });
277
+ assertThat (responseException )
278
+ .hasMessageThat ()
279
+ .isEqualTo ("404 Not Found\n GET " + SIMPLE_GENERIC_URL );
247
280
}
248
281
249
282
public void testSerialization () throws Exception {
250
283
HttpTransport transport = new MockHttpTransport ();
251
- HttpRequest request =
252
- transport .createRequestFactory ().buildGetRequest (HttpTesting .SIMPLE_GENERIC_URL );
284
+ HttpRequest request = transport .createRequestFactory ().buildGetRequest (SIMPLE_GENERIC_URL );
253
285
HttpResponse response = request .execute ();
254
- HttpResponseException e = new HttpResponseException (response );
286
+ HttpResponseException responseException = new HttpResponseException (response );
255
287
ByteArrayOutputStream out = new ByteArrayOutputStream ();
256
288
ObjectOutput s = new ObjectOutputStream (out );
257
- s .writeObject (e );
289
+ s .writeObject (responseException );
258
290
ByteArrayInputStream in = new ByteArrayInputStream (out .toByteArray ());
259
291
ObjectInputStream objectInput = new ObjectInputStream (in );
260
292
HttpResponseException e2 = (HttpResponseException ) objectInput .readObject ();
261
- assertEquals (e .getMessage (), e2 .getMessage ());
262
- assertEquals (e .getStatusCode (), e2 .getStatusCode ());
293
+ assertEquals (responseException .getMessage (), e2 .getMessage ());
294
+ assertEquals (responseException .getStatusCode (), e2 .getStatusCode ());
263
295
assertNull (e2 .getHeaders ());
264
296
}
265
297
}
0 commit comments