Path: blob/master/test/jdk/com/sun/net/httpserver/DateFormatterTest.java
41152 views
/*1* Copyright (c) 2020, 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*/2223import com.sun.net.httpserver.HttpExchange;24import com.sun.net.httpserver.HttpHandler;25import com.sun.net.httpserver.HttpServer;26import java.io.IOException;27import java.io.InputStream;28import java.io.OutputStream;29import java.net.InetAddress;30import java.net.InetSocketAddress;31import java.net.URI;32import java.net.URISyntaxException;33import java.net.http.HttpClient;34import java.net.http.HttpRequest;35import java.net.http.HttpResponse;36import java.util.concurrent.Executors;37import java.util.regex.Matcher;38import java.util.regex.Pattern;3940import jdk.test.lib.net.URIBuilder;41import org.testng.annotations.AfterTest;42import org.testng.annotations.BeforeTest;43import org.testng.annotations.Test;44import static java.net.http.HttpResponse.BodyHandlers.ofString;45import static org.testng.Assert.assertTrue;46import static org.testng.Assert.fail;4748/**49* @test50* @bug 824530751* @summary Test for DateFormatter in ExchangeImpl52* @modules java.net.http53* @library /test/lib54* @build DateFormatterTest55* @run testng/othervm DateFormatterTest56*/57public class DateFormatterTest {5859private HttpServer server;6061static URI httpURI;62static final Integer ITERATIONS = 10;63static String format;64static Pattern pattern;6566@BeforeTest67public void setUp() throws IOException, URISyntaxException {68String days = "(Mon|Tue|Wed|Thu|Fri|Sat|Sun)(,)";69String dayNo = "(\\s\\d\\d\\s)";70String month = "(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)";71String hour = "(\\s\\d\\d\\d\\d\\s\\d\\d)(:)(\\d\\d)(:)(\\d\\d\\s)";72String zone = "(GMT)";73format = days + dayNo + month + hour + zone;74pattern = Pattern.compile(format);75server = HttpServer.create(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), 10);76server.createContext("/server", new DateFormatHandler());77server.setExecutor(Executors.newCachedThreadPool());78httpURI = URIBuilder.newBuilder()79.host(server.getAddress().getAddress())80.port(server.getAddress().getPort())81.scheme("http")82.path("/server")83.build();84server.start();85}8687@AfterTest88public void cleanUp() {89server.stop(1);90}9192@Test93public void testDateFormat() throws Exception {94HttpClient client = HttpClient.newBuilder()95.build();96HttpRequest request = HttpRequest.newBuilder(httpURI)97.GET()98.build();99for (int i = 0; i < ITERATIONS; i++) {100HttpResponse<String> response = client.send(request, ofString());101String date = response.headers().firstValue("Date").orElse("null");102if (date.equals("null"))103fail("Date not present");104Matcher matcher = pattern.matcher(date);105assertTrue(matcher.matches());106}107}108109public static class DateFormatHandler implements HttpHandler {110111@Override112public void handle(HttpExchange exchange) throws IOException {113try (InputStream is = exchange.getRequestBody();114OutputStream os = exchange.getResponseBody()) {115byte[] bytes = is.readAllBytes();116exchange.sendResponseHeaders(200, bytes.length);117os.write(bytes);118}119}120}121}122123124