Search in sources :

Example 11 with Result

use of ninja.Result in project ninja by ninjaframework.

the class InstrumentedNinja method onRouteRequest.

@Override
@Timed
public void onRouteRequest(Context.Impl context) {
    activeRequests.inc();
    String httpMethod = context.getMethod();
    Route route = router.getRouteFor(httpMethod, context.getRequestPath());
    context.setRoute(route);
    if (route != null) {
        allRequestsMeter.mark();
        try {
            Result result = route.getFilterChain().next(context);
            resultHandler.handleResult(result, context);
        } catch (Exception exception) {
            if (exception instanceof BadRequestException) {
                badRequests.mark();
            } else {
                internalServerErrors.mark();
            }
            Result result = onException(context, exception);
            renderErrorResultAndCatchAndLogExceptions(result, context);
        }
    } else {
        // throw a 404 "not found" because we did not find the route
        routesNotFound.mark();
        Result result = getNotFoundResult(context);
        renderErrorResultAndCatchAndLogExceptions(result, context);
    }
    activeRequests.dec();
}
Also used : BadRequestException(ninja.exceptions.BadRequestException) Route(ninja.Route) BadRequestException(ninja.exceptions.BadRequestException) Result(ninja.Result)

Example 12 with Result

use of ninja.Result in project ninja by ninjaframework.

the class NinjaServletContextTest method testAddCookieViaResult.

@Test
public void testAddCookieViaResult() {
    Cookie cookie = Cookie.builder("cookie", "yum").setDomain("domain").build();
    context.init(servletContext, httpServletRequest, httpServletResponse);
    // context.addCookie(cookie);
    // generate an arbitrary result:
    Result result = Results.html();
    result.addCookie(cookie);
    // finalize the headers => the cookies must be copied over to the servletcookies
    context.finalizeHeaders(result);
    // and verify the stuff:
    ArgumentCaptor<javax.servlet.http.Cookie> cookieCaptor = ArgumentCaptor.forClass(javax.servlet.http.Cookie.class);
    verify(httpServletResponse).addCookie(cookieCaptor.capture());
    javax.servlet.http.Cookie resultCookie = cookieCaptor.getValue();
    assertThat(resultCookie.getName(), equalTo("cookie"));
    assertThat(resultCookie.getValue(), equalTo("yum"));
    assertThat(resultCookie.getPath(), equalTo("/"));
    assertThat(resultCookie.getSecure(), equalTo(false));
    assertThat(resultCookie.getMaxAge(), equalTo(-1));
}
Also used : Cookie(ninja.Cookie) Result(ninja.Result) Test(org.junit.Test)

Example 13 with Result

use of ninja.Result in project ninja by ninjaframework.

the class ApiControllerMockTest method testThatPostArticleReturnsNotFoundWhenArticleDaoReturnsFalse.

@Test
public void testThatPostArticleReturnsNotFoundWhenArticleDaoReturnsFalse() {
    when(articleDao.postArticle(null, null)).thenReturn(false);
    Result result = apiController.postArticleJson(null, null);
    assertEquals(404, result.getStatusCode());
}
Also used : Result(ninja.Result) Test(org.junit.Test)

Example 14 with Result

use of ninja.Result in project ninja by ninjaframework.

the class ApiControllerMockTest method testThatPostArticleReturnsOkWhenArticleDaoReturnsTrue.

@Test
public void testThatPostArticleReturnsOkWhenArticleDaoReturnsTrue() {
    when(articleDao.postArticle(null, null)).thenReturn(true);
    Result result = apiController.postArticleJson(null, null);
    assertEquals(200, result.getStatusCode());
}
Also used : Result(ninja.Result) Test(org.junit.Test)

Example 15 with Result

use of ninja.Result in project ninja by ninjaframework.

the class ResultHandlerTest method testRenderPictureFromBytes.

@Test
public void testRenderPictureFromBytes() {
    final byte[] toRender = new byte[] { 1, 2, 3 };
    final String contentType = "image/png";
    Result result = Results.ok();
    result.contentType(contentType);
    result.renderRaw(toRender);
    resultHandler.handleResult(result, context);
    assertEquals(contentType, result.getContentType());
}
Also used : Result(ninja.Result) Test(org.junit.Test)

Aggregations

Result (ninja.Result)37 Test (org.junit.Test)26 TemplateModel (freemarker.template.TemplateModel)6 ArrayList (java.util.ArrayList)6 List (java.util.List)6 Cookie (ninja.Cookie)6 SimpleScalar (freemarker.template.SimpleScalar)4 Context (ninja.Context)3 Timed (ninja.metrics.Timed)3 ResponseStreams (ninja.utils.ResponseStreams)3 BeansWrapper (freemarker.ext.beans.BeansWrapper)2 StringModel (freemarker.ext.beans.StringModel)2 Date (java.util.Date)2 ConstraintViolation (ninja.validation.ConstraintViolation)2 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)2 Before (org.junit.Before)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1