View Javadoc
1   /*
2    * Copyright (c) 2019, Thomas Wolf <thomas.wolf@paranor.ch> and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * http://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
9    */
10  package org.eclipse.jgit.util;
11  
12  import static org.junit.Assert.assertEquals;
13  import static org.junit.Assert.assertNotNull;
14  
15  import java.io.IOException;
16  import java.net.InetSocketAddress;
17  import java.net.Proxy;
18  import java.net.ProxySelector;
19  import java.net.SocketAddress;
20  import java.net.URI;
21  import java.net.URL;
22  import java.util.Collections;
23  import java.util.List;
24  
25  import org.junit.Test;
26  
27  public class HttpSupportTest {
28  
29  	private static class TestProxySelector extends ProxySelector {
30  
31  		private static final Proxy DUMMY = new Proxy(Proxy.Type.HTTP,
32  				InetSocketAddress.createUnresolved("localhost", 0));
33  
34  		@Override
35  		public List<Proxy> select(URI uri) {
36  			if ("http".equals(uri.getScheme())
37  					&& "somehost".equals(uri.getHost())) {
38  				return Collections.singletonList(DUMMY);
39  			}
40  			return Collections.singletonList(Proxy.NO_PROXY);
41  		}
42  
43  		@Override
44  		public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
45  			// Empty
46  		}
47  	}
48  
49  	@Test
50  	public void testMalformedUri() throws Exception {
51  		// Valid URL, but backslash is not allowed in a URI in the userinfo part
52  		// per RFC 3986: https://tools.ietf.org/html/rfc3986#section-3.2.1 .
53  		// Test that conversion to URI to call the ProxySelector does not throw
54  		// an exception.
55  		Proxy proxy = HttpSupport.proxyFor(new TestProxySelector(), new URL(
56  				"http://infor\\c.jones@somehost/somewhere/someproject.git"));
57  		assertNotNull(proxy);
58  		assertEquals(Proxy.Type.HTTP, proxy.type());
59  	}
60  
61  	@Test
62  	public void testCorrectUri() throws Exception {
63  		// Backslash escaped as %5C is correct.
64  		Proxy proxy = HttpSupport.proxyFor(new TestProxySelector(), new URL(
65  				"http://infor%5Cc.jones@somehost/somewhere/someproject.git"));
66  		assertNotNull(proxy);
67  		assertEquals(Proxy.Type.HTTP, proxy.type());
68  	}
69  }