test/jdk/java/net/httpclient/AsFileDownloadTest.java
author dfuchs
Fri, 23 Feb 2018 16:25:32 +0000
branchhttp-client-branch
changeset 56166 56c52d6417d1
parent 56132 c8a1eccbc719
child 56167 96fa4f49a9ff
permissions -rw-r--r--
http-client-branch: HTTP/2 200 response headers should not be appended to the 100-continue response headers
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
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    26
 * @summary Basic test for asFileDownload
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
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    40
 */
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
import com.sun.net.httpserver.HttpExchange;
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    43
import com.sun.net.httpserver.HttpHandler;
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    44
import com.sun.net.httpserver.HttpServer;
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    45
import com.sun.net.httpserver.HttpsConfigurator;
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    46
import com.sun.net.httpserver.HttpsServer;
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    47
import java.io.IOException;
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    48
import java.io.InputStream;
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    49
import java.io.OutputStream;
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    50
import java.io.UncheckedIOException;
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    51
import java.net.InetSocketAddress;
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    52
import java.net.URI;
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    53
import java.nio.file.Files;
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    54
import java.nio.file.Path;
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    55
import java.nio.file.Paths;
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    56
import java.util.ArrayList;
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    57
import java.util.Arrays;
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    58
import java.util.List;
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    59
import javax.net.ssl.SSLContext;
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    60
import java.net.http.HttpClient;
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    61
import java.net.http.HttpRequest;
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    62
import java.net.http.HttpResponse;
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    63
import java.net.http.HttpResponse.BodyHandler;
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    64
import jdk.testlibrary.SimpleSSLContext;
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    65
import jdk.test.lib.util.FileUtils;
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    66
import org.testng.annotations.AfterTest;
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    67
import org.testng.annotations.BeforeTest;
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    68
import org.testng.annotations.DataProvider;
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    69
import org.testng.annotations.Test;
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    70
import static java.lang.System.out;
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    71
import static java.net.http.HttpRequest.BodyPublisher.fromString;
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    72
import static java.net.http.HttpResponse.BodyHandler.asFileDownload;
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    73
import static java.nio.charset.StandardCharsets.UTF_8;
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    74
import static java.nio.file.StandardOpenOption.*;
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    75
import static org.testng.Assert.assertEquals;
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    76
import static org.testng.Assert.assertTrue;
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    77
import static org.testng.Assert.fail;
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    78
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    79
public class AsFileDownloadTest {
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    80
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    81
    SSLContext sslContext;
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    82
    HttpServer httpTestServer;         // HTTP/1.1    [ 4 servers ]
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    83
    HttpsServer httpsTestServer;       // HTTPS/1.1
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    84
    Http2TestServer http2TestServer;   // HTTP/2 ( h2c )
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    85
    Http2TestServer https2TestServer;  // HTTP/2 ( h2  )
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    86
    String httpURI;
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    87
    String httpsURI;
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    88
    String http2URI;
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    89
    String https2URI;
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    90
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    91
    Path tempDir;
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    92
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    93
    static final String[][] contentDispositionValues = new String[][] {
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    94
          // URI query     Content-Type header value         Expected filename
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    95
            { "001", "Attachment; filename=example001.html", "example001.html" },
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    96
            { "002", "attachment; filename=example002.html", "example002.html" },
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    97
            { "003", "ATTACHMENT; filename=example003.html", "example003.html" },
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    98
            { "004", "attAChment; filename=example004.html", "example004.html" },
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
    99
            { "005", "attachmeNt; filename=example005.html", "example005.html" },
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   100
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   101
            { "006", "attachment; Filename=example006.html", "example006.html" },
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   102
            { "007", "attachment; FILENAME=example007.html", "example007.html" },
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   103
            { "008", "attachment; fileName=example008.html", "example008.html" },
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   104
            { "009", "attachment; fIlEnAMe=example009.html", "example009.html" },
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   105
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   106
            { "010", "attachment; filename=Example010.html", "Example010.html" },
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   107
            { "011", "attachment; filename=EXAMPLE011.html", "EXAMPLE011.html" },
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   108
            { "012", "attachment; filename=eXample012.html", "eXample012.html" },
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   109
            { "013", "attachment; filename=example013.HTML", "example013.HTML" },
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   110
            { "014", "attachment; filename  =eXaMpLe014.HtMl", "eXaMpLe014.HtMl"},
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   111
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   112
            { "015", "attachment; filename=a",               "a"               },
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   113
            { "016", "attachment; filename= b",              "b"               },
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   114
            { "017", "attachment; filename=  c",             "c"               },
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   115
            { "018", "attachment; filename=    d",           "d"               },
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   116
            { "019", "attachment; filename=e  ; filename*=utf-8''eee.txt",  "e"},
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   117
            { "020", "attachment; filename*=utf-8''fff.txt; filename=f",    "f"},
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   118
            { "021", "attachment;  filename=g",              "g"               },
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   119
            { "022", "attachment;   filename= h",            "h"               },
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   120
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   121
            { "023", "attachment; filename=\"space name\"",                       "space name" },
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   122
            { "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
   123
            { "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
   124
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   125
            { "030", "attachment; filename=foo/file1.txt",        "file1.txt" },
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   126
            { "031", "attachment; filename=foo/bar/file2.txt",    "file2.txt" },
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   127
            { "032", "attachment; filename=baz\\file3.txt",       "file3.txt" },
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   128
            { "033", "attachment; filename=baz\\bar\\file4.txt",  "file4.txt" },
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   129
            { "034", "attachment; filename=x/y\\file5.txt",       "file5.txt" },
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   130
            { "035", "attachment; filename=x/y\\file6.txt",       "file6.txt" },
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   131
            { "036", "attachment; filename=x/y\\z/file7.txt",     "file7.txt" },
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   132
            { "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
   133
            { "038", "attachment; filename=/root/file9.txt",      "file9.txt" },
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   134
            { "039", "attachment; filename=../file10.txt",        "file10.txt" },
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   135
            { "040", "attachment; filename=..\\file11.txt",       "file11.txt" },
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   136
            { "041", "attachment; filename=foo/../../file12.txt", "file12.txt" },
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   137
    };
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   138
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   139
    @DataProvider(name = "positive")
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   140
    public Object[][] positive() {
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   141
        List<Object[]> list = new ArrayList<>();
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
        Arrays.asList(contentDispositionValues).stream()
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   144
                .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
   145
                .forEach(list::add);
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   146
        Arrays.asList(contentDispositionValues).stream()
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   147
                .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
   148
                .forEach(list::add);
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   149
        Arrays.asList(contentDispositionValues).stream()
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   150
                .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
   151
                .forEach(list::add);
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   152
        Arrays.asList(contentDispositionValues).stream()
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   153
                .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
   154
                .forEach(list::add);
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   155
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   156
        return list.stream().toArray(Object[][]::new);
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   157
    }
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   158
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   159
    @Test(dataProvider = "positive")
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   160
    void test(String uriString, String contentDispositionValue, String expectedFilename)
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   161
        throws Exception
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   162
    {
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
   163
        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
   164
        HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   165
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   166
        URI uri = URI.create(uriString);
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   167
        HttpRequest request = HttpRequest.newBuilder(uri)
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   168
                .POST(fromString("May the luck of the Irish be with you!"))
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   169
                .build();
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   170
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   171
        BodyHandler bh = asFileDownload(tempDir.resolve(uri.getPath().substring(1)),
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   172
                                        CREATE, TRUNCATE_EXISTING, WRITE);
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   173
        HttpResponse<Path> response = client.send(request, bh);
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   174
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   175
        out.println("Got response: " + response);
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   176
        out.println("Got body Path: " + response.body());
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   177
        String fileContents = new String(Files.readAllBytes(response.body()), UTF_8);
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   178
        out.println("Got body: " + fileContents);
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   179
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   180
        assertEquals(response.statusCode(),200);
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   181
        assertEquals(response.body().getFileName().toString(), expectedFilename);
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   182
        assertTrue(response.headers().firstValue("Content-Disposition").isPresent());
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   183
        assertEquals(response.headers().firstValue("Content-Disposition").get(),
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   184
                     contentDispositionValue);
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   185
        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
   186
    }
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   187
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   188
    // --- Negative
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   189
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   190
    static final String[][] contentDispositionBADValues = new String[][] {
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   191
            // URI query     Content-Type header value
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   192
            { "100", ""                                    },  // empty
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   193
            { "101", "filename=example.html"               },  // no attachment
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   194
            { "102", "attachment; filename=space name"     },  // unquoted with space
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   195
            { "103", "attachment; filename="               },  // empty filename param
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   196
            { "104", "attachment; filename=\""             },  // single quote
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   197
            { "105", "attachment; filename=\"\""           },  // empty quoted
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   198
            { "106", "attachment; filename=."              },  // dot
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   199
            { "107", "attachment; filename=.."             },  // dot dot
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   200
            { "108", "attachment; filename=\".."           },  // badly quoted dot dot
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   201
            { "109", "attachment; filename=\"..\""         },  // quoted dot dot
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   202
            { "110", "attachment; filename=\"bad"          },  // badly quoted
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   203
            { "111", "attachment; filename=\"bad;"         },  // badly quoted with ';'
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   204
            { "112", "attachment; filename=\"bad ;"        },  // badly quoted with ' ;'
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   205
            { "113", "attachment; filename*=utf-8''xx.txt "},  // no "filename" param
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   206
    };
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   207
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   208
    @DataProvider(name = "negative")
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   209
    public Object[][] negative() {
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   210
        List<Object[]> list = new ArrayList<>();
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
        Arrays.asList(contentDispositionBADValues).stream()
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   213
                .map(e -> new Object[] {httpURI +  "?" + e[0], e[1]})
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   214
                .forEach(list::add);
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   215
        Arrays.asList(contentDispositionBADValues).stream()
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   216
                .map(e -> new Object[] {httpsURI +  "?" + e[0], e[1]})
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   217
                .forEach(list::add);
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   218
        Arrays.asList(contentDispositionBADValues).stream()
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   219
                .map(e -> new Object[] {http2URI +  "?" + e[0], e[1]})
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   220
                .forEach(list::add);
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   221
        Arrays.asList(contentDispositionBADValues).stream()
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   222
                .map(e -> new Object[] {https2URI +  "?" + e[0], e[1]})
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   223
                .forEach(list::add);
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   224
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   225
        return list.stream().toArray(Object[][]::new);
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   226
    }
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   227
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   228
    @Test(dataProvider = "negative")
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   229
    void negativeTest(String uriString, String contentDispositionValue)
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   230
            throws Exception
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   231
    {
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
   232
        out.printf("negativeTest(%s, %s): starting", uriString, contentDispositionValue);
56132
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   233
        HttpClient client = HttpClient.newBuilder().sslContext(sslContext).build();
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   234
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   235
        URI uri = URI.create(uriString);
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   236
        HttpRequest request = HttpRequest.newBuilder(uri)
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   237
                .POST(fromString("Does not matter"))
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   238
                .build();
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   239
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   240
        BodyHandler bh = asFileDownload(tempDir, CREATE, TRUNCATE_EXISTING, WRITE);
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   241
        try {
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   242
            HttpResponse<Path> response = client.send(request, bh);
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   243
            fail("UNEXPECTED response: " + response + ", path:" + response.body());
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   244
        } catch (UncheckedIOException | IOException ioe) {
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   245
            System.out.println("Caught expected: " + ioe);
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   246
        }
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   247
    }
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   248
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   249
    // -- Infrastructure
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
    @BeforeTest
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   252
    public void setup() throws Exception {
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   253
        tempDir = Paths.get("asFileDownloadTest.tmp.dir");
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   254
        FileUtils.deleteFileIfExistsWithRetry(tempDir);
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   255
        Files.createDirectory(tempDir);
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   256
        // Unique dirs per test run, based on the URI path
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   257
        Files.createDirectories(tempDir.resolve("http1/afdt/"));
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   258
        Files.createDirectories(tempDir.resolve("https1/afdt/"));
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   259
        Files.createDirectories(tempDir.resolve("http2/afdt/"));
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   260
        Files.createDirectories(tempDir.resolve("https2/afdt/"));
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   261
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   262
        sslContext = new SimpleSSLContext().get();
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   263
        if (sslContext == null)
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   264
            throw new AssertionError("Unexpected null sslContext");
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   265
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   266
        InetSocketAddress sa = new InetSocketAddress(0);
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   267
        httpTestServer = HttpServer.create(sa, 0);
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   268
        httpTestServer.createContext("/http1/afdt", new Http1FileDispoHandler());
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   269
        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
   270
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   271
        httpsTestServer = HttpsServer.create(sa, 0);
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   272
        httpsTestServer.setHttpsConfigurator(new HttpsConfigurator(sslContext));
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   273
        httpsTestServer.createContext("/https1/afdt", new Http1FileDispoHandler());
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   274
        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
   275
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   276
        http2TestServer = new Http2TestServer("127.0.0.1", false, 0);
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   277
        http2TestServer.addHandler(new Http2FileDispoHandler(), "/http2/afdt");
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   278
        int port = http2TestServer.getAddress().getPort();
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   279
        http2URI = "http://127.0.0.1:" + port + "/http2/afdt";
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   280
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   281
        https2TestServer = new Http2TestServer("127.0.0.1", true, 0);
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   282
        https2TestServer.addHandler(new Http2FileDispoHandler(), "/https2/afdt");
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   283
        port = https2TestServer.getAddress().getPort();
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   284
        https2URI = "https://127.0.0.1:" + port + "/https2/afdt";
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   285
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   286
        httpTestServer.start();
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   287
        httpsTestServer.start();
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   288
        http2TestServer.start();
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   289
        https2TestServer.start();
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   290
    }
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   291
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   292
    @AfterTest
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   293
    public void teardown() throws Exception {
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   294
        httpTestServer.stop(0);
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   295
        httpsTestServer.stop(0);
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   296
        http2TestServer.stop();
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   297
        https2TestServer.stop();
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
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   300
    static String contentDispositionValueFromURI(URI uri) {
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   301
        String queryIndex = uri.getQuery();
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   302
        String[][] values;
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   303
        if (queryIndex.startsWith("0"))  // positive tests start with '0'
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   304
            values = contentDispositionValues;
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   305
        else if (queryIndex.startsWith("1"))  // negative tests start with '1'
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   306
            values = contentDispositionBADValues;
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   307
        else
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   308
            throw new AssertionError("SERVER: UNEXPECTED query:" + queryIndex);
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   309
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   310
        return Arrays.asList(values).stream()
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   311
                .filter(e -> e[0].equals(queryIndex))
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   312
                .map(e -> e[1])
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   313
                .findFirst()
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   314
                .orElseThrow();
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   315
    }
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
    static class Http1FileDispoHandler implements HttpHandler {
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   318
        @Override
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   319
        public void handle(HttpExchange t) throws IOException {
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   320
            try (InputStream is = t.getRequestBody();
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   321
                 OutputStream os = t.getResponseBody()) {
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   322
                byte[] bytes = is.readAllBytes();
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   323
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   324
                t.getResponseHeaders().set("Content-Disposition",
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   325
                        contentDispositionValueFromURI(t.getRequestURI()));
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   326
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   327
                t.sendResponseHeaders(200, bytes.length);
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   328
                os.write(bytes);
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   329
            }
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   330
        }
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   331
    }
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   332
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   333
    static class Http2FileDispoHandler implements Http2Handler {
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   334
        @Override
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   335
        public void handle(Http2TestExchange t) throws IOException {
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   336
            try (InputStream is = t.getRequestBody();
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   337
                 OutputStream os = t.getResponseBody()) {
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   338
                byte[] bytes = is.readAllBytes();
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   339
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   340
                t.getResponseHeaders().addHeader("Content-Disposition",
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   341
                        contentDispositionValueFromURI(t.getRequestURI()));
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   342
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   343
                t.sendResponseHeaders(200, bytes.length);
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   344
                os.write(bytes);
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   345
            }
c8a1eccbc719 http-client-branch: fix asFileDownload handler and add test
chegar
parents:
diff changeset
   346
        }
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
}