io.restassured.http.Cookie Java Examples

The following examples show how to use io.restassured.http.Cookie. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PlaceholderHandlerTest.java    From heat with Apache License 2.0 6 votes vote down vote up
private Response buildResponse() {
    ResponseBuilder rspBuilder = new ResponseBuilder();
    rspBuilder.setStatusCode(200);
    rspBuilder.setBody("{\"field_path\":\"field_value\",\"array1\":[{\"array_field1\":\"array_field_value1\"}]}");

    List<Header> headerList = new ArrayList();
    Header header1 = new Header("test_header", "test_value");
    Header header2 = new Header("test_header2", "test_value2");
    headerList.add(header1);
    headerList.add(header2);
    Headers headers = new Headers(headerList);
    rspBuilder.setHeaders(headers);

    List<Cookie> cookieList = new ArrayList();
    Cookie cookie1 = new Cookie.Builder("test_cookie", "test_value").build();
    Cookie cookie2 = new Cookie.Builder("test_cookie2", "test_value2").build();
    cookieList.add(cookie1);
    cookieList.add(cookie2);
    Cookies cookies = new Cookies(cookieList);
    rspBuilder.setCookies(cookies);

    return rspBuilder.build();
}
 
Example #2
Source File: ApplicationTest.java    From prebid-server-java with Apache License 2.0 5 votes vote down vote up
@Test
public void setuidShouldUpdateRubiconUidInUidCookie() throws IOException {
    // when
    final Cookie uidsCookie = given(SPEC)
            // this uids cookie value stands for {"uids":{"rubicon":"J5VLCWQP-26-CWFT","adnxs":"12345"},
            // "bday":"2017-08-15T19:47:59.523908376Z"}
            .cookie("uids", "eyJ1aWRzIjp7InJ1Ymljb24iOiJKNVZMQ1dRUC0yNi1DV0ZUIiwiYWRueHMiOiIxMjM0"
                    + "NSJ9LCJiZGF5IjoiMjAxNy0wOC0xNVQxOTo0Nzo1OS41MjM5MDgzNzZaIn0=")
            // this constant is ok to use as long as it coincides with family name
            .queryParam("bidder", RUBICON)
            .queryParam("uid", "updatedUid")
            .queryParam("gdpr", "1")
            .queryParam("gdpr_consent", "BOEFEAyOEFEAyAHABDENAI4AAAB9vABAASA")
            .when()
            .get("/setuid")
            .then()
            .extract()
            .detailedCookie("uids");

    // then
    assertThat(uidsCookie.getDomain()).isEqualTo("cookie-domain");
    assertThat(uidsCookie.getMaxAge()).isEqualTo(7776000);
    assertThat(uidsCookie.getExpiryDate().toInstant())
            .isCloseTo(Instant.now().plus(90, ChronoUnit.DAYS), within(10, ChronoUnit.SECONDS));

    final Uids uids = decodeUids(uidsCookie.getValue());
    assertThat(uids.getBday()).isEqualTo("2017-08-15T19:47:59.523908376Z"); // should be unchanged
    assertThat(uids.getUidsLegacy()).isEmpty();
    assertThat(uids.getUids().get(RUBICON).getUid()).isEqualTo("updatedUid");
    assertThat(uids.getUids().get(RUBICON).getExpires().toInstant())
            .isCloseTo(Instant.now().plus(14, ChronoUnit.DAYS), within(10, ChronoUnit.SECONDS));
    assertThat(uids.getUids().get("adnxs").getUid()).isEqualTo("12345");
    assertThat(uids.getUids().get("adnxs").getExpires().toInstant())
            .isCloseTo(Instant.now().minus(5, ChronoUnit.MINUTES), within(10, ChronoUnit.SECONDS));
}
 
Example #3
Source File: LoginIntegrationTest.java    From api-layer with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void doLoginWithValidBodyLoginRequest() {
    LoginRequest loginRequest = new LoginRequest(USERNAME, PASSWORD);

    Cookie cookie = given()
        .contentType(JSON)
        .body(loginRequest)
    .when()
        .post(String.format("%s://%s:%d%s%s", SCHEME, HOST, PORT, BASE_PATH, LOGIN_ENDPOINT))
    .then()
        .statusCode(is(SC_NO_CONTENT))
        .cookie(COOKIE_NAME, not(isEmptyString()))
        .extract().detailedCookie(COOKIE_NAME);

    assertTrue(cookie.isHttpOnly());
    assertThat(cookie.getValue(), is(notNullValue()));
    assertThat(cookie.getMaxAge(), is(-1));

    int i = cookie.getValue().lastIndexOf('.');
    String untrustedJwtString = cookie.getValue().substring(0, i + 1);
    Claims claims = Jwts.parser()
        .parseClaimsJwt(untrustedJwtString)
        .getBody();

    assertThat(claims.getId(), not(isEmptyString()));
    assertThat(claims.getSubject(), is(USERNAME));
}
 
