Skip to content

Commit 7684b78

Browse files
committed
better assets handler initial setup, using onStart callback
1 parent b459fd9 commit 7684b78

3 files changed

Lines changed: 54 additions & 16 deletions

File tree

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.jooby;
2+
3+
import org.jooby.test.ServerFeature;
4+
import org.junit.Test;
5+
6+
import com.google.inject.Key;
7+
import com.google.inject.name.Names;
8+
9+
public class EnvConfCallbackFeature extends ServerFeature {
10+
11+
{
12+
Key<String> key = Key.get(String.class, Names.named(("envcallback")));
13+
on("dev", conf -> {
14+
use((env, config, binder) -> {
15+
binder.bind(key).toInstance(env.name());
16+
});
17+
});
18+
19+
get("/", req -> req.require(key));
20+
}
21+
22+
@Test
23+
public void devCallback() throws Exception {
24+
request().get("/").expect("dev");
25+
}
26+
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3129,7 +3129,8 @@ public Route.Definition assets(final String path) {
31293129
@Override
31303130
public Route.Definition assets(final String path, final String location) {
31313131
AssetHandler handler = new AssetHandler(location);
3132-
on("*", conf -> {
3132+
onStart(r -> {
3133+
Config conf = r.require(Config.class);
31333134
handler
31343135
.cdn(conf.getString("assets.cdn"))
31353136
.lastModified(conf.getBoolean("assets.lastModified"))

jooby/src/test/java/org/jooby/JoobyTest.java

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,7 @@ public Object m1() {
524524
expect(injector.getInstance(Route.KEY)).andReturn(Collections.emptySet());
525525
expect(injector.getInstance(WebSocket.KEY)).andReturn(Collections.emptySet());
526526
injector.injectMembers(isA(Jooby.class));
527+
unit.registerMock(Injector.class, injector);
527528

528529
AppPrinter printer = unit.constructor(AppPrinter.class)
529530
.args(Set.class, Set.class, Config.class)
@@ -2062,7 +2063,8 @@ public void assets() throws Exception {
20622063
.expect(requestScope)
20632064
.expect(webSockets)
20642065
.expect(tmpdir)
2065-
.expect(err).expect(executor("deferred"))
2066+
.expect(err)
2067+
.expect(executor("deferred"))
20662068
.expect(unit -> {
20672069
Mutant ifModifiedSince = unit.mock(Mutant.class);
20682070
expect(ifModifiedSince.toOptional(Long.class)).andReturn(Optional.empty());
@@ -2084,6 +2086,15 @@ public void assets() throws Exception {
20842086
Route.Chain chain = unit.get(Route.Chain.class);
20852087
chain.next(req, rsp);
20862088
})
2089+
.expect(unit -> {
2090+
Config conf = unit.get(Config.class);
2091+
expect(conf.getString("assets.cdn")).andReturn("").times(2);
2092+
expect(conf.getBoolean("assets.lastModified")).andReturn(true).times(2);
2093+
expect(conf.getBoolean("assets.etag")).andReturn(true).times(2);
2094+
2095+
Injector injector = unit.get(Injector.class);
2096+
expect(injector.getInstance(Key.get(Config.class))).andReturn(conf).times(2);
2097+
})
20872098
.run(unit -> {
20882099
Jooby jooby = new Jooby();
20892100

@@ -2259,32 +2270,32 @@ public void ws() throws Exception {
22592270
.expect(routeHandler)
22602271
.expect(params)
22612272
.expect(requestScope)
2262-
.expect(
2263-
unit -> {
2264-
Multibinder<WebSocket.Definition> multibinder = unit.mock(Multibinder.class);
2273+
.expect(unit -> {
2274+
Multibinder<WebSocket.Definition> multibinder = unit.mock(Multibinder.class);
22652275

2266-
LinkedBindingBuilder<WebSocket.Definition> binding = unit
2267-
.mock(LinkedBindingBuilder.class);
2268-
binding.toInstance(unit.capture(WebSocket.Definition.class));
2276+
LinkedBindingBuilder<WebSocket.Definition> binding = unit
2277+
.mock(LinkedBindingBuilder.class);
2278+
binding.toInstance(unit.capture(WebSocket.Definition.class));
22692279

2270-
expect(multibinder.addBinding()).andReturn(binding);
2280+
expect(multibinder.addBinding()).andReturn(binding);
22712281

2272-
Binder binder = unit.get(Binder.class);
2282+
Binder binder = unit.get(Binder.class);
22732283

2274-
expect(Multibinder.newSetBinder(binder, WebSocket.Definition.class)).andReturn(
2275-
multibinder);
2276-
})
2284+
expect(Multibinder.newSetBinder(binder, WebSocket.Definition.class)).andReturn(
2285+
multibinder);
2286+
})
22772287
.expect(tmpdir)
2278-
.expect(err).expect(executor("deferred"))
2288+
.expect(err)
2289+
.expect(executor("deferred"))
22792290
.run(unit -> {
22802291

22812292
Jooby jooby = new Jooby();
22822293

22832294
WebSocket.Definition ws = jooby.ws("/", (socket) -> {
22842295
});
22852296
assertEquals("/", ws.pattern());
2286-
assertEquals(MediaType.all, ws.consumes());
2287-
assertEquals(MediaType.all, ws.produces());
2297+
assertEquals(MediaType.plain, ws.consumes());
2298+
assertEquals(MediaType.plain, ws.produces());
22882299
defs.add(ws);
22892300

22902301
jooby.start();

0 commit comments

Comments
 (0)