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

Commit

Permalink
Adjust HTTP Codes (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
Katsute committed Oct 21, 2022
1 parent 0e4c1cb commit a93c8d9
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 17 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>dev.katsute</groupId>
<artifactId>simplehttpserver</artifactId>
<version>5.0.0-RC-2-SNAPSHOT</version>
<version>5.0.0-RC-2</version>

<profiles>
<profile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import com.sun.net.httpserver.HttpHandler;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.util.Objects;

/**
Expand Down Expand Up @@ -50,7 +49,7 @@ public RedirectHandler(final String link){
@Override
public final void handle(final HttpExchange exchange) throws IOException{
exchange.getResponseHeaders().set("Location", link);
exchange.sendResponseHeaders(HttpURLConnection.HTTP_OK, 0);
exchange.sendResponseHeaders(308, 0); // permanent redirect
exchange.close();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import dev.katsute.simplehttpserver.SimpleHttpServer;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.util.Objects;

/**
Expand Down Expand Up @@ -69,6 +70,8 @@ public TemporaryHandler(final HttpHandler handler, final long maxTime){
public synchronized final void handle(final HttpExchange exchange) throws IOException{
if(expiry == null || System.currentTimeMillis() < expiry) // expire on first connection or past expiry
handler.handle(exchange);
else
exchange.sendResponseHeaders(HttpURLConnection.HTTP_NOT_FOUND, 0);
exchange.getHttpContext().getServer().removeContext(exchange.getHttpContext());
exchange.close();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,10 @@ public final void handle(final HttpExchange exchange) throws IOException{
* @since 5.0.0
*/
public void handle(final SimpleHttpExchange exchange, final File source, final byte[] bytes) throws IOException {
exchange.send(bytes, HttpURLConnection.HTTP_OK);
if(source == null)
exchange.send(HttpURLConnection.HTTP_NOT_FOUND);
else
exchange.send(bytes, HttpURLConnection.HTTP_OK);
}

//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ public final void handle(final SimpleHttpExchange exchange) throws IOException {
}finally{
throttler.deleteConnection(exchange);
}
else
exchange.send(429); // too many requests
exchange.close();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.katsute.simplehttpserver.handler;

import dev.katsute.simplehttpserver.Requests;
import dev.katsute.simplehttpserver.SimpleHttpServer;
import org.junit.jupiter.api.*;

