Skip to content

Commit d0083c4

Browse files
committed
Preserve AssetHandler configuration in Request.assets
Issue jooby-project#356
1 parent bbcf529 commit d0083c4

6 files changed

Lines changed: 79 additions & 84 deletions

File tree

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package org.jooby.issues;
2+
3+
import org.jooby.handlers.AssetHandler;
4+
import org.jooby.test.ServerFeature;
5+
6+
import static org.junit.Assert.assertNotNull;
7+
import static org.junit.Assert.assertNull;
8+
import static org.junit.Assert.assertTrue;
9+
10+
import org.junit.Assert;
11+
import org.junit.Test;
12+
13+
public class Issue356 extends ServerFeature {
14+
{
15+
assets("/assets/file.css", new AssetHandler("/")
16+
.etag(false).lastModified(false).maxAge(86400));
17+
18+
assets("/assets/favicon.ico", new AssetHandler("/")
19+
.etag(false).lastModified(true).maxAge(1209600));
20+
21+
assets("/assets/file.js", new AssetHandler("/")
22+
.etag(true).lastModified(false).maxAge(172800));
23+
24+
assets("/assets/empty.css", new AssetHandler("/")
25+
.etag(true).lastModified(true).maxAge(604800));
26+
}
27+
28+
@Test
29+
public void etag() throws Exception {
30+
request().get("/assets/file.css").execute().header("ETag", Assert::assertNull);
31+
request().get("/assets/favicon.ico").execute().header("ETag", Assert::assertNull);
32+
request().get("/assets/file.js").execute().header("ETag", Assert::assertNotNull);
33+
request().get("/assets/empty.css").execute().header("ETag", Assert::assertNotNull);
34+
}
35+
36+
@Test
37+
public void lastModified() throws Exception {
38+
request().get("/assets/file.css").execute().header("Last-Modified", Assert::assertNull);
39+
request().get("/assets/favicon.ico").execute().header("Last-Modified", Assert::assertNotNull);
40+
request().get("/assets/file.js").execute().header("Last-Modified", Assert::assertNull);
41+
request().get("/assets/empty.css").execute().header("Last-Modified", Assert::assertNotNull);
42+
}
43+
44+
@Test
45+
public void maxAge() throws Exception {
46+
request().get("/assets/file.css").execute().header("Cache-Control", value -> {
47+
assertNotNull(value);
48+
assertTrue(value.contains("max-age=86400"));
49+
});
50+
request().get("/assets/favicon.ico").execute().header("Cache-Control", value -> {
51+
assertNotNull(value);
52+
assertTrue(value.contains("max-age=1209600"));
53+
});
54+
request().get("/assets/file.js").execute().header("Cache-Control", value -> {
55+
assertNotNull(value);
56+
assertTrue(value.contains("max-age=172800"));
57+
});
58+
request().get("/assets/empty.css").execute().header("Cache-Control", value -> {
59+
assertNotNull(value);
60+
assertTrue(value.contains("max-age=604800"));
61+
});
62+
}
63+
}

jooby/src/main/java/org/jooby/Jooby.java

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@
9191
import org.jooby.Session.Store;
9292
import org.jooby.handlers.AssetHandler;
9393
import org.jooby.internal.AppPrinter;
94-
import org.jooby.internal.AssetProxy;
9594
import org.jooby.internal.BuiltinParser;
9695
import org.jooby.internal.BuiltinRenderer;
9796
import org.jooby.internal.DefaulErrRenderer;
@@ -3033,7 +3032,14 @@ public Route.Definition assets(final String path) {
30333032
*/
30343033
@Override
30353034
public Route.Definition assets(final String path, final String location) {
3036-
return assets(path, new AssetHandler(location));
3035+
AssetHandler handler = new AssetHandler(location);
3036+
on("*", conf -> {
3037+
handler
3038+
.cdn(conf.getString("assets.cdn"))
3039+
.lastModified(conf.getBoolean("assets.lastModified"))
3040+
.etag(conf.getBoolean("assets.etag"));
3041+
});
3042+
return assets(path, handler);
30373043
}
30383044

30393045
/**
@@ -3080,16 +3086,7 @@ public Route.Definition assets(final String path, final String location) {
30803086
*/
30813087
@Override
30823088
public Route.Definition assets(final String path, final AssetHandler handler) {
3083-
3084-
AssetProxy router = new AssetProxy();
3085-
Route.Definition asset = new Route.Definition("GET", path, router);
3086-
on("*", conf -> {
3087-
router.fwd(handler
3088-
.cdn(conf.getString("assets.cdn"))
3089-
.lastModified(conf.getBoolean("assets.lastModified"))
3090-
.etag(conf.getBoolean("assets.etag")));
3091-
});
3092-
return appendDefinition(asset);
3089+
return appendDefinition(new Route.Definition("GET", path, handler));
30933090
}
30943091

30953092
/**

jooby/src/main/java/org/jooby/Route.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
import java.util.Set;
3737
import java.util.stream.Collectors;
3838

39-
import org.jooby.internal.AssetProxy;
4039
import org.jooby.internal.DeferredExecution;
4140
import org.jooby.internal.RouteImpl;
4241
import org.jooby.internal.RouteMatcher;
@@ -1333,11 +1332,8 @@ public String toString() {
13331332
*/
13341333
private Route asRoute(final String method, final RouteMatcher matcher,
13351334
final List<MediaType> produces) {
1336-
Route.Filter filter = this.filter;
1337-
if (filter instanceof AssetProxy) {
1338-
filter = ((AssetProxy) filter).delegate();
1339-
}
1340-
return new RouteImpl(filter, this, method, matcher.path(), produces, matcher.vars(), mapper);
1335+
return new RouteImpl(filter, this, method, matcher.path(), produces,
1336+
matcher.vars(), mapper);
13411337
}
13421338

13431339
}

jooby/src/main/java/org/jooby/internal/AssetProxy.java

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

jooby/src/test/java/org/jooby/internal/AssetProxyTest.java

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

jooby/src/test/java/org/jooby/test/Client.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public Request(final Client server, final Executor executor,
7272
this.req = req;
7373
}
7474

75-
private Response execute() throws Exception {
75+
public Response execute() throws Exception {
7676
this.rsp = executor.execute(req).returnResponse();
7777
return new Response(server, rsp);
7878
}
@@ -252,7 +252,10 @@ public Response header(final String headerName, final Optional<Object> headerVal
252252
}
253253

254254
public Response header(final String headerName, final Callback callback) throws Exception {
255-
callback.execute(rsp.getFirstHeader(headerName).getValue());
255+
callback.execute(
256+
Optional.ofNullable(rsp.getFirstHeader(headerName))
257+
.map(Header::getValue)
258+
.orElse(null));
256259
return this;
257260
}
258261

0 commit comments

Comments
 (0)