View Javadoc
1   /*
2    * Copyright (C) 2017, Thomas Wolf <thomas.wolf@paranor.ch>
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available
6    * under the terms of the Eclipse Distribution License v1.0 which
7    * accompanies this distribution, is reproduced below, and is
8    * available at http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
19   * - Redistributions in binary form must reproduce the above
20   *   copyright notice, this list of conditions and the following
21   *   disclaimer in the documentation and/or other materials provided
22   *   with the distribution.
23   *
24   * - Neither the name of the Eclipse Foundation, Inc. nor the
25   *   names of its contributors may be used to endorse or promote
26   *   products derived from this software without specific prior
27   *   written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42   */
43  
44  package org.eclipse.jgit.transport;
45  
46  import static org.junit.Assert.assertEquals;
47  import static org.junit.Assert.assertTrue;
48  
49  import org.eclipse.jgit.lib.Config;
50  import org.junit.Before;
51  import org.junit.Test;
52  
53  /**
54   * Tests for correctly resolving URIs when reading http.* values from a
55   * {@link Config}.
56   */
57  public class HttpConfigTest {
58  
59  	private static final String DEFAULT = "[http]\n" + "\tpostBuffer = 1\n"
60  			+ "\tsslVerify= true\n" + "\tfollowRedirects = true\n"
61  			+ "\tmaxRedirects = 5\n\n";
62  
63  	private Config config;
64  
65  	@Before
66  	public void setUp() {
67  		config = new Config();
68  	}
69  
70  	@Test
71  	public void testDefault() throws Exception {
72  		HttpConfig http = new HttpConfig(config,
73  				new URIish("http://example.com/path/repo.git"));
74  		assertEquals(1024 * 1024, http.getPostBuffer());
75  		assertTrue(http.isSslVerify());
76  		assertEquals(HttpConfig.HttpRedirectMode.INITIAL,
77  				http.getFollowRedirects());
78  	}
79  
80  	@Test
81  	public void testMatchSuccess() throws Exception {
82  		config.fromText(DEFAULT + "[http \"http://example.com\"]\n"
83  				+ "\tpostBuffer = 1024\n");
84  		HttpConfig http = new HttpConfig(config,
85  				new URIish("http://example.com/path/repo.git"));
86  		assertEquals(1024, http.getPostBuffer());
87  		http = new HttpConfig(config,
88  				new URIish("https://example.com/path/repo.git"));
89  		assertEquals(1, http.getPostBuffer());
90  		http = new HttpConfig(config,
91  				new URIish("http://example.org/path/repo.git"));
92  		assertEquals(1, http.getPostBuffer());
93  		http = new HttpConfig(config,
94  				new URIish("http://example.com:80/path/repo.git"));
95  		assertEquals(1024, http.getPostBuffer());
96  		http = new HttpConfig(config,
97  				new URIish("http://example.com:8080/path/repo.git"));
98  		assertEquals(1, http.getPostBuffer());
99  	}
100 
101 	@Test
102 	public void testMatchWithOnlySchemeInConfig() throws Exception {
103 		config.fromText(
104 				DEFAULT + "[http \"http://\"]\n" + "\tpostBuffer = 1024\n");
105 		HttpConfig http = new HttpConfig(config,
106 				new URIish("http://example.com/path/repo.git"));
107 		assertEquals(1, http.getPostBuffer());
108 	}
109 
110 	@Test
111 	public void testMatchWithPrefixUriInConfig() throws Exception {
112 		config.fromText(DEFAULT + "[http \"http://example\"]\n"
113 				+ "\tpostBuffer = 1024\n");
114 		HttpConfig http = new HttpConfig(config,
115 				new URIish("http://example.com/path/repo.git"));
116 		assertEquals(1, http.getPostBuffer());
117 	}
118 
119 	@Test
120 	public void testMatchCaseSensitivity() throws Exception {
121 		config.fromText(DEFAULT + "[http \"http://exAMPle.com\"]\n"
122 				+ "\tpostBuffer = 1024\n");
123 		HttpConfig http = new HttpConfig(config,
124 				new URIish("http://example.com/path/repo.git"));
125 		assertEquals(1024, http.getPostBuffer());
126 	}
127 
128 	@Test
129 	public void testMatchWithInvalidUriInConfig() throws Exception {
130 		config.fromText(
131 				DEFAULT + "[http \"///\"]\n" + "\tpostBuffer = 1024\n");
132 		HttpConfig http = new HttpConfig(config,
133 				new URIish("http://example.com/path/repo.git"));
134 		assertEquals(1, http.getPostBuffer());
135 	}
136 
137 	@Test
138 	public void testMatchWithInvalidAndValidUriInConfig() throws Exception {
139 		config.fromText(DEFAULT + "[http \"///\"]\n" + "\tpostBuffer = 1024\n"
140 				+ "[http \"http://example.com\"]\n" + "\tpostBuffer = 2048\n");
141 		HttpConfig http = new HttpConfig(config,
142 				new URIish("http://example.com/path/repo.git"));
143 		assertEquals(2048, http.getPostBuffer());
144 	}
145 
146 	@Test
147 	public void testMatchWithHostEndingInSlash() throws Exception {
148 		config.fromText(DEFAULT + "[http \"http://example.com/\"]\n"
149 				+ "\tpostBuffer = 1024\n");
150 		HttpConfig http = new HttpConfig(config,
151 				new URIish("http://example.com/path/repo.git"));
152 		assertEquals(1024, http.getPostBuffer());
153 	}
154 
155 	@Test
156 	public void testMatchWithUser() throws Exception {
157 		config.fromText(DEFAULT + "[http \"http://example.com/path\"]\n"
158 				+ "\tpostBuffer = 1024\n"
159 				+ "[http \"http://example.com/path/repo\"]\n"
160 				+ "\tpostBuffer = 2048\n"
161 				+ "[http \"http://user@example.com/path\"]\n"
162 				+ "\tpostBuffer = 4096\n");
163 		HttpConfig http = new HttpConfig(config,
164 				new URIish("http://example.com/path/repo.git"));
165 		assertEquals(1024, http.getPostBuffer());
166 		http = new HttpConfig(config,
167 				new URIish("http://user@example.com/path/repo.git"));
168 		assertEquals(4096, http.getPostBuffer());
169 		http = new HttpConfig(config,
170 				new URIish("http://user@example.com/path/repo/foo.git"));
171 		assertEquals(2048, http.getPostBuffer());
172 		http = new HttpConfig(config,
173 				new URIish("http://user@example.com/path/foo.git"));
174 		assertEquals(4096, http.getPostBuffer());
175 		http = new HttpConfig(config,
176 				new URIish("http://example.com/path/foo.git"));
177 		assertEquals(1024, http.getPostBuffer());
178 		http = new HttpConfig(config,
179 				new URIish("http://User@example.com/path/repo/foo.git"));
180 		assertEquals(2048, http.getPostBuffer());
181 		http = new HttpConfig(config,
182 				new URIish("http://User@example.com/path/foo.git"));
183 		assertEquals(1024, http.getPostBuffer());
184 	}
185 
186 	@Test
187 	public void testMatchLonger() throws Exception {
188 		config.fromText(DEFAULT + "[http \"http://example.com/path\"]\n"
189 				+ "\tpostBuffer = 1024\n"
190 				+ "[http \"http://example.com/path/repo\"]\n"
191 				+ "\tpostBuffer = 2048\n");
192 		HttpConfig http = new HttpConfig(config,
193 				new URIish("http://example.com/path/repo.git"));
194 		assertEquals(1024, http.getPostBuffer());
195 		http = new HttpConfig(config,
196 				new URIish("http://example.com/foo/repo.git"));
197 		assertEquals(1, http.getPostBuffer());
198 		http = new HttpConfig(config,
199 				new URIish("https://example.com/path/repo.git"));
200 		assertEquals(1, http.getPostBuffer());
201 		http = new HttpConfig(config,
202 				new URIish("http://example.com/path/repo/.git"));
203 		assertEquals(2048, http.getPostBuffer());
204 		http = new HttpConfig(config, new URIish("http://example.com/path"));
205 		assertEquals(1024, http.getPostBuffer());
206 		http = new HttpConfig(config,
207 				new URIish("http://user@example.com/path"));
208 		assertEquals(1024, http.getPostBuffer());
209 	}
210 }