Search in sources :

Example 6 with Cookie

use of io.restassured.http.Cookie in project rest-assured by rest-assured.

the class RequestPrinter method addCookies.

private static void addCookies(FilterableRequestSpecification requestSpec, StringBuilder builder) {
    builder.append("Cookies:");
    final Cookies cookies = requestSpec.getCookies();
    if (!cookies.exist()) {
        appendTwoTabs(builder).append(NONE).append(NEW_LINE);
    }
    int i = 0;
    for (Cookie cookie : cookies) {
        if (i++ == 0) {
            appendTwoTabs(builder);
        } else {
            appendFourTabs(builder);
        }
        builder.append(cookie).append(NEW_LINE);
    }
}
Also used : Cookie(io.restassured.http.Cookie) Cookies(io.restassured.http.Cookies)

Example 7 with Cookie

use of io.restassured.http.Cookie in project rest-assured by rest-assured.

the class CookieFilterTest method addCookiesToMatchingUrlRequest.

@Test
public void addCookiesToMatchingUrlRequest() {
    cookieFilter.filter(reqOriginDomain, response, testFilterContext);
    cookieFilter.filter(reqOriginDomain, response, testFilterContext);
    assertThat(reqOriginDomain.getCookies().size(), Matchers.is(1));
    for (Cookie cookie : reqOriginDomain.getCookies()) {
        assertThat(cookie.getName(), Matchers.is("cookieName"));
        assertThat(cookie.getValue(), Matchers.is("cookieValue"));
    }
}
Also used : Cookie(io.restassured.http.Cookie) Test(org.junit.Test)

Example 8 with Cookie

use of io.restassured.http.Cookie in project rest-assured by rest-assured.

the class MockMvcRequestSpecificationMergingTest method cookies_are_merged_when_defined_in_specification.

@Test
public void cookies_are_merged_when_defined_in_specification() {
    // Given
    Cookie otherCookie = new Cookie.Builder("cookie1", "value1").build();
    Cookie thisCookie = new Cookie.Builder("cookie2", "value2").build();
    MockMvcRequestSpecification specToMerge = new MockMvcRequestSpecBuilder().addCookie(otherCookie).build();
    // When
    MockMvcRequestSpecification spec = RestAssuredMockMvc.given().cookie(thisCookie).spec(specToMerge);
    // Then
    Assertions.assertThat(implOf(spec).getCookies()).containsOnly(thisCookie, otherCookie);
}
Also used : Cookie(io.restassured.http.Cookie) MockMvcRequestSpecification(io.restassured.module.mockmvc.specification.MockMvcRequestSpecification) MockMvcRequestSpecBuilder(io.restassured.module.mockmvc.specification.MockMvcRequestSpecBuilder) Test(org.junit.Test)

Example 9 with Cookie

use of io.restassured.http.Cookie in project rest-assured by rest-assured.

the class CookieMatcherTest method testSetVersion.

