1 /* 2 * Copyright (c) 2020, 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. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 import com.sun.net.httpserver.HttpExchange; 25 import com.sun.net.httpserver.HttpHandler; 26 import com.sun.net.httpserver.HttpServer; 27 import java.io.IOException; 28 import java.io.InputStream; 29 import java.io.OutputStream; 30 import java.net.InetAddress; 31 import java.net.InetSocketAddress; 32 import java.net.URI; 33 import java.net.URISyntaxException; 34 import java.net.http.HttpClient; 35 import java.net.http.HttpRequest; 36 import java.net.http.HttpResponse; 37 import java.util.concurrent.Executors; 38 import java.util.regex.Matcher; 39 import java.util.regex.Pattern; 40 41 import jdk.test.lib.net.URIBuilder; 42 import org.testng.annotations.AfterTest; 43 import org.testng.annotations.BeforeTest; 44 import org.testng.annotations.Test; 45 import static java.net.http.HttpResponse.BodyHandlers.ofString; 46 import static org.testng.Assert.assertTrue; 47 import static org.testng.Assert.fail; 48 49 /** 50 * @test 51 * @bug 8245307 52 * @summary Test for DateFormatter in ExchangeImpl 53 * @modules java.net.http 54 * @library /test/lib 55 * @build DateFormatterTest 56 * @run testng/othervm DateFormatterTest 57 */ 58 public class DateFormatterTest { 59 60 private HttpServer server; 61 62 static URI httpURI; 63 static final Integer ITERATIONS = 10; 64 static String format; 65 static Pattern pattern; 66 67 @BeforeTest setUp()68 public void setUp() throws IOException, URISyntaxException { 69 String days = "(Mon|Tue|Wed|Thu|Fri|Sat|Sun)(,)"; 70 String dayNo = "(\\s\\d\\d\\s)"; 71 String month = "(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)"; 72 String hour = "(\\s\\d\\d\\d\\d\\s\\d\\d)(:)(\\d\\d)(:)(\\d\\d\\s)"; 73 String zone = "(GMT)"; 74 format = days + dayNo + month + hour + zone; 75 pattern = Pattern.compile(format); 76 server = HttpServer.create(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), 10); 77 server.createContext("/server", new DateFormatHandler()); 78 server.setExecutor(Executors.newCachedThreadPool()); 79 httpURI = URIBuilder.newBuilder() 80 .host(server.getAddress().getAddress()) 81 .port(server.getAddress().getPort()) 82 .scheme("http") 83 .path("/server") 84 .build(); 85 server.start(); 86 } 87 88 @AfterTest cleanUp()89 public void cleanUp() { 90 server.stop(1); 91 } 92 93 @Test testDateFormat()94 public void testDateFormat() throws Exception { 95 HttpClient client = HttpClient.newBuilder() 96 .build(); 97 HttpRequest request = HttpRequest.newBuilder(httpURI) 98 .GET() 99 .build(); 100 for (int i = 0; i < ITERATIONS; i++) { 101 HttpResponse<String> response = client.send(request, ofString()); 102 String date = response.headers().firstValue("Date").orElse("null"); 103 if (date.equals("null")) 104 fail("Date not present"); 105 Matcher matcher = pattern.matcher(date); 106 assertTrue(matcher.matches()); 107 } 108 } 109 110 public static class DateFormatHandler implements HttpHandler { 111 112 @Override handle(HttpExchange exchange)113 public void handle(HttpExchange exchange) throws IOException { 114 try (InputStream is = exchange.getRequestBody(); 115 OutputStream os = exchange.getResponseBody()) { 116 byte[] bytes = is.readAllBytes(); 117 exchange.sendResponseHeaders(200, bytes.length); 118 os.write(bytes); 119 } 120 } 121 } 122 } 123