• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package fi.iki.elonen;
2 
3 import org.junit.Test;
4 
5 import java.io.BufferedReader;
6 import java.io.FileReader;
7 import java.util.ArrayList;
8 import java.util.Arrays;
9 import java.util.List;
10 import java.util.UUID;
11 
12 import static junit.framework.Assert.assertEquals;
13 
14 public class HttpPostRequestTest extends HttpServerTest {
15 
16     public static final String CONTENT_LENGTH = "Content-Length: ";
17     public static final String FIELD = "caption";
18     public static final String VALUE = "Summer vacation";
19     public static final String FIELD2 = "location";
20     public static final String VALUE2 = "Grand Canyon";
21     public static final String POST_RAW_CONTENT_FILE_ENTRY = "postData";
22     public static final String VALUE_TEST_SIMPLE_RAW_DATA_WITH_AMPHASIS = "Test raw data & Result value";
23 
24     @Test
testSimpleRawPostData()25     public void testSimpleRawPostData() throws Exception {
26         String header = "POST " + URI + " HTTP/1.1\n";
27         String content = VALUE_TEST_SIMPLE_RAW_DATA_WITH_AMPHASIS + "\n";
28         int size = content.length() + header.length();
29         int contentLengthHeaderValueSize = String.valueOf(size).length();
30         int contentLength = size + contentLengthHeaderValueSize + CONTENT_LENGTH.length();
31         String input = header + CONTENT_LENGTH + (contentLength+4) + "\r\n\r\n" + content;
32         invokeServer(input);
33         assertEquals(0, testServer.parms.size());
34         assertEquals(1, testServer.files.size());
35         assertEquals(VALUE_TEST_SIMPLE_RAW_DATA_WITH_AMPHASIS, testServer.files.get(POST_RAW_CONTENT_FILE_ENTRY));
36     }
37 
38     @Test
testSimplePostWithSingleMultipartFormField()39     public void testSimplePostWithSingleMultipartFormField() throws Exception {
40         String divider = UUID.randomUUID().toString();
41         String header = "POST " + URI + " HTTP/1.1\nContent-Type: " +
42                 "multipart/form-data; boundary=" + divider + "\n";
43         String content = "--" + divider + "\n" +
44                 "Content-Disposition: form-data; name=\""+FIELD+"\"\n" +
45                 "\n" +
46                 VALUE +"\n" +
47                 "--" + divider + "--\n";
48         int size = content.length() + header.length();
49         int contentLengthHeaderValueSize = String.valueOf(size).length();
50         int contentLength = size + contentLengthHeaderValueSize + CONTENT_LENGTH.length();
51         String input = header + CONTENT_LENGTH + (contentLength+4) + "\r\n\r\n" + content;
52         invokeServer(input);
53 
54         assertEquals(1, testServer.parms.size());
55         assertEquals(VALUE, testServer.parms.get(FIELD));
56     }
57 
58     @Test
testPostWithMultipleMultipartFormFields()59     public void testPostWithMultipleMultipartFormFields() throws Exception {
60         String divider = UUID.randomUUID().toString();
61         String header = "POST " + URI + " HTTP/1.1\nContent-Type: " +
62                 "multipart/form-data; boundary=" + divider + "\n";
63         String content = "--" + divider + "\n" +
64                 "Content-Disposition: form-data; name=\""+FIELD+"\"\n" +
65                 "\n" +
66                 VALUE +"\n" +"--" + divider + "\n" +
67                 "Content-Disposition: form-data; name=\""+FIELD2+"\"\n" +
68                 "\n" +
69                 VALUE2 +"\n" +
70                 "--" + divider + "--\n";
71         int size = content.length() + header.length();
72         int contentLengthHeaderValueSize = String.valueOf(size).length();
73         int contentLength = size + contentLengthHeaderValueSize + CONTENT_LENGTH.length();
74         String input = header + CONTENT_LENGTH + (contentLength+4) + "\r\n\r\n" + content;
75         invokeServer(input);
76 
77         assertEquals(2, testServer.parms.size());
78         assertEquals(VALUE, testServer.parms.get(FIELD));
79         assertEquals(VALUE2, testServer.parms.get(FIELD2));
80     }
81 
82     @Test
testPostWithMultipleMultipartFormFieldsWhereContentTypeWasSeparatedByComma()83     public void testPostWithMultipleMultipartFormFieldsWhereContentTypeWasSeparatedByComma() throws Exception {
84         String divider = UUID.randomUUID().toString();
85         String header = "POST " + URI + " HTTP/1.1\nContent-Type: " +
86                 "multipart/form-data, boundary=" + divider + "\n";
87         String content = "--" + divider + "\n" +
88                 "Content-Disposition: form-data; name=\""+FIELD+"\"\n" +
89                 "\n" +
90                 VALUE +"\n" +"--" + divider + "\n" +
91                 "Content-Disposition: form-data; name=\""+FIELD2+"\"\n" +
92                 "\n" +
93                 VALUE2 +"\n" +
94                 "--" + divider + "--\n";
95         int size = content.length() + header.length();
96         int contentLengthHeaderValueSize = String.valueOf(size).length();
97         int contentLength = size + contentLengthHeaderValueSize + CONTENT_LENGTH.length();
98         String input = header + CONTENT_LENGTH + (contentLength+4) + "\r\n\r\n" + content;
99         invokeServer(input);
100 
101         assertEquals(2, testServer.parms.size());
102         assertEquals(VALUE, testServer.parms.get(FIELD));
103         assertEquals(VALUE2, testServer.parms.get(FIELD2));
104     }
105 
106     @Test
testPostWithMultipartFormUpload()107     public void testPostWithMultipartFormUpload() throws Exception {
108         String filename = "GrandCanyon.txt";
109         String fileContent = VALUE;
110         String input = preparePostWithMultipartForm(filename, fileContent);
111 
112         invokeServer(input);
113 
114         assertEquals(1, testServer.parms.size());
115         BufferedReader reader = new BufferedReader(new FileReader(testServer.files.get(FIELD)));
116         List<String> lines = readLinesFromFile(reader);
117         assertLinesOfText(new String[]{fileContent}, lines);
118     }
119 
120     @Test
testPostWithMultipartFormUploadFilenameHasSpaces()121     public void testPostWithMultipartFormUploadFilenameHasSpaces() throws Exception {
122       String fileNameWithSpace = "Grand Canyon.txt";
123       String fileContent = VALUE;
124       String input = preparePostWithMultipartForm(fileNameWithSpace, fileContent);
125 
126       invokeServer(input);
127 
128       String fileNameAfter = new ArrayList<String>(testServer.parms.values()).get(0);
129 
130       assertEquals(fileNameWithSpace, fileNameAfter);
131     }
132 
133     /**
134      * contains common preparation steps for testing POST with Multipart Form
135      * @param fileName Name of file to be uploaded
136      * @param fileContent Content of file to be uploaded
137      * @return input String with POST request complete information including header, length and content
138      */
preparePostWithMultipartForm(String fileName, String fileContent)139     private String preparePostWithMultipartForm(String fileName, String fileContent) {
140         String divider = UUID.randomUUID().toString();
141         String header = "POST " + URI + " HTTP/1.1\nContent-Type: " +
142                 "multipart/form-data, boundary=" + divider + "\n";
143         String content = "--" + divider + "\n" +
144                 "Content-Disposition: form-data; name=\""+FIELD+"\"; filename=\""+fileName+"\"\n" +
145                 "Content-Type: image/jpeg\r\n"+
146                 "\r\n" +
147                 fileContent +"\r\n" +
148                 "--" + divider + "--\n";
149         int size = content.length() + header.length();
150         int contentLengthHeaderValueSize = String.valueOf(size).length();
151         int contentLength = size + contentLengthHeaderValueSize + CONTENT_LENGTH.length();
152         String input = header + CONTENT_LENGTH + (contentLength+5) + "\r\n\r\n" + content;
153 
154         return input;
155     }
156 
157 }
158