You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

HttpConfigTest.java 8.3KB

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