package org.wso2.carbon.esb.nhttp.transport.test;

import java.io.File;

import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import org.wso2.esb.integration.common.utils.ESBIntegrationTest;
import org.wso2.esb.integration.common.utils.servers.WireMonitorServer;

/**
 * This test case is written to track the issue reported in
 * https://wso2.org/jira/browse/ESBJAVA-5121.This checks whether the order of
 * the auth headers is correct before sending the request to the endpoint
 */

public class CheckAuthHeaderOrderTestCase extends ESBIntegrationTest {

	public WireMonitorServer wireServer;

	@BeforeClass(alwaysRun = true)
	public void setEnvironment() throws Exception {
		init();
		wireServer = new WireMonitorServer(8991);
		wireServer.start();
		loadESBConfigurationFromClasspath(File.separator + "artifacts" + File.separator + "ESB" + File.separator
				+ "nhttp" + File.separator + "transport" + File.separator + "auth-headers.xml");
	}

	@Test(groups = { "wso2.esb" }, description = "Sending a Message Via REST to check the order of the auth headers")
	public void testAuthHeaderOrder() throws Exception {

		DefaultHttpClient httpClient = new DefaultHttpClient();

		HttpPost httpPost = new HttpPost(getApiInvocationURL("stockquote") + "/order/");
		httpPost.addHeader("WWW-Authenticate", "NTLM");
		httpPost.addHeader("WWW-Authenticate", "Basic realm=\"BasicSecurityFilterProvider\"");
		httpPost.addHeader("WWW-Authenticate", "ANTLM3");

		httpClient.execute(httpPost);

		String response = wireServer.getCapturedMessage();

		Assert.assertNotNull(response);
		Assert.assertTrue(response.contains(
				"WWW-Authenticate: NTLM\r\nWWW-Authenticate: Basic realm=\"BasicSecurityFilterProvider\"\r\nWWW-Authenticate: ANTLM3"));

	}

	@AfterClass(alwaysRun = true)
	public void destroy() throws Exception {
		super.cleanup();
	}

}

@@ -1,8 +1,646 @@
+/*%0A * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.%0A *%0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A *%0A * http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A */%0A%0A
 package 
@@ -708,76 +708,8 @@
 e;%0A%0A
-import org.apache.http.Header;%0Aimport org.apache.http.HttpResponse;%0A
 impo

Add License header