Re: related javascript files not fetched

"Wolfgang Fahl" <[email protected]> Tue, 18 Aug 2009 18:38:36 +0200
Newsgroups gmane.comp.web.httpunit.devel
Organization BITPlan GmbH
Message-ID <[email protected]>
Hello Dan,

thank you for your JUnit test example
you wrote:
> My prior complaint about it not loading javascript files
> seems to have been transient, a SVN update has fixed it.
> 
> However, the second part of my question was more
> about a 404 on javascript files, if you look at
> the following test there is no exception thrown
> even though xyz.js does not exist.
> 
>  public void testBadJavascriptFile() throws Exception {
>    defineResource("OnCommand.html",
>      "<html><head>" +
>      "<script language='JavaScript' src='xyz.js'></script></head>" +
>      "<body>Hello</body></html>" );
>    HttpUnitOptions.setExceptionsThrownOnErrorStatus(true);
>    HttpUnitOptions.setExceptionsThrownOnScriptError(true);
>    HttpUnitOptions.clearScriptErrorMessages();
>    WebConversation wc = new WebConversation();
>    wc.setExceptionsThrownOnErrorStatus(true);
>    try {
>      WebResponse response = wc.getResponse( getHostPath() + "/OnCommand.html" );
>      fail("there should have been an exception");
>    } catch (Throwable th) {
>        th.printStackTrace();
>      // not sure what to expect here, currently no expection is produced
>    }
>  }
> 
> 
> Also HttpUnitOptions.getScriptErrorMessages() does not show
> any errors, but a WebClientListener will show a 404 from the server.
I've checked out 
<html>
  <head>
	 <script language='JavaScript' src='xyz.js'></script>
  </head>
  <body>Hello</body>
</html>



on my Apache webserver and it will happily serve the 
page without any complaint.

The Apache error log will contain:
[Tue Aug 18 12:14:56 2009] [error] [client 2.0.0.33] 
File does not exist: F:/h/Source/PHP/xyz.js, referer: 
http://luna/test1.html

So basically we don't want to get an error on the 
JavaScript loading as was already pointed out in bug 
report 1055450 by Robert Wadura
http://sourceforge.net/tracker/index.php?func=detail&aid=1055450&group_id=6550&atid=106550

But we still would like to know if such a condition was 
met. Subversion revision 1036 has the change (and 1037 a 
modified release notes with acknowledgments to your 
support):
http://httpunit.svn.sourceforge.net/viewvc/httpunit?view=rev&revision=1036


This is now the testcase for the new behaviour:
   /**
    * test Detection of Javascript files that can not be found
    * behaviour pointed out by Dan Lipofsky
    * @throws Exception
    */
   public void testBadJavascriptFile() throws Exception {
	   // define xyz.js to create a 404 error
	   // we don't do this - it should be a default behaviour of the Pseudo Server!
	   // defineResource( "xyz.js", "File does not exist: xyz.js", 404);
	   defineResource("OnCommand.html",
	     "<html><head>" +
	     "<script language='JavaScript' src='xyz.js'></script></head>" +
	     "<body>Hello</body></html>" );
	   boolean originalState =
	  		HttpUnitOptions.getExceptionsThrownOnErrorStatus();
	   boolean originalScriptState=
		   HttpUnitOptions.getExceptionsThrownOnScriptError();   
	   boolean oldDebug=	HttpUnitUtils.setEXCEPTION_DEBUG(false);

	   // make sure exceptions are thrown
	   HttpUnitOptions.setExceptionsThrownOnErrorStatus(false);
	   for (int i=0;i<2;i++) {
		   boolean throwScriptException=i==0;
		   HttpUnitOptions.setExceptionsThrownOnScriptError(throwScriptException);
		   HttpUnitOptions.clearScriptErrorMessages();
		   WebConversation wc = new WebConversation();
		   try {
		     WebResponse response = wc.getResponse( getHostPath() + "/OnCommand.html" );
		     // WebResponse response = wc.getResponse( getHostPath() + "/xyz.js" );
		     // assertEquals( 404, response.getResponseCode() );
		     if (throwScriptException) {
		    	 fail("there should have been an exception");
		     } else {
			   String[] errMsgs = HttpUnitOptions.getScriptErrorMessages();
			   assertTrue("There should be an error Message",errMsgs.length==1);
			   String errMsg=errMsgs[0];
			   assertEquals(errMsg,"? failed: com.meterware.httpunit.ScriptException: unable to find /xyz.js");
		     }  
		   } catch (ScriptException se) {
			   assertTrue(throwScriptException);
		   } catch (Exception e) {
			   fail("there should be no exception when throwScriptException is "+throwScriptException);
		   }		   
	   }
	   // Restore exceptions state
	   HttpUnitOptions.setExceptionsThrownOnErrorStatus(originalState );
	   HttpUnitOptions.setExceptionsThrownOnScriptError(originalScriptState);  		   
	   HttpUnitUtils.setEXCEPTION_DEBUG(oldDebug);
   }


In WebClientTest I had to add another test case to check 
how undefined resources are handled in general by the 
pseudo-server.

Russell and others who are more familiar with this code 
- could you please check the "FIXME" part of this 
testcase. IMHO the responseMessage should be set and not 
be null and the text should be empty in the case where 
throwExeption is false. Unfortunately the naming in 
PseudoServer is confusing - there the responseMessage is 
called responseText. When handling the error and 
inputStreams the state of these is traced very carefully 
but on the other hand in an error condition the text is 
taken from the errorStream. The refactoring between 1.6 
and 1.7 seems to have made this code a bit more 
complicated than might be necessary. What do you think 
can be done about this?
	/**
	 * check access to undefined resources
	 * @throws IOException 
	 */
	public void testUndefinedResource() throws IOException {
		boolean originalState = HttpUnitOptions
				.getExceptionsThrownOnErrorStatus();
		// try two cases for throwException true on i==0, false on i==1
		for (int i = 0; i <2; i++) {
			boolean throwException = i == 0;
			HttpUnitOptions.setExceptionsThrownOnErrorStatus(throwException);
			WebResponse response = null;
			try {
				WebConversation wc = new WebConversation();
				WebRequest request = new GetMethodWebRequest(getHostPath()
						+ "/undefined");
				response = wc.getResponse(request);
				if (throwException) {
					fail("there should have been an exception here");
				}
			} catch (HttpNotFoundException hnfe) {
				assertTrue(throwException);
				response=hnfe.getResponse();
			} catch (Exception e) {
				fail("there should be no exception here");
			}
			assertTrue(response != null);
			assertEquals(HttpURLConnection.HTTP_NOT_FOUND, response
					.getResponseCode());
			if (throwException) {
				assertEquals("with throwException="+throwException,"", response.getText());
				assertEquals("with throwException="+throwException,"unable to find 
/undefined",response.getResponseMessage());
			} else {
				// FIXME what do we expect here and how do we get it!
				assertEquals("with throwException="+throwException,"unable to find /undefined", response.getText());
				assertNull(response.getResponseMessage());				
			}
		}
		HttpUnitOptions.setExceptionsThrownOnErrorStatus(originalState);
	}


Yours

Wolfgang

BITPlan - smart solutions
Pater-Delp-Str. 1, D-47877 Willich Schiefbahn
Tel. +49 2154 811-480, Fax +49 2154 811-481
Web: http://www.bitplan.de
bitplan GmbH, Willich - HRB 6820 Krefeld, VAT-ID: 10258040548, 
Geschäftsführer: Wolfgang Fahl

------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day 
trial. Simplify your report design, integration and deployment - and focus on 
what you do best, core application coding. Discover what's new with 
Crystal Reports now.  http://p.sf.net/sfu/bobj-july

_______________________________________________
Httpunit-develop mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/httpunit-develop