Menu

Faking a Bad post using WebClient

2003-01-17
2003-01-20
  • Erik Ramfelt

    Erik Ramfelt - 2003-01-17

    Ive developed a server that can be run as a servlet and as a standalone server, and im using httpunit and servletunit to test them. Ive implemented one abstract Testcase that I test both implementations with, then I have two testcases extending the abstract one, one for each server.

    But now im having problem testing in doing a POST without a "Content-Length" to my socket server. I want to test that if my server gets a post without the header then it should not receive the data, and send back a 400. But it seems that the WebConversation always writes a "Content-Length" as a header when contacting the server. Is there anyway to fool the WebConversation to not send a "Content-LEngth" ?

    This problem shows only when testing with WebConversation and not with the ServletRunner.newClient(). Shouldnt these two implementations be the same?

    My testcode looks like this :
    try
    {
    WebClient wc = getWebClient();
    File imageFile = new File( IMAGENAME_JP2 );
    WebRequest request = getPostMethodWebRequest( new FileInputStream( imageFile ) );

    request.setHeaderField("User", "****" );
    request.setHeaderField("Password", "****" );
    request.setHeaderField("Filename", "demo.jpg" );
    request.setHeaderField("Content-Length", "0" ); //this gets set to the correct size of the file, which I dont want it too
               
    WebResponse response = wc.getResponse( request );
    assertNull( response ); // this should not happen, there should be a HttpException with a content-length of 0.
    } catch ( HttpException e )
    {
    assertEquals( "Expecting a bad request code", 400, e.getResponseCode() );
    }

    Or am I doing everything totaly wrong?

     
    • Russell Gold

      Russell Gold - 2003-01-19

      Unfortunately, the content-length header is set by the underlying HttpURLConnection, so you may not be able to control it. I am wondering though, where do you expect to find a web agent which sets the wrong content-length? And why isn't the servletunit test sufficient?

       
    • Erik Ramfelt

      Erik Ramfelt - 2003-01-20

      Ok, i understand.

      The cause? Well I wanted to test bad post requests, since im doing (or was) my own Web requests  using sockets then I needed to test if I failed to add the header. BUT I would also test that my servlet/server did not go bonkers when something like that happened, if it shouldnt happen.

      Since the code base is for two servers, one servlet and one standalone I would like to test them both. Since they use the same abstract testcase so there was no problem by making the tests for two different servers. Necessary?, probably not but I want to be sure that the server doesnt behaves badly when something goes amiss.

       

Log in to post a comment.

Auth0 Logo