use of ninja.utils.Message in project ninja by ninjaframework.
the class NinjaDefaultTest method testGetBadRequest.
@Test
public void testGetBadRequest() throws Exception {
when(ninjaProperties.getWithDefault(Matchers.eq(NinjaConstant.LOCATION_VIEW_HTML_BAD_REQUEST_KEY), Matchers.eq(NinjaConstant.LOCATION_VIEW_FTL_HTML_BAD_REQUEST))).thenReturn(NinjaConstant.LOCATION_VIEW_FTL_HTML_BAD_REQUEST);
// real test:
Result result = ninjaDefault.getBadRequestResult(contextImpl, new BadRequestException("not important"));
assertThat(result.getStatusCode(), equalTo(Result.SC_400_BAD_REQUEST));
assertThat(result.getTemplate(), equalTo(NinjaConstant.LOCATION_VIEW_FTL_HTML_BAD_REQUEST));
assertTrue(result.getRenderable() instanceof Message);
verify(messages).getWithDefault(Matchers.eq(NinjaConstant.I18N_NINJA_SYSTEM_BAD_REQUEST_TEXT_KEY), Matchers.eq(NinjaConstant.I18N_NINJA_SYSTEM_BAD_REQUEST_TEXT_DEFAULT), Matchers.eq(contextImpl), any(Optional.class));
verify(ninjaProperties).getWithDefault(Matchers.eq(NinjaConstant.LOCATION_VIEW_HTML_BAD_REQUEST_KEY), Matchers.eq(NinjaConstant.LOCATION_VIEW_FTL_HTML_BAD_REQUEST));
}
use of ninja.utils.Message in project ninja by ninjaframework.
the class NinjaDefault method getUnauthorizedResult.
@Override
public Result getUnauthorizedResult(Context context) {
if (isDiagnosticsEnabled()) {
DiagnosticError diagnosticError = DiagnosticErrorBuilder.build401UnauthorizedDiagnosticError();
return Results.unauthorized().render(diagnosticError);
}
String messageI18n = messages.getWithDefault(NinjaConstant.I18N_NINJA_SYSTEM_UNAUTHORIZED_REQUEST_TEXT_KEY, NinjaConstant.I18N_NINJA_SYSTEM_UNAUTHORIZED_REQUEST_TEXT_DEFAULT, context, Optional.<Result>empty());
Message message = new Message(messageI18n);
// WWW-Authenticate must be included per the spec
// http://www.ietf.org/rfc/rfc2617.txt 3.2.1 The WWW-Authenticate Response Header
Result result = Results.unauthorized().addHeader(Result.WWW_AUTHENTICATE, "None").supportedContentTypes(Result.TEXT_HTML, Result.APPLICATION_JSON, Result.APPLICATION_XML).fallbackContentType(Result.TEXT_HTML).render(message).template(ninjaProperties.getWithDefault(NinjaConstant.LOCATION_VIEW_HTML_UNAUTHORIZED_KEY, NinjaConstant.LOCATION_VIEW_FTL_HTML_UNAUTHORIZED));
return result;
}
use of ninja.utils.Message in project ninja by ninjaframework.
the class NinjaDefault method getBadRequestResult.
@Override
public Result getBadRequestResult(Context context, Exception exception) {
if (isDiagnosticsEnabled()) {
DiagnosticError diagnosticError = DiagnosticErrorBuilder.build400BadRequestDiagnosticError(exception, true);
return Results.badRequest().render(diagnosticError);
}
String messageI18n = messages.getWithDefault(NinjaConstant.I18N_NINJA_SYSTEM_BAD_REQUEST_TEXT_KEY, NinjaConstant.I18N_NINJA_SYSTEM_BAD_REQUEST_TEXT_DEFAULT, context, Optional.<Result>empty());
Message message = new Message(messageI18n);
Result result = Results.badRequest().supportedContentTypes(Result.TEXT_HTML, Result.APPLICATION_JSON, Result.APPLICATION_XML).fallbackContentType(Result.TEXT_HTML).render(message).template(ninjaProperties.getWithDefault(NinjaConstant.LOCATION_VIEW_HTML_BAD_REQUEST_KEY, NinjaConstant.LOCATION_VIEW_FTL_HTML_BAD_REQUEST));
return result;
}
use of ninja.utils.Message in project ninja by ninjaframework.
the class NotFoundTest method testThatNotFoundWorksJson.
@Test
public void testThatNotFoundWorksJson() {
Response response = makeRequest(Request.GET().url(testServerUrl().path("/_non_existing_url")).addHeader(HttpConstants.HEADER_ACCEPT, HttpConstants.APPLICATION_JSON));
assertEquals(404, response.httpStatus);
Message message = response.payloadJsonAs(Message.class);
Assert.assertThat(message.text, CoreMatchers.equalTo(NinjaConstant.I18N_NINJA_SYSTEM_NOT_FOUND_TEXT_DEFAULT));
}
Aggregations