001: // $Id: FlushOnCloseTest.java 1379 2007-06-25 08:43:44Z grro $
002: /*
003: * Copyright (c) xsocket.org, 2006. All rights reserved.
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: *
019: * Please refer to the LGPL license at: http://www.gnu.org/copyleft/lesser.txt
020: * The latest copy of this software may be found on http://www.xsocket.org/
021: */
022: package org.xsocket.web.http;
023:
024: import java.io.File;
025: import java.io.FileWriter;
026: import java.io.IOException;
027: import java.nio.BufferUnderflowException;
028: import java.util.logging.Level;
029:
030: import org.junit.Assert;
031:
032: import org.junit.Test;
033: import org.xsocket.DataConverter;
034: import org.xsocket.web.http.HttpConnection;
035: import org.xsocket.web.http.IHttpConnection;
036: import org.xsocket.web.http.IRequestHeader;
037:
038: /**
039: *
040: * @author grro@xsocket.org
041: */
042: public final class HttpConnectionClientTest {
043:
044: private static final String DATA = "This is my letter to you.\r\n"
045: + "Last year I went .. .......\r\n"
046: + "bla bla .. .. ....\r\n"
047: + "........ .. . .bla bla ..\r\n" + "that's it\r\n";
048:
049: private static String filename = null;
050:
051: static {
052: try {
053: File file = File.createTempFile("test", "deleteMe");
054: file.deleteOnExit();
055:
056: FileWriter fw = new FileWriter(file);
057: fw.write(DATA);
058: fw.close();
059:
060: filename = file.getAbsolutePath();
061: } catch (Exception e) {
062: e.printStackTrace();
063: }
064: }
065:
066: @Test
067: public void testGetSynchronRead() throws Exception {
068: QAUtil.setLogLevel(Level.FINE);
069:
070: IHttpConnection con = new HttpConnection("www.web.de", 80);
071:
072: IRequestHeader requestHeader = con.createRequestHeader("GET",
073: "/");
074: con.sendMessage(requestHeader);
075:
076: QAUtil.sleep(1000);
077:
078: IResponseHeader header = (ResponseHeader) con
079: .receiveMessageHeader();
080: Assert.assertTrue(header.getStatus() == 200);
081:
082: String body = con.receiveMessageBody().readString();
083: Assert.assertTrue(body.length() > 100);
084: //System.out.println(body);
085: }
086:
087: @Test
088: public void testGetAsynchronRead() throws Exception {
089: QAUtil.setLogLevel(Level.FINE);
090:
091: IHttpConnection con = new HttpConnection("www.web.de", 80);
092:
093: IRequestHeader requestHeader = con.createRequestHeader("GET",
094: "/");
095: con.sendMessage(requestHeader);
096:
097: QAUtil.sleep(1000);
098:
099: IResponseHeader header = (ResponseHeader) con
100: .receiveMessageHeader();
101: assert (header.getStatus() == 200);
102:
103: INonBlockingReadableChannelHandler hdl = new INonBlockingReadableChannelHandler() {
104: @Override
105: public void onData(INonBlockingReadableChannel channel)
106: throws BufferUnderflowException, IOException {
107: String data = DataConverter.toString(channel
108: .readByteBuffer());
109: System.out.println(data);
110: }
111: };
112:
113: con.receiveMessageBody(hdl);
114:
115: QAUtil.sleep(1000);
116: }
117:
118: /*
119: @Test
120: public void testWritePostWithoutBody() throws Exception {
121: QAUtil.setLogLevel(Level.FINE);
122:
123: IHttpConnection con = new HttpConnection("www.web.de", 80);
124:
125: IRequestHeader requestHeader = con.createRequestHeader("POST", "/");
126: con.sendMessage(requestHeader);
127:
128: QAUtil.sleep(10000);
129: }
130:
131:
132: @Test
133: public void testWritePostPlainBody() throws Exception {
134: QAUtil.setLogLevel(Level.FINE);
135:
136: IHttpConnection con = new HttpConnection("www.web.de", 80);
137:
138: IRequestHeader requestHeader = con.createRequestHeader("POST", "/");
139: FileChannel fc = new RandomAccessFile(filename, "r").getChannel();
140:
141:
142: IWriteableBodyChannel bodyHandle = con.sendMessageHeader(requestHeader, (int) fc.size());
143: bodyHandle.transferFrom(fc);
144: bodyHandle.close();
145:
146: QAUtil.sleep(10000);
147: }
148:
149: @Test
150: public void testWritePostChunkedBody() throws Exception {
151: QAUtil.setLogLevel(Level.FINE);
152:
153: IHttpConnection con = new HttpConnection("www.web.de", 80);
154:
155: IRequestHeader requestHeader = con.createRequestHeader("POST", "/");
156: FileChannel fc = new RandomAccessFile(filename, "r").getChannel();
157:
158:
159: IWriteableBodyChannel bodyHandle = con.sendChunkedMessageHeader(requestHeader);
160: bodyHandle.transferFrom(fc);
161: bodyHandle.close();
162:
163: QAUtil.sleep(10000);
164: }*/
165: }
|