|
50 | 50 | import org.junit.Before;
|
51 | 51 | import org.junit.Test;
|
52 | 52 |
|
| 53 | +import static org.junit.Assert.assertArrayEquals; |
53 | 54 | import static org.junit.Assert.assertEquals;
|
| 55 | +import static org.junit.Assert.assertNull; |
54 | 56 | import static org.junit.Assert.assertTrue;
|
55 | 57 | import static org.junit.Assert.fail;
|
56 | 58 |
|
@@ -115,19 +117,57 @@ public boolean verify(String hostname, SSLSession session) {
|
115 | 117 | assertEquals(-1, connection.getInputStream().read());
|
116 | 118 | }
|
117 | 119 |
|
118 |
| - @Test public void post() throws Exception { |
| 120 | + byte[] postBytes = "FGHIJ".getBytes(Util.UTF_8); |
| 121 | + |
| 122 | + /** An output stream can be written to more than once, so we can't guess content length. */ |
| 123 | + @Test public void noDefaultContentLengthOnPost() throws Exception { |
| 124 | + MockResponse response = new MockResponse().setBody("ABCDE"); |
| 125 | + server.enqueue(response); |
| 126 | + server.play(); |
| 127 | + |
| 128 | + HttpURLConnection connection = client.open(server.getUrl("/foo")); |
| 129 | + connection.setDoOutput(true); |
| 130 | + connection.getOutputStream().write(postBytes); |
| 131 | + assertContent("ABCDE", connection, Integer.MAX_VALUE); |
| 132 | + |
| 133 | + RecordedRequest request = server.takeRequest(); |
| 134 | + assertEquals("POST /foo HTTP/1.1", request.getRequestLine()); |
| 135 | + assertArrayEquals(postBytes, request.getBody()); |
| 136 | + assertNull(request.getHeader("Content-Length")); |
| 137 | + } |
| 138 | + |
| 139 | + @Test public void userSuppliedContentLengthHeader() throws Exception { |
| 140 | + MockResponse response = new MockResponse().setBody("ABCDE"); |
| 141 | + server.enqueue(response); |
| 142 | + server.play(); |
| 143 | + |
| 144 | + HttpURLConnection connection = client.open(server.getUrl("/foo")); |
| 145 | + connection.setRequestProperty("Content-Length", String.valueOf(postBytes.length)); |
| 146 | + connection.setDoOutput(true); |
| 147 | + connection.getOutputStream().write(postBytes); |
| 148 | + assertContent("ABCDE", connection, Integer.MAX_VALUE); |
| 149 | + |
| 150 | + RecordedRequest request = server.takeRequest(); |
| 151 | + assertEquals("POST /foo HTTP/1.1", request.getRequestLine()); |
| 152 | + assertArrayEquals(postBytes, request.getBody()); |
| 153 | + assertEquals(postBytes.length, Integer.parseInt(request.getHeader("Content-Length"))); |
| 154 | + } |
| 155 | + |
| 156 | + @Test public void setFixedLengthStreamingModeSetsContentLength() throws Exception { |
119 | 157 | MockResponse response = new MockResponse().setBody("ABCDE");
|
120 | 158 | server.enqueue(response);
|
121 | 159 | server.play();
|
122 | 160 |
|
123 | 161 | HttpURLConnection connection = client.open(server.getUrl("/foo"));
|
| 162 | + connection.setFixedLengthStreamingMode(postBytes.length); |
124 | 163 | connection.setDoOutput(true);
|
125 |
| - connection.getOutputStream().write("FGHIJ".getBytes(Util.UTF_8)); |
| 164 | + connection.getOutputStream().write(postBytes); |
126 | 165 | assertContent("ABCDE", connection, Integer.MAX_VALUE);
|
127 | 166 |
|
128 | 167 | RecordedRequest request = server.takeRequest();
|
129 | 168 | assertEquals("POST /foo HTTP/1.1", request.getRequestLine());
|
130 |
| - assertEquals("FGHIJ", request.getUtf8Body()); |
| 169 | + assertArrayEquals(postBytes, request.getBody()); |
| 170 | + assertEquals(postBytes.length, Integer.parseInt(request.getHeader("Content-Length"))); |
131 | 171 | }
|
132 | 172 |
|
133 | 173 | @Test public void spdyConnectionReuse() throws Exception {
|
|
0 commit comments