Menu

#125 Handling same named Headers with HtmlUnit

Release_3.0
closed-fixed
Core API (28)
5
2011-03-14
2011-02-23
No

Intro: Great work thanks!

Scenario:
Server sends two same named cookies (e.g. servlet container adds JSESSIONID and Servlet filter has to correct that cookie, but cannot kill the first cookie from the response). The 2nd header cookie sets a special attribute, e.g. "HttpOnly" which the first one does not have.

Problem:
assertCookieValueMatch("JSESSIONID", "(.)*HttpOnly");
will take the first cookie with that name and fail. This is because HtmlUnitTestingEngineImpl in public String getHeader(String name) trusts on com.gargoylesoftware.htmlunit.Webresponse#getResponseHeaderValue(String name) which will return the first header.

Also the workaround "getAllHeaders()" is a bit unclean. In HtmlUnitTestingEngineImpl#getAllHeaders() all values are put into a map by header name. The latter same named header fields will overwrite the earlier ones.

Expected behaviour:
For HtmlUnitTestingEngineImple#getHeader(String name) the last same named header should be returned. Take e.g. this specification (regarding Cookie setting) : http://tools.ietf.org/html/draft-ietf-httpstate-cookie-21#section-4.1.2 -> The last cookie wins.
For HtmlUnitTestingEngineImpl#getAllHeaders() some data structure (multi - map?) should be returned that preserves all headers in that order.

Thanks a lot for your great work!

Achim

P.S.: If you'd like a patch, just contact me.

- HtmlUnitTestingEngineImpl

Discussion

  • Julien HENRY

    Julien HENRY - 2011-02-23

    Hi Achim,

    Thanks for your detailed report. Could you please provide a patch with the fix and possibly a JUnit test that verify this situation.

    Thanks

    Julien

     
  • Achim Westermann

    Hi Julian,
    it took me some time to set up eclipse with m2eclipse (dependency hell), but now I had the time to write a test and propose a bugfix. Caution there is an API change!

    I attach two patches. One for the new test to see what is not working as I expect it. And one proposed bugfix.

    I worked on assertCookieValueMatch to use the last cookie with the given name being checked. Forget about my statement about com.gargoylesoftware.htmlunit.Webresponse#getResponseHeaderValue(String name) in this matter. I found out that assertCookieValueMatch works on getTestingEngine().getCookies() which returns everything right!

    I worked on ITestingEngine.getAllHeaders() and the HTMLunit impl to avoid loosing same named cookies. I did that by changing the data structure returned to allow passing multiple same named cookies. I did this in favour of a commons collections MultiValueMap as the latter would have disallowed to use generic code (and forced us to use casts) .

    If the patch is not working I could attach the plain files. Eclipse -> checkout maven project via scm did have it's on idea on which projects to create in my workspace (even if everything works well).

    kind regards,

    Achim

     
  • Achim Westermann

    Test case for multiple cookie headers

     
  • Achim Westermann

    Proposed bugfix.

     
  • Julien HENRY

    Julien HENRY - 2011-03-14

    Hi Achim,

    Thanks for the patch and test case. I applied it with several modifications. I tried to keep binary compatibility so I keep existing getAllHeaders method but deprecated it. I have added a new method: getResponseHeaders that returns all headers (even duplicate).

    Feel free to comment here if I miss anything.

    Regards,

    Julien

     
  • Julien HENRY

    Julien HENRY - 2011-03-14
    • assigned_to: nobody --> henryju
    • milestone: --> Release_3.0
    • labels: --> Core API
    • status: open --> closed-fixed
     

Log in to post a comment.