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.

HttpParserTest.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright (C) 2018, 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.internal.transport.sshd.proxy;
  11. import static org.junit.Assert.assertEquals;
  12. import static org.junit.Assert.assertNull;
  13. import static org.junit.Assert.assertTrue;
  14. import java.util.Arrays;
  15. import java.util.LinkedHashMap;
  16. import java.util.List;
  17. import java.util.Map;
  18. import org.junit.Test;
  19. public class HttpParserTest {
  20. private static final String STATUS_LINE = "HTTP/1.1. 407 Authentication required";
  21. @Test
  22. public void testEmpty() throws Exception {
  23. String[] lines = { STATUS_LINE };
  24. List<AuthenticationChallenge> challenges = HttpParser
  25. .getAuthenticationHeaders(Arrays.asList(lines),
  26. "WWW-Authenticate:");
  27. assertTrue("No challenges expected", challenges.isEmpty());
  28. }
  29. @Test
  30. public void testRFC7235Example() throws Exception {
  31. // The example from RFC 7235, sec. 4.1, slightly modified ("kind"
  32. // argument with whitespace around '=')
  33. String[] lines = { STATUS_LINE,
  34. "WWW-Authenticate: Newauth realm=\"apps\", type=1 , kind = \t2 ",
  35. " \t title=\"Login to \\\"apps\\\"\", Basic realm=\"simple\"" };
  36. List<AuthenticationChallenge> challenges = HttpParser
  37. .getAuthenticationHeaders(Arrays.asList(lines),
  38. "WWW-Authenticate:");
  39. assertEquals("Unexpected number of challenges", 2, challenges.size());
  40. assertNull("No token expected", challenges.get(0).getToken());
  41. assertNull("No token expected", challenges.get(1).getToken());
  42. assertEquals("Unexpected mechanism", "Newauth",
  43. challenges.get(0).getMechanism());
  44. assertEquals("Unexpected mechanism", "Basic",
  45. challenges.get(1).getMechanism());
  46. Map<String, String> expectedArguments = new LinkedHashMap<>();
  47. expectedArguments.put("realm", "apps");
  48. expectedArguments.put("type", "1");
  49. expectedArguments.put("kind", "2");
  50. expectedArguments.put("title", "Login to \"apps\"");
  51. assertEquals("Unexpected arguments", expectedArguments,
  52. challenges.get(0).getArguments());
  53. expectedArguments.clear();
  54. expectedArguments.put("realm", "simple");
  55. assertEquals("Unexpected arguments", expectedArguments,
  56. challenges.get(1).getArguments());
  57. }
  58. @Test
  59. public void testMultipleHeaders() {
  60. String[] lines = { STATUS_LINE,
  61. "Server: Apache",
  62. "WWW-Authenticate: Newauth realm=\"apps\", type=1 , kind = \t2 ",
  63. " \t title=\"Login to \\\"apps\\\"\", Basic realm=\"simple\"",
  64. "Content-Type: text/plain",
  65. "WWW-Authenticate: Other 0123456789=== , YetAnother, ",
  66. "WWW-Authenticate: Negotiate ",
  67. "WWW-Authenticate: Negotiate a87421000492aa874209af8bc028" };
  68. List<AuthenticationChallenge> challenges = HttpParser
  69. .getAuthenticationHeaders(Arrays.asList(lines),
  70. "WWW-Authenticate:");
  71. assertEquals("Unexpected number of challenges", 6, challenges.size());
  72. assertEquals("Mismatched challenge", "Other",
  73. challenges.get(2).getMechanism());
  74. assertEquals("Token expected", "0123456789===",
  75. challenges.get(2).getToken());
  76. assertEquals("Mismatched challenge", "YetAnother",
  77. challenges.get(3).getMechanism());
  78. assertNull("No token expected", challenges.get(3).getToken());
  79. assertTrue("No arguments expected",
  80. challenges.get(3).getArguments().isEmpty());
  81. assertEquals("Mismatched challenge", "Negotiate",
  82. challenges.get(4).getMechanism());
  83. assertNull("No token expected", challenges.get(4).getToken());
  84. assertEquals("Mismatched challenge", "Negotiate",
  85. challenges.get(5).getMechanism());
  86. assertEquals("Token expected", "a87421000492aa874209af8bc028",
  87. challenges.get(5).getToken());
  88. }
  89. @Test
  90. public void testStopOnEmptyLine() {
  91. String[] lines = { STATUS_LINE, "Server: Apache",
  92. "WWW-Authenticate: Newauth realm=\"apps\", type=1 , kind = \t2 ",
  93. " \t title=\"Login to \\\"apps\\\"\", Basic realm=\"simple\"",
  94. "Content-Type: text/plain",
  95. "WWW-Authenticate: Other 0123456789===", "",
  96. // Not headers anymore; this would be the body
  97. "WWW-Authenticate: Negotiate ",
  98. "WWW-Authenticate: Negotiate a87421000492aa874209af8bc028" };
  99. List<AuthenticationChallenge> challenges = HttpParser
  100. .getAuthenticationHeaders(Arrays.asList(lines),
  101. "WWW-Authenticate:");
  102. assertEquals("Unexpected number of challenges", 3, challenges.size());
  103. }
  104. }