Path: blob/master/test/jdk/sun/net/www/AuthHeaderTest.java
41149 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* @modules java.base/sun.net.www27* @library ../../../sun/net/www/httptest/28* @build HttpCallback TestHttpServer ClosedChannelList HttpTransaction29* @run main AuthHeaderTest30* @summary AuthHeaderTest bug31*/3233import java.io.*;34import java.net.*;3536public class AuthHeaderTest implements HttpCallback {3738static int count = 0;39static String authstring;4041void errorReply (HttpTransaction req, String reply) throws IOException {42req.addResponseHeader ("Connection", "close");43req.addResponseHeader ("Www-authenticate", reply);44req.sendResponse (401, "Unauthorized");45req.orderlyClose();46}4748void okReply (HttpTransaction req) throws IOException {49req.setResponseEntityBody ("Hello .");50req.sendResponse (200, "Ok");51req.orderlyClose();52}5354public void request (HttpTransaction req) {55try {56authstring = req.getRequestHeader ("Authorization");57System.out.println (authstring);58switch (count) {59case 0:60errorReply (req, "Basic realm=\"wallyworld\"");61break;62case 1:63/* client stores a username/pw for wallyworld64*/65okReply (req);66break;67}68count ++;69} catch (IOException e) {70e.printStackTrace();71}72}7374static void read (InputStream is) throws IOException {75int c;76System.out.println ("reading");77while ((c=is.read()) != -1) {78System.out.write (c);79}80System.out.println ("");81System.out.println ("finished reading");82}8384static void client (String u) throws Exception {85URL url = new URL (u);86System.out.println ("client opening connection to: " + u);87URLConnection urlc = url.openConnection (Proxy.NO_PROXY);88InputStream is = urlc.getInputStream ();89read (is);90is.close();91}9293static TestHttpServer server;9495public static void main (String[] args) throws Exception {96MyAuthenticator auth = new MyAuthenticator ();97Authenticator.setDefault (auth);98InetAddress loopback = InetAddress.getLoopbackAddress();99try {100server = new TestHttpServer (new AuthHeaderTest(), 1, 10, loopback, 0);101System.out.println ("Server: listening on port: " + server.getAuthority());102client ("http://" + server.getAuthority() + "/d1/foo.html");103} catch (Exception e) {104if (server != null) {105server.terminate();106}107throw e;108}109int f = auth.getCount();110if (f != 1) {111except ("Authenticator was called "+f+" times. Should be 1");112}113server.terminate();114}115116public static void except (String s) {117server.terminate();118throw new RuntimeException (s);119}120121static class MyAuthenticator extends Authenticator {122MyAuthenticator () {123super ();124}125126int count = 0;127128public PasswordAuthentication getPasswordAuthentication () {129PasswordAuthentication pw;130pw = new PasswordAuthentication ("user", "pass2".toCharArray());131count ++;132return pw;133}134135public int getCount () {136return (count);137}138}139}140141142