jdk/test/java/net/httpclient/BasicAuthTest.java
changeset 42460 7133f144981a
parent 40684 2e37c119dc2a
equal deleted inserted replaced
42459:1ad58e0cbf16 42460:7133f144981a
     2  * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
     8  *
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    14  * version 2 for more details (a copy is included in the LICENSE file that
    12  * version 2 for more details (a copy is included in the LICENSE file that
    24  */
    22  */
    25 
    23 
    26 /**
    24 /**
    27  * @test
    25  * @test
    28  * @bug 8087112
    26  * @bug 8087112
    29  * @modules java.httpclient
    27  * @modules jdk.incubator.httpclient
    30  *          jdk.httpserver
    28  *          jdk.httpserver
    31  * @run main/othervm BasicAuthTest
    29  * @run main/othervm BasicAuthTest
    32  * @summary Basic Authentication Test
    30  * @summary Basic Authentication Test
    33  */
    31  */
    34 
    32 
    41 import java.io.InputStream;
    39 import java.io.InputStream;
    42 import java.io.OutputStream;
    40 import java.io.OutputStream;
    43 import java.net.InetSocketAddress;
    41 import java.net.InetSocketAddress;
    44 import java.net.PasswordAuthentication;
    42 import java.net.PasswordAuthentication;
    45 import java.net.URI;
    43 import java.net.URI;
    46 import java.net.http.*;
    44 import jdk.incubator.http.*;
    47 import java.util.concurrent.ExecutorService;
    45 import java.util.concurrent.ExecutorService;
    48 import java.util.concurrent.Executors;
    46 import java.util.concurrent.Executors;
    49 import static java.nio.charset.StandardCharsets.US_ASCII;
    47 import static java.nio.charset.StandardCharsets.US_ASCII;
       
    48 import static jdk.incubator.http.HttpRequest.BodyProcessor.fromString;
       
    49 import static jdk.incubator.http.HttpResponse.BodyHandler.asString;
    50 
    50 
    51 public class BasicAuthTest {
    51 public class BasicAuthTest {
    52 
    52 
    53     static volatile boolean ok;
    53     static volatile boolean ok;
    54     static final String RESPONSE = "Hello world";
    54     static final String RESPONSE = "Hello world";
    65         ClientAuth ca = new ClientAuth();
    65         ClientAuth ca = new ClientAuth();
    66         ServerAuth sa = new ServerAuth("foo realm");
    66         ServerAuth sa = new ServerAuth("foo realm");
    67         serverContext.setAuthenticator(sa);
    67         serverContext.setAuthenticator(sa);
    68         server.setExecutor(e);
    68         server.setExecutor(e);
    69         server.start();
    69         server.start();
    70         HttpClient client = HttpClient.create()
    70         HttpClient client = HttpClient.newBuilder()
    71                                       .authenticator(ca)
    71                                       .authenticator(ca)
    72                                       .build();
    72                                       .build();
    73 
    73 
    74         try {
    74         try {
    75             URI uri = new URI("http://127.0.0.1:" + Integer.toString(port) + "/test/foo");
    75             URI uri = new URI("http://127.0.0.1:" + Integer.toString(port) + "/test/foo");
    76             HttpRequest req = client.request(uri).GET();
    76             HttpRequest req = HttpRequest.newBuilder(uri).GET().build();
    77 
    77 
    78             HttpResponse resp = req.response();
    78             HttpResponse resp = client.send(req, asString());
    79             ok = resp.statusCode() == 200 &&
    79             ok = resp.statusCode() == 200 && resp.body().equals(RESPONSE);
    80                     resp.body(HttpResponse.asString()).equals(RESPONSE);
       
    81 
    80 
    82             if (!ok || ca.count != 1)
    81             if (!ok || ca.count != 1)
    83                 throw new RuntimeException("Test failed");
    82                 throw new RuntimeException("Test failed");
    84 
    83 
    85             // repeat same request, should succeed but no additional authenticator calls
    84             // repeat same request, should succeed but no additional authenticator calls
    86 
    85 
    87             req = client.request(uri).GET();
    86             resp = client.send(req, asString());
    88             resp = req.response();
    87             ok = resp.statusCode() == 200 && resp.body().equals(RESPONSE);
    89             ok = resp.statusCode() == 200 &&
       
    90                     resp.body(HttpResponse.asString()).equals(RESPONSE);
       
    91 
    88 
    92             if (!ok || ca.count != 1)
    89             if (!ok || ca.count != 1)
    93                 throw new RuntimeException("Test failed");
    90                 throw new RuntimeException("Test failed");
    94 
    91 
    95             // try a POST
    92             // try a POST
    96 
    93 
    97             req = client.request(uri)
    94             req = HttpRequest.newBuilder(uri)
    98                     .body(HttpRequest.fromString(POST_BODY))
    95                              .POST(fromString(POST_BODY))
    99                     .POST();
    96                              .build();
   100             resp = req.response();
    97             resp = client.send(req, asString());
   101             ok = resp.statusCode() == 200;
    98             ok = resp.statusCode() == 200;
   102 
    99 
   103             if (!ok || ca.count != 1)
   100             if (!ok || ca.count != 1)
   104                 throw new RuntimeException("Test failed");
   101                 throw new RuntimeException("Test failed");
   105         } finally {
   102         } finally {
   106             client.executorService().shutdownNow();
       
   107             server.stop(0);
   103             server.stop(0);
   108             e.shutdownNow();
   104             e.shutdownNow();
   109         }
   105         }
   110         System.out.println("OK");
   106         System.out.println("OK");
   111     }
   107     }