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.lib.Config;
17  import org.junit.Before;
18  import org.junit.Test;
19  
20  /**
21   * Tests for correctly resolving URIs when reading http.* values from a
22   * {@link Config}.
23   */
24  public class HttpConfigTest {
25  
26  	private static final String DEFAULT = "[http]\n" + "\tpostBuffer = 1\n"
27  			+ "\tsslVerify= true\n" + "\tfollowRedirects = true\n"
28  			+ "\tmaxRedirects = 5\n\n";
29  
30  	private Config config;
31  
32  	@Before
33  	public void setUp() {
34  		config = new Config();
35  	}
36  
37  	@Test
38  	public void testDefault() throws Exception {
39  		HttpConfig http = new HttpConfig(config,
40  				new URIish("http://example.com/path/repo.git"));
41  		assertEquals(1024 * 1024, http.getPostBuffer());
42  		assertTrue(http.isSslVerify());
43  		assertEquals(HttpConfig.HttpRedirectMode.INITIAL,
44  				http.getFollowRedirects());
45  	}
46  
47  	@Test
48  	public void testMatchSuccess() throws Exception {
49  		config.fromText(DEFAULT + "[http \"http://example.com\"]\n"
50  				+ "\tpostBuffer = 1024\n");
51  		HttpConfig http = new HttpConfig(config,
52  				new URIish("http://example.com/path/repo.git"));
53  		assertEquals(1024, http.getPostBuffer());
54  		http = new HttpConfig(config,
55  				new URIish("https://example.com/path/repo.git"));
56  		assertEquals(1, http.getPostBuffer());
57  		http = new HttpConfig(config,
58  				new URIish("http://example.org/path/repo.git"));
59  		assertEquals(1, http.getPostBuffer());
60  		http = new HttpConfig(config,
61  				new URIish("http://example.com:80/path/repo.git"));
62  		assertEquals(1024, http.getPostBuffer());
63  		http = new HttpConfig(config,
64  				new URIish("http://example.com:8080/path/repo.git"));
65  		assertEquals(1, http.getPostBuffer());
66  	}
67  
68  	@Test
69  	public void testMatchWithOnlySchemeInConfig() throws Exception {
70  		config.fromText(
71  				DEFAULT + "[http \"http://\"]\n" + "\tpostBuffer = 1024\n");
72  		HttpConfig http = new HttpConfig(config,
73  				new URIish("http://example.com/path/repo.git"));
74  		assertEquals(1, http.getPostBuffer());
75  	}
76  
77  	@Test
78  	public void testMatchWithPrefixUriInConfig() throws Exception {
79  		config.fromText(DEFAULT + "[http \"http://example\"]\n"
80  				+ "\tpostBuffer = 1024\n");
81  		HttpConfig http = new HttpConfig(config,
82  				new URIish("http://example.com/path/repo.git"));
83  		assertEquals(1, http.getPostBuffer());
84  	}
85  
86  	@Test
87  	public void testMatchCaseSensitivity() throws Exception {
88  		config.fromText(DEFAULT + "[http \"http://exAMPle.com\"]\n"
89  				+ "\tpostBuffer = 1024\n");
90  		HttpConfig http = new HttpConfig(config,
91  				new URIish("http://example.com/path/repo.git"));
92  		assertEquals(1024, http.getPostBuffer());
93  	}
94  
95  	@Test
96  	public void testMatchWithInvalidUriInConfig() throws Exception {
97  		config.fromText(
98  				DEFAULT + "[http \"///\"]\n" + "\tpostBuffer = 1024\n");
99  		HttpConfig http = new HttpConfig(config,
100 				new URIish("http://example.com/path/repo.git"));
101 		assertEquals(1, http.getPostBuffer());
102 	}
103 
104 	@Test
105 	public void testMatchWithInvalidAndValidUriInConfig() throws Exception {
106 		config.fromText(DEFAULT + "[http \"///\"]\n" + "\tpostBuffer = 1024\n"
107 				+ "[http \"http://example.com\"]\n" + "\tpostBuffer = 2048\n");
108 		HttpConfig http = new HttpConfig(config,
109 				new URIish("http://example.com/path/repo.git"));
110 		assertEquals(2048, http.getPostBuffer());
111 	}
112 
113 	@Test
114 	public void testMatchWithHostEndingInSlash() throws Exception {
115 		config.fromText(DEFAULT + "[http \"http://example.com/\"]\n"
116 				+ "\tpostBuffer = 1024\n");
117 		HttpConfig http = new HttpConfig(config,
118 				new URIish("http://example.com/path/repo.git"));
119 		assertEquals(1024, http.getPostBuffer());
120 	}
121 
122 	@Test
123 	public void testMatchWithUser() throws Exception {
124 		config.fromText(DEFAULT + "[http \"http://example.com/path\"]\n"
125 				+ "\tpostBuffer = 1024\n"
126 				+ "[http \"http://example.com/path/repo\"]\n"
127 				+ "\tpostBuffer = 2048\n"
128 				+ "[http \"http://user@example.com/path\"]\n"
129 				+ "\tpostBuffer = 4096\n");
130 		HttpConfig http = new HttpConfig(config,
131 				new URIish("http://example.com/path/repo.git"));
132 		assertEquals(1024, http.getPostBuffer());
133 		http = new HttpConfig(config,
134 				new URIish("http://user@example.com/path/repo.git"));
135 		assertEquals(4096, http.getPostBuffer());
136 		http = new HttpConfig(config,
137 				new URIish("http://user@example.com/path/repo/foo.git"));
138 		assertEquals(2048, http.getPostBuffer());
139 		http = new HttpConfig(config,
140 				new URIish("http://user@example.com/path/foo.git"));
141 		assertEquals(4096, http.getPostBuffer());
142 		http = new HttpConfig(config,
143 				new URIish("http://example.com/path/foo.git"));
144 		assertEquals(1024, http.getPostBuffer());
145 		http = new HttpConfig(config,
146 				new URIish("http://User@example.com/path/repo/foo.git"));
147 		assertEquals(2048, http.getPostBuffer());
148 		http = new HttpConfig(config,
149 				new URIish("http://User@example.com/path/foo.git"));
150 		assertEquals(1024, http.getPostBuffer());
151 	}
152 
153 	@Test
154 	public void testMatchLonger() throws Exception {
155 		config.fromText(DEFAULT + "[http \"http://example.com/path\"]\n"
156 				+ "\tpostBuffer = 1024\n"
157 				+ "[http \"http://example.com/path/repo\"]\n"
158 				+ "\tpostBuffer = 2048\n");
159 		HttpConfig http = new HttpConfig(config,
160 				new URIish("http://example.com/path/repo.git"));
161 		assertEquals(1024, http.getPostBuffer());
162 		http = new HttpConfig(config,
163 				new URIish("http://example.com/foo/repo.git"));
164 		assertEquals(1, http.getPostBuffer());
165 		http = new HttpConfig(config,
166 				new URIish("https://example.com/path/repo.git"));
167 		assertEquals(1, http.getPostBuffer());
168 		http = new HttpConfig(config,
169 				new URIish("http://example.com/path/repo/.git"));
170 		assertEquals(2048, http.getPostBuffer());
171 		http = new HttpConfig(config, new URIish("http://example.com/path"));
172 		assertEquals(1024, http.getPostBuffer());
173 		http = new HttpConfig(config,
174 				new URIish("http://user@example.com/path"));
175 		assertEquals(1024, http.getPostBuffer());
176 	}
177 }