/*
 * Copyright (c) 2008-2016 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * 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.cometd.client;

import java.util.Collections;

import org.cometd.client.transport.LongPollingTransport;
import org.eclipse.jetty.client.api.Request;
import org.eclipse.jetty.security.ConstraintMapping;
import org.eclipse.jetty.security.ConstraintSecurityHandler;
import org.eclipse.jetty.security.HashLoginService;
import org.eclipse.jetty.security.authentication.BasicAuthenticator;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.util.B64Code;
import org.eclipse.jetty.util.security.Constraint;
import org.eclipse.jetty.util.security.Credential;
import org.junit.Assert;
import org.junit.Test;

public class HandshakeWithAuthenticationTest extends ClientServerTest {
    @Test
    public void testHandshakeWithBasicAuthenticationHeaders() throws Exception {
        startServer(null);
        int port = connector.getLocalPort();
        server.stop();

        final String userName = "cometd";
        final String password = "cometd";
        String[] roles = {"admin"};
        HashLoginService loginService = new HashLoginService("CometD-Realm");
        loginService.putUser(userName, Credential.getCredential(password), roles);
        server.addBean(loginService);

        Handler handler = server.getHandler();
        ConstraintSecurityHandler security = new ConstraintSecurityHandler();
        server.setHandler(security);
        security.setHandler(handler);

        Constraint constraint = new Constraint();
        constraint.setAuthenticate(true);
        constraint.setRoles(roles);

        ConstraintMapping mapping = new ConstraintMapping();
        mapping.setPathSpec(cometdServletPath + "/*");
        mapping.setConstraint(constraint);

        security.setConstraintMappings(Collections.singletonList(mapping));
        security.setAuthenticator(new BasicAuthenticator());
        security.setLoginService(loginService);

        connector.setPort(port);
        server.start();

        LongPollingTransport transport = new LongPollingTransport(null, httpClient) {
            @Override
            protected void customize(Request request) {
                String authorization = userName + ":" + password;
                authorization = B64Code.encode(authorization);
                request.header("Authorization", "Basic " + authorization);
            }
        };
        BayeuxClient client = new BayeuxClient(cometdURL, transport);

        client.handshake();

        Assert.assertTrue(client.waitFor(5000, BayeuxClient.State.CONNECTED));

        // Allow long poll to establish
        Thread.sleep(1000);

        disconnectBayeuxClient(client);
    }
}

@@ -645,16 +645,144 @@
 lient;%0A%0A
+import java.io.BufferedWriter;%0Aimport java.nio.charset.StandardCharsets;%0Aimport java.nio.file.Files;%0Aimport java.nio.file.Path;%0A
 import j
@@ -1276,59 +1276,8 @@
 nt;%0A
-import org.eclipse.jetty.util.security.Credential;%0A
 impo
@@ -1682,29 +1682,298 @@
 ring
-%5B%5D
  role
-s
  = 
-%7B
 %22admin%22
-%7D
 ;
+%0A        Path authProps = Files.createTempFile(%22cometd-autentication%22, %22.properties%22);%0A        try (BufferedWriter writer = Files.newBufferedWriter(authProps, StandardCharsets.UTF_8)) %7B%0A            writer.write(String.format(%22%25s:%25s,%25s%22, userName, password, role));%0A        %7D
 %0A   
@@ -2072,67 +2072,38 @@
 ice.
-putUser(userName, Credential.getCredential(password), roles
+setConfig(authProps.toString()
 );%0A 
@@ -2465,13 +2465,26 @@
 les(
+new String%5B%5D%7B
 role
-s
+%7D
 );%0A%0A

Make test compile with Jetty 9.4.0.