View Javadoc
1   /*
2    * MIT License
3    *
4    * Copyright (c) 2010-2024 The Waffle Project Contributors: https://github.com/Waffle/waffle/graphs/contributors
5    *
6    * Permission is hereby granted, free of charge, to any person obtaining a copy
7    * of this software and associated documentation files (the "Software"), to deal
8    * in the Software without restriction, including without limitation the rights
9    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10   * copies of the Software, and to permit persons to whom the Software is
11   * furnished to do so, subject to the following conditions:
12   *
13   * The above copyright notice and this permission notice shall be included in all
14   * copies or substantial portions of the Software.
15   *
16   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22   * SOFTWARE.
23   */
24  package waffle.spring;
25  
26  import java.io.IOException;
27  
28  import javax.servlet.ServletException;
29  
30  import org.junit.jupiter.api.AfterEach;
31  import org.junit.jupiter.api.Assertions;
32  import org.junit.jupiter.api.BeforeEach;
33  import org.junit.jupiter.api.Test;
34  import org.springframework.context.ApplicationContext;
35  import org.springframework.context.support.AbstractApplicationContext;
36  import org.springframework.context.support.ClassPathXmlApplicationContext;
37  
38  import waffle.mock.http.SimpleHttpRequest;
39  import waffle.mock.http.SimpleHttpResponse;
40  
41  /**
42   * The Class NegotiateSecurityFilterEntryPointTest.
43   */
44  class NegotiateSecurityFilterEntryPointTest {
45  
46      /** The entry point. */
47      private NegotiateSecurityFilterEntryPoint entryPoint;
48  
49      /** The ctx. */
50      private ApplicationContext ctx;
51  
52      /**
53       * Sets the up.
54       */
55      @BeforeEach
56      void setUp() {
57          final String[] configFiles = new String[] { "springTestFilterBeans.xml" };
58          this.ctx = new ClassPathXmlApplicationContext(configFiles);
59          this.entryPoint = (NegotiateSecurityFilterEntryPoint) this.ctx.getBean("negotiateSecurityFilterEntryPoint");
60      }
61  
62      /**
63       * Shut down.
64       */
65      @AfterEach
66      void shutDown() {
67          ((AbstractApplicationContext) this.ctx).close();
68      }
69  
70      /**
71       * Test challenge get.
72       *
73       * @throws IOException
74       *             Signals that an I/O exception has occurred.
75       * @throws ServletException
76       *             the servlet exception
77       */
78      @Test
79      void testChallengeGET() throws IOException, ServletException {
80          final SimpleHttpRequest request = new SimpleHttpRequest();
81          request.setMethod("GET");
82          final SimpleHttpResponse response = new SimpleHttpResponse();
83          this.entryPoint.commence(request, response, null);
84          final String[] wwwAuthenticates = response.getHeaderValues("WWW-Authenticate");
85          Assertions.assertEquals(3, wwwAuthenticates.length);
86          Assertions.assertEquals("NTLM", wwwAuthenticates[0]);
87          Assertions.assertEquals("Negotiate", wwwAuthenticates[1]);
88          Assertions.assertEquals("Basic realm=\"TestRealm\"", wwwAuthenticates[2]);
89          Assertions.assertEquals(2, response.getHeaderNamesSize());
90          Assertions.assertEquals("keep-alive", response.getHeader("Connection"));
91          Assertions.assertEquals(401, response.getStatus());
92      }
93  
94      /**
95       * Test get set provider.
96       *
97       * @throws IOException
98       *             Signals that an I/O exception has occurred.
99       * @throws ServletException
100      *             the servlet exception
101      */
102     @Test
103     void testGetSetProvider() throws IOException, ServletException {
104         Assertions.assertNotNull(this.entryPoint.getProvider());
105         this.entryPoint.setProvider(null);
106         final SimpleHttpRequest request = new SimpleHttpRequest();
107         request.setMethod("GET");
108         final SimpleHttpResponse response = new SimpleHttpResponse();
109         final Throwable exception = Assertions.assertThrows(ServletException.class, () -> {
110             this.entryPoint.commence(request, response, null);
111         });
112         Assertions.assertEquals("Missing NegotiateEntryPoint.Provider", exception.getMessage());
113     }
114 }