Skip to content
This repository was archived by the owner on Jul 31, 2022. It is now read-only.

Commit 59a17e1

Browse files
committed
SimpleHttpExchange tests
1 parent 226f8c6 commit 59a17e1

File tree

6 files changed

+193
-160
lines changed

6 files changed

+193
-160
lines changed

src/test/java/simplehttpexchange/SimpleHttpExchangeCookieSessionTests.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
import java.util.concurrent.ExecutionException;
1313
import java.util.concurrent.atomic.AtomicReference;
1414

15-
public class SimpleHttpExchangeCookieSessionTests {
15+
public final class SimpleHttpExchangeCookieSessionTests {
1616

1717
@Test
18-
public void testSession() throws IOException, ExecutionException, InterruptedException{
19-
final int port = 20004;
18+
public final void testSession() throws IOException, ExecutionException, InterruptedException{
19+
final int port = 8080;
2020

2121
final SimpleHttpServer server = SimpleHttpServer.create(port);
2222
final AtomicReference<SimpleHttpExchange> exchangeRef = new AtomicReference<>();
@@ -48,7 +48,6 @@ public void testSession() throws IOException, ExecutionException, InterruptedExc
4848
client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
4949
.thenApply(HttpResponse::body).get();
5050

51-
5251
// exchange
5352

5453
final SimpleHttpExchange exchange = exchangeRef.get();

src/test/java/simplehttpexchange/SimpleHttpExchangeGetPostTests.java

Lines changed: 0 additions & 153 deletions
This file was deleted.

src/test/java/simplehttpexchange/SimpleHttpExchangeReadTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
import java.util.concurrent.ExecutionException;
1313
import java.util.concurrent.atomic.AtomicReference;
1414

15-
public class SimpleHttpExchangeReadTests {
15+
public final class SimpleHttpExchangeReadTests {
1616

1717
@Test
18-
public void read() throws IOException, InterruptedException, ExecutionException{
19-
final int port = 20001;
18+
public final void read() throws IOException, InterruptedException, ExecutionException{
19+
final int port = 8080;
2020

2121
final SimpleHttpServer server = SimpleHttpServer.create(port);
2222
final AtomicReference<SimpleHttpExchange> exchangeRef = new AtomicReference<>();
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package simplehttpexchange.io;
2+
3+
import com.kttdevelopment.simplehttpserver.*;
4+
import com.kttdevelopment.simplehttpserver.var.RequestMethod;
5+
import com.sun.net.httpserver.HttpContext;
6+
import org.junit.Assert;
7+
import org.junit.Test;
8+
9+
import java.io.IOException;
10+
import java.net.URI;
11+
import java.net.http.*;
12+
import java.util.concurrent.ExecutionException;
13+
import java.util.concurrent.atomic.AtomicReference;
14+
15+
public final class SimpleHttpExchangeGetTest {
16+
17+
@Test
18+
public final void get() throws IOException, ExecutionException, InterruptedException{
19+
final int port = 8080;
20+
21+
final SimpleHttpServer server = SimpleHttpServer.create(port);
22+
final AtomicReference<SimpleHttpExchange> exchangeRef = new AtomicReference<>();
23+
final AtomicReference<String> exchangeResponse = new AtomicReference<>();
24+
final SimpleHttpHandler handler = exchange -> {
25+
exchangeRef.set(exchange);
26+
exchangeResponse.set(exchange.toString());
27+
exchange.send(exchange.toString());
28+
};
29+
30+
final String context = "";
31+
final AtomicReference<HttpContext> contextRef = new AtomicReference<>();
32+
contextRef.set(server.createContext(context,handler));
33+
server.start();
34+
35+
final String queryKey = "test", queryValue = "value";
36+
final String url = "http://localhost:" + port + context + '?' + queryKey + '=' + queryValue;
37+
38+
HttpRequest request = HttpRequest.newBuilder()
39+
.uri(URI.create(url))
40+
.GET()
41+
.build();
42+
43+
final String response = HttpClient.newHttpClient().sendAsync(request, HttpResponse.BodyHandlers.ofString())
44+
.thenApply(HttpResponse::body).get();
45+
46+
Assert.assertEquals("Response body did not match response sent", exchangeResponse.get(), response);
47+
48+
// exchange
49+
final SimpleHttpExchange exchange = exchangeRef.get();
50+
51+
Assert.assertEquals("Client request method did not match exchange request method (GET)", RequestMethod.GET, exchange.getRequestMethod());
52+
Assert.assertTrue("Exchange was missing client GET map", exchange.hasGet());
53+
Assert.assertEquals("Exchange GET did not match client GET", queryValue, exchange.getGetMap().get(queryKey));
54+
}
55+
56+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package simplehttpexchange.io;
2+
3+
import com.kttdevelopment.simplehttpserver.*;
4+
import com.kttdevelopment.simplehttpserver.var.RequestMethod;
5+
import org.junit.Assert;
6+
import org.junit.Test;
7+
8+
import java.io.IOException;
9+
import java.net.URI;
10+
import java.net.http.*;
11+
import java.util.Map;
12+
import java.util.concurrent.ExecutionException;
13+
import java.util.concurrent.atomic.AtomicReference;
14+
15+
public final class SimpleHttpExchangeMultipartFormTest {
16+
17+
@SuppressWarnings({"rawtypes", "StringBufferReplaceableByString", "SpellCheckingInspection"})
18+
@Test
19+
public final void postMultipartFormData() throws IOException, ExecutionException, InterruptedException{
20+
final int port = 8080;
21+
22+
final SimpleHttpServer server = SimpleHttpServer.create(port);
23+
final AtomicReference<SimpleHttpExchange> exchangeRef = new AtomicReference<>();
24+
final AtomicReference<String> exchangeResponse = new AtomicReference<>();
25+
final SimpleHttpHandler handler = exchange -> {
26+
exchangeRef.set(exchange);
27+
exchangeResponse.set(exchange.toString());
28+
exchange.send(exchange.toString());
29+
};
30+
31+
final String context = "";
32+
server.createContext(context,handler);
33+
server.start();
34+
35+
final String boundary = "d74496d66958873e";
36+
37+
final String url = "http://localhost:" + port + context ;
38+
39+
final String key = "key", value = "value";
40+
final String fkey = "fileKey", filename = "fileName.txt", fvalue = "fileValue";
41+
42+
final StringBuilder OUT = new StringBuilder();
43+
OUT.append("--------------------------").append(boundary).append("\r\n");
44+
OUT.append("Content-Disposition: ").append("form-data; ").append("name=\"").append(key).append('\"').append("\r\n\r\n");
45+
OUT.append(value).append("\r\n");
46+
OUT.append("--------------------------").append(boundary).append("\r\n");
47+
OUT.append("Content-Disposition: ").append("form-data; ").append("name=\"").append(fkey).append("\"; ");
48+
OUT.append("filename=\"").append(filename).append('\"').append('\n');
49+
OUT.append("Content-Type: ").append("text/plain").append("\r\n\r\n");
50+
OUT.append(fvalue).append("\r\n");
51+
OUT.append("--------------------------").append(boundary).append("--");
52+
53+
final HttpRequest request = HttpRequest.newBuilder()
54+
.uri(URI.create(url))
55+
.header("Content-type","multipart/form-data; boundary=" + boundary)
56+
.POST(HttpRequest.BodyPublishers.ofString(OUT.toString()))
57+
.build();
58+
59+
HttpClient.newHttpClient().sendAsync(request, HttpResponse.BodyHandlers.ofString())
60+
.thenApply(HttpResponse::body).get();
61+
62+
// exchange
63+
final SimpleHttpExchange exchange = exchangeRef.get();
64+
65+
Assert.assertEquals("Client request method did not match exchange request method (POST)", RequestMethod.POST,exchange.getRequestMethod());
66+
Assert.assertTrue("Exchange was missing client POST map", exchange.hasPost());
67+
68+
Assert.assertEquals("Client form value did not match server value",value, ((Map) exchange.getPostMap().get(key)).get("value"));
69+
Assert.assertEquals("Client file name did not match server value",filename, ((Map) ((Map) ((Map) ((Map) exchange.getPostMap().get(fkey)).get("headers")).get("Content-Disposition")).get("parameters")).get("filename"));
70+
Assert.assertEquals("Client file value did not match server value",fvalue, ((Map) exchange.getPostMap().get(fkey)).get("value"));
71+
72+
server.stop();
73+
}
74+
75+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package simplehttpexchange.io;
2+
3+
import com.kttdevelopment.simplehttpserver.*;
4+
import com.kttdevelopment.simplehttpserver.var.RequestMethod;
5+
import com.sun.net.httpserver.HttpContext;
6+
import org.junit.Assert;
7+
import org.junit.Test;
8+
9+
import java.io.IOException;
10+
import java.net.URI;
11+
import java.net.http.*;
12+
import java.util.concurrent.ExecutionException;
13+
import java.util.concurrent.atomic.AtomicReference;
14+
15+
public final class SimpleHttpExchangePostTest {
16+
17+
@Test
18+
public void postSimple() throws IOException, ExecutionException, InterruptedException{
19+
final int port = 20003;
20+
21+
final SimpleHttpServer server = SimpleHttpServer.create(port);
22+
final AtomicReference<SimpleHttpExchange> exchangeRef = new AtomicReference<>();
23+
final AtomicReference<String> exchangeResponse = new AtomicReference<>();
24+
final SimpleHttpHandler handler = exchange -> {
25+
exchangeRef.set(exchange);
26+
exchangeResponse.set(exchange.toString());
27+
exchange.send(exchange.toString());
28+
};
29+
30+
final String context = "";
31+
final AtomicReference<HttpContext> contextRef = new AtomicReference<>();
32+
contextRef.set(server.createContext(context,handler));
33+
server.start();
34+
35+
final String queryKey = "test", queryValue = "value";
36+
final String url = "http://localhost:" + port + context ;
37+
38+
final HttpRequest request = HttpRequest.newBuilder()
39+
.uri(URI.create(url))
40+
.POST(HttpRequest.BodyPublishers.ofString(queryKey + '=' + queryValue))
41+
.build();
42+
43+
HttpClient.newHttpClient().sendAsync(request, HttpResponse.BodyHandlers.ofString())
44+
.thenApply(HttpResponse::body).get();
45+
46+
// exchange
47+
final SimpleHttpExchange exchange = exchangeRef.get();
48+
49+
Assert.assertEquals("Client request method did not match exchange request method (POST)", RequestMethod.POST, exchange.getRequestMethod());
50+
Assert.assertTrue("Exchange was missing client POST map", exchange.hasPost());
51+
Assert.assertEquals("Exchange POST did not match client POST", queryValue, exchange.getPostMap().get(queryKey));
52+
53+
server.stop();
54+
}
55+
56+
}

0 commit comments

Comments
 (0)