Skip to content
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
2 changes: 1 addition & 1 deletion avaje-jex/src/main/java/io/avaje/jex/Jex.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ default Jex post(String path, ExchangeHandler handler, Role... roles) {
* @param roles An array of roles that are associated with this endpoint.
*/
default Jex put(String path, ExchangeHandler handler, Role... roles) {
routing().get(path, handler, roles);
routing().put(path, handler, roles);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ final class PathParser {

PathParser(String path, boolean ignoreTrailingSlashes) {
this.rawPath = path;
final RegBuilder regBuilder = new RegBuilder();
final RegBuilder regBuilder = new RegBuilder(ignoreTrailingSlashes);
for (String rawSeg : path.split("/")) {
if (!rawSeg.isEmpty()) {
segmentCount++;
Expand Down
10 changes: 10 additions & 0 deletions avaje-jex/src/main/java/io/avaje/jex/routes/RegBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,15 @@
final class RegBuilder {
private final StringJoiner full = new StringJoiner("/");
private final StringJoiner extract = new StringJoiner("/");
private final boolean ignoreTrailingSlashes;
private boolean trailingSlash;
private boolean multiSlash;
private boolean literal = true;

public RegBuilder(boolean ignoreTrailingSlashes) {
this.ignoreTrailingSlashes = ignoreTrailingSlashes;
}

void add(PathSegment pathSegment, List<String> paramNames) {
full.add(pathSegment.asRegexString(false));
extract.add(pathSegment.asRegexString(true));
Expand Down Expand Up @@ -48,6 +53,11 @@ private String wrap(String parts) {
if (trailingSlash) {
parts += "\\/";
}

if (!ignoreTrailingSlashes) {
return "^/" + parts;
}

return "^/" + parts + "/?$";
}

Expand Down
25 changes: 25 additions & 0 deletions avaje-jex/src/test/java/io/avaje/jex/ShutDownTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.avaje.jex;

import static org.assertj.core.api.Assertions.assertThat;

import java.util.ArrayList;
import java.util.List;

import org.junit.jupiter.api.Test;

class ShutDownTest {

@Test
void shutDownHooks() {

List<String> results = new ArrayList<>();
var jex = Jex.create().config(c -> c.socketBacklog(0));
jex.lifecycle().onShutdown(() -> results.add("onShut"));
jex.lifecycle().registerShutdownHook(() -> results.add("onHook"));
var server = jex.start();

server.onShutdown(() -> results.add("serverShut"));
server.shutdown();
assertThat(results).hasSize(2); // 2 because jvm shutdown won't run in a junit
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import io.avaje.jex.Jex;
import io.avaje.jex.core.Constants;
import io.avaje.jex.core.TestPair;
import io.avaje.jex.http.ContentType;

class CompressionTest {

Expand All @@ -27,7 +28,8 @@ static TestPair init() {
r.get(
"/compress",
ctx ->
ctx.write(CompressionTest.class.getResourceAsStream("/64KB.json")))
ctx.contentType(ContentType.APPLICATION_JSON)
.write(CompressionTest.class.getResourceAsStream("/64KB.json")))
.get(
"/sus",
ctx ->
Expand Down
42 changes: 42 additions & 0 deletions avaje-jex/src/test/java/io/avaje/jex/http/TrailingSlashTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package io.avaje.jex.http;

import static org.assertj.core.api.Assertions.assertThat;

import java.net.http.HttpResponse;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Test;

import io.avaje.jex.Jex;
import io.avaje.jex.core.TestPair;

class TrailingSlashTest {

static TestPair pair = init();

static TestPair init() {
final Jex app =
Jex.create()
.config(c -> c.socketBacklog(0).ignoreTrailingSlashes(false))
.get("/slash", ctx -> {});

return TestPair.create(app);
}

@AfterAll
static void end() {
pair.shutdown();
}

@Test
void get() {
HttpResponse<String> res = pair.request().path("slash/").GET().asString();
assertThat(res.statusCode()).isEqualTo(404);
}

@Test
void getNoTrailing() {
HttpResponse<String> res = pair.request().path("slash").GET().asString();
assertThat(res.statusCode()).isEqualTo(204);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ void matches_trailingSlash_honor() {

assertTrue(pathParser.matches("/one/1/"));
assertTrue(pathParser.matches("/one/2/"));
assertTrue(pathParser.matches("/one/3//")); // accepts trailing double slash?
assertFalse(pathParser.matches("/one/3//")); // accepts trailing double slash?
assertFalse(pathParser.matches("/one/3///")); // but not triple slash?
assertFalse(pathParser.matches("/one/1"));
assertFalse(pathParser.matches("/one/2"));
Expand Down