Search in sources :

Example 1 with Response

use of org.doctester.testbrowser.Response in project ninja by ninjaframework.

the class ApiControllerDocTesterTest method testGetAndPostArticleViaJson.

@Test
public void testGetAndPostArticleViaJson() {
    // /////////////////////////////////////////////////////////////////////
    // Test initial data:
    // /////////////////////////////////////////////////////////////////////
    sayNextSection("Retrieving articles for a user (Json)");
    say("Retrieving all articles of a user is a GET request to " + GET_ARTICLES_URL);
    Response response = sayAndMakeRequest(Request.GET().url(testServerUrl().path(GET_ARTICLES_URL.replace("{username}", "bob@gmail.com"))));
    ArticlesDto articlesDto = getGsonWithLongToDateParsing().fromJson(response.payload, ArticlesDto.class);
    sayAndAssertThat("We get back all 3 articles of that user", articlesDto.articles.size(), CoreMatchers.is(3));
    // /////////////////////////////////////////////////////////////////////
    // Post new article:
    // /////////////////////////////////////////////////////////////////////
    sayNextSection("Posting new article (Json)");
    say("Posting a new article is a post request to " + POST_ARTICLE_URL);
    say("Please note that you have to be authenticated in order to be allowed to post.");
    ArticleDto articleDto = new ArticleDto();
    articleDto.content = "contentcontent";
    articleDto.title = "new title new title";
    response = sayAndMakeRequest(Request.POST().url(testServerUrl().path(POST_ARTICLE_URL.replace("{username}", USER))).payload(articleDto));
    sayAndAssertThat("You have to be authenticated in order to post articles", response.httpStatus, CoreMatchers.is(403));
    doLogin();
    say("Now we are authenticated and expect the post to succeed...");
    response = sayAndMakeRequest(Request.POST().url(testServerUrl().path(POST_ARTICLE_URL.replace("{username}", USER))).contentTypeApplicationJson().payload(articleDto));
    sayAndAssertThat("After successful login we are able to post articles", response.httpStatus, CoreMatchers.is(200));
    // /////////////////////////////////////////////////////////////////////
    // Fetch articles again => assert we got a new one ...
    // /////////////////////////////////////////////////////////////////////
    say("If we now fetch the articles again we are getting a new article (the one we have posted successfully");
    response = sayAndMakeRequest(Request.GET().url(testServerUrl().path(GET_ARTICLES_URL.replace("{username}", "bob@gmail.com"))));
    articlesDto = getGsonWithLongToDateParsing().fromJson(response.payload, ArticlesDto.class);
    // one new result:
    sayAndAssertThat("We are now getting 4 articles.", articlesDto.articles.size(), CoreMatchers.is(4));
    // /////////////////////////////////////////////////////////////////////
    // Fetch single article
    // /////////////////////////////////////////////////////////////////////
    say("We can also fetch an individual article via the Json Api.");
    say("That's a GET request to: " + GET_ARTICLE_URL);
    response = sayAndMakeRequest(Request.GET().url(testServerUrl().path(GET_ARTICLE_URL.replace("{username}", "bob@gmail.com").replace("{id}", "1"))));
    Article article = getGsonWithLongToDateParsing().fromJson(response.payload, Article.class);
    // one new result:
    sayAndAssertThat("And we got back the first article", article.id, CoreMatchers.is(1L));
}
Also used : Response(org.doctester.testbrowser.Response) Article(models.Article) ArticlesDto(models.ArticlesDto) ArticleDto(models.ArticleDto) Test(org.junit.Test)

Example 2 with Response

use of org.doctester.testbrowser.Response in project ninja by ninjaframework.

the class NotFoundTest method testThatNotFoundWorksHtml.

@Test
public void testThatNotFoundWorksHtml() {
    Response response = makeRequest(Request.GET().url(testServerUrl().path("/_non_existing_url")));
    assertEquals(404, response.httpStatus);
    assertTrue(response.payload.contains("The requested route cannot be found."));
}
Also used : Response(org.doctester.testbrowser.Response) Test(org.junit.Test)

Example 3 with Response

use of org.doctester.testbrowser.Response in project ninja by ninjaframework.

the class NotFoundTest method testThatNotFoundWorksXml.

@Test
public void testThatNotFoundWorksXml() {
    Response response = makeRequest(Request.GET().url(testServerUrl().path("/_non_existing_url")).addHeader(HttpConstants.HEADER_ACCEPT, HttpConstants.APPLICATION_XML));
    assertEquals(404, response.httpStatus);
    Message message = response.payloadXmlAs(Message.class);
    Assert.assertThat(message.text, CoreMatchers.equalTo(NinjaConstant.I18N_NINJA_SYSTEM_NOT_FOUND_TEXT_DEFAULT));
}
Also used : Response(org.doctester.testbrowser.Response) Message(ninja.utils.Message) Test(org.junit.Test)

Example 4 with Response

use of org.doctester.testbrowser.Response in project ninja by ninjaframework.

the class ApplicationControllerTest method testDevModeIndexMissing012.

@Test
public void testDevModeIndexMissing012() {
    // Server runs in Test mode making pure "dev" routes unavailable
    Response response = makeRequest(Request.GET().url(testServerUrl().path("/0/1/2/mode/dev")));
    Assert.assertThat(response.httpStatus, CoreMatchers.equalTo(Result.SC_404_NOT_FOUND));
}
Also used : Response(org.doctester.testbrowser.Response) Test(org.junit.Test)

Example 5 with Response

use of org.doctester.testbrowser.Response in project ninja by ninjaframework.

the class ApplicationControllerTest method testTestModeIndex012.

@Test
public void testTestModeIndex012() {
    Response response = makeRequest(Request.GET().url(testServerUrl().path("/0/1/2/mode/test")));
    Assert.assertThat(response.payload, CoreMatchers.equalTo("test mode works."));
}
Also used : Response(org.doctester.testbrowser.Response) Test(org.junit.Test)

Aggregations

Response (org.doctester.testbrowser.Response)23 Test (org.junit.Test)23 Message (ninja.utils.Message)2 Article (models.Article)1 ArticleDto (models.ArticleDto)1 ArticlesDto (models.ArticlesDto)1