@@ -66,4 +66,90 @@ public void testDoFilterRequestedWithFalse() {
6666 }
6767 }
6868
69+ @ Test
70+ // A web administrator loads plugin UI modules with <script>/import(), which cannot set
71+ // headers, so a GET for a web plugin asset passes without X-Requested-With.
72+ public void testDoFilterAllowsWebPluginAssetGetWithoutHeader () throws Exception {
73+ mirthProperties .setProperty ("server.api.require-requested-with" , "true" );
74+ RequestedWithFilter testFilter = new RequestedWithFilter (mirthProperties );
75+
76+ HttpServletRequest mockReq = mockRequest ("GET" , "/webplugins/demo/web/plugin.js" , null );
77+ HttpServletResponse mockResp = Mockito .mock (HttpServletResponse .class );
78+ FilterChain mockFilterChain = Mockito .mock (FilterChain .class );
79+
80+ testFilter .doFilter (mockReq , mockResp , mockFilterChain );
81+ verify (mockResp , never ()).sendError (Mockito .anyInt (), Mockito .anyString ());
82+ verify (mockFilterChain ).doFilter (mockReq , mockResp );
83+ }
84+
85+ @ Test
86+ // Without path info (a configured context path), the full request URI still identifies a
87+ // web plugin asset.
88+ public void testDoFilterAllowsWebPluginAssetGetByRequestUriWithoutHeader () throws Exception {
89+ mirthProperties .setProperty ("server.api.require-requested-with" , "true" );
90+ RequestedWithFilter testFilter = new RequestedWithFilter (mirthProperties );
91+
92+ HttpServletRequest mockReq = mockRequest ("GET" , null , "/oie/api/webplugins/demo/plugin.json" );
93+ HttpServletResponse mockResp = Mockito .mock (HttpServletResponse .class );
94+ FilterChain mockFilterChain = Mockito .mock (FilterChain .class );
95+
96+ testFilter .doFilter (mockReq , mockResp , mockFilterChain );
97+ verify (mockResp , never ()).sendError (Mockito .anyInt (), Mockito .anyString ());
98+ verify (mockFilterChain ).doFilter (mockReq , mockResp );
99+ }
100+
101+ @ Test
102+ // Only GET is exempt: any other method on the same path still needs the header.
103+ public void testDoFilterRejectsWebPluginPostWithoutHeader () throws Exception {
104+ mirthProperties .setProperty ("server.api.require-requested-with" , "true" );
105+ RequestedWithFilter testFilter = new RequestedWithFilter (mirthProperties );
106+
107+ HttpServletRequest mockReq = mockRequest ("POST" , "/webplugins/demo/web/plugin.js" , null );
108+ HttpServletResponse mockResp = Mockito .mock (HttpServletResponse .class );
109+ FilterChain mockFilterChain = Mockito .mock (FilterChain .class );
110+
111+ testFilter .doFilter (mockReq , mockResp , mockFilterChain );
112+ verify (mockResp ).sendError (HttpServletResponse .SC_BAD_REQUEST , "All requests must have 'X-Requested-With' header" );
113+ verify (mockFilterChain , never ()).doFilter (mockReq , mockResp );
114+ }
115+
116+ @ Test
117+ // The discovery list (/webplugins without a trailing segment) is not an asset and keeps
118+ // the header requirement.
119+ public void testDoFilterRejectsWebPluginListWithoutHeader () throws Exception {
120+ mirthProperties .setProperty ("server.api.require-requested-with" , "true" );
121+ RequestedWithFilter testFilter = new RequestedWithFilter (mirthProperties );
122+
123+ HttpServletRequest mockReq = mockRequest ("GET" , "/webplugins" , null );
124+ HttpServletResponse mockResp = Mockito .mock (HttpServletResponse .class );
125+ FilterChain mockFilterChain = Mockito .mock (FilterChain .class );
126+
127+ testFilter .doFilter (mockReq , mockResp , mockFilterChain );
128+ verify (mockResp ).sendError (HttpServletResponse .SC_BAD_REQUEST , "All requests must have 'X-Requested-With' header" );
129+ verify (mockFilterChain , never ()).doFilter (mockReq , mockResp );
130+ }
131+
132+ @ Test
133+ // Every other GET keeps the header requirement.
134+ public void testDoFilterRejectsOtherGetWithoutHeader () throws Exception {
135+ mirthProperties .setProperty ("server.api.require-requested-with" , "true" );
136+ RequestedWithFilter testFilter = new RequestedWithFilter (mirthProperties );
137+
138+ HttpServletRequest mockReq = mockRequest ("GET" , "/channels" , null );
139+ HttpServletResponse mockResp = Mockito .mock (HttpServletResponse .class );
140+ FilterChain mockFilterChain = Mockito .mock (FilterChain .class );
141+
142+ testFilter .doFilter (mockReq , mockResp , mockFilterChain );
143+ verify (mockResp ).sendError (HttpServletResponse .SC_BAD_REQUEST , "All requests must have 'X-Requested-With' header" );
144+ verify (mockFilterChain , never ()).doFilter (mockReq , mockResp );
145+ }
146+
147+ private static HttpServletRequest mockRequest (String method , String pathInfo , String requestUri ) {
148+ HttpServletRequest mockReq = Mockito .mock (HttpServletRequest .class );
149+ Mockito .when (mockReq .getMethod ()).thenReturn (method );
150+ Mockito .when (mockReq .getPathInfo ()).thenReturn (pathInfo );
151+ Mockito .when (mockReq .getRequestURI ()).thenReturn (requestUri );
152+ return mockReq ;
153+ }
154+
69155}
0 commit comments