Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/net/www/protocol/http/Modified.java
38867 views
/*1* Copyright (c) 1998, 2001, 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* @summary Test HttpURLConnection setIfModifiedSince27*28*/2930import java.net.*;31import java.io.*;3233public class Modified implements Runnable {3435ServerSocket ss;3637public void run() {38try {39Socket s = ss.accept();40boolean gotIfModified = false;4142BufferedReader in = new BufferedReader(43new InputStreamReader(s.getInputStream()) );4445String str = null;46do {47str = in.readLine();48if (str.startsWith("If-Modified-Since")) {49gotIfModified = true;50}51if (str.equals("")) {52break;53}54} while (str != null);5556PrintStream out = new PrintStream(57new BufferedOutputStream(58s.getOutputStream() ));5960if (gotIfModified) {61out.print("HTTP/1.1 304 Not Modified\r\n");62} else {63out.print("HTTP/1.1 200 OK\r\n");64}6566out.print("Content-Type: text/html\r\n");67out.print("Connection: close\r\n");68out.print("\r\n");69out.flush();7071s.close();7273} catch (Exception e) {74e.printStackTrace();75}76}7778Modified() throws Exception {7980ss = new ServerSocket(0);81Thread thr = new Thread(this);82thr.start();8384URL testURL = new URL("http://localhost:" + ss.getLocalPort() +85"/index.html");86URLConnection URLConn = testURL.openConnection();87HttpURLConnection httpConn;8889if (URLConn instanceof HttpURLConnection) {90httpConn = (HttpURLConnection)URLConn;91httpConn.setAllowUserInteraction(false);92httpConn.setIfModifiedSince(9990000000000L);93int response = httpConn.getResponseCode();94if (response != 304)95throw new RuntimeException("setModifiedSince failure.");96}97}9899public static void main(String args[]) throws Exception {100new Modified();101}102}103104105