From 2e265fcb3d777c6a2813b8f0390c3294bd6cbd37 Mon Sep 17 00:00:00 2001 From: inoshperera Date: Sun, 29 Mar 2020 12:19:09 +0530 Subject: [PATCH] test fixes --- .../WebappAuthenticationValveTest.java | 23 +++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/test/java/org/wso2/carbon/webapp/authenticator/framework/WebappAuthenticationValveTest.java b/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/test/java/org/wso2/carbon/webapp/authenticator/framework/WebappAuthenticationValveTest.java index 69cbac221a..91dfad24ca 100644 --- a/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/test/java/org/wso2/carbon/webapp/authenticator/framework/WebappAuthenticationValveTest.java +++ b/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/test/java/org/wso2/carbon/webapp/authenticator/framework/WebappAuthenticationValveTest.java @@ -55,8 +55,9 @@ public class WebappAuthenticationValveTest { @Test(description = "This method tests the invoke method of the WebAppAuthenticationValve with the context path " + "starting with carbon") - public void testInvokeWithContextSkippedScenario1() { + public void testInvokeWithContextSkippedScenario1() throws NoSuchFieldException, IllegalAccessException { Request request = new Request(); + getCoyoteRequest(request); Context context = new StandardContext(); context.setPath("carbon"); CompositeValve compositeValve = Mockito.mock(CompositeValve.class); @@ -64,6 +65,7 @@ public class WebappAuthenticationValveTest { request.setContext(context); webappAuthenticationValve.invoke(request, null, compositeValve); request = new TestRequest("", "test"); + getCoyoteRequest(request); context = new StandardContext(); compositeValve = Mockito.mock(CompositeValve.class); Mockito.doNothing().when(compositeValve).continueInvocation(Mockito.any(), Mockito.any()); @@ -73,8 +75,9 @@ public class WebappAuthenticationValveTest { @Test(description = "This method tests the behaviour of the invoke method of WebAuthenticationValve when " + "un-secured endpoints are invoked.") - public void testInvokeUnSecuredEndpoints() { + public void testInvokeUnSecuredEndpoints() throws IllegalAccessException, NoSuchFieldException { Request request = new TestRequest("", "test"); + getCoyoteRequest(request); Context context = new StandardContext(); context.setPath("carbon1"); context.addParameter("doAuthentication", String.valueOf(true)); @@ -85,6 +88,22 @@ public class WebappAuthenticationValveTest { webappAuthenticationValve.invoke(request, null, compositeValve); } + private void getCoyoteRequest(Request request) throws + IllegalAccessException, + NoSuchFieldException { + + Field headersField = org.apache.coyote.Request.class.getDeclaredField("headers"); + headersField.setAccessible(true); + org.apache.coyote.Request coyoteRequest = new org.apache.coyote.Request(); + + MimeHeaders mimeHeaders = new MimeHeaders(); + MessageBytes bytes = mimeHeaders.addValue("content-type"); + bytes.setString("test"); + + headersField.set(coyoteRequest, mimeHeaders); + request.setCoyoteRequest(coyoteRequest); + } + @Test(description = "This method tests the behaviour of the invoke method of WebAuthenticationValve when " + "secured endpoints are invoked.") public void testInvokeSecuredEndpoints() throws NoSuchFieldException, IllegalAccessException {