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

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