Example #4
Source File: ApiCatalogLoginIntegrationTest.java    From api-layer with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void doLoginWithValidBodyLoginRequest() {
    LoginRequest loginRequest = new LoginRequest(USERNAME, PASSWORD);

    Cookie cookie = given()
        .contentType(JSON)
        .body(loginRequest)
    .when()
        .post(String.format("%s://%s:%d%s%s%s", SCHEME, HOST, PORT, CATALOG_PREFIX, CATALOG_SERVICE_ID,LOGIN_ENDPOINT))
    .then()
        .statusCode(is(SC_NO_CONTENT))
        .cookie(COOKIE_NAME, not(isEmptyString()))
        .extract().detailedCookie(COOKIE_NAME);

    assertTrue(cookie.isHttpOnly());
    assertThat(cookie.getValue(), is(notNullValue()));
    assertThat(cookie.getMaxAge(), is(-1));

    int i = cookie.getValue().lastIndexOf('.');
    String untrustedJwtString = cookie.getValue().substring(0, i + 1);
    Claims claims = Jwts.parser()
        .parseClaimsJwt(untrustedJwtString)
        .getBody();

    assertThat(claims.getId(), not(isEmptyString()));
    assertThat(claims.getSubject(), is(USERNAME));
}
 
Example #5
Source File: UsingWithRestAssuredTest.java    From curl-logger with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test(groups = "end-to-end-samples")
public void customizedCookie() {

  Consumer<String> curlConsumer = mock(Consumer.class);

  List<Cookie> cookies = new ArrayList<>();
  cookies.add(
      new Cookie.Builder("token", "tokenValue").setDomain("testing.com").setPath("/access")
          .build());

  //@formatter:off
  given()

      .redirects().follow(false)
      .baseUri(MOCK_BASE_URI)
      .port(MOCK_PORT)
      .cookies(new Cookies(cookies))
      .config(config()
          .httpClient(httpClientConfig()
              .reuseHttpClientInstance()
              .httpClientFactory(new MyHttpClientFactory(curlConsumer))))
      .when()
      .get("/access")
      .then()
      .statusCode(200);
  //@formatter:on

  verify(curlConsumer).accept("curl 'http://localhost:" + MOCK_PORT
      + "/access' -b 'token=tokenValue' -H 'Accept: */*' --compressed -k -v");
}
 
Example #6
Source File: GatewayBasicFuncTest.java    From knox with Apache License 2.0 4 votes vote down vote up
@Test( timeout = TestUtils.MEDIUM_TIMEOUT )
public void testBasicJsonUseCase() throws IOException {
  LOG_ENTER();
  String root = "/tmp/GatewayBasicFuncTest/testBasicJsonUseCase";
  String username = "hdfs";
  String password = "hdfs-password";
  /* Create a directory.
  curl -i -X PUT "http://<HOST>:<PORT>/<PATH>?op=MKDIRS[&permission=<OCTAL>]"

  The client receives a respond with a boolean JSON object:
  HTTP/1.1 HttpStatus.SC_OK OK
  Content-Type: application/json
  Transfer-Encoding: chunked

  {"boolean": true}
  */
  driver.getMock( "WEBHDFS" )
      .expect()
      .method( "PUT" )
      .pathInfo( "/v1" + root + "/dir" )
      .queryParam( "op", "MKDIRS" )
      .queryParam( "user.name", username )
      .respond()
      .status( HttpStatus.SC_OK )
      .content( driver.getResourceBytes( "webhdfs-success.json" ) )
      .contentType( "application/json" );
  Cookie cookie = given()
      //.log().all()
      .auth().preemptive().basic( username, password )
      .header("X-XSRF-Header", "jksdhfkhdsf")
      .queryParam( "op", "MKDIRS" )
      .then()
      //.log().all()
      .statusCode( HttpStatus.SC_OK )
      .contentType( "application/json" )
      .body( "boolean", is( true ) )
      .when().put( driver.getUrl( "WEBHDFS" ) + "/v1" + root + "/dir" )
                      .getDetailedCookie( GatewayServer.KNOXSESSIONCOOKIENAME);
  assertThat( cookie.isSecured(), is( true ) );
  assertThat( cookie.isHttpOnly(), is( true ) );
  assertThat( cookie.getPath(), is( "/gateway/cluster" ) );
  assertThat( cookie.getValue().length(), greaterThan( 16 ) );
  driver.assertComplete();
  LOG_EXIT();
}
 
Example #7
Source File: RestAssuredAdvancedLiveTest.java    From tutorials with MIT License 4 votes vote down vote up
@Test
public void whenUseCookieBuilder_thenOK(){
    Cookie myCookie = new Cookie.Builder("session_id", "1234").setSecured(true).setComment("session id cookie").build();
    given().cookie(myCookie).when().get("/users/eugenp").then().statusCode(200);
}