test/jdk/java/net/httpclient/AsFileDownloadTest.java
author chegar
Wed, 07 Mar 2018 14:06:39 +0000
branchhttp-client-branch
changeset 56257 82a9340bdda6
parent 56167 96fa4f49a9ff
child 56265 ec34ae013fbe
permissions -rw-r--r--
http-client-branch: rework file permissions to use limited doPriv
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
56132
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
     1
/*
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
     4
 *
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
     7
 * published by the Free Software Foundation.
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
     8
 *
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    13
 * accompanied this code).
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    14
 *
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    18
 *
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    21
 * questions.
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    22
 */
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    23
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    24
/*
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    25
 * @test
56257
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents: 56167
diff changeset
    26
 * @summary Basic test for ofFileDownload
56132
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    27
 * @bug 8196965
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    28
 * @modules java.base/sun.net.www.http
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    29
 *          java.net.http/jdk.internal.net.http.common
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    30
 *          java.net.http/jdk.internal.net.http.frame
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    31
 *          java.net.http/jdk.internal.net.http.hpack
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    32
 *          java.logging
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    33
 *          jdk.httpserver
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    34
 * @library /lib/testlibrary /test/lib http2/server
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    35
 * @build Http2TestServer
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    36
 * @build jdk.testlibrary.SimpleSSLContext
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    37
 * @build jdk.test.lib.Platform
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    38
 * @build jdk.test.lib.util.FileUtils
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    39
 * @run testng/othervm AsFileDownloadTest
56257
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents: 56167
diff changeset
    40
 * @run testng/othervm/java.security.policy=AsFileDownloadTest.policy AsFileDownloadTest
56132
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    41
 */
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    42
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    43
import com.sun.net.httpserver.HttpExchange;
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    44
import com.sun.net.httpserver.HttpHandler;
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    45
import com.sun.net.httpserver.HttpServer;
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    46
import com.sun.net.httpserver.HttpsConfigurator;
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    47
import com.sun.net.httpserver.HttpsServer;
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    48
import java.io.IOException;
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    49
import java.io.InputStream;
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    50
import java.io.OutputStream;
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    51
import java.io.UncheckedIOException;
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    52
import java.net.InetSocketAddress;
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    53
import java.net.URI;
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56166
diff changeset
    54
import java.net.http.HttpClient;
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56166
diff changeset
    55
import java.net.http.HttpRequest;
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56166
diff changeset
    56
import java.net.http.HttpRequest.BodyPublishers;
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56166
diff changeset
    57
import java.net.http.HttpResponse;
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56166
diff changeset
    58
import java.net.http.HttpResponse.BodyHandler;
56132
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    59
import java.nio.file.Files;
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    60
import java.nio.file.Path;
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    61
import java.nio.file.Paths;
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    62
import java.util.ArrayList;
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    63
import java.util.Arrays;
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    64
import java.util.List;
56257
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents: 56167
diff changeset
    65
import java.util.logging.ConsoleHandler;
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents: 56167
diff changeset
    66
import java.util.logging.Level;
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents: 56167
diff changeset
    67
import java.util.logging.Logger;
56132
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    68
import javax.net.ssl.SSLContext;
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    69
import jdk.testlibrary.SimpleSSLContext;
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    70
import jdk.test.lib.util.FileUtils;
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    71
import org.testng.annotations.AfterTest;
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    72
import org.testng.annotations.BeforeTest;
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    73
import org.testng.annotations.DataProvider;
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    74
import org.testng.annotations.Test;
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    75
import static java.lang.System.out;
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56166
diff changeset
    76
