package qub;

public class HttpClientTests
{
    public static void test(TestRunner runner, Function1<Test,HttpClient> creator)
    {
        runner.testGroup(HttpClient.class, () ->
        {
            runner.testGroup("send(HttpRequest)", () ->
            {
                runner.test("with null", (Test test) ->
                {
                    final HttpClient httpClient = creator.run(test);
                    final Result<HttpResponse> response = httpClient.send(null);
                    test.assertError(new IllegalArgumentException("request cannot be null."), response);
                });

                runner.test("with unknown host", (Test test) ->
                {
                    final HttpClient httpClient = creator.run(test);
                    final HttpRequest httpRequest = HttpRequest.create(HttpMethod.GET, "http://www.idontexistbecauseimnotagoodurl.com").getValue();
                    final Result<HttpResponse> httpResponse = httpClient.send(httpRequest);
                    test.assertError(new java.net.UnknownHostException("www.idontexistbecauseimnotagoodurl.com"), httpResponse);
                });

                runner.test("with GET request to www.example.com", (Test test) ->
                {
                    final HttpClient httpClient = creator.run(test);
                    final HttpRequest httpRequest = HttpRequest.create(HttpMethod.GET, "http://www.example.com").getValue();

                    final Result<HttpResponse> httpResponseResult = httpClient.send(httpRequest);
                    test.assertSuccess(httpResponseResult);

                    final HttpResponse httpResponse = httpResponseResult.getValue();
                    test.assertEqual("HTTP/1.1", httpResponse.getHttpVersion());
                    test.assertEqual(200, httpResponse.getStatusCode());
                    test.assertEqual("OK", httpResponse.getReasonPhrase());
                    test.assertNotNull(httpResponse.getHeaders());
                    test.assertSuccess("1270", httpResponse.getHeaders().getValue("content-length"));
                    test.assertNotNull(httpResponse.getBody());
                    final String bodyString = httpResponse.getBody().asCharacterReadStream().readString(3000).getValue();
                    test.assertNotNull(bodyString);
                    test.assertTrue(bodyString.contains("<html>"));
                    test.assertTrue(bodyString.contains("<h1>Example Domain</h1>"));
                    test.assertTrue(bodyString.contains("</html>"));
                });

                runner.test("with GET request to http://www.treasurydirect.gov/TA_WS/securities/auctioned?format=json&type=Bill", (Test test) ->
                {
                    final HttpClient httpClient = creator.run(test);
                    final HttpRequest httpRequest = HttpRequest.create(HttpMethod.GET, "http://www.treasurydirect.gov/TA_WS/securities/auctioned?format=json&type=Bill").getValue();

                    final Result<HttpResponse> httpResponseResult = httpClient.send(httpRequest);
                    test.assertSuccess(httpResponseResult);

                    final HttpResponse httpResponse = httpResponseResult.getValue();
                    test.assertEqual("HTTP/1.0", httpResponse.getHttpVersion());
                    test.assertEqual(302, httpResponse.getStatusCode());
                    test.assertEqual("Found", httpResponse.getReasonPhrase());
                    test.assertNotNull(httpResponse.getHeaders());
                    test.assertSuccess("https://http://www.treasurydirect.gov/TA_WS/securities/auctioned?format=json&type=Bill", httpResponse.getHeaders().getValue("location"));
                    test.assertSuccess("0", httpResponse.getHeaders().getValue("content-length"));
                    test.assertNull(httpResponse.getBody());
                });
            });
        });
    }
}

@@ -2323,16 +2323,95 @@
 tring);%0A
+                    test.assertTrue(bodyString.startsWith(%22%3C!doctype html%3E%22));%0A
         

Add more assertions to HttpClient tests