-
-
Notifications
You must be signed in to change notification settings - Fork 198
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
template-engine: must initialize flash attributes only when there is …
…one flash cookie - fix #3607
- Loading branch information
Showing
6 changed files
with
107 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Jooby https://jooby.io | ||
* Apache License Version 2.0 https://jooby.io/LICENSE.txt | ||
* Copyright 2014 Edgar Espina | ||
*/ | ||
package io.jooby; | ||
|
||
import static org.mockito.Mockito.*; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import io.jooby.buffer.DataBuffer; | ||
import io.jooby.buffer.DefaultDataBufferFactory; | ||
|
||
public class Issue3607 { | ||
|
||
private static class TemplateEngineImpl implements TemplateEngine { | ||
@Override | ||
public DataBuffer render(Context ctx, ModelAndView<?> modelAndView) throws Exception { | ||
// do nothing | ||
return DefaultDataBufferFactory.sharedInstance.wrap(new byte[0]); | ||
} | ||
} | ||
|
||
@Test | ||
public void shouldNotGenerateEmptyFlashMap() throws Exception { | ||
var ctx = mock(Context.class); | ||
|
||
var templateEngine = new TemplateEngineImpl(); | ||
templateEngine.encode(ctx, ModelAndView.map("index.html")); | ||
|
||
verify(ctx, times(1)).flashOrNull(); | ||
verify(ctx, times(1)).sessionOrNull(); | ||
verify(ctx, times(1)).setDefaultResponseType(MediaType.html); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Jooby https://jooby.io | ||
* Apache License Version 2.0 https://jooby.io/LICENSE.txt | ||
* Copyright 2014 Edgar Espina | ||
*/ | ||
package io.jooby.i3607; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
import java.util.Map; | ||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
import io.jooby.FlashMap; | ||
import io.jooby.ModelAndView; | ||
import io.jooby.junit.ServerTest; | ||
import io.jooby.junit.ServerTestRunner; | ||
import io.jooby.pebble.PebbleModule; | ||
|
||
public class Issue3607 { | ||
@ServerTest | ||
public void shouldNotGenerateEmptyFlashMap(ServerTestRunner runner) throws InterruptedException { | ||
var latch = new CountDownLatch(1); | ||
var mustBeNull = new AtomicBoolean(false); | ||
runner | ||
.define( | ||
app -> { | ||
app.install(new PebbleModule()); | ||
app.use( | ||
next -> | ||
ctx -> { | ||
ctx.onComplete( | ||
done -> { | ||
mustBeNull.set(!done.getAttributes().containsKey(FlashMap.NAME)); | ||
latch.countDown(); | ||
}); | ||
return next.apply(ctx); | ||
}); | ||
app.get("/3607", ctx -> ModelAndView.map("index.pebble", Map.of("name", "Pebble"))); | ||
}) | ||
.ready( | ||
client -> { | ||
client.get( | ||
"/3607", | ||
rsp -> { | ||
assertEquals("Hello Pebble!", rsp.body().string().trim()); | ||
}); | ||
}); | ||
latch.await(); | ||
assertTrue(mustBeNull.get(), "Flash map must be null"); | ||
} | ||
} |