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 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. package org.eclipse.jgit.transport;
  44. import static org.junit.Assert.assertEquals;
  45. import static org.junit.Assert.assertTrue;
  46. import org.eclipse.jgit.lib.Config;
  47. import org.junit.Before;
  48. import org.junit.Test;
  49. /**
  50. * Tests for correctly resolving URIs when reading http.* values from a
  51. * {@link Config}.
  52. */
  53. public class HttpConfigTest {
  54. private static final String DEFAULT = "[http]\n" + "\tpostBuffer = 1\n"
  55. + "\tsslVerify= true\n" + "\tfollowRedirects = true\n"
  56. + "\tmaxRedirects = 5\n\n";
  57. private Config config;
  58. @Before
  59. public void setUp() {
  60. config = new Config();
  61. }
  62. @Test
  63. public void testDefault() throws Exception {
  64. HttpConfig http = new HttpConfig(config,
  65. new URIish("http://example.com/path/repo.git"));
  66. assertEquals(1024 * 1024, http.getPostBuffer());
  67. assertTrue(http.isSslVerify());
  68. assertEquals(HttpConfig.HttpRedirectMode.INITIAL,
  69. http.getFollowRedirects());
  70. }
  71. @Test
  72. public void testMatchSuccess() throws Exception {
  73. config.fromText(DEFAULT + "[http \"http://example.com\"]\n"
  74. + "\tpostBuffer = 1024\n");
  75. HttpConfig http = new HttpConfig(config,
  76. new URIish("http://example.com/path/repo.git"));
  77. assertEquals(1024, http.getPostBuffer());
  78. http = new HttpConfig(config,
  79. new URIish("https://example.com/path/repo.git"));
  80. assertEquals(1, http.getPostBuffer());
  81. http = new HttpConfig(config,
  82. new URIish("http://example.org/path/repo.git"));
  83. assertEquals(1, http.getPostBuffer());
  84. http = new HttpConfig(config,
  85. new URIish("http://example.com:80/path/repo.git"));
  86. assertEquals(1024, http.getPostBuffer());
  87. http = new HttpConfig(config,
  88. new URIish("http://example.com:8080/path/repo.git"));
  89. assertEquals(1, http.getPostBuffer());
  90. }
  91. @Test
  92. public void testMatchWithOnlySchemeInConfig() throws Exception {
  93. config.fromText(
  94. DEFAULT + "[http \"http://\"]\n" + "\tpostBuffer = 1024\n");
  95. HttpConfig http = new HttpConfig(config,
  96. new URIish("http://example.com/path/repo.git"));
  97. assertEquals(1, http.getPostBuffer());
  98. }
  99. @Test
  100. public void testMatchWithPrefixUriInConfig() throws Exception {
  101. config.fromText(DEFAULT + "[http \"http://example\"]\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. @Test
  108. public void testMatchCaseSensitivity() throws Exception {
  109. config.fromText(DEFAULT + "[http \"http://exAMPle.com\"]\n"
  110. + "\tpostBuffer = 1024\n");
  111. HttpConfig http = new HttpConfig(config,
  112. new URIish("http://example.com/path/repo.git"));
  113. assertEquals(1024, http.getPostBuffer());
  114. }
  115. @Test
  116. public void testMatchWithInvalidUriInConfig() throws Exception {
  117. config.fromText(
  118. DEFAULT + "[http \"///\"]\n" + "\tpostBuffer = 1024\n");
  119. HttpConfig http = new HttpConfig(config,
  120. new URIish("http://example.com/path/repo.git"));
  121. assertEquals(1, http.getPostBuffer());
  122. }
  123. @Test
  124. public void testMatchWithInvalidAndValidUriInConfig() throws Exception {
  125. config.fromText(DEFAULT + "[http \"///\"]\n" + "\tpostBuffer = 1024\n"
  126. + "[http \"http://example.com\"]\n" + "\tpostBuffer = 2048\n");
  127. HttpConfig http = new HttpConfig(config,
  128. new URIish("http://example.com/path/repo.git"));
  129. assertEquals(2048, http.getPostBuffer());
  130. }
  131. @Test
  132. public void testMatchWithHostEndingInSlash() throws Exception {
  133. config.fromText(DEFAULT + "[http \"http://example.com/\"]\n"
  134. + "\tpostBuffer = 1024\n");
  135. HttpConfig http = new HttpConfig(config,
  136. new URIish("http://example.com/path/repo.git"));
  137. assertEquals(1024, http.getPostBuffer());
  138. }
  139. @Test
  140. public void testMatchWithUser() throws Exception {
  141. config.fromText(DEFAULT + "[http \"http://example.com/path\"]\n"
  142. + "\tpostBuffer = 1024\n"
  143. + "[http \"http://example.com/path/repo\"]\n"
  144. + "\tpostBuffer = 2048\n"
  145. + "[http \"http://user@example.com/path\"]\n"
  146. + "\tpostBuffer = 4096\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://user@example.com/path/repo.git"));
  152. assertEquals(4096, http.getPostBuffer());
  153. http = new HttpConfig(config,
  154. new URIish("http://user@example.com/path/repo/foo.git"));
  155. assertEquals(2048, http.getPostBuffer());
  156. http = new HttpConfig(config,
  157. new URIish("http://user@example.com/path/foo.git"));
  158. assertEquals(4096, http.getPostBuffer());
  159. http = new HttpConfig(config,
  160. new URIish("http://example.com/path/foo.git"));
  161. assertEquals(1024, http.getPostBuffer());
  162. http = new HttpConfig(config,
  163. new URIish("http://User@example.com/path/repo/foo.git"));
  164. assertEquals(2048, http.getPostBuffer());
  165. http = new HttpConfig(config,
  166. new URIish("http://User@example.com/path/foo.git"));
  167. assertEquals(1024, http.getPostBuffer());
  168. }
  169. @Test
  170. public void testMatchLonger() throws Exception {
  171. config.fromText(DEFAULT + "[http \"http://example.com/path\"]\n"
  172. + "\tpostBuffer = 1024\n"
  173. + "[http \"http://example.com/path/repo\"]\n"
  174. + "\tpostBuffer = 2048\n");
  175. HttpConfig http = new HttpConfig(config,
  176. new URIish("http://example.com/path/repo.git"));
  177. assertEquals(1024, http.getPostBuffer());
  178. http = new HttpConfig(config,
  179. new URIish("http://example.com/foo/repo.git"));
  180. assertEquals(1, http.getPostBuffer());
  181. http = new HttpConfig(config,
  182. new URIish("https://example.com/path/repo.git"));
  183. assertEquals(1, http.getPostBuffer());
  184. http = new HttpConfig(config,
  185. new URIish("http://example.com/path/repo/.git"));
  186. assertEquals(2048, http.getPostBuffer());
  187. http = new HttpConfig(config, new URIish("http://example.com/path"));
  188. assertEquals(1024, http.getPostBuffer());
  189. http = new HttpConfig(config,
  190. new URIish("http://user@example.com/path"));
  191. assertEquals(1024, http.getPostBuffer());
  192. }
  193. }