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

Optimized test cases. #89

Merged
merged 7 commits into from
Aug 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.kttdevelopment</groupId>
<artifactId>simplehttpserver</artifactId>
<version>03.05.07</version>
<version>03.05.08</version>
<packaging>jar</packaging>

<url>https://github.com/Ktt-Development/simplehttpserver</url>
Expand Down Expand Up @@ -79,8 +79,22 @@
</plugins>
</build>

<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>

<dependencies>
<!-- tests -->
<dependency>
<groupId>com.kttdevelopment.core</groupId>
<artifactId>test-util</artifactId>
<version>076417be59</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* @see SimpleHttpExchange
* @see Builder
* @since 02.00.00
* @version 03.05.00
* @version 03.05.08
* @author Ktt Development
*/
public class SimpleHttpCookie {
Expand Down Expand Up @@ -61,7 +61,6 @@ private SimpleHttpCookie(final String name, final String value, final String dom
this.httpOnly = httpOnly;
}

@SuppressWarnings("SpellCheckingInspection")
private final SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss");

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* @see SimpleHttpsServer
* @see SimpleHttpHandler
* @since 02.00.00
* @version 03.05.02
* @version 03.05.08
* @author Ktt Development
*/
@SuppressWarnings("SpellCheckingInspection")
Expand Down Expand Up @@ -270,12 +270,15 @@ public static SimpleHttpServer create(final int port, final int backlog) throws

/**
* Creates an empty context.
* <br>
* * {@link HttpServer} often does not throw an exception for duplicate contexts. <a href="https://github.com/Ktt-Development/simplehttpserver/issues/86">#86</a>
*
* @param context the context
* @return the http context associated with the context
* @throws IllegalArgumentException if the context is invalid or taken
* @throws IllegalArgumentException if the context is invalid or taken*
* @throws NullPointerException if the context is null
*
*
* @see HttpContext
* @see #createContext(String, HttpHandler)
* @see #removeContext(String)
Expand All @@ -287,11 +290,13 @@ public static SimpleHttpServer create(final int port, final int backlog) throws

/**
* Creates a context mapped to a specified {@link HttpHandler}.
* <br>
* * {@link HttpServer} often does not throw an exception for duplicate contexts. <a href="https://github.com/Ktt-Development/simplehttpserver/issues/86">#86</a>
*
* @param context the context
* @param handler the handler
* @return the http context associated with the context
* @throws IllegalArgumentException if the context is invalid or taken
* @throws IllegalArgumentException if the context is invalid or taken*
* @throws NullPointerException if the context is null
*
* @see HttpContext
Expand All @@ -308,11 +313,13 @@ public static SimpleHttpServer create(final int port, final int backlog) throws

/**
* Creates a context mapped to a specific {@link HttpContext} with an {@link Authenticator}.
* <br>
* * {@link HttpServer} often does not throw an exception for duplicate contexts. <a href="https://github.com/Ktt-Development/simplehttpserver/issues/86">#86</a>
*
* @param context the context
* @param authenticator authenticator
* @return the http context associated with the context
* @throws IllegalArgumentException if the context is invalid or taken
* @throws IllegalArgumentException if the context is invalid or taken*
* @throws NullPointerException if the context is null
*
* @see HttpContext
Expand All @@ -327,12 +334,14 @@ public static SimpleHttpServer create(final int port, final int backlog) throws

/**
* Creates a context mapped to a specific {@link HttpContext} with an {@link Authenticator}.
* <br>
* * {@link HttpServer} often does not throw an exception for duplicate contexts. <a href="https://github.com/Ktt-Development/simplehttpserver/issues/86">#86</a>
*
* @param context the context
* @param handler the handler
* @param authenticator authenticator
* @return the http context associated with the context
* @throws IllegalArgumentException if the context is invalid or taken
* @throws IllegalArgumentException if the context is invalid or taken*
* @throws NullPointerException if the context is null
*
* @see HttpContext
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*
* @see SimpleHttpServer
* @since 02.00.00
* @version 03.05.03
* @version 03.05.08
* @author Ktt Development
*/
@SuppressWarnings("SpellCheckingInspection")
Expand Down Expand Up @@ -147,15 +147,18 @@ public synchronized final HttpContext createContext(final String context, final

@Override
public synchronized final HttpContext createContext(final String context, final HttpHandler handler, final Authenticator authenticator){
if(!ContextUtil.getContext(context,true,false).equals("/") && handler instanceof RootHandler)
final String ct = ContextUtil.getContext(context,true,false);
if(!ct.equals("/") && handler instanceof RootHandler)
throw new IllegalArgumentException("RootHandler can only be used at the root '/' context");

final HttpHandler wrapper = exchange -> {
handle(exchange);
handler.handle(exchange);
};

final HttpContext hc = server.createContext(ContextUtil.getContext(context,true,false),wrapper);
final HttpContext hc = server.createContext(ct);

hc.setHandler(wrapper);
contexts.put(hc,handler);

if(authenticator != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*
* @see SimpleHttpsServer
* @since 03.04.00
* @version 03.05.03
* @version 03.05.08
* @author Ktt Development
*/
@SuppressWarnings("SpellCheckingInspection")
Expand Down Expand Up @@ -73,7 +73,7 @@ public final HttpsConfigurator getHttpsConfigurator(){
return server.getHttpsConfigurator();
}

// region copySimpleHttpServerImpl
// region copy

@Override
public synchronized final InetSocketAddress bind(final int port) throws IOException{
Expand Down Expand Up @@ -159,15 +159,18 @@ public synchronized final HttpContext createContext(final String context, final

@Override
public synchronized final HttpContext createContext(final String context, final HttpHandler handler, final Authenticator authenticator){
if(!ContextUtil.getContext(context,true,false).equals("/") && handler instanceof RootHandler)
final String ct = ContextUtil.getContext(context,true,false);
if(!ct.equals("/") && handler instanceof RootHandler)
throw new IllegalArgumentException("RootHandler can only be used at the root '/' context");

final HttpHandler wrapper = exchange -> {
handle(exchange);
handler.handle(exchange);
};

final HttpContext hc = server.createContext(ContextUtil.getContext(context,true,false),wrapper);
final HttpContext hc = server.createContext(ct);

hc.setHandler(wrapper);
contexts.put(hc,handler);

if(authenticator != null)
Expand Down Expand Up @@ -264,7 +267,7 @@ public synchronized final void stop(final int delay){
}
}

// endregion
// endregion copy

@Override
public String toString(){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
* @version 03.05.06
* @author Ktt Development
*/
@SuppressWarnings("SpellCheckingInspection")
class DirectoryEntry {

private final File directory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @version 01.00.00
* @author Ktt Development
*/
@SuppressWarnings({"unused", "SpellCheckingInspection"})
@SuppressWarnings({"unused", "SpellCheckingInspection", "RedundantSuppression"})
public abstract class HttpCode {

public static final int HTTP_Continue = 100;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
* @version 01.00.00
* @author Ktt Development
*/
@SuppressWarnings("SpellCheckingInspection")
public enum RequestMethod {
GET,
HEAD,
Expand Down
111 changes: 73 additions & 38 deletions src/test/java/ContextUtilTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,56 +2,91 @@
import org.junit.Assert;
import org.junit.Test;

public class ContextUtilTests {
import java.util.Arrays;

public final class ContextUtilTests {

private static final class test{

private final String expected, context;
private final boolean leadingSlash, trailingSlash;

public test(final String expected, final String context, final boolean leadingSlash, final boolean trailingSlash){
this.expected = expected;
this.context = context;
this.leadingSlash = leadingSlash;
this.trailingSlash = trailingSlash;
}

@Test
public void testContexts(){
Assert.assertEquals("Failed test on blank (leading slash)" ,"/", ContextUtil.getContext("",true,false));
Assert.assertEquals("Failed test on blank (trailing slash)" ,"/", ContextUtil.getContext("",false,true));
Assert.assertEquals("Failed test on blank (both slashes)" ,"/", ContextUtil.getContext("",true,true));

Assert.assertEquals("Failed test on single (leading slash)" ,"/a" , ContextUtil.getContext("a",true,false));
Assert.assertEquals("Failed test on single (trailing slash)" ,"a/" , ContextUtil.getContext("a",false,true));
Assert.assertEquals("Failed test on single (both slashes)" ,"/a/", ContextUtil.getContext("a",true,true));

Assert.assertEquals("/testLeading" , ContextUtil.getContext("testLeading",true,false));
Assert.assertEquals("testTrailing/" , ContextUtil.getContext("testTrailing",false,true));
Assert.assertEquals("testNone" , ContextUtil.getContext("/testNone/",false,false));
Assert.assertEquals("/testBoth/" , ContextUtil.getContext("testBoth",true,true));

Assert.assertEquals("testBackSlash/" , ContextUtil.getContext("testBackSlash\\",false,true));
Assert.assertEquals("testConsBackSlash/", ContextUtil.getContext("testConsBackSlash\\\\",false,true));
Assert.assertEquals("testConsFwdSlash/" , ContextUtil.getContext("testConsFwdSlash//",false,true));
}

@Test
public void testJoin(){
Assert.assertEquals("Failed test on last blank join" ,"a" ,ContextUtil.joinContexts(false,false,"a",""));
Assert.assertEquals("Failed test on last blank join+","/a/",ContextUtil.joinContexts(true,true,"a",""));
public final void testContexts(){
final test[] tests = {
new test("" , "/" , false , false),
new test("/" , "" , true , false),
new test("/" , "" , false , true ),
new test("/" , "" , true , true ),
new test("a" , "a" , false , false),
new test("/a" , "a" , true , false),
new test("a/" , "a" , false , true ),
new test("/a/" , "a" , true , true ),
new test("testNone" , "/testNone/" , false , false),
new test("/testLeading" , "testLeading" , true , false),
new test("testTrailing/" , "testTrailing" , false , true ),
new test("/testBoth/" , "testBoth" , true , true ),
new test("testNoneBackSlash" , "\\testNoneBackSlash\\" , false , false),
new test("/testBackSlash/" , "\\testBackSlash\\" , true , true ),
new test("/testConsecutiveBackSlash/" , "\\\\testConsecutiveBackSlash\\\\", true , true ),
new test("/testConsecutiveForwardSlash/" , "//testConsecutiveForwardSlash//" , true , true )
};

Assert.assertEquals("Failed test on first blank join" ,"a" ,ContextUtil.joinContexts(false,false,"","a"));
Assert.assertEquals("Failed test on first blank join+","/a/",ContextUtil.joinContexts(true,true,"","a"));
for(final test test : tests)
Assert.assertEquals(String.format("Incorrect context for #(\"%s\", %s, %s)", test.context,test.leadingSlash,test.trailingSlash),test.expected,ContextUtil.getContext(test.context,test.leadingSlash,test.trailingSlash));
}

Assert.assertEquals("Failed test on both blank join" ,"" ,ContextUtil.joinContexts(false,false,"",""));
Assert.assertEquals("Failed test on both blank join+","/",ContextUtil.joinContexts(true,true,"",""));
//

Assert.assertEquals("trailing/slash" ,ContextUtil.joinContexts(false,false,"trailing/","slash/"));
Assert.assertEquals("/trailing/slash+/" ,ContextUtil.joinContexts(true,true,"trailing/","slash+/"));
private static final class testJoin {

Assert.assertEquals("leading/slash" ,ContextUtil.joinContexts(false,false,"/leading","/slash"));
Assert.assertEquals("/leading/slash+/" ,ContextUtil.joinContexts(true,true,"/leading","/slash+"));
private final String expected;
private final String[] contexts;
private final boolean leadingSlash, trailingSlash;

Assert.assertEquals("double/slash" ,ContextUtil.joinContexts(false,false,"/double/","/slash/"));
Assert.assertEquals("/double/slash+/" ,ContextUtil.joinContexts(true,true,"/double/","/slash+/"));
public testJoin(final String expected, final boolean leadingSlash, final boolean trailingSlash, final String... contexts){
this.expected = expected;
this.leadingSlash = leadingSlash;
this.trailingSlash = trailingSlash;
this.contexts = contexts;
}

Assert.assertEquals("no/slash" ,ContextUtil.joinContexts(false,false,"no","slash"));
Assert.assertEquals("/no/slash+/" ,ContextUtil.joinContexts(true,true,"no","slash+"));
}

Assert.assertEquals("cons/slash" ,ContextUtil.joinContexts(false,false,"/cons/","/slash/"));
Assert.assertEquals("/cons/slash+/" ,ContextUtil.joinContexts(true,true,"/cons/","/slash+/"));
@Test
public final void testJoin(){
final testJoin[] tests = {
new testJoin("testBlank" ,false ,false ,"testBlank",""),
new testJoin("/testBlank/" ,true ,true ,"testBlank",""),
new testJoin("testBlank" ,false ,false ,"","testBlank"),
new testJoin("/testBlank/" ,true ,true ,"","testBlank"),
new testJoin("" ,false ,false ,"",""),
new testJoin("/" ,true ,true ,"",""),
new testJoin("trailing/slash" , false , false ,"trailing/","slash/"),
new testJoin("/trailing/slash/" , true , true ,"trailing/","slash/"),
new testJoin("leading/slash" , false , false ,"leading/","slash/"),
new testJoin("/leading/slash/" , true , true ,"leading/","slash/"),
new testJoin("double/slash" , false , false ,"/double/","/slash/"),
new testJoin("/double/slash/" , true , true ,"/double/","/slash/"),
new testJoin("no/slash" , false , false ,"no","slash"),
new testJoin("/no/slash/" , true , true ,"no","slash"),
new testJoin("consecutive/slash" , false , false ,"//consecutive//","//slash//"),
new testJoin("/consecutive/slash/" , true , true ,"//consecutive//","//slash//"),
new testJoin("mixed/slash" , false , false ,"\\mixed\\","//slash//"),
new testJoin("/mixed/slash/" , true , true ,"\\mixed\\","//slash//"),
};

Assert.assertEquals("mix/slash" ,ContextUtil.joinContexts(false,false,"\\mix\\","/slash/"));
Assert.assertEquals("/mix/slash+/" ,ContextUtil.joinContexts(true,true,"\\mix\\","/slash+/"));
for(final testJoin test : tests)
Assert.assertEquals(String.format("Incorrect context for #(%s, %s, %s)", test.leadingSlash, test.trailingSlash, Arrays.toString(test.contexts)), test.expected, ContextUtil.joinContexts(test.leadingSlash, test.trailingSlash, test.contexts));
}

}
Loading