Skip to content

Commit ad0afe0

Browse files
committed
template engine: no session available fix #3941
1 parent 85b0331 commit ad0afe0

4 files changed

Lines changed: 53 additions & 1 deletion

File tree

jooby/src/main/java/io/jooby/DefaultContext.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,10 @@ default Session session() {
167167
if (session == null) {
168168
Router router = getRouter();
169169
SessionStore store = router.getSessionStore();
170+
// edge-case: user ask for session or null, treat unsupported as null
171+
if (store == SessionStore.UNSUPPORTED) {
172+
return null;
173+
}
170174
session = store.findSession(this);
171175
if (session != null) {
172176
getAttributes().put(Session.NAME, session);

jooby/src/test/java/io/jooby/DefaultContextTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,13 @@ void sessionOrNullExisting() {
211211
assertSame(sessionMock, attributes.get(Session.NAME));
212212
}
213213

214+
@Test
215+
void sessionOrNullUnsupported() {
216+
when(router.getSessionStore()).thenReturn(SessionStore.UNSUPPORTED);
217+
218+
assertNull(ctx.sessionOrNull());
219+
}
220+
214221
@Test
215222
void sessionMissingValues() {
216223
// Session is null -> session(String) returns missing, session(String, String) returns default

tests/src/test/java/io/jooby/i3936/Issue3936.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ void shouldUnderstandHtmxRequest(ServerTestRunner runner) {
4040
"isError",
4141
true));
4242
app.install(new HtmxModule(globalErrorHandler));
43-
app.install(new HandlebarsModule(TestUtil.userdir("src/test/resources/htmx")));
43+
app.install(
44+
new HandlebarsModule(TestUtil.userdir("src", "test", "resources", "htmx")));
4445
app.install(new HibernateValidatorModule());
4546

4647
app.mvc(new TaskUIHtmx_(new TaskRepo3936()));
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Jooby https://jooby.io
3+
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
4+
* Copyright 2014 Edgar Espina
5+
*/
6+
package io.jooby.i3941;
7+
8+
import static org.junit.jupiter.api.Assertions.assertEquals;
9+
10+
import java.util.Map;
11+
12+
import io.jooby.ModelAndView;
13+
import io.jooby.SessionStore;
14+
import io.jooby.handlebars.HandlebarsModule;
15+
import io.jooby.junit.ServerTest;
16+
import io.jooby.junit.ServerTestRunner;
17+
import io.jooby.test.TestUtil;
18+
19+
public class Issue3941 {
20+
21+
@ServerTest
22+
public void shouldTriggerNoSessionError(ServerTestRunner runner) {
23+
runner
24+
.define(
25+
app -> {
26+
app.setSessionStore(SessionStore.UNSUPPORTED);
27+
app.install(
28+
new HandlebarsModule(TestUtil.userdir("src", "test", "resources", "views")));
29+
app.get("/3814", ctx -> ModelAndView.map("index.hbs", Map.of("name", "3941")));
30+
})
31+
.ready(
32+
http -> {
33+
http.get(
34+
"/3814",
35+
rsp -> {
36+
assertEquals("Hello 3941!", rsp.body().string().trim());
37+
});
38+
});
39+
}
40+
}

0 commit comments

Comments
 (0)