/** * Check that the basics are working */ @Test public void testSanity() throws Exception { WebConversation wc = new WebConversation(); WebRequest req = new GetMethodWebRequest(String.format("http://localhost:%d/test", port)); req.setParameter("cmd", QueryCommand.GET.name()); req.setParameter("param", "null"); WebResponse response = wc.getResponse(req); assertEquals("JSESSIONID", response.getNewCookieNames()[0]); }
import com.meterware.httpunit.cookies.CookieProperties; WebConversation wc = new WebConversation(); CookieProperties.setDomainMatchingStrict(false); // <- the important bit
WebConversation wc = new WebConversation(); WebResponse top = wc.getResponse( "http://www.meterware.com/Frames.html" ); // read a page with two frames WebResponse summary = wc.getFrameContents( "summary" ); // read the summary frame WebLink link = summary.getLinkWith( "Cake Recipe" ); // find the link (which targets "details" ); link.click(); // click on it WebResponse response= wc.getFrameContents( "details" ); // retrieve the details frame
/** * Creates a web response object which represents the response to the specified web request. **/ protected WebResponse newResponse( WebRequest request, FrameSelector targetFrame ) throws MalformedURLException, IOException { Properties savedProperties = (Properties) System.getProperties().clone(); try { if (_proxyHost != null) { System.setProperty( "proxyHost", _proxyHost ); System.setProperty( "proxyPort", Integer.toString( _proxyPort ) ); } URLConnection connection = openConnection( getRequestURL( request ) ); // [ 1518901 ] enable http connect and read timeouts (needs JDK 1.5) // XXX enable for 1.7 release in 2008 // comment out if you need this and have JDK 1.5 // if (_connectTimeout>=0) connection.setConnectTimeout( _connectTimeout ); // if (_readTimeout>=0) connection.setReadTimeout( _readTimeout ); if (HttpUnitOptions.isLoggingHttpHeaders()) { String urlString = request.getURLString(); System.out.println( "\nConnecting to " + request.getURL().getHost() ); System.out.println( "Sending:: " + request.getMethod() + " " + urlString ); } sendHeaders( connection, getHeaderFields( request.getURL() ) ); sendHeaders( connection, request.getHeaderDictionary() ); request.completeRequest( connection ); return new HttpWebResponse( this, targetFrame, request, connection, getExceptionsThrownOnErrorStatus() ); } finally { System.setProperties( savedProperties ); } }
private void verifyDeletedJob(WebConversation conversation, String jobId) throws IOException, SAXException { // Get the job again; it should return 404 since it's deleted already. // GET request to the jobId resource. String resourceUrl = serviceUrl + "/" + jobId; conversation.clearContents(); WebRequest getRequest = new GetMethodWebRequest(resourceUrl) ; try { conversation.getResponse(getRequest); Assert.fail("404 error is expected for request to deleted job."); } catch (HttpNotFoundException e) { // expected. } }
/** * Jumps to the specified page. */ protected WebResponse goTo(String url) throws IOException, SAXException, ProcessingException { return checkError(root.wc.getResponse(url)); }
WebConversation wc = new WebConversation(); WebResponse indexResp = wc.getResource(new GetMethodWebRequest(url)); WebLink[] links = new WebLink[1]; try { WebResponse resp = wc.getCurrentPage(); String fileName = resp.getURL().getFile(); fileName = fileName.substring(fileName.lastIndexOf("/") + 1);
WebConversation conversation = new WebConversation(); if (userid != null && password != null && realm != null) conversation.setAuthentication(realm, userid, password); String jobId = createJob(conversation, properties.parameters);
/** * Creates a web response object which represents the response to the specified web request. **/ protected WebResponse newResponse( WebRequest request, FrameSelector targetFrame ) throws MalformedURLException, IOException { Properties savedProperties = (Properties) System.getProperties().clone(); try { if (_proxyHost != null) { System.setProperty( "proxyHost", _proxyHost ); System.setProperty( "proxyPort", Integer.toString( _proxyPort ) ); } URLConnection connection = openConnection( getRequestURL( request ) ); // [ 1518901 ] enable http connect and read timeouts (needs JDK 1.5) // XXX enable for 1.7 release in 2008 // comment out if you need this and have JDK 1.5 // if (_connectTimeout>=0) connection.setConnectTimeout( _connectTimeout ); // if (_readTimeout>=0) connection.setReadTimeout( _readTimeout ); if (HttpUnitOptions.isLoggingHttpHeaders()) { String urlString = request.getURLString(); System.out.println( "\nConnecting to " + request.getURL().getHost() ); System.out.println( "Sending:: " + request.getMethod() + " " + urlString ); } sendHeaders( connection, getHeaderFields( request.getURL() ) ); sendHeaders( connection, request.getHeaderDictionary() ); request.completeRequest( connection ); return new HttpWebResponse( this, targetFrame, request, connection, getExceptionsThrownOnErrorStatus() ); } finally { System.setProperties( savedProperties ); } }
protected Object scrape() throws IOException, SAXException, ProcessingException { WebResponse r = wc.getResponse("https://www.dev.java.net/servlets/TLogin"); WebForm form = r.getFormWithID("loginform"); form.setParameter("loginID",userName); form.setParameter("password",password); checkError(form.submit(form.getSubmitButton("Login"))); // check if the login was successful if( wc.getCurrentPage().getURL().toExternalForm().indexOf("TLogin")!=-1) throw new ProcessingException("authentication failed. invalid username/password"); // create a special myself object. myself = new JNMyself(JavaNet.this,userName); users.put(userName,myself); return null; } }.run();
/** * get the Uniform Resource Locator for this request * @param request * @return the URL * @throws MalformedURLException */ private URL getRequestURL( WebRequest request ) throws MalformedURLException { DNSListener dnsListener = getClientProperties().getDnsListener(); if (dnsListener == null) return request.getURL(); String hostName = request.getURL().getHost(); String portPortion = request.getURL().getPort() == -1 ? "" : (":" + request.getURL().getPort()); setHeaderField( "Host", hostName + portPortion ); String actualHost = dnsListener.getIpAddress( hostName ); if (HttpUnitOptions.isLoggingHttpHeaders()) System.out.println( "Rerouting request to :: " + actualHost ); return new URL( request.getURL().getProtocol(), actualHost, request.getURL().getPort(), request.getURL().getFile() ); }
protected WebResponse getCurrentPage() { return root.wc.getCurrentPage(); }
/** * send the headers for the given connection based on the given Dictionary of headers * @param connection * @param headers */ private void sendHeaders( URLConnection connection, Dictionary headers ) { boolean sendReferer = getClientProperties().isSendReferer(); for (Enumeration e = headers.keys(); e.hasMoreElements();) { String key = (String) e.nextElement(); if ( sendReferer || !"referer".equalsIgnoreCase( key ) ) { connection.setRequestProperty( key, (String) headers.get( key ) ); if (HttpUnitOptions.isLoggingHttpHeaders()) { if (key.equalsIgnoreCase( "authorization" ) || key.equalsIgnoreCase( "proxy-authorization") ) { System.out.println( "Sending:: " + key + ": " + headers.get( key ) ); } else { System.out.println( "Sending:: " + key + ": " + connection.getRequestProperty( key ) ); } } } else if (HttpUnitOptions.isLoggingHttpHeaders()) { System.out.println( "Blocked sending referer:: "+ connection.getRequestProperty( key ) ); } } // for } }
protected void setUp() throws Exception { super.setUp(); wc = new WebConversation(); }
/** * Creates a web response object which represents the response to the specified web request. **/ protected WebResponse newResponse( WebRequest request, FrameSelector targetFrame ) throws MalformedURLException, IOException { Properties savedProperties = (Properties) System.getProperties().clone(); try { if (_proxyHost != null) { System.setProperty( "proxyHost", _proxyHost ); System.setProperty( "proxyPort", Integer.toString( _proxyPort ) ); } URLConnection connection = openConnection( getRequestURL( request ) ); // [ 1518901 ] enable http connect and read timeouts (needs JDK 1.5) // XXX enable for 1.7 release in 2008 // comment out if you need this and have JDK 1.5 // if (_connectTimeout>=0) connection.setConnectTimeout( _connectTimeout ); // if (_readTimeout>=0) connection.setReadTimeout( _readTimeout ); if (HttpUnitOptions.isLoggingHttpHeaders()) { String urlString = request.getURLString(); System.out.println( "\nConnecting to " + request.getURL().getHost() ); System.out.println( "Sending:: " + request.getMethod() + " " + urlString ); } sendHeaders( connection, getHeaderFields( request.getURL() ) ); sendHeaders( connection, request.getHeaderDictionary() ); request.completeRequest( connection ); return new HttpWebResponse( this, targetFrame, request, connection, getExceptionsThrownOnErrorStatus() ); } finally { System.setProperties( savedProperties ); } }
protected WebResponse head(WebConversation conversation, String resourceUrl) throws IOException, SAXException { log.debug("**************************************************"); log.debug("HTTP HEAD: " + resourceUrl); WebRequest getRequest = new HeadMethodWebRequest(resourceUrl); conversation.clearContents(); WebResponse response = conversation.getResponse(getRequest); assertNotNull("HEAD response to " + resourceUrl + " is null", response); log.debug(Util.getResponseHeaders(response)); log.debug("Response code: " + response.getResponseCode()); assertEquals("Non-200 GET response code to " + resourceUrl, 200, response.getResponseCode()); return response; }
protected WebResponse execute(WebConversation conversation, final WebRequest request) throws IOException, SAXException { // execute the operation WebResponse response = conversation.getResponse(request); assertNotNull("POST response to " + request.getURL().toString() + " is null", response); log.debug(Util.getResponseHeaders(response)); log.debug("Response code: " + response.getResponseCode()); // handle potential redirects response = getRedirectResponse(conversation, request, response); return response; }
WebConversation conversation = new WebConversation(); if (userid != null && password != null && realm != null) conversation.setAuthentication(realm, userid, password); String jobId = createJob(conversation, properties.parameters);
/** * get the Uniform Resource Locator for this request * @param request * @return the URL * @throws MalformedURLException */ private URL getRequestURL( WebRequest request ) throws MalformedURLException { DNSListener dnsListener = getClientProperties().getDnsListener(); if (dnsListener == null) return request.getURL(); String hostName = request.getURL().getHost(); String portPortion = request.getURL().getPort() == -1 ? "" : (":" + request.getURL().getPort()); setHeaderField( "Host", hostName + portPortion ); String actualHost = dnsListener.getIpAddress( hostName ); if (HttpUnitOptions.isLoggingHttpHeaders()) System.out.println( "Rerouting request to :: " + actualHost ); return new URL( request.getURL().getProtocol(), actualHost, request.getURL().getPort(), request.getURL().getFile() ); }
/** * Finds a hyper-link that has the specified text and whose * target URL starts with the given prefix. */ static WebLink findLink( WebConversation wc, String text, String urlPrefix ) throws ProcessingException, SAXException { WebLink[] links = wc.getCurrentPage().getLinks(); for( int i=0; i<links.length; i++ ) { if( links[i].asText().indexOf(text)==-1 ) continue; if( !links[i].getURLString().startsWith(urlPrefix)) continue; return links[i]; } throw new ProcessingException("no link found for '"+text+'\''); }