@Test
public void testSetVersion() throws ParseException {
    String[] cookies = new String[] { "DEVICE_ID=123; Domain=.test.com; Expires=Thu, 12-Oct-2023 09:34:31 GMT; Path=/; Secure; HttpOnly;", "SPRING_SECURITY_REMEMBER_ME_COOKIE=12345;Version=0;Domain=.test.com;Path=/;Max-Age=1209600", "COOKIE_WITH_ZERO_MAX_AGE=1234;Version=0;Domain=.test.com;Path=/;Max-Age=0", "COOKIE_WITH_NEGATIVE_MAX_AGE=123456;Version=0;Domain=.test.com;Path=/;Max-Age=-1" };
    Cookies result = CookieMatcher.getCookies(cookies);
    assertEquals(4, result.size());
    Cookie sprintCookie = result.get("SPRING_SECURITY_REMEMBER_ME_COOKIE");
    assertEquals(0, sprintCookie.getVersion());
    assertEquals("12345", sprintCookie.getValue());
    assertEquals(".test.com", sprintCookie.getDomain());
    assertEquals("/", sprintCookie.getPath());
    assertEquals(1209600, sprintCookie.getMaxAge());
    assertEquals(false, sprintCookie.isSecured());
    assertEquals(false, sprintCookie.isHttpOnly());
    Cookie cookieWithZeroMaxAge = result.get("COOKIE_WITH_ZERO_MAX_AGE");
    assertEquals(0, cookieWithZeroMaxAge.getVersion());
    assertEquals("1234", cookieWithZeroMaxAge.getValue());
    assertEquals(".test.com", cookieWithZeroMaxAge.getDomain());
    assertEquals("/", cookieWithZeroMaxAge.getPath());
    assertEquals(0, cookieWithZeroMaxAge.getMaxAge());
    assertEquals(false, cookieWithZeroMaxAge.isSecured());
    assertEquals(false, cookieWithZeroMaxAge.isHttpOnly());
    Cookie cookieWithNegativeMaxAge = result.get("COOKIE_WITH_NEGATIVE_MAX_AGE");
    assertEquals(0, cookieWithNegativeMaxAge.getVersion());
    assertEquals("123456", cookieWithNegativeMaxAge.getValue());
    assertEquals(".test.com", cookieWithNegativeMaxAge.getDomain());
    assertEquals("/", cookieWithNegativeMaxAge.getPath());
    assertEquals(-1, cookieWithNegativeMaxAge.getMaxAge());
    assertEquals(false, cookieWithNegativeMaxAge.isSecured());
    assertEquals(false, cookieWithNegativeMaxAge.isHttpOnly());
    Cookie deviceCookie = result.get("DEVICE_ID");
    assertEquals(-1, deviceCookie.getVersion());
    assertEquals("123", deviceCookie.getValue());
    assertEquals(".test.com", deviceCookie.getDomain());
    assertEquals("/", deviceCookie.getPath());
    assertEquals(new SimpleDateFormat("EEE, d-MMM-yyyy HH:mm:ss Z", Locale.ENGLISH).parse("Thu, 12-Oct-2023 09:34:31 GMT"), deviceCookie.getExpiryDate());
    assertEquals(true, deviceCookie.isSecured());
    assertEquals(true, deviceCookie.isHttpOnly());
}
Also used : Cookie(io.restassured.http.Cookie) Cookies(io.restassured.http.Cookies) SimpleDateFormat(java.text.SimpleDateFormat) Test(org.junit.Test)

Example 10 with Cookie

use of io.restassured.http.Cookie in project rest-assured by rest-assured.

the class CookieFilterTest method preserveCookies.

@Test
public void preserveCookies() {
    reqOriginDomain.cookie("cookieName", "cookieInitialValue");
    cookieFilter.filter((FilterableRequestSpecification) given(), response, testFilterContext);
    cookieFilter.filter(reqOriginDomain, response, testFilterContext);
    assertThat(reqOriginDomain.getCookies().size(), Matchers.is(1));
    for (Cookie cookie : reqOriginDomain.getCookies()) {
        assertThat(cookie.getName(), Matchers.is("cookieName"));
        assertThat(cookie.getValue(), Matchers.is("cookieInitialValue"));
    }
}
Also used : Cookie(io.restassured.http.Cookie) Test(org.junit.Test)

Aggregations

Cookie (io.restassured.http.Cookie)14 Test (org.junit.Test)13 RestAssuredMatchers.detailedCookie (io.restassured.matcher.RestAssuredMatchers.detailedCookie)6 Cookies (io.restassured.http.Cookies)5 MockMvcRequestSpecBuilder (io.restassured.module.mockmvc.specification.MockMvcRequestSpecBuilder)2 MockMvcRequestSpecification (io.restassured.module.mockmvc.specification.MockMvcRequestSpecification)2 Response (io.restassured.response.Response)2 SimpleDateFormat (java.text.SimpleDateFormat)1 VerifyTest (org.apache.knox.test.category.VerifyTest)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 IsEmptyString.isEmptyString (org.hamcrest.text.IsEmptyString.isEmptyString)1