⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 webxmltest.java

📁 这是远程web服务调用的一个包,可以将JSP直接作为服务
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            assertEquals( "Method", "Basic", e.getAuthenticationScheme() );        }        try {            wc.setAuthorization( "You", "peon" );            wc.getResponse( "http://localhost/SimpleServlet" );            fail( "Permitted wrong user to access" );        } catch (HttpException e) {            assertEquals( "Response code", 403, e.getResponseCode() );        }        wc.setAuthorization( "Me", "supervisor,agent" );        wc.getResponse( "http://localhost/SimpleServlet" );        InvocationContext ic = wc.newInvocation( "http://localhost/SimpleServlet" );        assertEquals( "Authenticated user", "Me", ic.getRequest().getRemoteUser() );        assertTrue( "User assigned to 'bogus' role", !ic.getRequest().isUserInRole( "bogus" ) );        assertTrue( "User not assigned to 'supervisor' role", ic.getRequest().isUserInRole( "supervisor" ) );    }    public void testFormAuthentication() throws Exception {        HttpUnitOptions.setLoggingHttpHeaders( true );        WebXMLString wxs = new WebXMLString();        wxs.addServlet( "/Logon", SimpleLogonServlet.class );        wxs.addServlet( "/Error", SimpleErrorServlet.class );        wxs.addServlet( "/Example/SimpleServlet", SimpleGetServlet.class );        wxs.requireFormAuthentication( "Sample Realm", "/Logon", "/Error" );        wxs.addSecureURL( "SecureArea1", "/Example/SimpleServlet" );        wxs.addAuthorizedRole( "SecureArea1", "supervisor" );        File webXml = createWebXml( wxs );        ServletRunner sr = new ServletRunner( webXml, "/samples" );        ServletUnitClient wc = sr.newClient();        WebResponse response = wc.getResponse( "http://localhost/samples/Example/SimpleServlet" );        WebForm form = response.getFormWithID( "login" );        assertNotNull( "did not find login form", form );        WebRequest request = form.getRequest();        request.setParameter( "j_username", "Me" );        request.setParameter( "j_password", "supervisor" );        response = wc.getResponse( request );        assertNotNull( "No response received after authentication", response );        assertEquals( "content type", "text/html", response.getContentType() );        assertEquals( "requested resource", SimpleGetServlet.RESPONSE_TEXT, response.getText() );        InvocationContext ic = wc.newInvocation( "http://localhost/samples/Example/SimpleServlet" );        assertEquals( "Authenticated user", "Me", ic.getRequest().getRemoteUser() );        assertTrue( "User assigned to 'bogus' role", !ic.getRequest().isUserInRole( "bogus" ) );        assertTrue( "User not assigned to 'supervisor' role", ic.getRequest().isUserInRole( "supervisor" ) );    }    public void testGetContextPath() throws Exception {        WebXMLString wxs = new WebXMLString();        wxs.addServlet( "/SimpleServlet", SimpleGetServlet.class );        ServletRunner sr = new ServletRunner( wxs.asInputStream(), "/mount" );        ServletUnitClient wc = sr.newClient();        InvocationContext ic = wc.newInvocation( "http://localhost/mount/SimpleServlet" );        assertEquals("/mount", ic.getRequest().getContextPath());        sr = new ServletRunner( wxs.asInputStream() );        wc = sr.newClient();        ic = wc.newInvocation( "http://localhost/SimpleServlet" );        assertEquals("", ic.getRequest().getContextPath());    }    public void testMountContextPath() throws Exception {        WebXMLString wxs = new WebXMLString();        wxs.addServlet( "/SimpleServlet", SimpleGetServlet.class );        ServletRunner sr = new ServletRunner( wxs.asInputStream(), "/mount" );        ServletUnitClient wc = sr.newClient();        InvocationContext ic = wc.newInvocation( "http://localhost/mount/SimpleServlet" );        assertTrue(ic.getServlet() instanceof SimpleGetServlet);        assertEquals("/mount/SimpleServlet", ic.getRequest().getRequestURI());        try {            ic = wc.newInvocation( "http://localhost/SimpleServlet" );            ic.getServlet();            fail("Attempt to access url outside of the webapp context path should have thrown a 404");        } catch (com.meterware.httpunit.HttpNotFoundException e) {}    }    public void testServletMapping() throws Exception {        WebXMLString wxs = new WebXMLString();        wxs.addServlet( "/foo/bar/*", Servlet1.class );        wxs.addServlet( "/baz/*", Servlet2.class );        wxs.addServlet( "/catalog", Servlet3.class );        wxs.addServlet( "*.bop", Servlet4.class );        wxs.addServlet( "/",     Servlet5.class );        ServletRunner sr = new ServletRunner( wxs.asInputStream() );        ServletUnitClient wc = sr.newClient();        checkMapping( wc, "http://localhost/foo/bar/index.html",  Servlet1.class, "/foo/bar",             "/index.html" );        checkMapping( wc, "http://localhost/foo/bar/index.bop",   Servlet1.class, "/foo/bar",             "/index.bop" );        checkMapping( wc, "http://localhost/baz",                 Servlet2.class, "/baz",                 null );        checkMapping( wc, "http://localhost/baz/index.html",      Servlet2.class, "/baz",                 "/index.html" );        checkMapping( wc, "http://localhost/catalog",             Servlet3.class, "/catalog",             null );        checkMapping( wc, "http://localhost/catalog/racecar.bop", Servlet4.class, "/catalog/racecar.bop", null );        checkMapping( wc, "http://localhost/index.bop",           Servlet4.class, "/index.bop",           null );        checkMapping( wc, "http://localhost/something/else",      Servlet5.class, "/something/else",      null );    }    private void checkMapping( ServletUnitClient wc, final String url, final Class servletClass, final String expectedPath, final String expectedInfo ) throws IOException, ServletException {        InvocationContext ic = wc.newInvocation( url );        assertTrue( "selected servlet is " + ic.getServlet() + " rather than " + servletClass, servletClass.isInstance( ic.getServlet() ) );        assertEquals( "ServletPath for " + url, expectedPath, ic.getRequest().getServletPath() );        assertEquals( "ServletInfo for " + url, expectedInfo, ic.getRequest().getPathInfo() );    }    /**     * Verifies that only those servlets designated will pre-load when the application is initialized.     * SimpleGetServlet and each of its subclasses adds its classname to the 'initialized' context attribute.     */    public void testLoadOnStartup() throws Exception {        WebXMLString wxs = new WebXMLString();        wxs.addServlet( "servlet1", "one", Servlet1.class );        wxs.setLoadOnStartup( "servlet1" );        wxs.addServlet( "servlet2", "two", Servlet2.class );        wxs.addServlet( "servlet3", "three", Servlet3.class );        ServletRunner sr = new ServletRunner( toInputStream( wxs.asText() ) );        ServletUnitClient wc = sr.newClient();        InvocationContext ic = wc.newInvocation( "http://localhost/three" );        assertEquals( "Initialized servlets", "Servlet1,Servlet3", ic.getServlet().getServletConfig().getServletContext().getAttribute( "initialized" ) );    }    /**     * Verifies that servlets pre-load in the order specified.     * SimpleGetServlet and each of its subclasses adds its classname to the 'initialized' context attribute.     */    public void testLoadOrder() throws Exception {        WebXMLString wxs = new WebXMLString();        wxs.addServlet( "servlet1", "one", Servlet1.class );        wxs.setLoadOnStartup( "servlet1", 2 );        wxs.addServlet( "servlet2", "two", Servlet2.class );        wxs.setLoadOnStartup( "servlet2", 3 );        wxs.addServlet( "servlet3", "three", Servlet3.class );        wxs.setLoadOnStartup( "servlet3", 1 );        ServletRunner sr = new ServletRunner( toInputStream( wxs.asText() ) );        ServletUnitClient wc = sr.newClient();        InvocationContext ic = wc.newInvocation( "http://localhost/two" );        assertEquals( "Initialized servlets", "Servlet3,Servlet1,Servlet2", ic.getServlet().getServletConfig().getServletContext().getAttribute( "initialized" ) );    }//===============================================================================================================//===============================================================================================================    static class SimpleLogonServlet extends HttpServlet {        static String RESPONSE_TEXT = "<html><body>\r\n" +                                      "<form id='login' action='j_security_check' method='POST'>\r\n" +                                      "  <input name='j_username' />\r\n" +                                      "  <input type='password' name='j_password' />\r\n" +                                      "</form></body></html>";        protected void doGet( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException {            resp.setContentType( "text/html" );            PrintWriter pw = resp.getWriter();            pw.print( RESPONSE_TEXT );            pw.close();        }    }//===============================================================================================================    static class SimpleErrorServlet extends HttpServlet {        static String RESPONSE_TEXT = "<html><body>Sorry could not login</body></html>";        protected void doGet( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException {            resp.setContentType( "text/html" );            PrintWriter pw = resp.getWriter();            pw.print( RESPONSE_TEXT );            pw.close();        }    }//===============================================================================================================    static class SimpleGetServlet extends HttpServlet {        static String RESPONSE_TEXT = "the desired content\r\n";        public void init() throws ServletException {            ServletConfig servletConfig = getServletConfig();            String initialized = (String) servletConfig.getServletContext().getAttribute( "initialized" );            if (initialized == null) initialized = getLocalName();            else initialized = initialized + "," + getLocalName();            servletConfig.getServletContext().setAttribute( "initialized", initialized );        }        private String getLocalName() {            String className = getClass().getName();            int dollarIndex = className.indexOf( '$' );            if (dollarIndex < 0) return className;            return className.substring( dollarIndex+1 );        }        protected void doGet( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException {            resp.setContentType( "text/html" );            PrintWriter pw = resp.getWriter();            pw.print( RESPONSE_TEXT );            pw.close();        }    }    static class Servlet1 extends SimpleGetServlet {}    static class Servlet2 extends SimpleGetServlet {}    static class Servlet3 extends SimpleGetServlet {}    static class Servlet4 extends SimpleGetServlet {}    static class Servlet5 extends SimpleGetServlet {}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -