aboutsummaryrefslogtreecommitdiff
path: root/core/src/test/java/fi/iki/elonen/HttpPostRequestTest.java
blob: 3cb37e06dc66110c1dfe176aaa198449a987008f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package fi.iki.elonen;

import org.junit.Test;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

import static junit.framework.Assert.assertEquals;

public class HttpPostRequestTest extends HttpServerTest {

    public static final String CONTENT_LENGTH = "Content-Length: ";
    public static final String FIELD = "caption";
    public static final String VALUE = "Summer vacation";
    public static final String FIELD2 = "location";
    public static final String VALUE2 = "Grand Canyon";
    public static final String POST_RAW_CONTENT_FILE_ENTRY = "postData";
    public static final String VALUE_TEST_SIMPLE_RAW_DATA_WITH_AMPHASIS = "Test raw data & Result value";

    @Test
    public void testSimpleRawPostData() throws Exception {
        String header = "POST " + URI + " HTTP/1.1\n";
        String content = VALUE_TEST_SIMPLE_RAW_DATA_WITH_AMPHASIS + "\n";
        int size = content.length() + header.length();
        int contentLengthHeaderValueSize = String.valueOf(size).length();
        int contentLength = size + contentLengthHeaderValueSize + CONTENT_LENGTH.length();
        String input = header + CONTENT_LENGTH + (contentLength+4) + "\r\n\r\n" + content;
        invokeServer(input);
        assertEquals(0, testServer.parms.size());
        assertEquals(1, testServer.files.size());
        assertEquals(VALUE_TEST_SIMPLE_RAW_DATA_WITH_AMPHASIS, testServer.files.get(POST_RAW_CONTENT_FILE_ENTRY));
    }

    @Test
    public void testSimplePostWithSingleMultipartFormField() throws Exception {
        String divider = UUID.randomUUID().toString();
        String header = "POST " + URI + " HTTP/1.1\nContent-Type: " +
                "multipart/form-data; boundary=" + divider + "\n";
        String content = "--" + divider + "\n" +
                "Content-Disposition: form-data; name=\""+FIELD+"\"\n" +
                "\n" +
                VALUE +"\n" +
                "--" + divider + "--\n";
        int size = content.length() + header.length();
        int contentLengthHeaderValueSize = String.valueOf(size).length();
        int contentLength = size + contentLengthHeaderValueSize + CONTENT_LENGTH.length();
        String input = header + CONTENT_LENGTH + (contentLength+4) + "\r\n\r\n" + content;
        invokeServer(input);

        assertEquals(1, testServer.parms.size());
        assertEquals(VALUE, testServer.parms.get(FIELD));
    }

    @Test
    public void testPostWithMultipleMultipartFormFields() throws Exception {
        String divider = UUID.randomUUID().toString();
        String header = "POST " + URI + " HTTP/1.1\nContent-Type: " +
                "multipart/form-data; boundary=" + divider + "\n";
        String content = "--" + divider + "\n" +
                "Content-Disposition: form-data; name=\""+FIELD+"\"\n" +
                "\n" +
                VALUE +"\n" +"--" + divider + "\n" +
                "Content-Disposition: form-data; name=\""+FIELD2+"\"\n" +
                "\n" +
                VALUE2 +"\n" +
                "--" + divider + "--\n";
        int size = content.length() + header.length();
        int contentLengthHeaderValueSize = String.valueOf(size).length();
        int contentLength = size + contentLengthHeaderValueSize + CONTENT_LENGTH.length();
        String input = header + CONTENT_LENGTH + (contentLength+4) + "\r\n\r\n" + content;
        invokeServer(input);

        assertEquals(2, testServer.parms.size());
        assertEquals(VALUE, testServer.parms.get(FIELD));
        assertEquals(VALUE2, testServer.parms.get(FIELD2));
    }

    @Test
    public void testPostWithMultipleMultipartFormFieldsWhereContentTypeWasSeparatedByComma() throws Exception {
        String divider = UUID.randomUUID().toString();
        String header = "POST " + URI + " HTTP/1.1\nContent-Type: " +
                "multipart/form-data, boundary=" + divider + "\n";
        String content = "--" + divider + "\n" +
                "Content-Disposition: form-data; name=\""+FIELD+"\"\n" +
                "\n" +
                VALUE +"\n" +"--" + divider + "\n" +
                "Content-Disposition: form-data; name=\""+FIELD2+"\"\n" +
                "\n" +
                VALUE2 +"\n" +
                "--" + divider + "--\n";
        int size = content.length() + header.length();
        int contentLengthHeaderValueSize = String.valueOf(size).length();
        int contentLength = size + contentLengthHeaderValueSize + CONTENT_LENGTH.length();
        String input = header + CONTENT_LENGTH + (contentLength+4) + "\r\n\r\n" + content;
        invokeServer(input);

        assertEquals(2, testServer.parms.size());
        assertEquals(VALUE, testServer.parms.get(FIELD));
        assertEquals(VALUE2, testServer.parms.get(FIELD2));
    }
    
    @Test
    public void testPostWithMultipartFormUpload() throws Exception {
        String filename = "GrandCanyon.txt";
        String fileContent = VALUE;
        String input = preparePostWithMultipartForm(filename, fileContent);
    
        invokeServer(input);
    
        assertEquals(1, testServer.parms.size());
        BufferedReader reader = new BufferedReader(new FileReader(testServer.files.get(FIELD)));
        List<String> lines = readLinesFromFile(reader);
        assertLinesOfText(new String[]{fileContent}, lines);
    }
    
    @Test
    public void testPostWithMultipartFormUploadFilenameHasSpaces() throws Exception {
      String fileNameWithSpace = "Grand Canyon.txt";
      String fileContent = VALUE;
      String input = preparePostWithMultipartForm(fileNameWithSpace, fileContent);
      
      invokeServer(input);
      
      String fileNameAfter = new ArrayList<String>(testServer.parms.values()).get(0);
      
      assertEquals(fileNameWithSpace, fileNameAfter);
    }
    
    /**
     * contains common preparation steps for testing POST with Multipart Form
     * @param fileName Name of file to be uploaded
     * @param fileContent Content of file to be uploaded
     * @return input String with POST request complete information including header, length and content
     */
    private String preparePostWithMultipartForm(String fileName, String fileContent) {
        String divider = UUID.randomUUID().toString();
        String header = "POST " + URI + " HTTP/1.1\nContent-Type: " +
                "multipart/form-data, boundary=" + divider + "\n";
        String content = "--" + divider + "\n" +
                "Content-Disposition: form-data; name=\""+FIELD+"\"; filename=\""+fileName+"\"\n" +
                "Content-Type: image/jpeg\r\n"+
                "\r\n" +
                fileContent +"\r\n" +
                "--" + divider + "--\n";
        int size = content.length() + header.length();
        int contentLengthHeaderValueSize = String.valueOf(size).length();
        int contentLength = size + contentLengthHeaderValueSize + CONTENT_LENGTH.length();
        String input = header + CONTENT_LENGTH + (contentLength+5) + "\r\n\r\n" + content;
        
        return input;
    }

}