use of ninja.Result in project ninja by ninjaframework.
the class DiagnosticErrorRendererTest method buildAndRenderDiagnosticErrorWithHTMLEntities.
@Test
public void buildAndRenderDiagnosticErrorWithHTMLEntities() throws Exception {
NinjaDefault ninja = Mockito.spy(new NinjaDefault());
doReturn(Boolean.TRUE).when(ninja).isDiagnosticsEnabled();
// build context that will have HTML entites in key spots
when(context.getMethod()).thenReturn("GET");
when(context.getContextPath()).thenReturn("/");
when(context.getAttributes()).thenReturn(ImmutableMap.of("TEST-ATTR", (Object) "Attribute with < > & entities"));
Result testResult = ninja.getInternalServerErrorResult(context, new Exception("Exception message with < > & entities"), null);
// renderable in testResult is the DiagnosticError
Assert.assertNotNull(testResult);
Assert.assertNotNull(testResult.getRenderable());
Assert.assertThat(testResult.getRenderable(), instanceOf(DiagnosticError.class));
DiagnosticError de = (DiagnosticError) testResult.getRenderable();
DiagnosticErrorRenderer renderer = DiagnosticErrorRenderer.build(context, testResult, de);
String out = renderer.render();
Assert.assertThat(out, containsString("Attribute with < > & entities"));
Assert.assertThat(out, containsString("Exception message with < > & entities"));
}
use of ninja.Result in project ninja by ninjaframework.
the class DiagnosticErrorRendererTest method buildAndRender401UnauthorizedDiagnosticError.
@Test
public void buildAndRender401UnauthorizedDiagnosticError() throws Exception {
when(context.getMethod()).thenReturn("GET");
when(context.getContextPath()).thenReturn("/");
Result testResult = Results.unauthorized();
DiagnosticError de = DiagnosticErrorBuilder.build401UnauthorizedDiagnosticError();
Assert.assertEquals(-1, de.getLineNumberOfError());
Assert.assertEquals(-1, de.getLineNumberOfSourceLines());
Assert.assertNull(de.getThrowable());
DiagnosticErrorRenderer renderer = DiagnosticErrorRenderer.build(context, testResult, de);
String out = renderer.render();
Assert.assertTrue(out.contains("Diagnostic Error"));
Assert.assertTrue(out.contains("401"));
}
use of ninja.Result in project ninja by ninjaframework.
the class LangImplTest method testGetLanguage.
@Test
public void testGetLanguage() {
Cookie cookie = Cookie.builder("NINJA_TEST" + NinjaConstant.LANG_COOKIE_SUFFIX, "de").build();
when(ninjaProperties.getOrDie(NinjaConstant.applicationCookiePrefix)).thenReturn("NINJA_TEST");
when(context.getCookie("NINJA_TEST" + NinjaConstant.LANG_COOKIE_SUFFIX)).thenReturn(cookie);
Lang lang = new LangImpl(ninjaProperties);
// 1) with context and result => but result does not have a default lang
Result result = Results.ok();
Optional<String> language = lang.getLanguage(context, Optional.of(result));
assertEquals("de", language.get());
// 2) with context and result => result has already new lang set...
result = Results.ok();
cookie = Cookie.builder("NINJA_TEST" + NinjaConstant.LANG_COOKIE_SUFFIX, "en").build();
result.addCookie(cookie);
language = lang.getLanguage(context, Optional.of(result));
assertEquals("en", language.get());
}
use of ninja.Result in project ninja by ninjaframework.
the class LangImplTest method testChangeLanguage.
@Test
public void testChangeLanguage() {
Cookie cookie = Cookie.builder("NINJA_TEST" + NinjaConstant.LANG_COOKIE_SUFFIX, "de").build();
when(ninjaProperties.getOrDie(NinjaConstant.applicationCookiePrefix)).thenReturn("NINJA_TEST");
Lang lang = new LangImpl(ninjaProperties);
// test with result
Result result = Results.noContent();
result = lang.setLanguage("to", result);
assertEquals("to", result.getCookie(cookie.getName()).getValue());
assertEquals(Result.SC_204_NO_CONTENT, result.getStatusCode());
}
use of ninja.Result in project ninja by ninjaframework.
the class TemplateEngineFreemarkerI18nMethodTest method testThatKeyWithPlaceholderReturnsDefaultKeyWhenKeyCannotBeFound.
@Test
public void testThatKeyWithPlaceholderReturnsDefaultKeyWhenKeyCannotBeFound() throws Exception {
Optional<Result> resultOptional = Optional.of(result);
List args = new ArrayList();
args.add(new SimpleScalar("my.message.key"));
args.add(new SimpleScalar("1000"));
Mockito.when(messages.get(Matchers.eq("my.message.key"), Matchers.eq(context), Matchers.eq(resultOptional), Matchers.any(Object.class))).thenReturn(Optional.<String>empty());
TemplateModel returnValue = templateEngineFreemarkerI18nMethod.exec(args);
assertThat(((SimpleScalar) returnValue).getAsString(), CoreMatchers.equalTo("my.message.key"));
// There must have been logged something because we did not find
// the value for the key...
Mockito.verify(mockAppender).doAppend(Matchers.anyObject());
}
Aggregations