Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

HttpConfigTest.java 9.1KB

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