View Javadoc
1   /*
2    * Copyright (C) 2017, 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    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
9    */
10  
11  package org.eclipse.jgit.transport;
12  
13  import static org.junit.Assert.assertEquals;
14  import static org.junit.Assert.assertTrue;
15  
16  import org.eclipse.jgit.junit.MockSystemReader;
17  import org.eclipse.jgit.lib.Config;
18  import org.eclipse.jgit.util.SystemReader;
19  import org.junit.Before;
20  import org.junit.Test;
21  
22  /**
23   * Tests for correctly resolving URIs when reading http.* values from a
24   * {@link Config}.
25   */
26  public class HttpConfigTest {
27  
28  	private static final String DEFAULT = "[http]\n" + "\tpostBuffer = 1\n"
29  			+ "\tsslVerify= true\n" + "\tfollowRedirects = true\n"
30  			+ "\textraHeader = x: y\n" + "\tuserAgent = Test/0.1\n"
31  			+ "\tmaxRedirects = 5\n\n";
32  
33  	private Config config;
34  
35  	@Before
36  	public void setUp() {
37  		config = new Config();
38  	}
39  
40  	@Test
41  	public void testDefault() throws Exception {
42  		HttpConfig http = new HttpConfig(config,
43  				new URIish("http://example.com/path/repo.git"));
44  		assertEquals(1024 * 1024, http.getPostBuffer());
45  		assertTrue(http.isSslVerify());
46  		assertEquals(HttpConfig.HttpRedirectMode.INITIAL,
47  				http.getFollowRedirects());
48  	}
49  
50  	@Test
51  	public void testMatchSuccess() throws Exception {
52  		config.fromText(DEFAULT + "[http \"http://example.com\"]\n"
53  				+ "\tpostBuffer = 1024\n");
54  		HttpConfig http = new HttpConfig(config,
55  				new URIish("http://example.com/path/repo.git"));
56  		assertEquals(1024, http.getPostBuffer());
57  		http = new HttpConfig(config,
58  				new URIish("https://example.com/path/repo.git"));
59  		assertEquals(1, http.getPostBuffer());
60  		http = new HttpConfig(config,
61  				new URIish("http://example.org/path/repo.git"));
62  		assertEquals(1, http.getPostBuffer());
63  		http = new HttpConfig(config,
64  				new URIish("http://example.com:80/path/repo.git"));
65  		assertEquals(1024, http.getPostBuffer());
66  		http = new HttpConfig(config,
67  				new URIish("http://example.com:8080/path/repo.git"));
68  		assertEquals(1, http.getPostBuffer());
69  	}
70  
71  	@Test
72  	public void testMatchWithOnlySchemeInConfig() throws Exception {
73  		config.fromText(
74  				DEFAULT + "[http \"http://\"]\n" + "\tpostBuffer = 1024\n");
75  		HttpConfig http = new HttpConfig(config,
76  				new URIish("http://example.com/path/repo.git"));
77  		assertEquals(1, http.getPostBuffer());
78  	}
79  
80  	@Test
81  	public void testMatchWithPrefixUriInConfig() throws Exception {
82  		config.fromText(DEFAULT + "[http \"http://example\"]\n"
83  				+ "\tpostBuffer = 1024\n");
84  		HttpConfig http = new HttpConfig(config,
85  				new URIish("http://example.com/path/repo.git"));
86  		assertEquals(1, http.getPostBuffer());
87  	}
88  
89  	@Test
90  	public void testMatchCaseSensitivity() throws Exception {
91  		config.fromText(DEFAULT + "[http \"http://exAMPle.com\"]\n"
92  				+ "\tpostBuffer = 1024\n");
93  		HttpConfig http = new HttpConfig(config,
94  				new URIish("http://example.com/path/repo.git"));
95  		assertEquals(1024, http.getPostBuffer());
96  	}
97  
98  	@Test
99  	public void testMatchWithInvalidUriInConfig() throws Exception {
100 		config.fromText(
101 				DEFAULT + "[http \"///#expectedWarning\"]\n"
102 						+ "\tpostBuffer = 1024\n");
103 		HttpConfig http = new HttpConfig(config,
104 				new URIish("http://example.com/path/repo.git"));
105 		assertEquals(1, http.getPostBuffer());
106 	}
107 
108 	@Test
109 	public void testMatchWithInvalidAndValidUriInConfig() throws Exception {
110 		config.fromText(DEFAULT + "[http \"///#expectedWarning\"]\n"
111 				+ "\tpostBuffer = 1024\n"
112 				+ "[http \"http://example.com\"]\n" + "\tpostBuffer = 2048\n");
113 		HttpConfig http = new HttpConfig(config,
114 				new URIish("http://example.com/path/repo.git"));
115 		assertEquals(2048, http.getPostBuffer());
116 	}
117 
118 	@Test
119 	public void testMatchWithHostEndingInSlash() throws Exception {
120 		config.fromText(DEFAULT + "[http \"http://example.com/\"]\n"
121 				+ "\tpostBuffer = 1024\n");
122 		HttpConfig http = new HttpConfig(config,
123 				new URIish("http://example.com/path/repo.git"));
124 		assertEquals(1024, http.getPostBuffer());
125 	}
126 
127 	@Test
128 	public void testMatchWithUser() throws Exception {
129 		config.fromText(DEFAULT + "[http \"http://example.com/path\"]\n"
130 				+ "\tpostBuffer = 1024\n"
131 				+ "[http \"http://example.com/path/repo\"]\n"
132 				+ "\tpostBuffer = 2048\n"
133 				+ "[http \"http://user@example.com/path\"]\n"
134 				+ "\tpostBuffer = 4096\n");
135 		HttpConfig http = new HttpConfig(config,
136 				new URIish("http://example.com/path/repo.git"));
137 		assertEquals(1024, http.getPostBuffer());
138 		http = new HttpConfig(config,
139 				new URIish("http://user@example.com/path/repo.git"));
140 		assertEquals(4096, http.getPostBuffer());
141 		http = new HttpConfig(config,
142 				new URIish("http://user@example.com/path/repo/foo.git"));
143 		assertEquals(2048, http.getPostBuffer());
144 		http = new HttpConfig(config,
145 				new URIish("http://user@example.com/path/foo.git"));
146 		assertEquals(4096, http.getPostBuffer());
147 		http = new HttpConfig(config,
148 				new URIish("http://example.com/path/foo.git"));
149 		assertEquals(1024, http.getPostBuffer());
150 		http = new HttpConfig(config,
151 				new URIish("http://User@example.com/path/repo/foo.git"));
152 		assertEquals(2048, http.getPostBuffer());
153 		http = new HttpConfig(config,
154 				new URIish("http://User@example.com/path/foo.git"));
155 		assertEquals(1024, http.getPostBuffer());
156 	}
157 
158 	@Test
159 	public void testMatchLonger() throws Exception {
160 		config.fromText(DEFAULT + "[http \"http://example.com/path\"]\n"
161 				+ "\tpostBuffer = 1024\n"
162 				+ "[http \"http://example.com/path/repo\"]\n"
163 				+ "\tpostBuffer = 2048\n");
164 		HttpConfig http = new HttpConfig(config,
165 				new URIish("http://example.com/path/repo.git"));
166 		assertEquals(1024, http.getPostBuffer());
167 		http = new HttpConfig(config,
168 				new URIish("http://example.com/foo/repo.git"));
169 		assertEquals(1, http.getPostBuffer());
170 		http = new HttpConfig(config,
171 				new URIish("https://example.com/path/repo.git"));
172 		assertEquals(1, http.getPostBuffer());
173 		http = new HttpConfig(config,
174 				new URIish("http://example.com/path/repo/.git"));
175 		assertEquals(2048, http.getPostBuffer());
176 		http = new HttpConfig(config, new URIish("http://example.com/path"));
177 		assertEquals(1024, http.getPostBuffer());
178 		http = new HttpConfig(config,
179 				new URIish("http://user@example.com/path"));
180 		assertEquals(1024, http.getPostBuffer());
181 	}
182 
183 	@Test
184 	public void testExtraHeaders() throws Exception {
185 		config.fromText(DEFAULT + "[http \"http://example.com\"]\n"
186 				+ "\textraHeader=foo: bar\n");
187 		HttpConfig http = new HttpConfig(config,
188 				new URIish("http://example.com/"));
189 		assertEquals(1, http.getExtraHeaders().size());
190 		assertEquals("foo: bar", http.getExtraHeaders().get(0));
191 	}
192 
193 	@Test
194 	public void testExtraHeadersMultiple() throws Exception {
195 		config.fromText(DEFAULT + "[http \"http://example.com\"]\n"
196 				+ "\textraHeader=foo: bar\n" //
197 				+ "\textraHeader=bar: foo\n");
198 		HttpConfig http = new HttpConfig(config,
199 				new URIish("http://example.com/"));
200 		assertEquals(2, http.getExtraHeaders().size());
201 		assertEquals("foo: bar", http.getExtraHeaders().get(0));
202 		assertEquals("bar: foo", http.getExtraHeaders().get(1));
203 	}
204 
205 	@Test
206 	public void testExtraHeadersReset() throws Exception {
207 		config.fromText(DEFAULT + "[http \"http://example.com\"]\n"
208 				+ "\textraHeader=foo: bar\n" //
209 				+ "\textraHeader=bar: foo\n" //
210 				+ "\textraHeader=\n");
211 		HttpConfig http = new HttpConfig(config,
212 				new URIish("http://example.com/"));
213 		assertTrue(http.getExtraHeaders().isEmpty());
214 	}
215 
216 	@Test
217 	public void testExtraHeadersResetAndMore() throws Exception {
218 		config.fromText(DEFAULT + "[http \"http://example.com\"]\n"
219 				+ "\textraHeader=foo: bar\n" //
220 				+ "\textraHeader=bar: foo\n" //
221 				+ "\textraHeader=\n" //
222 				+ "\textraHeader=baz: something\n");
223 		HttpConfig http = new HttpConfig(config,
224 				new URIish("http://example.com/"));
225 		assertEquals(1, http.getExtraHeaders().size());
226 		assertEquals("baz: something", http.getExtraHeaders().get(0));
227 	}
228 
229 	@Test
230 	public void testUserAgent() throws Exception {
231 		config.fromText(DEFAULT + "[http \"http://example.com\"]\n"
232 				+ "\tuserAgent=DummyAgent/4.0\n");
233 		HttpConfig http = new HttpConfig(config,
234 				new URIish("http://example.com/"));
235 		assertEquals("DummyAgent/4.0", http.getUserAgent());
236 	}
237 
238 	@Test
239 	public void testUserAgentEnvOverride() throws Exception {
240 		String mockAgent = "jgit-test/5.10.0";
241 		SystemReader originalReader = SystemReader.getInstance();
242 		SystemReader.setInstance(new MockSystemReader() {
243 
244 			@Override
245 			public String getenv(String variable) {
246 				if ("GIT_HTTP_USER_AGENT".equals(variable)) {
247 					return mockAgent;
248 				}
249 				return super.getenv(variable);
250 			}
251 		});
252 		try {
253 			config.fromText(DEFAULT + "[http \"http://example.com\"]\n"
254 					+ "\tuserAgent=DummyAgent/4.0\n");
255 			HttpConfig http = new HttpConfig(config,
256 					new URIish("http://example.com/"));
257 			assertEquals(mockAgent, http.getUserAgent());
258 		} finally {
259 			SystemReader.setInstance(originalReader);
260 		}
261 	}
262 
263 	@Test
264 	public void testUserAgentNonAscii() throws Exception {
265 		config.fromText(DEFAULT + "[http \"http://example.com\"]\n"
266 				+ "\tuserAgent= d ümmy Agent -5.10\n");
267 		HttpConfig http = new HttpConfig(config,
268 				new URIish("http://example.com/"));
269 		assertEquals("d.mmy.Agent.-5.10", http.getUserAgent());
270 	}
271 }