Uploaded image for project: 'HttpComponents HttpClient'
  1. HttpComponents HttpClient
  2. HTTPCLIENT-1759

Contribute new tests that use the in-process HttpServer

    XMLWordPrintableJSON

Details

    • Patch, Important

    Description

      Please accept this contribution of new tests that use the in-process HttpServer similar to the way it is used with the LocalServerTestBase. A special request handler is used to check HTTP requests. Before the request is sent, the request handler is told what to expect in the request as far as headers, parameters, path, content type, and body are concerned. The handler checks the request against the expectations. Then, the handler is also told what response to return, and the response is checked when the HTTP client receives the response.

      Every HTTP method is tested in this way.

      The tests use a testing framework that is is designed to work with any HTTP client. For instance Groovy's HttpBuilder and RESTClient use the Apache HttpClient to make HTTP requests. The testing framework here can test the HttpBuilder and RESTClient as well as HttpClient. There just has to be an adapter for each HTTP client. I have included one for Apache HttpClient5.

      Since I would like to reuse the test framework for other projects, I have not put the framework in the test area so it can potentially be used for any project that uses HttpClient to run the same tests or create new tests. Care has been taken to make sure no testing dependency such as JUnit or EasyMock is used in the framework.

      I used test-first development when creating this test framework. So, I have submitted a lot of tests which I put under the test area.

      Usage. Generally, the idea is that you specify a test with a set of List's and Map's. If you write the test with Groovy, it could look something like this:

                final HttpServerTestingFramework framework = new HttpServerTestingFramework(new HttpClient5TestingAdapter());
      
                framework.addTest(
                                    request: [
                                        path: '/stuff',
                                        method:'GET',
                                        query: [param : 'something'],
                                        headers: [header1:'stuff', header2:'more-stuff'],
                                        contentType: 'text/plain; charset=us-ascii',
                                        body: 'What is the meaning of life?',
                                    ],
                                    response: [
                                        status:201,
                                        headers: [header3:'header_stuff',],
                                        contentType: 'text/html; charset=us-ascii',
                                        body: '<HTML>42</HTML>',
                                    ],
                )
                framework.runTests();
      

      There is also a set of tests that are created by default that exercise all of the HTTP methods. If you don't want these tests to run, then just delete the tests after creating the framework:

      framework.deleteTests();

      You can see TestHttpServerTestingFramework.java to see how the framework is used to test HttpClient5. Especially the defaultTests() test:

          @Test
          public void defaultTests() throws Exception {
              final HttpServerTestingAdapter adapter = new HttpClient5TestingAdapter();
      
              // create the framework without deleting the default tests.
              final HttpServerTestingFramework framework = new HttpServerTestingFramework();
              framework.setAdapter(adapter);
      
              framework.runTests();
      
          }
      

      I have submitted a patch file. There are no changes to the existing HttpClient code. All files in the patch are new, and they do not interfere with the current functionality of HttpClient. I created a new package called org.apache.hc.client5.http.testframework.

      Attachments

        1. patch.txt
          141 kB
          John Lewis

        Issue Links

          Activity

            People

              olegk Oleg Kalnichevski
              lewijwjohn John Lewis
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: