Re: HeadMethodWebRequest problems

<[email protected]> Thu, 20 Aug 2009 13:32:44 -0500
Newsgroups gmane.comp.web.httpunit.devel
Message-ID <047619DE22807D4192B0B9EB64E45CC12485A155B6@MSGCMSV21024.ent.wfb.bank.corp>
I have another test case regarding this.
It seems that on a HEAD request you should not be calling the HTML parsers
at all.  In the case below we fail because the parser is called (even though
it is called with the empty string).  Also the HTMLParserListener I implemented
is outputting a ton of stuff, it looks like something somewhere is not getting
the empty string back from the HEAD request (I am not sure what at this point).
(the listener should probably have "fail" calls as well, I just wanted to see all the output).

// has to be in this package since parses aren't public classes or you have to implement all parser methods
package com.meterware.httpunit.parsing;
...
    public void testHeadMethodWebRequest2() throws Exception {
        defineResource( "SimplePage.html",
            "<html><head><title>A Sample Page</title></head>\n" +
            "<body>Hello</body></html>\n" );
        HttpUnitOptions.setExceptionsThrownOnErrorStatus(true);
        try {
            HTMLParserFactory.setParserWarningsEnabled(true);
            HTMLParserFactory.setHTMLParser(new NekoHTMLParser() {
                    @Override
                    public void parse(URL pageURL, String pageText, DocumentAdapter adapter) {
                        System.err.println("Parsing URL=" + pageURL + "\n" + pageText);
                        fail("Should not be parsing a HEAD request");
                    }
                });
            HTMLParserFactory.addHTMLParserListener(new HTMLParserListener() {
                    public void error(URL url, String msg, int line, int column) {
                        System.err.println("ERROR @url=" + url + ": (" + line + ", " + column + "):" + msg);
                    }

                    public void warning(URL url, String msg, int line, int column) {
                        System.err.println("WARN @url=" + url + ": (" + line + ", " + column + "):" + msg);
                    }

                });
            WebConversation wc = new WebConversation();
            // create a HeadMethodWebRequest
            // see http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html for definition
            WebRequest request = new HeadMethodWebRequest( getHostPath() + "/SimplePage.html" );
            WebResponse simplePage = wc.getResponse( request );
            String text=simplePage.getText();
            // no body should be returned
            assertEquals("",text);
        } finally {
            HTMLParserFactory.reset();
        }
    }

Thanks,
Dan

From: Wolfgang Fahl [mailto:[email protected]]
Sent: Wednesday, August 19, 2009 3:48 AM
To: Lipofsky, Dan; Discussion of use and development of HttpUnit
Subject: Re: [Httpunit-develop] HeadMethodWebRequest problems

Dear Dan,

thanks for pointing this out and supplying the testCase.

Subversion Revision 1040
http://httpunit.svn.sourceforge.net/viewvc/httpunit?view=rev&revision=1040

has the fix for it.

WebPageText.java contains your test. I have added an assertion that no body is returned as asked for in
RFC 2616
http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html
9.4 HEAD
The HEAD method is identical to GET except that the server MUST NOT return a message-body in the response. The metainformation contained in the HTTP headers in response to a HEAD request SHOULD be identical to the information sent in response to a GET request. This method can be used for obtaining metainformation about the entity implied by the request without transferring the entity-body itself. This method is often used for testing hypertext links for validity, accessibility, and recent modification.
The response to a HEAD request MAY be cacheable in the sense that the information contained in the response MAY be used to update a previously cached entity from that resource. If the new field values indicate that the cached entity differs from the current entity (as would be indicated by a change in Content-Length, Content-MD5, ETag or Last-Modified), then the cache MUST treat the cache entry as stale.



     /**
     *   add   test   for   HeadMethodWebRequest
     *   @author   Dan   Lipofsky   2009 - 08 - 19
     *   @throws   Exception
     */
    public   void  testHeadMethodWebRequest() throws  Exception {
        defineResource( "SimplePage.html" ,
            "<html><head><title>A Sample Page</title></head>\n"  +
            "<body>Hello</body></html>\n"  );
        HttpUnitOptions.setExceptionsThrownOnErrorStatus( true );
        WebConversation wc = new  WebConversation();
        // create a HeadMethodWebRequest
        // see http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html for definition
        WebRequest request = new  HeadMethodWebRequest( getHostPath() + "/SimplePage.html"  );
        WebResponse simplePage = wc.getResponse( request );
        String text=simplePage.getText();
        // no body should be returned
        assertEquals( "" ,text);
      }


Keep up the good work!

Yours
  Wolfgang

> I tried to use HeadMethodWebRequest, but I have problems because it is
> calling the HTMLParser I set with HTMLParserFactory.setHTMLParser(...)
> which it shouldn't be doing because HEAD will have no body in the response.
>
> I tried to look in the unit tests, and I see no tests
> for HeadMethodWebRequest.  Perhaps the test server doesn't
> support it because this gave me a 404:
>
>   public void testHead() throws Exception {
>     defineResource( "SimplePage.html",
>         "<html><head><title>A Sample Page</title></head>\n" +
>         "<body>Hello</body></html>\n" );
>     HttpUnitOptions.setExceptionsThrownOnErrorStatus(true);
>     WebConversation wc = new WebConversation();
>     WebRequest request = new HeadMethodWebRequest( getHostPath() + "/SimplePage.html" );
>     WebResponse simplePage = wc.getResponse( request );
>   }


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