Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012, 2018 Oracle and/or its affiliates. All rights reserved. |
| 3 | * |
| 4 | * This program and the accompanying materials are made available under the |
| 5 | * terms of the Eclipse Public License v. 2.0, which is available at |
| 6 | * http://www.eclipse.org/legal/epl-2.0. |
| 7 | * |
| 8 | * This Source Code may also be made available under the following Secondary |
| 9 | * Licenses when the conditions for such availability set forth in the |
| 10 | * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, |
| 11 | * version 2 with the GNU Classpath Exception, which is available at |
| 12 | * https://www.gnu.org/software/classpath/license.html. |
| 13 | * |
| 14 | * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 |
| 15 | */ |
| 16 | |
| 17 | import java.io.*; |
| 18 | import java.net.*; |
| 19 | import java.util.List; |
| 20 | import java.util.Map; |
| 21 | |
| 22 | import com.sun.ejte.ccl.reporter.*; |
| 23 | import org.glassfish.grizzly.test.http2.*; |
| 24 | |
| 25 | /* |
| 26 | * Unit test for Http2 Push static file |
| 27 | */ |
| 28 | public class WebTest { |
| 29 | |
| 30 | private static String TEST_NAME = "servlet-4.0-push-static"; |
| 31 | private static String EXPECTED_PUSH_BODY = "body { color: yellow; }"; |
| 32 | private static String EXPECTED_BODY = "my.css"; |
| 33 | |
| 34 | private static SimpleReporterAdapter stat |
| 35 | = new SimpleReporterAdapter("appserv-tests"); |
| 36 | |
| 37 | private String host; |
| 38 | private int port; |
| 39 | private String contextRoot; |
| 40 | |
| 41 | public WebTest(String[] args) { |
| 42 | host = args[0]; |
| 43 | port = Integer.parseInt(args[1]); |
| 44 | contextRoot = args[2]; |
| 45 | } |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 46 | |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 47 | public static void main(String[] args) { |
| 48 | stat.addDescription("Unit test for http2 push static file"); |
| 49 | WebTest webTest = new WebTest(args); |
| 50 | webTest.doTest(); |
| 51 | stat.printSummary(TEST_NAME); |
| 52 | } |
| 53 | |
| 54 | public void doTest() { |
| 55 | try (HttpClient httpClient = HttpClient.builder(). |
| 56 | host(host).port(port).build()) { |
| 57 | httpClient.request().path(contextRoot + "/test").build().send(); |
| 58 | HttpResponse httpResponse = httpClient.getHttpResponse(); |
| 59 | HttpResponse httpResponse2 = httpClient.getHttpResponse(); |
| 60 | if (!verify(httpResponse) || !verify(httpResponse2)) { |
| 61 | throw new Exception("Incorrect result"); |
| 62 | } |
| 63 | stat.addStatus(TEST_NAME, stat.PASS); |
| 64 | } catch(Throwable t) { |
| 65 | stat.addStatus(TEST_NAME, stat.FAIL); |
| 66 | t.printStackTrace(); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | private boolean verify(HttpResponse response) { |
| 71 | if (response == null) { |
| 72 | System.out.println("--> response is null"); |
| 73 | return false; |
| 74 | } |
| 75 | |
| 76 | String body = response.getBody().trim(); |
| 77 | System.out.println("--> body: " + body); |
| 78 | return (response.isPush() ? EXPECTED_PUSH_BODY.equals(body) : body.contains(EXPECTED_BODY)); |
| 79 | } |
| 80 | } |