Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/net/www/AuthHeaderTest.java
38840 views
/*1* Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 480430926* @library ../../../sun/net/www/httptest/27* @build HttpCallback TestHttpServer ClosedChannelList HttpTransaction28* @run main AuthHeaderTest29* @summary AuthHeaderTest bug30*/3132import java.io.*;33import java.net.*;3435public class AuthHeaderTest implements HttpCallback {3637static int count = 0;38static String authstring;3940void errorReply (HttpTransaction req, String reply) throws IOException {41req.addResponseHeader ("Connection", "close");42req.addResponseHeader ("Www-authenticate", reply);43req.sendResponse (401, "Unauthorized");44req.orderlyClose();45}4647void okReply (HttpTransaction req) throws IOException {48req.setResponseEntityBody ("Hello .");49req.sendResponse (200, "Ok");50req.orderlyClose();51}5253public void request (HttpTransaction req) {54try {55authstring = req.getRequestHeader ("Authorization");56System.out.println (authstring);57switch (count) {58case 0:59errorReply (req, "Basic realm=\"wallyworld\"");60break;61case 1:62/* client stores a username/pw for wallyworld63*/64okReply (req);65break;66}67count ++;68} catch (IOException e) {69e.printStackTrace();70}71}7273static void read (InputStream is) throws IOException {74int c;75System.out.println ("reading");76while ((c=is.read()) != -1) {77System.out.write (c);78}79System.out.println ("");80System.out.println ("finished reading");81}8283static void client (String u) throws Exception {84URL url = new URL (u);85System.out.println ("client opening connection to: " + u);86URLConnection urlc = url.openConnection ();87InputStream is = urlc.getInputStream ();88read (is);89is.close();90}9192static TestHttpServer server;9394public static void main (String[] args) throws Exception {95MyAuthenticator auth = new MyAuthenticator ();96Authenticator.setDefault (auth);97try {98server = new TestHttpServer (new AuthHeaderTest(), 1, 10, 0);99System.out.println ("Server: listening on port: " + server.getLocalPort());100client ("http://localhost:"+server.getLocalPort()+"/d1/foo.html");101} catch (Exception e) {102if (server != null) {103server.terminate();104}105throw e;106}107int f = auth.getCount();108if (f != 1) {109except ("Authenticator was called "+f+" times. Should be 1");110}111server.terminate();112}113114public static void except (String s) {115server.terminate();116throw new RuntimeException (s);117}118119static class MyAuthenticator extends Authenticator {120MyAuthenticator () {121super ();122}123124int count = 0;125126public PasswordAuthentication getPasswordAuthentication () {127PasswordAuthentication pw;128pw = new PasswordAuthentication ("user", "pass2".toCharArray());129count ++;130return pw;131}132133public int getCount () {134return (count);135}136}137}138139140