/*
 * Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH
 * under one or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information regarding copyright
 * ownership. Camunda licenses this file to you under the Apache License,
 * Version 2.0; you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.camunda.bpm;

import com.sun.jersey.api.client.ClientResponse;
import org.junit.Before;
import org.junit.Test;

import javax.ws.rs.core.MultivaluedMap;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

public class SessionCookieSecurityIT extends AbstractWebappIntegrationTest {

  @Override
  protected String getApplicationContextPath() {
    return "camunda/api/admin/auth/user/default/login/tasklist";
  }

  @Before
  public void preventRaceConditions() throws InterruptedException {
    // just wait some seconds before starting because of Wildfly / Cargo race conditions
    Thread.sleep(5 * 1000);
  }

  @Test(timeout=10000)
  public void shouldCheckPresenceOfProperties() {
    // given

    // when
    ClientResponse response = client.resource(APP_BASE_PATH)
        .type("application/x-www-form-urlencoded")
        .entity("username=demo&password=demo")
        .post(ClientResponse.class);

    // then
    assertEquals(200, response.getStatus());
    assertTrue(isCookieHeaderValuePresent("HttpOnly", response));
    assertFalse(isCookieHeaderValuePresent("Secure", response));

    // cleanup
    response.close();
  }

  protected boolean isCookieHeaderValuePresent(String expectedHeaderValue, ClientResponse response) {
    MultivaluedMap<String, String> headers = response.getHeaders();

    List<String> values = headers.get("Set-Cookie");
    for (String value : values) {
      if (value.startsWith("JSESSIONID=")) {
        return value.contains(expectedHeaderValue);
      }
    }

    return false;
  }

}

@@ -965,16 +965,17 @@
 uedMap;%0A
+%0A
 import j
@@ -1288,48 +1288,27 @@
 a/ap
-i/admin/auth/user/default/login/tasklist
+p/tasklist/default/
 %22;%0A 
@@ -1684,109 +1684,10 @@
    .
-type(%22application/x-www-form-urlencoded%22)%0A        .entity(%22username=demo&password=demo%22)%0A        .pos
+ge
 t(Cl

improve session cookie integration test