Expand All @@ -25,6 +26,7 @@ static void afterAll(){
@Test
final void testRedirect() throws IOException{
server.createContext("redirect", new RedirectHandler("https://github.com/KatsuteDev/simplehttpserver"));
Assertions.assertEquals(308, Requests.getCode("http://localhost:8080/redirect"));
Assertions.assertEquals("https://github.com/KatsuteDev/simplehttpserver", getRedirectedURL("http://localhost:8080/redirect"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import org.junit.jupiter.api.*;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.concurrent.Executors;

final class ThrottlerTests {
Expand Down Expand Up @@ -52,7 +51,7 @@ final class Exchange {
@Test
final void testExchange0(){
server.createContext("exchange/0", new ThrottledHandler(ThrottlerTests.ExchangeThrottler(0), handler));
Assertions.assertThrows(UncheckedIOException.class, () -> Requests.getCode("http://localhost:8080/exchange/0"));
Assertions.assertEquals(429, Requests.getCode("http://localhost:8080/exchange/0"));
}

@Test
Expand All @@ -62,7 +61,7 @@ final void testExchange1() throws InterruptedException{
new Thread(() -> Requests.getCode("http://localhost:8080/exchange/1")).start();
Thread.sleep(250);

Assertions.assertThrows(UncheckedIOException.class, () -> Requests.getCode("http://localhost:8080/exchange/1"));
Assertions.assertEquals(429, Requests.getCode("http://localhost:8080/exchange/1"));
}

@Test
Expand Down Expand Up @@ -99,7 +98,7 @@ final class ServerExchange {
@Test
final void testExchange0(){
server.createContext("server/exchange/0", new ThrottledHandler(ThrottlerTests.ServerExchangeThrottler(0, -1, false), handler));
Assertions.assertThrows(UncheckedIOException.class, () -> Requests.getCode("http://localhost:8080/server/exchange/0"));
Assertions.assertEquals(429, Requests.getCode("http://localhost:8080/server/exchange/0"));
}

@Test
Expand All @@ -109,7 +108,7 @@ final void testExchange1() throws InterruptedException{
new Thread(() -> Requests.getCode("http://localhost:8080/server/exchange/1")).start();
Thread.sleep(250);

Assertions.assertThrows(UncheckedIOException.class, () -> Requests.getCode("http://localhost:8080/server/exchange/1"));
Assertions.assertEquals(429, Requests.getCode("http://localhost:8080/server/exchange/1"));
}

@Test
Expand All @@ -125,7 +124,7 @@ final void testExchangeU() throws InterruptedException{
@Test
final void testServerExchange0(){
server.createContext("server/exchange-server/0", new ThrottledHandler(ThrottlerTests.ServerExchangeThrottler(-1, 0, false), handler));
Assertions.assertThrows(UncheckedIOException.class, () -> Requests.getCode("http://localhost:8080/server/exchange-server/0"));
Assertions.assertEquals(429, Requests.getCode("http://localhost:8080/server/exchange-server/0"));
}

@Test
Expand All @@ -135,7 +134,7 @@ final void testServerExchange1() throws InterruptedException{
new Thread(() -> Requests.getCode("http://localhost:8080/server/exchange-server/1")).start();
Thread.sleep(250);

Assertions.assertThrows(UncheckedIOException.class, () -> Requests.getCode("http://localhost:8080/server/exchange-server/1"));
Assertions.assertEquals(429, Requests.getCode("http://localhost:8080/server/exchange-server/1"));
}

@Test
Expand Down Expand Up @@ -182,7 +181,7 @@ final void afterEach() throws InterruptedException{
@Test
final void testSession0(){
server.createContext("session/0", new ThrottledHandler(ThrottlerTests.SessionThrottler(server.getSessionHandler(), 0), handler));
Assertions.assertThrows(UncheckedIOException.class, () -> Requests.getCode("http://localhost:8080/session/0"));
Assertions.assertEquals(429, Requests.getCode("http://localhost:8080/session/0"));
}

@Test
Expand All @@ -195,7 +194,7 @@ final void testSession1() throws InterruptedException{
new Thread(() -> Requests.getCode("http://localhost:8080/session/1")).start();
Thread.sleep(250);

Assertions.assertThrows(UncheckedIOException.class, () -> Requests.getCode("http://localhost:8080/session/1"));
Assertions.assertEquals(429, Requests.getCode("http://localhost:8080/session/1"));
}

@Test
Expand Down Expand Up @@ -240,7 +239,7 @@ final void afterEach() throws InterruptedException{
@Test
final void testSession0(){
server.createContext("server/session/0", new ThrottledHandler(ThrottlerTests.ServerSessionThrottler(server.getSessionHandler(),0, -1, false), handler));
Assertions.assertThrows(UncheckedIOException.class, () -> Requests.getCode("http://localhost:8080/server/session/0"));
Assertions.assertEquals(429, Requests.getCode("http://localhost:8080/server/session/0"));
}

@Test
Expand All @@ -253,7 +252,7 @@ final void testSession1() throws InterruptedException{
new Thread(() -> Requests.getCode("http://localhost:8080/server/session/1")).start();
Thread.sleep(250);

Assertions.assertThrows(UncheckedIOException.class, () -> Requests.getCode("http://localhost:8080/server/session/1"));
Assertions.assertEquals(429, Requests.getCode("http://localhost:8080/server/session/1"));
}

@Test
Expand All @@ -272,7 +271,7 @@ final void testSessionU() throws InterruptedException{
@Test
final void testServerSession0(){
server.createContext("server/session-server/0", new ThrottledHandler(ThrottlerTests.ServerSessionThrottler(server.getSessionHandler(),-1, 0, false), handler));
Assertions.assertThrows(UncheckedIOException.class, () -> Requests.getCode("http://localhost:8080/server/session-server/0"));
Assertions.assertEquals(429, Requests.getCode("http://localhost:8080/server/session-server/0"));
}

@Test
Expand All @@ -285,7 +284,7 @@ final void testServerSession1() throws InterruptedException{
new Thread(() -> Requests.getCode("http://localhost:8080/server/session-server/1")).start();
Thread.sleep(250);

Assertions.assertThrows(UncheckedIOException.class, () -> Requests.getCode("http://localhost:8080/server/session-server/1"));
Assertions.assertEquals(429, Requests.getCode("http://localhost:8080/server/session-server/1"));
}

@Test
Expand Down

0 comments on commit a93c8d9

Please sign in to comment.