import static java.net.http.HttpResponse.BodyHandlers.ofFileDownload;
56132
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    77
import static java.nio.charset.StandardCharsets.UTF_8;
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    78
import static java.nio.file.StandardOpenOption.*;
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    79
import static org.testng.Assert.assertEquals;
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    80
import static org.testng.Assert.assertTrue;
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    81
import static org.testng.Assert.fail;
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    82
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    83
public class AsFileDownloadTest {
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    84
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    85
    SSLContext sslContext;
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    86
    HttpServer httpTestServer;         // HTTP/1.1    [ 4 servers ]
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    87
    HttpsServer httpsTestServer;       // HTTPS/1.1
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    88
    Http2TestServer http2TestServer;   // HTTP/2 ( h2c )
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    89
    Http2TestServer https2TestServer;  // HTTP/2 ( h2  )
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    90
    String httpURI;
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    91
    String httpsURI;
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    92
    String http2URI;
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    93
    String https2URI;
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    94
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    95
    Path tempDir;
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    96
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    97
    static final String[][] contentDispositionValues = new String[][] {
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    98
          // URI query     Content-Type header value         Expected filename
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    99
            { "001", "Attachment; filename=example001.html", "example001.html" },
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   100
            { "002", "attachment; filename=example002.html", "example002.html" },
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   101
            { "003", "ATTACHMENT; filename=example003.html", "example003.html" },
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   102
            { "004", "attAChment; filename=example004.html", "example004.html" },
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   103
            { "005", "attachmeNt; filename=example005.html", "example005.html" },
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   104
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   105
            { "006", "attachment; Filename=example006.html", "example006.html" },
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   106
            { "007", "attachment; FILENAME=example007.html", "example007.html" },
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   107
            { "008", "attachment; fileName=example008.html", "example008.html" },
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   108
            { "009", "attachment; fIlEnAMe=example009.html", "example009.html" },
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   109
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   110
            { "010", "attachment; filename=Example010.html", "Example010.html" },
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   111
            { "011", "attachment; filename=EXAMPLE011.html", "EXAMPLE011.html" },
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   112
            { "012", "attachment; filename=eXample012.html", "eXample012.html" },
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   113
            { "013", "attachment; filename=example013.HTML", "example013.HTML" },
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   114
            { "014", "attachment; filename  =eXaMpLe014.HtMl", "eXaMpLe014.HtMl"},
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   115
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   116
            { "015", "attachment; filename=a",               "a"               },
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   117
            { "016", "attachment; filename= b",              "b"               },
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   118
            { "017", "attachment; filename=  c",             "c"               },
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   119
            { "018", "attachment; filename=    d",           "d"               },
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   120
            { "019", "attachment; filename=e  ; filename*=utf-8''eee.txt",  "e"},
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   121
            { "020", "attachment; filename*=utf-8''fff.txt; filename=f",    "f"},
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   122
            { "021", "attachment;  filename=g",              "g"               },
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   123
            { "022", "attachment;   filename= h",            "h"               },
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   124
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   125
            { "023", "attachment; filename=\"space name\"",                       "space name" },
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   126
            { "024", "attachment; filename=me.txt; filename*=utf-8''you.txt",     "me.txt"     },
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   127
            { "025", "attachment; filename=\"m y.txt\"; filename*=utf-8''you.txt", "m y.txt"   },
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   128
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   129
            { "030", "attachment; filename=foo/file1.txt",        "file1.txt" },
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   130
            { "031", "attachment; filename=foo/bar/file2.txt",    "file2.txt" },
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   131
            { "032", "attachment; filename=baz\\file3.txt",       "file3.txt" },
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   132
            { "033", "attachment; filename=baz\\bar\\file4.txt",  "file4.txt" },
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   133
            { "034", "attachment; filename=x/y\\file5.txt",       "file5.txt" },
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   134
            { "035", "attachment; filename=x/y\\file6.txt",       "file6.txt" },
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   135
            { "036", "attachment; filename=x/y\\z/file7.txt",     "file7.txt" },
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   136
            { "037", "attachment; filename=x/y\\z/\\x/file8.txt", "file8.txt" },
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   137
            { "038", "attachment; filename=/root/file9.txt",      "file9.txt" },
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   138
            { "039", "attachment; filename=../file10.txt",        "file10.txt" },
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   139
            { "040", "attachment; filename=..\\file11.txt",       "file11.txt" },
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   140
            { "041", "attachment; filename=foo/../../file12.txt", "file12.txt" },
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   141
    };
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   142
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   143
    @DataProvider(name = "positive")
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   144
    public Object[][] positive() {
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   145
        List<Object[]> list = new ArrayList<>();
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   146
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   147
        Arrays.asList(contentDispositionValues).stream()
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   148
                .map(e -> new Object[] {httpURI +  "?" + e[0], e[1], e[2]})
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   149
                .forEach(list::add);
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   150
        Arrays.asList(contentDispositionValues).stream()
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   151
                .map(e -> new Object[] {httpsURI +  "?" + e[0], e[1], e[2]})
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   152
                .forEach(list::add);
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   153
        Arrays.asList(contentDispositionValues).stream()
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   154
                .map(e -> new Object[] {http2URI +  "?" + e[0], e[1], e[2]})
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   155
                .forEach(list::add);
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   156
        Arrays.asList(contentDispositionValues).stream()
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   157
                .map(e -> new Object[] {https2URI +  "?" + e[0], e[1], e[2]})
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   158
                .forEach(list::add);
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   159
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   160
        return list.stream().toArray(Object[][]::new);
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   161
    }
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   162
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   163
    @Test(dataProvider = "positive")
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   164
    void test(String uriString, String contentDispositionValue, String expectedFilename)
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   165
        throws Exception
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   166
    {
56166
56c52d6417d1 http-client-branch: HTTP/2 200 response headers should not be appended to the 100-continue response headers
dfuchs
parents: 56132
diff changeset
   167
        out.printf("test(%s, %s, %s): starting", uriString, contentDispositionValue, expectedFilename);
56132
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   168
        HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   169
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   170
        URI uri = URI.create(uriString);
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   171
        HttpRequest request = HttpRequest.newBuilder(uri)
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56166
diff changeset
   172
                .POST(BodyPublishers.ofString("May the luck of the Irish be with you!"))
56132
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   173
                .build();
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   174
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56166
diff changeset
   175
        BodyHandler bh = ofFileDownload(tempDir.resolve(uri.getPath().substring(1)),
56132
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   176
                                        CREATE, TRUNCATE_EXISTING, WRITE);
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   177
        HttpResponse<Path> response = client.send(request, bh);
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   178
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   179
        out.println("Got response: " + response);
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   180
        out.println("Got body Path: " + response.body());
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   181
        String fileContents = new String(Files.readAllBytes(response.body()), UTF_8);
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   182
        out.println("Got body: " + fileContents);
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   183
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   184
        assertEquals(response.statusCode(),200);
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   185
        assertEquals(response.body().getFileName().toString(), expectedFilename);
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   186
        assertTrue(response.headers().firstValue("Content-Disposition").isPresent());
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   187
        assertEquals(response.headers().firstValue("Content-Disposition").get(),
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   188
                     contentDispositionValue);
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   189
        assertEquals(fileContents, "May the luck of the Irish be with you!");
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   190
    }
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   191
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   192
    // --- Negative
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   193
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   194
    static final String[][] contentDispositionBADValues = new String[][] {
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   195
            // URI query     Content-Type header value
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   196
            { "100", ""                                    },  // empty
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   197
            { "101", "filename=example.html"               },  // no attachment
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   198
            { "102", "attachment; filename=space name"     },  // unquoted with space
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   199
            { "103", "attachment; filename="               },  // empty filename param
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   200
            { "104", "attachment; filename=\""             },  // single quote
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   201
            { "105", "attachment; filename=\"\""           },  // empty quoted
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   202
            { "106", "attachment; filename=."              },  // dot
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   203
            { "107", "attachment; filename=.."             },  // dot dot
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   204
            { "108", "attachment; filename=\".."           },  // badly quoted dot dot
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   205
            { "109", "attachment; filename=\"..\""         },  // quoted dot dot
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   206
            { "110", "attachment; filename=\"bad"          },  // badly quoted
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   207
            { "111", "attachment; filename=\"bad;"         },  // badly quoted with ';'
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   208
            { "112", "attachment; filename=\"bad ;"        },  // badly quoted with ' ;'
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   209
            { "113", "attachment; filename*=utf-8''xx.txt "},  // no "filename" param
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   210
    };
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   211
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   212
    @DataProvider(name = "negative")
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   213
    public Object[][] negative() {
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   214
        List<Object[]> list = new ArrayList<>();
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   215
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   216
        Arrays.asList(contentDispositionBADValues).stream()
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   217
                .map(e -> new Object[] {httpURI +  "?" + e[0], e[1]})
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   218
                .forEach(list::add);
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   219
        Arrays.asList(contentDispositionBADValues).stream()
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   220
                .map(e -> new Object[] {httpsURI +  "?" + e[0], e[1]})
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   221
                .forEach(list::add);
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   222
        Arrays.asList(contentDispositionBADValues).stream()
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   223
                .map(e -> new Object[] {http2URI +  "?" + e[0], e[1]})
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   224
                .forEach(list::add);
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   225
        Arrays.asList(contentDispositionBADValues).stream()
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   226
                .map(e -> new Object[] {https2URI +  "?" + e[0], e[1]})
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   227
                .forEach(list::add);
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   228
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   229
        return list.stream().toArray(Object[][]::new);
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   230
    }
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   231
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   232
    @Test(dataProvider = "negative")
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   233
    void negativeTest(String uriString, String contentDispositionValue)
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   234
            throws Exception
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   235
    {
56166
56c52d6417d1 http-client-branch: HTTP/2 200 response headers should not be appended to the 100-continue response headers
dfuchs
parents: 56132
diff changeset
   236
        out.printf("negativeTest(%s, %s): starting", uriString, contentDispositionValue);
56132
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   237
        HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   238
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   239
        URI uri = URI.create(uriString);
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   240
        HttpRequest request = HttpRequest.newBuilder(uri)
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56166
diff changeset
   241
                .POST(BodyPublishers.ofString("Does not matter"))
56132
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   242
                .build();
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   243
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56166
diff changeset
   244
        BodyHandler bh = ofFileDownload(tempDir, CREATE, TRUNCATE_EXISTING, WRITE);
56132
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   245
        try {
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   246
            HttpResponse<Path> response = client.send(request, bh);
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   247
            fail("UNEXPECTED response: " + response + ", path:" + response.body());
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   248
        } catch (UncheckedIOException | IOException ioe) {
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   249
            System.out.println("Caught expected: " + ioe);
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   250
        }
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   251
    }
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   252
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   253
    // -- Infrastructure
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   254
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   255
    @BeforeTest
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   256
    public void setup() throws Exception {
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   257
        tempDir = Paths.get("asFileDownloadTest.tmp.dir");
56257
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents: 56167
diff changeset
   258
        if (Files.exists(tempDir))
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents: 56167
diff changeset
   259
            throw new AssertionError("Unexpected test work dir existence: " + tempDir.toString());
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents: 56167
diff changeset
   260
56132
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   261
        Files.createDirectory(tempDir);
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   262
        // Unique dirs per test run, based on the URI path
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   263
        Files.createDirectories(tempDir.resolve("http1/afdt/"));
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   264
        Files.createDirectories(tempDir.resolve("https1/afdt/"));
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   265
        Files.createDirectories(tempDir.resolve("http2/afdt/"));
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   266
        Files.createDirectories(tempDir.resolve("https2/afdt/"));
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   267
56257
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents: 56167
diff changeset
   268
        // HTTP/1.1 server logging in case of security exceptions ( uncomment if needed )
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents: 56167
diff changeset
   269
        //Logger logger = Logger.getLogger("com.sun.net.httpserver");
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents: 56167
diff changeset
   270
        //ConsoleHandler ch = new ConsoleHandler();
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents: 56167
diff changeset
   271
        //logger.setLevel(Level.ALL);
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents: 56167
diff changeset
   272
        //ch.setLevel(Level.ALL);
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents: 56167
diff changeset
   273
        //logger.addHandler(ch);
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents: 56167
diff changeset
   274
56132
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   275
        sslContext = new SimpleSSLContext().get();
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   276
        if (sslContext == null)
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   277
            throw new AssertionError("Unexpected null sslContext");
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   278
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   279
        InetSocketAddress sa = new InetSocketAddress(0);
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   280
        httpTestServer = HttpServer.create(sa, 0);
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   281
        httpTestServer.createContext("/http1/afdt", new Http1FileDispoHandler());
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   282
        httpURI = "http://127.0.0.1:" + httpTestServer.getAddress().getPort() + "/http1/afdt";
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   283
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   284
        httpsTestServer = HttpsServer.create(sa, 0);
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   285
        httpsTestServer.setHttpsConfigurator(new HttpsConfigurator(sslContext));
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   286
        httpsTestServer.createContext("/https1/afdt", new Http1FileDispoHandler());
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   287
        httpsURI = "https://127.0.0.1:" + httpsTestServer.getAddress().getPort() + "/https1/afdt";
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   288
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   289
        http2TestServer = new Http2TestServer("127.0.0.1", false, 0);
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   290
        http2TestServer.addHandler(new Http2FileDispoHandler(), "/http2/afdt");
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   291
        int port = http2TestServer.getAddress().getPort();
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   292
        http2URI = "http://127.0.0.1:" + port + "/http2/afdt";
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   293
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   294
        https2TestServer = new Http2TestServer("127.0.0.1", true, 0);
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   295
        https2TestServer.addHandler(new Http2FileDispoHandler(), "/https2/afdt");
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   296
        port = https2TestServer.getAddress().getPort();
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   297
        https2URI = "https://127.0.0.1:" + port + "/https2/afdt";
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   298
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   299
        httpTestServer.start();
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   300
        httpsTestServer.start();
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   301
        http2TestServer.start();
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   302
        https2TestServer.start();
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   303
    }
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   304
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   305
    @AfterTest
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   306
    public void teardown() throws Exception {
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   307
        httpTestServer.stop(0);
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   308
        httpsTestServer.stop(0);
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   309
        http2TestServer.stop();
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   310
        https2TestServer.stop();
56257
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents: 56167
diff changeset
   311
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents: 56167
diff changeset
   312
        if (System.getSecurityManager() == null && Files.exists(tempDir)) {
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents: 56167
diff changeset
   313
            // clean up before next run with security manager
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents: 56167
diff changeset
   314
            FileUtils.deleteFileTreeWithRetry(tempDir);
82a9340bdda6 http-client-branch: rework file permissions to use limited doPriv
chegar
parents: 56167
diff changeset
   315
        }
56132
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   316
    }
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   317
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   318
    static String contentDispositionValueFromURI(URI uri) {
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   319
        String queryIndex = uri.getQuery();
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   320
        String[][] values;
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   321
        if (queryIndex.startsWith("0"))  // positive tests start with '0'
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   322
            values = contentDispositionValues;
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   323
        else if (queryIndex.startsWith("1"))  // negative tests start with '1'
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   324
            values = contentDispositionBADValues;
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   325
        else
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   326
            throw new AssertionError("SERVER: UNEXPECTED query:" + queryIndex);
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   327
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   328
        return Arrays.asList(values).stream()
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   329
                .filter(e -> e[0].equals(queryIndex))
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   330
                .map(e -> e[1])
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   331
                .findFirst()
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   332
                .orElseThrow();
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   333
    }
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   334
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   335
    static class Http1FileDispoHandler implements HttpHandler {
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   336
        @Override
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   337
        public void handle(HttpExchange t) throws IOException {
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   338
            try (InputStream is = t.getRequestBody();
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   339
                 OutputStream os = t.getResponseBody()) {
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   340
                byte[] bytes = is.readAllBytes();
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   341
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   342
                t.getResponseHeaders().set("Content-Disposition",
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   343
                        contentDispositionValueFromURI(t.getRequestURI()));
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   344
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   345
                t.sendResponseHeaders(200, bytes.length);
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   346
                os.write(bytes);
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   347
            }
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   348
        }
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   349
    }
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   350
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   351
    static class Http2FileDispoHandler implements Http2Handler {
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   352
        @Override
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   353
        public void handle(Http2TestExchange t) throws IOException {
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   354
            try (InputStream is = t.getRequestBody();
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   355
                 OutputStream os = t.getResponseBody()) {
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   356
                byte[] bytes = is.readAllBytes();
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   357
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   358
                t.getResponseHeaders().addHeader("Content-Disposition",
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   359
                        contentDispositionValueFromURI(t.getRequestURI()));
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   360
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   361
                t.sendResponseHeaders(200, bytes.length);
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   362
                os.write(bytes);
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   363
            }
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   364
        }
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   365
    }
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   366
}