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

📄 webclienttest.java

📁 这是远程web服务调用的一个包,可以将JSP直接作为服务
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	    String page2 = getHostPath() + '/' + formSource;        defineResource( linkSource, "<html><head></head><body><a href=\"tellMe\">Go</a></body></html>" );	    defineResource( formSource, "<html><body><form action=\"tellMe\"><input type=submit></form></body></html>" );        defineResource( resourceName, new PseudoServlet() {            public WebResource getGetResponse() {                String referer = getHeader( "Referer" );                return new WebResource( referer == null ? "null" : referer, "text/plain" );            }        } );        WebConversation wc   = new WebConversation();        WebResponse response = wc.getResponse( page0 );        assertEquals( "Content type", "text/plain", response.getContentType() );        assertEquals( "Default Referer header", "null", response.getText().trim() );        response = wc.getResponse( page1 );        response = wc.getResponse( response.getLinks()[0].getRequest() );        assertEquals( "Link Referer header", page1, response.getText().trim() );        response = wc.getResponse( page2 );        response = wc.getResponse( response.getForms()[0].getRequest() );        assertEquals( "Form Referer header", page2, response.getText().trim() );    }    public void testRedirectedRefererHeader() throws Exception {        String linkSource = "fromLink";        String linkTarget = "anOldOne";        String resourceName = "tellMe";        defineResource( linkSource, "<html><head></head><body><a href='" + linkTarget + "'>Go</a></body></html>" );        defineResource( linkTarget, "ignored content", HttpURLConnection.HTTP_MOVED_PERM );        addResourceHeader( linkTarget, "Location: " + getHostPath() + '/' + resourceName );        defineResource( resourceName, new PseudoServlet() {            public WebResource getGetResponse() {                String referer = getHeader( "Referer" );                return new WebResource( referer == null ? "null" : referer, "text/plain" );            }        } );        WebConversation wc   = new WebConversation();        WebResponse response = wc.getResponse( getHostPath() + '/' + linkSource );        response = wc.getResponse( response.getLinks()[0].getRequest() );        assertEquals( "Link Referer header", getHostPath() + '/' + linkSource, response.getText().trim() );    }    public void testGZIPDisabled() throws Exception {        String expectedResponse = "Here is my answer";        defineResource( "Compressed.html", new CompressedPseudoServlet( expectedResponse ) );        WebConversation wc = new WebConversation();        wc.getClientProperties().setAcceptGzip( false );        WebResponse wr = wc.getResponse( getHostPath() + "/Compressed.html" );        assertNull( "Should not have received a Content-Encoding header", wr.getHeaderField( "Content-encoding" ) );        assertEquals( "Content-Type", "text/plain", wr.getContentType() );        assertEquals( "Content", expectedResponse, wr.getText().trim() );    }    public void testGZIPHandling() throws Exception {        String expectedResponse = "Here is my answer. It needs to be reasonably long to make compression smaller " +                                  "than the raw message. It should be obvious when you reach that point. " +                                  "Of course it is more than that - it needs to be long enough to cause a problem.";        defineResource( "Compressed.html", new CompressedPseudoServlet( expectedResponse ) );        WebConversation wc = new WebConversation();        WebResponse wr = wc.getResponse( getHostPath() + "/Compressed.html" );        assertEquals( "Content-Encoding header", "gzip", wr.getHeaderField( "Content-encoding" ) );        assertEquals( "Content-Type", "text/plain", wr.getContentType() );        assertEquals( "Content", expectedResponse, wr.getText().trim() );    }    private class CompressedPseudoServlet extends PseudoServlet {        private String _responseText;        private boolean _suppressLengthHeader;        public CompressedPseudoServlet( String responseText ) {            _responseText = responseText;        }        public CompressedPseudoServlet( String responseText, boolean suppressLengthHeader ) {            this( responseText );            _suppressLengthHeader = suppressLengthHeader;        }        public WebResource getGetResponse() throws IOException {            if (!userAcceptsGZIP()) {                return new WebResource( _responseText.getBytes(), "text/plain" );            } else {                WebResource result = new WebResource( getCompressedContents(), "text/plain" );                if (_suppressLengthHeader) result.suppressAutomaticLengthHeader();                result.addHeader( "Content-Encoding: gzip" );                return result;            }        }        private boolean userAcceptsGZIP() {            String header = getHeader( "Accept-Encoding" );            if (header == null) return false;            return header.toLowerCase().indexOf( "gzip" ) >= 0;        }        private byte[] getCompressedContents() throws IOException {            ByteArrayOutputStream baos = new ByteArrayOutputStream();            GZIPOutputStream gzip = new GZIPOutputStream( baos );            OutputStreamWriter out = new OutputStreamWriter( gzip );            out.write( _responseText );            out.flush();            out.close();            return baos.toByteArray();        }    }    public void testGZIPUndefinedLengthHandling() throws Exception {        String expectedResponse = "Here is my answer. It needs to be reasonably long to make compression smaller " +                                  "than the raw message. It should be obvious when you reach that point. " +                                  "Of course it is more than that - it needs to be long enough to cause a problem.";        defineResource( "Compressed.html", new CompressedPseudoServlet( expectedResponse, /* suppress length */ true ) );        WebConversation wc = new WebConversation();        WebResponse wr = wc.getResponse( getHostPath() + "/Compressed.html" );        assertEquals( "Content-Encoding header", "gzip", wr.getHeaderField( "Content-encoding" ) );        assertEquals( "Content-Type", "text/plain", wr.getContentType() );        assertEquals( "Content", expectedResponse, wr.getText().trim() );    }    public void testClientListener() throws Exception {        defineWebPage( "Target", "This is another page with <a href=Form.html target='_top'>one link</a>" );        defineWebPage( "Form", "This is a page with a simple form: " +                "<form action=submit><input name=name><input type=submit></form>" +                "<a href=Target.html target=red>a link</a>" );        defineResource( "Frames.html",                "<HTML><HEAD><TITLE>Initial</TITLE></HEAD>" +                "<FRAMESET cols='20%,80%'>" +                "    <FRAME src='Target.html' name='red'>" +                "    <FRAME src=Form.html name=blue>" +                "</FRAMESET></HTML>" );        WebConversation wc = new WebConversation();        ArrayList messageLog = new ArrayList();        wc.addClientListener( new ListenerExample( messageLog ) );        wc.getResponse( getHostPath() + "/Frames.html" );        assertEquals( "Num logged items", 6, messageLog.size() );        for (int i = 0; i < 3; i++) {            verifyRequestResponsePair( messageLog, 2 * i );        }    }    private void verifyRequestResponsePair( ArrayList messageLog, int i ) throws MalformedURLException {        assertTrue( "Logged item " + i + " is not a web request, but " + messageLog.get( i ).getClass(),                messageLog.get( i ) instanceof WebRequest );        assertTrue( "Logged item " + (i + 1) + " is not a web response, but " + messageLog.get( i + 1 ).getClass(),                messageLog.get( i + 1 ) instanceof WebResponse );        assertEquals( "Response target", ((WebRequest) messageLog.get( i )).getTarget(), ((WebResponse) messageLog.get( i + 1 )).getFrameName() );        assertEquals( "Response URL", ((WebRequest) messageLog.get( i )).getURL(), ((WebResponse) messageLog.get( i + 1 )).getURL() );    }    private static class ListenerExample implements WebClientListener {        private List _messageLog;        public ListenerExample( List messageLog ) {            _messageLog = messageLog;        }        public void requestSent( WebClient src, WebRequest req ) {            _messageLog.add( req );        }        public void responseReceived( WebClient src, WebResponse resp ) {            _messageLog.add( resp );        }    }    public void testRedirect() throws Exception {        String resourceName = "something/redirected";        String resourceValue = "the desired content";        String redirectName = "anOldOne";        defineResource( resourceName, resourceValue );        defineResource( redirectName, "ignored content", HttpURLConnection.HTTP_MOVED_PERM );        addResourceHeader( redirectName, "Location: " + getHostPath() + '/' + resourceName );        WebConversation wc = new WebConversation();        WebResponse response = wc.getResponse( getHostPath() + '/' + redirectName );        assertEquals( "requested resource", resourceValue, response.getText().trim() );        assertEquals( "content type", "text/html", response.getContentType() );        assertEquals( "status", HttpURLConnection.HTTP_OK, response.getResponseCode() );    }    public void testDuplicateHeaderRedirect() throws Exception {        String resourceName = "something/redirected";        String resourceValue = "the desired content";        String redirectName = "anOldOne";        defineResource( resourceName, resourceValue );        defineResource( redirectName, "ignored content", HttpURLConnection.HTTP_MOVED_PERM );        addResourceHeader( redirectName, "Location: " + getHostPath() + '/' + resourceName );        addResourceHeader( redirectName, "Location: " + getHostPath() + '/' + resourceName );        WebConversation wc = new WebConversation();        WebResponse response = wc.getResponse( getHostPath() + '/' + redirectName );        assertEquals( "requested resource", resourceValue, response.getText().trim() );        assertEquals( "content type", "text/html", response.getContentType() );    }    public void testDisabledRedirect() throws Exception {        String resourceName = "something/redirected";        String resourceValue = "the desired content";        String redirectName = "anOldOne";        String redirectValue = "old content";        defineResource( resourceName, resourceValue );        defineResource( redirectName, redirectValue, HttpURLConnection.HTTP_MOVED_PERM );        addResourceHeader( redirectName, "Location: " + getHostPath() + '/' + resourceName );        WebConversation wc = new WebConversation();        wc.getClientProperties().setAutoRedirect( false );        WebResponse response = wc.getResponse( getHostPath() + '/' + redirectName );        assertEquals( "requested resource", redirectValue, response.getText().trim() );        assertEquals( "content type", "text/html", response.getContentType() );    }    public void testDNSOverride() throws Exception {        WebConversation wc = new WebConversation();        wc.getClientProperties().setDnsListener( new DNSListener() {            public String getIpAddress( String hostName ) { return "127.0.0.1"; }        });        defineResource( "whereAmI", new PseudoServlet() {            public WebResource getGetResponse() {                WebResource webResource = new WebResource( "found host header: " + getHeader( "Host" ) );                webResource.addHeader( "Set-Cookie: type=short" );                return webResource;            }        } );        defineResource( "checkCookies", new PseudoServlet() {            public WebResource getGetResponse() {                return new WebResource( "found cookies: " + getHeader( "Cookie" ) );            }        } );        WebResponse wr = wc.getResponse( "http://meterware.com:" + getHostPort() + "/whereAmI" );        assertEquals( "Submitted host header", "found host header: meterware.com:" + getHostPort(), wr.getText() );        assertEquals( "Returned cookie 'type'", "short", wc.getCookieValue( "type" ) );        wr = wc.getResponse( "http://meterware.com:" + getHostPort() + "/checkCookies" );        assertEquals( "Submitted cookie header", "found cookies: type=short", wr.getText() );    }}

⌨️ 快捷键说明

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