summaryrefslogtreecommitdiffstats
blob: ff85ec8f9a992e6d1d11a7cfbbc9463166993e88 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*******************************************************************************
 * Copyright (c) 2004, 2010 Tasktop Technologies and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Tasktop Technologies - initial API and implementation
 *******************************************************************************/

package org.eclipse.mylyn.commons.repositories.http.tests;

import static org.junit.Assert.assertEquals;

import java.net.InetSocketAddress;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.eclipse.mylyn.commons.repositories.http.core.HttpUtil;
import org.eclipse.mylyn.commons.sdk.util.TestProxy;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/**
 * @author Steffen Pingel
 */
public class HttpUtilTest {

	private TestProxy testProxy;

	private InetSocketAddress proxyAddress;

	private DefaultHttpClient client;

	private ThreadSafeClientConnManager connectionManager;

	public HttpUtilTest() {
	}

	@Before
	public void setUp() throws Exception {
		testProxy = new TestProxy();
		int proxyPort = testProxy.startAndWait();
		assert proxyPort > 0;
		proxyAddress = new InetSocketAddress("localhost", proxyPort);

		connectionManager = new ThreadSafeClientConnManager();
		client = new DefaultHttpClient() {
			@Override
			protected ClientConnectionManager createClientConnectionManager() {
				return connectionManager;
			}
		};
	}

	@After
	public void tearDown() throws Exception {
		testProxy.stop();
	}

	@Test
	public void testGetRequestPoolConnections() throws Exception {
		HttpRequestBase request = new HttpGet("http://" + proxyAddress.toString());

		HttpUtil.configureClient(client, null);
		assertEquals(0, connectionManager.getConnectionsInPool());

		HttpResponse response = HttpUtil.execute(client, null, request, null);
		assertEquals(HttpStatus.SC_NOT_FOUND, response.getStatusLine().getStatusCode());
		assertEquals(1, connectionManager.getConnectionsInPool());
	}

}