1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
43
44 class NegotiateSecurityFilterEntryPointTest {
45
46
47 private NegotiateSecurityFilterEntryPoint entryPoint;
48
49
50 private ApplicationContext ctx;
51
52
53
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
64
65 @AfterEach
66 void shutDown() {
67 ((AbstractApplicationContext) this.ctx).close();
68 }
69
70
71
72
73
74
75
76
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
96
97
98
99
100
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 }