001: // Copyright (C) 2003,2004,2005 by Object Mentor, Inc. All rights reserved.
002: // Released under the terms of the GNU General Public License version 2 or later.
003: package fitnesse.http;
004:
005: import fitnesse.testutil.AbstractRegex;
006: import java.io.ByteArrayInputStream;
007:
008: public class RequestBuilderTest extends AbstractRegex {
009: private RequestBuilder builder;
010:
011: public void setUp() {
012: builder = new RequestBuilder("/");
013: }
014:
015: public void testDeafultValues() throws Exception {
016: builder = new RequestBuilder("/someResource");
017: String text = builder.getText();
018: assertHasRegexp("GET /someResource HTTP/1.1\r\n", text);
019: }
020:
021: public void testHostHeader_RFC2616_section_14_23() throws Exception {
022: builder = new RequestBuilder("/someResource");
023: String text = builder.getText();
024: assertSubString("Host: \r\n", text);
025:
026: builder.setHostAndPort("some.host.com", 123);
027: text = builder.getText();
028: assertSubString("Host: some.host.com:123\r\n", text);
029: }
030:
031: public void testChangingMethod() throws Exception {
032: builder.setMethod("POST");
033: String text = builder.getText();
034: assertHasRegexp("POST / HTTP/1.1\r\n", text);
035: }
036:
037: public void testAddInput() throws Exception {
038: builder.addInput("responder", "saveData");
039: String content = "!fixture fit.ColumnFixture\n" + "\n"
040: + "!path classes\n" + "\n" + "!2 ";
041: builder.addInput("pageContent", content);
042:
043: String inputString = builder.inputString();
044: assertSubString("responder=saveData", inputString);
045: assertSubString(
046: "pageContent=%21fixture+fit.ColumnFixture%0A%0A%21path+classes%0A%0A%212+",
047: inputString);
048: assertSubString("&", inputString);
049: }
050:
051: public void testGETMethodWithInputs() throws Exception {
052: builder.addInput("key", "value");
053: String text = builder.getText();
054: assertSubString("GET /?key=value HTTP/1.1\r\n", text);
055: }
056:
057: public void testPOSTMethodWithInputs() throws Exception {
058: builder.setMethod("POST");
059: builder.addInput("key", "value");
060: String text = builder.getText();
061: assertSubString("POST / HTTP/1.1\r\n", text);
062: assertSubString("key=value", text);
063: }
064:
065: public void testAddingCredentials() throws Exception {
066: builder.addCredentials("Aladdin", "open sesame");
067: assertSubString(
068: "Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==",
069: builder.getText());
070: }
071:
072: public void testGetBoundary() throws Exception {
073: String boundary = builder.getBoundary();
074:
075: assertEquals(boundary, builder.getBoundary());
076: Thread.sleep(10);
077: assertFalse(boundary.equals(new RequestBuilder("blah")
078: .getBoundary()));
079: }
080:
081: public void testMultipartOnePart() throws Exception {
082: builder.addInputAsPart("myPart", "part data");
083: String text = builder.getText();
084:
085: assertSubString("POST", text);
086: assertSubString("Content-Type: multipart/form-data; boundary=",
087: text);
088: String boundary = builder.getBoundary();
089: assertSubString("--" + boundary, text);
090: assertSubString("\r\n\r\npart data\r\n", text);
091: assertSubString("--" + boundary + "--", text);
092: }
093:
094: public void testMultipartWithInputStream() throws Exception {
095: ByteArrayInputStream input = new ByteArrayInputStream(
096: "data from input stream".getBytes());
097: builder.addInputAsPart("input", input, 89, "text/html");
098: String text = builder.getText();
099:
100: assertSubString("Content-Type: text/html", text);
101: assertSubString("\r\n\r\ndata from input stream\r\n", text);
102: }
103:
104: public void testMultipartWithRequestParser() throws Exception {
105: builder.addInputAsPart("part1", "data 1");
106: builder.addInput("input1", "input1 value");
107: builder.addInputAsPart("part2", "data 2");
108: String text = builder.getText();
109:
110: Request request = new Request(new ByteArrayInputStream(text
111: .getBytes()));
112: request.parse();
113: assertEquals("data 1", request.getInput("part1"));
114: assertEquals("data 2", request.getInput("part2"));
115: assertEquals("input1 value", request.getInput("input1"));
116: }
117: }
|