-
Notifications
You must be signed in to change notification settings - Fork 180
Expand file tree
/
Copy pathPostTest.java
More file actions
204 lines (181 loc) · 8.32 KB
/
Copy pathPostTest.java
File metadata and controls
204 lines (181 loc) · 8.32 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package org.webbitserver.handler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
import org.apache.http.impl.nio.client.HttpAsyncClients;
import org.junit.After;
import org.junit.Test;
import org.webbitserver.HttpControl;
import org.webbitserver.HttpHandler;
import org.webbitserver.HttpRequest;
import org.webbitserver.HttpResponse;
import org.webbitserver.helpers.NamingThreadFactory;
import org.webbitserver.netty.NettyWebServer;
import java.io.IOException;
import java.io.InputStream;
import java.net.*;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicBoolean;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.webbitserver.testutil.HttpClient.contents;
import static org.webbitserver.testutil.HttpClient.httpPost;
public class PostTest {
private NettyWebServer webServer = new NettyWebServer(59504);
@After
public void die() throws InterruptedException, ExecutionException {
webServer.stop().get();
}
@Test
public void exposesBodyInRequest() throws IOException, InterruptedException, ExecutionException {
webServer.add(new HttpHandler() {
@Override
public void handleHttpRequest(HttpRequest request, HttpResponse response, HttpControl control) throws Exception {
response.content("Body = {" + request.body() + "}").end();
}
}).start().get();
String result = contents(httpPost(webServer, "/", "hello\n world"));
assertEquals("Body = {hello\n world}", result);
}
@Test
public void exposesPostBodyAsParameters() throws IOException, InterruptedException, ExecutionException {
webServer.add(new HttpHandler() {
@Override
public void handleHttpRequest(HttpRequest request, HttpResponse response, HttpControl control) throws Exception {
response.content("a=" + request.postParam("a") + ", b=" + request.postParam("b")).end();
}
}).start().get();
String result = contents(httpPost(webServer, "/", "b=foo&a=hello%20world&c=d"));
assertEquals("a=hello world, b=foo", result);
}
@Test
public void exposesPostParamKeys() throws IOException, InterruptedException, ExecutionException {
webServer.add(new HttpHandler() {
@Override
public void handleHttpRequest(HttpRequest request, HttpResponse response, HttpControl control) throws Exception {
ArrayList<String> keysList = new ArrayList<String>(request.postParamKeys());
Collections.sort(keysList);
response.content("keys=" + keysList.toString()).end();
}
}).start().get();
String result = contents(httpPost(webServer, "/", "b=foo&a=hello%20world&c=d&b=duplicate"));
assertEquals("keys=[a, b, c]", result);
}
@Test
public void exposesPostBodyAsBytes() throws IOException, ExecutionException, InterruptedException {
webServer.add(new HttpHandler() {
@Override
public void handleHttpRequest(HttpRequest request, HttpResponse response, HttpControl control) throws Exception {
response.content(Arrays.toString(request.bodyAsBytes())).end();
}
}).start().get();
byte[] byteArray = new byte[]{87, 79, 87, 46, 46, 46};
String result = contents(httpPost(webServer, "/", new String(byteArray)));
assertEquals(Arrays.toString(byteArray), result);
}
@Test
public void request_body_longer_than_max_content_length_causes_500_and_does_not_invoke_handlers() throws IOException, ExecutionException, InterruptedException {
webServer.connectionExceptionHandler(new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread t, Throwable e) {
}
});
webServer.uncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread t, Throwable e) {
}
});
webServer.add(new HttpHandler() {
@Override
public void handleHttpRequest(HttpRequest request, HttpResponse response, HttpControl control) throws Exception {
response.error(new RuntimeException("Should never get here"));
}
}).start().get();
StringBuilder body = new StringBuilder();
for (int i = 0; i < 65537; i++) {
body.append(".");
}
URLConnection urlConnection = httpPost(webServer, "/", body.toString());
HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;
assertEquals(413, httpURLConnection.getResponseCode());
}
@Test
public void max_content_length_can_be_increased() throws IOException, ExecutionException, InterruptedException {
webServer.maxContentLength(65537);
webServer.add(new HttpHandler() {
@Override
public void handleHttpRequest(HttpRequest request, HttpResponse response, HttpControl control) throws Exception {
response.content("length:" + request.bodyAsBytes().length).end();
}
}).start().get();
StringBuilder body = new StringBuilder();
for (int i = 0; i < 65537; i++) {
body.append(".");
}
URLConnection urlConnection = httpPost(webServer, "/", body.toString());
String result = contents(urlConnection);
HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;
assertEquals(200, httpURLConnection.getResponseCode());
assertEquals("length:65537", result);
}
@Test
public void close_connection_midway_through_response_does_not_error_infinitely() throws Exception {
final List<Throwable> connectionExceptions = new CopyOnWriteArrayList<Throwable>();
final List<Throwable> uncaughtExceptions = new CopyOnWriteArrayList<Throwable>();
webServer.connectionExceptionHandler(new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread t, Throwable e) {
System.out.println("connectionException:" + e);
System.out.println("caused by:" + e.getCause());
connectionExceptions.add(e);
}
});
webServer.uncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread t, Throwable e) {
System.out.println("uncaughtException:" + e);
uncaughtExceptions.add(e);
}
});
final CountDownLatch latch = new CountDownLatch(1);
final CountDownLatch latch2 = new CountDownLatch(1);
final CountDownLatch latch3 = new CountDownLatch(1);
webServer.add(new HttpHandler() {
@Override
public void handleHttpRequest(HttpRequest request, HttpResponse response, HttpControl control) throws Exception {
response.content("length:" + request.bodyAsBytes().length);
latch2.countDown();
latch.await();
response.end();
latch3.countDown();
}
}).start().get();
final StringBuilder body = new StringBuilder();
for (int i = 0; i < 200; i++) {
body.append(".");
}
CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault();
try {
httpclient.start();
URI url = new URI(webServer.getUri() + "/");
HttpPost request = new HttpPost(url);
httpclient.execute(request, null);
latch2.await();
} finally {
httpclient.close();
}
latch.countDown();
latch3.await();
URLConnection urlConnection = httpPost(webServer, "/", body.toString());
HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;
assertEquals(200, httpURLConnection.getResponseCode());
assertThat(connectionExceptions.isEmpty(), is(true));
assertThat(uncaughtExceptions.isEmpty(), is(true));
}
}