BasicAuthTest.java revision 15491:6f390eafc676
1/* 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. 4 * 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 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * 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 13 * 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 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26/** 27 * @test 28 * @bug 8087112 29 * @modules java.httpclient 30 * jdk.httpserver 31 * @run main/othervm BasicAuthTest 32 * @summary Basic Authentication Test 33 */ 34 35import com.sun.net.httpserver.BasicAuthenticator; 36import com.sun.net.httpserver.HttpContext; 37import com.sun.net.httpserver.HttpExchange; 38import com.sun.net.httpserver.HttpHandler; 39import com.sun.net.httpserver.HttpServer; 40import java.io.IOException; 41import java.io.InputStream; 42import java.io.OutputStream; 43import java.net.InetSocketAddress; 44import java.net.PasswordAuthentication; 45import java.net.URI; 46import java.net.http.*; 47import java.util.concurrent.ExecutorService; 48import java.util.concurrent.Executors; 49import static java.nio.charset.StandardCharsets.US_ASCII; 50 51public class BasicAuthTest { 52 53 static volatile boolean ok; 54 static final String RESPONSE = "Hello world"; 55 static final String POST_BODY = "This is the POST body 123909090909090"; 56 57 public static void main(String[] args) throws Exception { 58 HttpServer server = HttpServer.create(new InetSocketAddress(0), 10); 59 ExecutorService e = Executors.newCachedThreadPool(); 60 Handler h = new Handler(); 61 HttpContext serverContext = server.createContext("/test", h); 62 int port = server.getAddress().getPort(); 63 System.out.println("Server port = " + port); 64 65 ClientAuth ca = new ClientAuth(); 66 ServerAuth sa = new ServerAuth("foo realm"); 67 serverContext.setAuthenticator(sa); 68 server.setExecutor(e); 69 server.start(); 70 HttpClient client = HttpClient.create() 71 .authenticator(ca) 72 .build(); 73 74 try { 75 URI uri = new URI("http://127.0.0.1:" + Integer.toString(port) + "/test/foo"); 76 HttpRequest req = client.request(uri).GET(); 77 78 HttpResponse resp = req.response(); 79 ok = resp.statusCode() == 200 && 80 resp.body(HttpResponse.asString()).equals(RESPONSE); 81 82 if (!ok || ca.count != 1) 83 throw new RuntimeException("Test failed"); 84 85 // repeat same request, should succeed but no additional authenticator calls 86 87 req = client.request(uri).GET(); 88 resp = req.response(); 89 ok = resp.statusCode() == 200 && 90 resp.body(HttpResponse.asString()).equals(RESPONSE); 91 92 if (!ok || ca.count != 1) 93 throw new RuntimeException("Test failed"); 94 95 // try a POST 96 97 req = client.request(uri) 98 .body(HttpRequest.fromString(POST_BODY)) 99 .POST(); 100 resp = req.response(); 101 ok = resp.statusCode() == 200; 102 103 if (!ok || ca.count != 1) 104 throw new RuntimeException("Test failed"); 105 } finally { 106 client.executorService().shutdownNow(); 107 server.stop(0); 108 e.shutdownNow(); 109 } 110 System.out.println("OK"); 111 } 112 113 static class ServerAuth extends BasicAuthenticator { 114 115 ServerAuth(String realm) { 116 super(realm); 117 } 118 119 @Override 120 public boolean checkCredentials(String username, String password) { 121 if (!"user".equals(username) || !"passwd".equals(password)) { 122 return false; 123 } 124 return true; 125 } 126 127 } 128 129 static class ClientAuth extends java.net.Authenticator { 130 volatile int count = 0; 131 132 @Override 133 protected PasswordAuthentication getPasswordAuthentication() { 134 count++; 135 return new PasswordAuthentication("user", "passwd".toCharArray()); 136 } 137 } 138 139 static class Handler implements HttpHandler { 140 static volatile boolean ok; 141 142 @Override 143 public void handle(HttpExchange he) throws IOException { 144 String method = he.getRequestMethod(); 145 InputStream is = he.getRequestBody(); 146 if (method.equalsIgnoreCase("POST")) { 147 String requestBody = new String(is.readAllBytes(), US_ASCII); 148 if (!requestBody.equals(POST_BODY)) { 149 he.sendResponseHeaders(500, -1); 150 ok = false; 151 } else { 152 he.sendResponseHeaders(200, -1); 153 ok = true; 154 } 155 } else { // GET 156 he.sendResponseHeaders(200, RESPONSE.length()); 157 OutputStream os = he.getResponseBody(); 158 os.write(RESPONSE.getBytes(US_ASCII)); 159 os.close(); 160 ok = true; 161 } 162 } 163 164 } 165} 166