Search in sources :

Example 6 with Cookies

use of io.restassured.http.Cookies 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 7 with Cookies

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

the class ResponsePrinter method print.

/**
 * Prints the response to the print stream
 *
 * @return A string of representing the response
 */
public static String print(ResponseOptions responseOptions, ResponseBody responseBody, PrintStream stream, LogDetail logDetail, boolean shouldPrettyPrint) {
    final StringBuilder builder = new StringBuilder();
    if (logDetail == ALL || logDetail == STATUS) {
        builder.append(responseOptions.statusLine());
    }
    if (logDetail == ALL || logDetail == HEADERS) {
        final Headers headers = responseOptions.headers();
        if (headers.exist()) {
            appendNewLineIfAll(logDetail, builder).append(toString(headers));
        }
    } else if (logDetail == COOKIES) {
        final Cookies cookies = responseOptions.detailedCookies();
        if (cookies.exist()) {
            appendNewLineIfAll(logDetail, builder).append(cookies.toString());
        }
    }
    if (logDetail == ALL || logDetail == BODY) {
        String responseBodyToAppend;
        if (shouldPrettyPrint) {
            responseBodyToAppend = new Prettifier().getPrettifiedBodyIfPossible(responseOptions, responseBody);
        } else {
            responseBodyToAppend = responseBody.asString();
        }
        if (logDetail == ALL && !isBlank(responseBodyToAppend)) {
            builder.append("\n\n");
        }
        builder.append(responseBodyToAppend);
    }
    String response = builder.toString();
    stream.println(response);
    return response;
}
Also used : Headers(io.restassured.http.Headers) Cookies(io.restassured.http.Cookies) Prettifier(io.restassured.internal.support.Prettifier)

Example 8 with Cookies

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

the class CookieITest method removesDoubleQuotesFromCookieWithExpiresDate.

@Test
public void removesDoubleQuotesFromCookieWithExpiresDate() throws Exception {
    Cookies cookies = when().get("/cookieWithDoubleQuoteExpiresDate").then().extract().detailedCookies();
    assertThat(cookies.asList(), hasSize(1));
    Cookie cookie = cookies.get("name");
    assertThat(cookie.getExpiryDate(), equalTo(DateUtils.parseDate("Sat, 02 May 2009 23:38:25 GMT")));
}
Also used : Cookie(io.restassured.http.Cookie) RestAssuredMatchers.detailedCookie(io.restassured.matcher.RestAssuredMatchers.detailedCookie) Cookies(io.restassured.http.Cookies) Test(org.junit.Test)

Example 9 with Cookies

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

the class CookieITest method detailedCookiesAllowsToGetMultiValues.

@Test
public void detailedCookiesAllowsToGetMultiValues() throws Exception {
    final Cookies cookies = get("/multiCookie").detailedCookies();
    assertThat(cookies.getValues("cookie1"), hasItems("cookieValue1", "cookieValue2"));
}
Also used : Cookies(io.restassured.http.Cookies) Test(org.junit.Test)

Example 10 with Cookies

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

the class CookieITest method setsExpiresPropertyToNullWhenCookieHasInvalidExpiresDate.

@Test
public void setsExpiresPropertyToNullWhenCookieHasInvalidExpiresDate() throws Exception {
    Cookies cookies = when().get("/cookieWithInvalidExpiresDate").then().extract().detailedCookies();
    assertThat(cookies.asList(), hasSize(1));
    Cookie cookie = cookies.get("name");
    assertThat(cookie.getExpiryDate(), nullValue());
}
Also used : Cookie(io.restassured.http.Cookie) RestAssuredMatchers.detailedCookie(io.restassured.matcher.RestAssuredMatchers.detailedCookie) Cookies(io.restassured.http.Cookies) Test(org.junit.Test)

Aggregations

Cookies (io.restassured.http.Cookies)10 Test (org.junit.Test)8 Cookie (io.restassured.http.Cookie)5 RestAssuredMatchers.detailedCookie (io.restassured.matcher.RestAssuredMatchers.detailedCookie)3 RequestSpecBuilder (io.restassured.builder.RequestSpecBuilder)1 ResponseSpecBuilder (io.restassured.builder.ResponseSpecBuilder)1 Headers (io.restassured.http.Headers)1 Prettifier (io.restassured.internal.support.Prettifier)1 RequestSpecification (io.restassured.specification.RequestSpecification)1 ResponseSpecification (io.restassured.specification.ResponseSpecification)1 SimpleDateFormat (java.text.SimpleDateFormat)1