Path: blob/master/test/jdk/sun/net/www/protocol/http/Modified.java
41159 views
/*1* Copyright (c) 1998, 2019, 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 409260526* @library /test/lib27* @run main/othervm Modified28* @run main/othervm -Djava.net.preferIPv6Addresses=true Modified29* @summary Test HttpURLConnection setIfModifiedSince30*31*/3233import java.net.*;34import java.io.*;35import jdk.test.lib.net.URIBuilder;3637public class Modified implements Runnable {3839ServerSocket ss;4041public void run() {42try {43Socket s = ss.accept();44boolean gotIfModified = false;4546BufferedReader in = new BufferedReader(47new InputStreamReader(s.getInputStream()) );4849String str = null;50do {51str = in.readLine();52if (str.startsWith("If-Modified-Since")) {53gotIfModified = true;54}55if (str.equals("")) {56break;57}58} while (str != null);5960PrintStream out = new PrintStream(61new BufferedOutputStream(62s.getOutputStream() ));6364if (gotIfModified) {65out.print("HTTP/1.1 304 Not Modified\r\n");66} else {67out.print("HTTP/1.1 200 OK\r\n");68}6970out.print("Content-Type: text/html\r\n");71out.print("Connection: close\r\n");72out.print("\r\n");73out.flush();7475s.close();7677} catch (Exception e) {78e.printStackTrace();79}80}8182Modified() throws Exception {8384InetAddress loopback = InetAddress.getLoopbackAddress();85InetSocketAddress address = new InetSocketAddress(loopback, 0);86ss = new ServerSocket();87ss.bind(address);88int port = ss.getLocalPort();8990Thread thr = new Thread(this);91thr.start();9293URL testURL = URIBuilder.newBuilder()94.scheme("http")95.host(loopback)96.port(port)97.path("/index.html")98.toURL();99URLConnection URLConn = testURL.openConnection(Proxy.NO_PROXY);100HttpURLConnection httpConn;101102if (URLConn instanceof HttpURLConnection) {103httpConn = (HttpURLConnection)URLConn;104httpConn.setAllowUserInteraction(false);105httpConn.setIfModifiedSince(9990000000000L);106int response = httpConn.getResponseCode();107if (response != 304)108throw new RuntimeException("setModifiedSince failure.");109}110}111112public static void main(String args[]) throws Exception {113new Modified();114}115}116117118