Skip to content
This repository was archived by the owner on May 10, 2024. It is now read-only.

Commit 337eb27

Browse files
committed
all tests except file handler
* added IO tests * fix multipart form issue * (tests) fix gzip request
1 parent cf15cd8 commit 337eb27

File tree

7 files changed

+265
-9
lines changed

7 files changed

+265
-9
lines changed

src/main/java/dev/katsute/simplehttpserver/Record.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public class Record {
4141
value = Objects.requireNonNull(Objects.requireNonNull(entry.getValue()).get("value")).toString();
4242

4343
final Map<String,Header> headers = new HashMap<>();
44-
for(final Map.Entry<String,Map<String,?>> e : Objects.requireNonNull((Map<String,Map<String,?>>) entry.getValue().get("Headers")).entrySet()){
44+
for(final Map.Entry<String,Map<String,?>> e : Objects.requireNonNull((Map<String,Map<String,?>>) entry.getValue().get("headers")).entrySet()){
4545
headers.put(e.getKey(), new Header(
4646
Objects.requireNonNull(e.getValue().get("header-name")).toString(),
4747
Objects.requireNonNull(e.getValue().get("header-value")).toString(),

src/main/java/dev/katsute/simplehttpserver/SimpleHttpServerImpl.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ public synchronized final HttpContext createContext(final String context, final
139139
handle(exchange);
140140
handler.handle(exchange);
141141
};
142+
142143
hc.setHandler(wrapper);
143144

144145
contexts.put(hc, handler);

src/test/java/dev/katsute/simplehttpserver/Requests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.io.*;
44
import java.net.*;
55
import java.nio.charset.StandardCharsets;
6+
import java.util.zip.GZIPInputStream;
67

78
public class Requests {
89

@@ -45,7 +46,7 @@ public static String getBody(final HttpURLConnection conn){
4546

4647
public static String getBody(final HttpURLConnection conn, final boolean ignoreError){
4748
final StringBuilder OUT = new StringBuilder();
48-
try(final BufferedReader IN = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8))){
49+
try(final BufferedReader IN = new BufferedReader(new InputStreamReader("gzip".equalsIgnoreCase(conn.getContentEncoding()) ? new GZIPInputStream(conn.getInputStream()) : conn.getInputStream(), StandardCharsets.UTF_8))){
4950
String buffer;
5051
while((buffer = IN.readLine()) != null)
5152
OUT.append(buffer).append('\n');

src/test/java/dev/katsute/simplehttpserver/exchange/ExchangeIOTests.java

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package dev.katsute.simplehttpserver.exchange;
2+
3+
import dev.katsute.simplehttpserver.*;
4+
import org.junit.jupiter.api.*;
5+
6+
import java.io.IOException;
7+
import java.io.OutputStream;
8+
import java.net.HttpURLConnection;
9+
import java.net.URL;
10+
import java.nio.charset.StandardCharsets;
11+
import java.util.Map;
12+
import java.util.concurrent.atomic.AtomicReference;
13+
14+
final class ExchangeMultipartFormTests {
15+
16+
private static SimpleHttpServer server;
17+
18+
private static SimpleHttpExchange exchange;
19+
20+
private static final String boundary = "d74496d66958873e";
21+
22+
private static final String key = "key", value = "value";
23+
private static final String fkey = "fileKey", filename = "fileName.txt", fvalue = "fileValue", contentType = "text/plain";
24+
25+
@BeforeAll
26+
static void beforeAll() throws IOException{
27+
server = SimpleHttpServer.create(8080);
28+
29+
AtomicReference<SimpleHttpExchange> exchangeRef = new AtomicReference<>();
30+
31+
server.createContext("exchange", (SimpleHttpHandler) e -> {
32+
exchangeRef.set(e);
33+
e.send(200);
34+
e.close();
35+
});
36+
37+
server.start();
38+
39+
final StringBuilder OUT = new StringBuilder();
40+
OUT.append("--------------------------").append(boundary).append("\r\n");
41+
OUT.append("Content-Disposition: ").append("form-data; ").append("name=\"").append(key).append('\"').append("\r\n\r\n");
42+
OUT.append(value).append("\r\n");
43+
OUT.append("--------------------------").append(boundary).append("\r\n");
44+
OUT.append("Content-Disposition: ").append("form-data; ").append("name=\"").append(fkey).append("\"; ");
45+
OUT.append("filename=\"").append(filename).append('\"').append("\r\n");
46+
OUT.append("Content-Type: ").append(contentType).append("\r\n\r\n");
47+
OUT.append(fvalue).append("\r\n");
48+
OUT.append("--------------------------").append(boundary).append("--");
49+
50+
{
51+
final HttpURLConnection conn = (HttpURLConnection) new URL("http://localhost:8080/exchange").openConnection();
52+
conn.setConnectTimeout(5000);
53+
conn.setReadTimeout(5000);
54+
conn.setRequestProperty("Content-type", "multipart/form-data; boundary=" + boundary);
55+
conn.setRequestMethod("POST");
56+
conn.setDoOutput(true);
57+
58+
try(final OutputStream OS = conn.getOutputStream()){
59+
final byte[] bytes = OUT.toString().getBytes(StandardCharsets.UTF_8);
60+
OS.write(bytes, 0, bytes.length);
61+
}
62+
63+
conn.getOutputStream().flush();
64+
conn.getOutputStream().close();
65+
66+
Requests.getBody(conn);
67+
}
68+
69+
exchange = exchangeRef.get();
70+
71+
server.stop();
72+
}
73+
74+
@Test
75+
final void testPOST(){
76+
Assertions.assertEquals("POST", exchange.getRequestMethod().toUpperCase());
77+
Assertions.assertTrue(exchange.hasPost());
78+
}
79+
80+
@Test
81+
final void testData(){
82+
Assertions.assertEquals(value, ((Map<?,?>) exchange.getPostMap().get(key)).get("value"));
83+
84+
Assertions.assertEquals(filename, ((Map<?,?>) ((Map<?,?>) ((Map<?,?>) ((Map<?,?>) exchange.getPostMap().get(fkey)).get("headers")).get("Content-Disposition")).get("parameters")).get("filename"));
85+
Assertions.assertEquals(contentType, ((Map<?,?>) ((Map<?,?>) ((Map<?,?>) exchange.getPostMap().get(fkey)).get("headers")).get("Content-Type")).get("header-value"));
86+
Assertions.assertEquals(fvalue, ((Map<?,?>) exchange.getPostMap().get(fkey)).get("value"));
87+
}
88+
89+
@Test
90+
final void testMultipart(){
91+
Assertions.assertNotNull(exchange.getMultipartFormData());
92+
Assertions.assertEquals(2, exchange.getMultipartFormData().getEntries().size());
93+
94+
Assertions.assertEquals(value, exchange.getMultipartFormData().getEntry(key).getValue());
95+
96+
Assertions.assertEquals(filename, ((FileRecord) exchange.getMultipartFormData().getEntry(fkey)).getFileName());
97+
Assertions.assertEquals(contentType, ((FileRecord) exchange.getMultipartFormData().getEntry(fkey)).getContentType());
98+
Assertions.assertEquals(fvalue, new String(((FileRecord) exchange.getMultipartFormData().getEntry(fkey)).getBytes()));
99+
}
100+
101+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package dev.katsute.simplehttpserver.exchange;
2+
3+
import dev.katsute.simplehttpserver.*;
4+
import org.junit.jupiter.api.*;
5+
6+
import java.io.IOException;
7+
import java.io.OutputStream;
8+
import java.net.HttpURLConnection;
9+
import java.net.URL;
10+
import java.nio.charset.StandardCharsets;
11+
import java.util.concurrent.atomic.AtomicReference;
12+
13+
final class ExchangePostTests {
14+
15+
private static SimpleHttpServer server;
16+
17+
private static SimpleHttpExchange exchange;
18+
19+
@BeforeAll
20+
static void beforeAll() throws IOException{
21+
server = SimpleHttpServer.create(8080);
22+
23+
AtomicReference<SimpleHttpExchange> exchangeRef = new AtomicReference<>();
24+
25+
server.createContext("exchange", (SimpleHttpHandler) e -> {
26+
exchangeRef.set(e);
27+
e.send(200);
28+
e.close();
29+
});
30+
31+
server.start();
32+
33+
{
34+
final HttpURLConnection conn = (HttpURLConnection) new URL("http://localhost:8080/exchange").openConnection();
35+
conn.setConnectTimeout(5000);
36+
conn.setReadTimeout(5000);
37+
conn.setRequestMethod("POST");
38+
conn.setDoOutput(true);
39+
40+
try(final OutputStream OS = conn.getOutputStream()){
41+
final byte[] bytes = "key=value&alt=a%2B%3F%26%7D".getBytes(StandardCharsets.UTF_8);
42+
OS.write(bytes, 0, bytes.length);
43+
}
44+
45+
conn.getOutputStream().flush();
46+
conn.getOutputStream().close();
47+
48+
Requests.getBody(conn);
49+
}
50+
51+
exchange = exchangeRef.get();
52+
53+
server.stop();
54+
}
55+
56+
@Test
57+
final void testPOST(){
58+
Assertions.assertEquals("POST", exchange.getRequestMethod().toUpperCase());
59+
Assertions.assertTrue(exchange.hasPost());
60+
Assertions.assertEquals("value", exchange.getPostMap().get("key"));
61+
Assertions.assertEquals("a+?&}", exchange.getPostMap().get("alt"));
62+
63+
Assertions.assertNull(exchange.getMultipartFormData());
64+
}
65+
66+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package dev.katsute.simplehttpserver.exchange;
2+
3+
import dev.katsute.simplehttpserver.*;
4+
import org.junit.jupiter.api.*;
5+
import org.junit.jupiter.api.io.TempDir;
6+
7+
import java.io.File;
8+
import java.io.IOException;
9+
import java.net.HttpURLConnection;
10+
import java.nio.file.Files;
11+
12+
final class ExchangeSendTests {
13+
14+
private static SimpleHttpServer server;
15+
16+
private static int testCode = HttpURLConnection.HTTP_ACCEPTED;
17+
private static String testContent = String.valueOf(System.currentTimeMillis());
18+
19+
@TempDir
20+
private static File dir = new File(testContent);
21+
22+
@BeforeAll
23+
static void beforeAll() throws IOException{
24+
server = SimpleHttpServer.create(8080);
25+
26+
server.createContext("code", (SimpleHttpHandler) e -> {
27+
e.send(testCode);
28+
e.close();
29+
});
30+
31+
server.createContext("bytes", (SimpleHttpHandler) e -> {
32+
e.send(testContent.getBytes());
33+
e.close();
34+
});
35+
36+
server.createContext("bytes/gzip", (SimpleHttpHandler) e -> {
37+
e.send(testContent.getBytes(), true);
38+
e.close();
39+
});
40+
41+
server.createContext("string", (SimpleHttpHandler) e -> {
42+
e.send(testContent);
43+
e.close();
44+
});
45+
46+
server.createContext("string/gzip", (SimpleHttpHandler) e -> {
47+
e.send(testContent, true);
48+
e.close();
49+
});
50+
51+
final File testFile = new File(dir, testContent);
52+
Files.write(testFile.toPath(), testContent.getBytes());
53+
server.createContext("file", (SimpleHttpHandler) e -> {
54+
e.send(testFile);
55+
e.close();
56+
});
57+
58+
server.createContext("file/gzip", (SimpleHttpHandler) e -> {
59+
e.send(testFile, true);
60+
e.close();
61+
});
62+
63+
server.start();
64+
}
65+
66+
@AfterAll
67+
static void afterAll(){
68+
server.stop();
69+
}
70+
71+
@Test
72+
final void testCode(){
73+
Assertions.assertEquals(testCode, Requests.getCode("http://localhost:8080/code"));
74+
}
75+
76+
@Test
77+
final void testBytes(){
78+
Assertions.assertEquals(testContent, Requests.getBody("http://localhost:8080/bytes"));
79+
Assertions.assertEquals(testContent, Requests.getBody("http://localhost:8080/bytes/gzip"));
80+
}
81+
82+
@Test
83+
final void testString(){
84+
Assertions.assertEquals(testContent, Requests.getBody("http://localhost:8080/string"));
85+
Assertions.assertEquals(testContent, Requests.getBody("http://localhost:8080/string/gzip"));
86+
}
87+
88+
@Test
89+
final void testFile(){
90+
Assertions.assertEquals(testContent, Requests.getBody("http://localhost:8080/file"));
91+
Assertions.assertEquals(testContent, Requests.getBody("http://localhost:8080/file/gzip"));
92+
}
93+
94+
}

0 commit comments

Comments
 (0)