001: /*
002: * Copyright (c) xsocket.org, 2006 - 2008. All rights reserved.
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: * Please refer to the LGPL license at: http://www.gnu.org/copyleft/lesser.txt
019: * The latest copy of this software may be found on http://www.xsocket.org/
020: */
021: package org.xsocket.connection.http;
022:
023: import java.io.File;
024: import java.io.FileReader;
025: import java.io.IOException;
026: import java.io.LineNumberReader;
027: import java.io.RandomAccessFile;
028: import java.net.SocketTimeoutException;
029: import java.net.URI;
030: import java.net.URISyntaxException;
031: import java.nio.channels.FileChannel;
032:
033: import org.junit.Assert;
034: import org.junit.Test;
035:
036: import org.xsocket.Execution;
037: import org.xsocket.connection.IServer;
038: import org.xsocket.connection.Server;
039: import org.xsocket.connection.ConnectionUtils;
040: import org.xsocket.connection.IConnection.FlushMode;
041: import org.xsocket.connection.http.HttpResponse;
042: import org.xsocket.connection.http.client.GetRequest;
043: import org.xsocket.connection.http.client.HttpClient;
044: import org.xsocket.connection.http.client.IHttpClientEndpoint;
045: import org.xsocket.connection.http.client.IHttpResponseHandler;
046: import org.xsocket.connection.http.server.HttpServer;
047: import org.xsocket.connection.http.server.IHttpResponseContext;
048: import org.xsocket.connection.http.server.IHttpRequestHandler;
049:
050: /**
051: *
052: * @author grro@xsocket.org
053: */
054: public final class HttpClientGetTest {
055:
056: public static void main(String[] args) throws Exception {
057: final int port = 9728;
058: WebContainer servletEngine = new WebContainer(port,
059: new HeaderInfoServlet());
060: servletEngine.start();
061:
062: HttpClient httpclient = new HttpClient();
063: ConnectionUtils.registerMBean(httpclient);
064:
065: for (int j = 0; j < 100000000; j++) {
066: HttpResponse response = httpclient.call(new GetRequest(
067: "http://www.web.de:80/"));
068:
069: Assert.assertEquals(200, response.getStatus());
070: System.out.println(j);
071: }
072:
073: servletEngine.stop();
074: }
075:
076: @Test
077: public void testLiveGet() throws Exception {
078: IHttpClientEndpoint httpClient = new HttpClient();
079:
080: HttpResponse response = httpClient.call(new GetRequest(
081: "http://www.web.de/index.html"));
082: Assert.assertEquals(302, response.getStatus());
083: }
084:
085: @Test
086: public void testLiveGetFollowRedirect() throws Exception {
087: HttpClient httpClient = new HttpClient();
088:
089: HttpResponse response = httpClient
090: .callFollowRedirects(new GetRequest(
091: "http://www.web.de/index.html"));
092: Assert.assertEquals(200, response.getStatus());
093: }
094:
095: @Test
096: public void testLiveGetFollowRedirectSend() throws Exception {
097: HttpClient httpClient = new HttpClient();
098:
099: MultiThreadedResponseHandler hdl = new MultiThreadedResponseHandler();
100: httpClient.sendFollowRedirects(new GetRequest(
101: "http://www.web.de/index.html"), hdl);
102:
103: QAUtil.sleep(2000);
104:
105: Assert.assertEquals(200, hdl.response.getStatus());
106: }
107:
108: /*
109: @Test
110: public void testLiveGetHttps() throws Exception {
111: HttpClient httpClient = new HttpClient(SSLContext.getDefault()); // SSLContext.getDefault() -> Java 1.6!
112:
113: Response response = httpClient.callFollowRedirects(new GetRequest("https://www.web.de/"));
114: Assert.assertEquals(200, response.getStatus());
115: }
116: */
117:
118: @Test
119: public void testBlockingTransferTo() throws Exception {
120:
121: IServer server = new HttpServer(new HeaderInfoServerHandler());
122: ConnectionUtils.start(server);
123:
124: HttpClient httpClient = new HttpClient();
125:
126: HttpResponse response = httpClient.call(new GetRequest(
127: "http://localhost:" + server.getLocalPort() + "/"));
128:
129: File file = File.createTempFile("test", null);
130: file.createNewFile();
131: System.out.println("write to file " + file.getAbsolutePath());
132:
133: FileChannel fc = new RandomAccessFile(file, "rw").getChannel();
134: response.getBlockingBody().transferTo(fc);
135: fc.close();
136:
137: LineNumberReader lnr = new LineNumberReader(
138: new FileReader(file));
139: String line = lnr.readLine();
140:
141: Assert.assertEquals("method= GET", line);
142:
143: server.close();
144: }
145:
146: @Test
147: public void testCallTimeout() throws Exception {
148: IServer server = new Server(new DevNullHandler());
149: ConnectionUtils.start(server);
150:
151: HttpClient httpClient = new HttpClient();
152: httpClient.setResponseTimeoutMillis(500);
153:
154: try {
155: httpClient.call(new GetRequest("http://localhost:"
156: + server.getLocalPort() + "/"));
157: Assert.fail("timeout exepction shoud haven been thrown");
158: } catch (SocketTimeoutException expeceted) {
159: }
160:
161: Assert.assertEquals(1, httpClient.getNumCreated());
162: Assert.assertEquals(1, httpClient.getNumDestroyed());
163: Assert.assertEquals(0, httpClient.getIdleConnectionInfos()
164: .size());
165:
166: httpClient.close();
167: server.close();
168: }
169:
170: @Test
171: public void testHeaders() throws Exception {
172: HttpClient httpClient = new HttpClient();
173: ConnectionUtils.registerMBean(httpClient);
174:
175: IServer server = new HttpServer(new HeaderInfoServerHandler());
176: ConnectionUtils.start(server);
177:
178: for (int i = 0; i < 5; i++) {
179: GetRequest request = new GetRequest("http://localhost:"
180: + server.getLocalPort() + "/");
181: request.addHeader("header1", "value1");
182: request.addHeader("header2", "value2");
183:
184: HttpResponse response = httpClient.call(request);
185: String body = response.getBlockingBody().toString();
186:
187: Assert.assertTrue(body.indexOf("header1: value1") != -1);
188: Assert.assertTrue(body.indexOf("header2: value2") != -1);
189: }
190:
191: httpClient.close();
192: server.close();
193: }
194:
195: @Test
196: public void testLiveRedirect() throws Exception {
197: HttpClient httpClient = new HttpClient();
198: ConnectionUtils.registerMBean(httpClient);
199:
200: HttpResponse response = httpClient
201: .callFollowRedirects(new GetRequest(
202: "http://www.web.de:80/invalidpath"));
203: String body = response.getBlockingBody().readString();
204:
205: Assert
206: .assertTrue(body
207: .indexOf("Die gew\u00FCnschte Seite wurde leider nicht gefunden") != -1);
208: }
209:
210: @Test
211: public void testInvokeOnHeader() throws Exception {
212:
213: int delay = 500;
214: IServer server = new HttpServer(new DelayServerHandler(delay));
215: ConnectionUtils.start(server);
216:
217: HttpClient httpClient = new HttpClient();
218: HttpResponse response = httpClient.call(new GetRequest(
219: "http://localhost:" + server.getLocalPort() + "/"));
220:
221: Assert.assertFalse(response.getNonBlockingBody().isComplete());
222:
223: QAUtil.sleep(delay * 2);
224:
225: Assert.assertTrue(response.getNonBlockingBody().isComplete());
226:
227: /*
228: HttpClient httpClient = new HttpClient();
229: GetMethod getMeth = new GetMethod("http://localhost:" + server.getLocalPort() + "/test?size=10000&rate=45");
230: httpClient.executeMethod(getMeth);
231: int statusCode = getMeth.getStatusCode();
232:
233: */
234:
235: httpClient.close();
236: //con.close();
237: server.close();
238: }
239:
240: /* @Test
241: public void testSimpleCompare() throws Exception {
242:
243: ItWorksServer server = new ItWorksServer(0);
244: ConnectionUtils.start(server);
245:
246: // warm up
247: call("localhost", server.getLocalPort(), 20, true);
248: call("localhost", server.getLocalPort(), 20, false);
249:
250: long start = System.currentTimeMillis();
251: call("localhost", server.getLocalPort(), 2000, true);
252: long elapsedPooled = System.currentTimeMillis() - start;
253:
254: start = System.currentTimeMillis();
255: call("localhost", server.getLocalPort(), 2000, false);
256: long elapsedUnpooled = System.currentTimeMillis() - start;
257:
258:
259: System.out.println("\r\npooled " + DataConverter.toFormatedDuration(elapsedPooled) + " " +
260: " unpooled " + DataConverter.toFormatedDuration(elapsedUnpooled));
261:
262: server.close();
263:
264: Assert.assertTrue(elapsedPooled < elapsedUnpooled);
265: }
266: */
267:
268: private void call(String hostname, int port, int loops,
269: boolean isPooled) throws IOException, URISyntaxException {
270:
271: HttpClient httpClient = new HttpClient();
272: httpClient.setPooled(isPooled);
273:
274: for (int i = 0; i < loops; i++) {
275: HttpResponse response = httpClient.call(new GetRequest(
276: "http://" + hostname + ":" + port + "/"));
277: Assert.assertEquals(200, response.getStatus());
278: }
279:
280: httpClient.close();
281: }
282:
283: @Test
284: public void testNonThreadedAsynchGet() throws Exception {
285:
286: IServer server = new HttpServer(new HeaderInfoServerHandler());
287: ConnectionUtils.start(server);
288:
289: IHttpClientEndpoint httpClient = new HttpClient();
290:
291: NonThreadedResponseHandler hdl = new NonThreadedResponseHandler();
292: httpClient.send(new GetRequest("http://localhost:"
293: + server.getLocalPort() + "/"), hdl);
294:
295: QAUtil.sleep(500);
296:
297: HttpResponse response = hdl.getResponse();
298:
299: httpClient.close();
300: server.close();
301:
302: Assert.assertTrue(hdl.threadname.startsWith("xDispatcher"));
303: Assert.assertEquals(200, response.getStatus());
304: }
305:
306: @Test
307: public void testCallMaxRedirect() throws Exception {
308:
309: IServer server = new HttpServer(new RedirectLoopServerHandler());
310: ConnectionUtils.start(server);
311:
312: HttpClient httpClient = new HttpClient();
313:
314: try {
315: HttpResponse response = httpClient
316: .callFollowRedirects(new GetRequest(
317: "http://localhost:" + server.getLocalPort()
318: + "/test"));
319: Assert
320: .fail("max redirect exception should have been thrown");
321: } catch (IOException maxRedirect) {
322: System.out.println(".");
323: }
324:
325: server.close();
326: }
327:
328: @Test
329: public void testCallRelativeRedirect() throws Exception {
330:
331: IServer server = new HttpServer(new RelativeRedirectHandler());
332: ConnectionUtils.start(server);
333:
334: HttpClient httpClient = new HttpClient();
335:
336: HttpResponse response = httpClient
337: .callFollowRedirects(new GetRequest("http://localhost:"
338: + server.getLocalPort() + "/test"));
339: Assert.assertEquals(200, response.getStatus());
340:
341: server.close();
342: }
343:
344: @Test
345: public void testSendMaxRedirect() throws Exception {
346:
347: IServer server = new HttpServer(new RedirectLoopServerHandler());
348: ConnectionUtils.start(server);
349:
350: HttpClient httpClient = new HttpClient();
351:
352: NonThreadedResponseHandler hdl = new NonThreadedResponseHandler();
353:
354: httpClient.sendFollowRedirects(new GetRequest(
355: "http://localhost:" + server.getLocalPort() + "/test"),
356: hdl);
357: Assert.assertNull(hdl.getResponse()); // redirect loop should bee terminated -> response is null
358:
359: server.close();
360: }
361:
362: @Test
363: public void testLiveNonThreadedAsynchGetRedirect() throws Exception {
364: HttpClient httpClient = new HttpClient();
365:
366: NonThreadedResponseHandler hdl = new NonThreadedResponseHandler();
367: httpClient.sendFollowRedirects(new GetRequest(
368: "http://www.web.de:80/invalidpath"), hdl);
369:
370: QAUtil.sleep(1000);
371:
372: Assert
373: .assertTrue(hdl.getThreadname().startsWith(
374: "xDispatcher"));
375: Assert
376: .assertTrue(hdl
377: .getResponse()
378: .getBlockingBody()
379: .readString()
380: .indexOf(
381: "Die gew\u00FCnschte Seite wurde leider nicht gefunden") != -1);
382: Assert.assertEquals(200, hdl.getResponse().getStatus());
383: }
384:
385: @Test
386: public void testLiveMultiThreadedAsynchGetRedirect()
387: throws Exception {
388:
389: HttpClient httpClient = new HttpClient();
390:
391: MultiThreadedResponseHandler hdl = new MultiThreadedResponseHandler();
392: httpClient.sendFollowRedirects(new GetRequest(
393: "http://www.web.de:80/invalidpath"), hdl);
394:
395: QAUtil.sleep(1000);
396:
397: HttpResponse response = hdl.getResponse();
398:
399: Assert.assertFalse(hdl.threadname.startsWith("xDispatcher"));
400: Assert
401: .assertTrue(hdl.response
402: .getBlockingBody()
403: .readString()
404: .indexOf(
405: "Die gew\u00FCnschte Seite wurde leider nicht gefunden") != -1);
406: Assert.assertEquals(200, response.getStatus());
407: }
408:
409: @Test
410: public void testMultiThreadedAsynchGet() throws Exception {
411:
412: IServer server = new HttpServer(new HeaderInfoServerHandler());
413: ConnectionUtils.start(server);
414:
415: HttpClient httpClient = new HttpClient();
416:
417: Assert.assertEquals(0, httpClient.getActiveConnectionInfos()
418: .size());
419: Assert.assertEquals(0, httpClient.getIdleConnectionInfos()
420: .size());
421:
422: MultiThreadedResponseHandler hdl = new MultiThreadedResponseHandler();
423: httpClient.send(new GetRequest("http://localhost:"
424: + server.getLocalPort() + "/"), hdl);
425:
426: QAUtil.sleep(500);
427:
428: Assert.assertEquals(0, httpClient.getActiveConnectionInfos()
429: .size());
430: Assert.assertEquals(1, httpClient.getIdleConnectionInfos()
431: .size());
432:
433: HttpResponse response = hdl.getResponse();
434:
435: httpClient.close();
436: server.close();
437:
438: Assert.assertFalse(hdl.threadname.startsWith("xDispatcher"));
439: Assert.assertEquals(200, response.getStatus());
440: }
441:
442: @Execution(Execution.NONTHREADED)
443: private static final class NonThreadedResponseHandler implements
444: IHttpResponseHandler {
445:
446: private String threadname = null;
447: private HttpResponse response = null;
448:
449: public void onResponse(HttpResponse response)
450: throws IOException {
451: threadname = Thread.currentThread().getName();
452: this .response = response;
453: }
454:
455: public HttpResponse getResponse() {
456: return response;
457: }
458:
459: String getThreadname() {
460: return threadname;
461: }
462: }
463:
464: @Execution(Execution.MULTITHREADED)
465: private static final class MultiThreadedResponseHandler implements
466: IHttpResponseHandler {
467:
468: private String threadname = null;
469: private HttpResponse response = null;
470:
471: public void onResponse(HttpResponse response)
472: throws IOException {
473: threadname = Thread.currentThread().getName();
474: this .response = response;
475: }
476:
477: public HttpResponse getResponse() {
478: return response;
479: }
480:
481: String getThreadname() {
482: return threadname;
483: }
484: }
485:
486: private static final class DelayServerHandler implements
487: IHttpRequestHandler {
488:
489: private int delay = 0;
490:
491: public DelayServerHandler(int delay) {
492: this .delay = delay;
493: }
494:
495: public void onRequest(HttpRequest request,
496: IHttpResponseContext httpServerEndpoint)
497: throws IOException {
498:
499: int size = 400;
500:
501: BodyDataSink bodyDataSink = httpServerEndpoint
502: .send(new HttpResponseHeader(200, "text/plain",
503: size));
504: bodyDataSink.setFlushmode(FlushMode.ASYNC);
505:
506: bodyDataSink.write((byte) 45);
507:
508: QAUtil.sleep(delay);
509:
510: byte[] bytes = new byte[size - 1];
511: for (int i = 0; i < bytes.length; i++) {
512: bytes[i] = 45;
513: }
514: bodyDataSink.write(bytes);
515: }
516: }
517:
518: private static final class RedirectLoopServerHandler implements
519: IHttpRequestHandler {
520:
521: public void onRequest(HttpRequest request,
522: IHttpResponseContext httpServerEndpoint)
523: throws IOException {
524: URI targetURL = request.getTargetURI();
525:
526: HttpResponse response = new HttpResponse(302);
527: response.setHeader("Location", targetURL.toString());
528: httpServerEndpoint.send(response);
529: }
530: }
531:
532: private static final class RelativeRedirectHandler implements
533: IHttpRequestHandler {
534:
535: public void onRequest(HttpRequest request,
536: IHttpResponseContext httpServerEndpoint)
537: throws IOException {
538:
539: if (request.getRequestURI().equals("/test")) {
540: HttpResponse response = new HttpResponse(302);
541: response.setHeader("Location", "/redirected");
542: httpServerEndpoint.send(response);
543:
544: } else if (request.getRequestURI().equals("/redirected")) {
545: httpServerEndpoint.send(200);
546:
547: } else {
548: httpServerEndpoint.sendError(500);
549: }
550: }
551: }
552: }
|