Skip to content

Commit 273f4fa

Browse files
committed
allow to set a custom/fake Server fix #430
1 parent 93b2921 commit 273f4fa

2 files changed

Lines changed: 53 additions & 1 deletion

File tree

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,8 @@ public EnvDep(final Predicate<String> predicate, final Consumer<Config> callback
653653
/** Bean parser . */
654654
private Optional<Parser> beanParser = Optional.empty();
655655

656+
private ServerLookup server = new ServerLookup();
657+
656658
public Jooby() {
657659
this(null);
658660
}
@@ -665,7 +667,7 @@ public Jooby() {
665667
*/
666668
public Jooby(final String prefix) {
667669
this.prefix = prefix;
668-
use(new ServerLookup());
670+
use(server);
669671
}
670672

671673
/**
@@ -679,6 +681,25 @@ public Jooby use(final Jooby app) {
679681
return use(Optional.empty(), app);
680682
}
681683

684+
/**
685+
* Use the provided HTTP server.
686+
*
687+
* @param server Server.
688+
* @return This jooby instance.
689+
*/
690+
public Jooby server(final Class<? extends Server> server) {
691+
requireNonNull(server, "Server required.");
692+
// remove server lookup
693+
List<Object> tmp = bag.stream()
694+
.skip(1)
695+
.collect(Collectors.toList());
696+
tmp.add(0,
697+
(Module) (env, conf, binder) -> binder.bind(Server.class).to(server).asEagerSingleton());
698+
bag.clear();
699+
bag.addAll(tmp);
700+
return this;
701+
}
702+
682703
/**
683704
* Import content from provide application (routes, parsers/renderers, start/stop callbacks, ...
684705
* etc.). Routes will be mounted at the provided path.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.jooby.issues;
2+
3+
import org.jooby.Jooby;
4+
import org.jooby.spi.Server;
5+
import org.junit.Test;
6+
7+
public class Issue430 {
8+
9+
public static class NOOP implements Server {
10+
11+
@Override
12+
public void start() throws Exception {
13+
throw new UnsupportedOperationException();
14+
}
15+
16+
@Override
17+
public void stop() throws Exception {
18+
}
19+
20+
@Override
21+
public void join() throws InterruptedException {
22+
}
23+
24+
}
25+
26+
@Test(expected = UnsupportedOperationException.class)
27+
public void customServer() throws Throwable {
28+
new Jooby().server(NOOP.class).start();
29+
}
30+
31+
}

0 commit comments

Comments
 (0)