test/jdk/java/net/httpclient/LineStreamsAndSurrogatesTest.java
author jboes
Fri, 08 Nov 2019 11:15:16 +0000
changeset 59029 3786a0962570
parent 57880 ff08db52ad92
permissions -rw-r--r--
8232853: AuthenticationFilter.Cache::remove may throw ConcurrentModificationException Summary: Change implementation to use iterator instead of plain LinkedList Reviewed-by: dfuchs, vtewari
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     1
/*
57880
ff08db52ad92 8230000: some httpclients testng tests run zero test
dfuchs
parents: 49765
diff changeset
     2
 * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     4
 *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     7
 * published by the Free Software Foundation.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     8
 *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    13
 * accompanied this code).
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    14
 *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    18
 *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    21
 * questions.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    22
 */
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    23
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    24
import java.io.BufferedReader;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    25
import java.io.ByteArrayInputStream;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    26
import java.io.InputStreamReader;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    27
import java.io.StringReader;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    28
import java.net.http.HttpResponse.BodySubscriber;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    29
import java.net.http.HttpResponse.BodySubscribers;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    30
import java.nio.ByteBuffer;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    31
import java.nio.charset.Charset;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    32
import java.nio.charset.MalformedInputException;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    33
import java.util.Arrays;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    34
import java.util.List;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    35
import java.util.concurrent.ExecutionException;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    36
import java.util.concurrent.SubmissionPublisher;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    37
import java.util.concurrent.atomic.AtomicReference;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    38
import java.util.stream.Collectors;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    39
import java.util.stream.Stream;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    40
import org.testng.annotations.Test;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    41
import static java.nio.charset.StandardCharsets.UTF_8;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    42
import static java.nio.charset.StandardCharsets.UTF_16;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    43
import static org.testng.Assert.assertEquals;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    44
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    45
/*
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    46
 * @test
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    47
 * @summary tests for BodySubscribers returned by asLines.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    48
 *       In particular tests that surrogate characters are handled
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    49
 *       correctly.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    50
 * @modules java.net.http java.logging
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    51
 * @run testng/othervm LineStreamsAndSurrogatesTest
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    52
 */
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    53
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    54
public class LineStreamsAndSurrogatesTest {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    55
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    56
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    57
    static final Class<NullPointerException> NPE = NullPointerException.class;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    58
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    59
    private static final List<String> lines(String text) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    60
        return new BufferedReader(new StringReader(text)).lines().collect(Collectors.toList());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    61
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    62
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    63
    @Test
57880
ff08db52ad92 8230000: some httpclients testng tests run zero test
dfuchs
parents: 49765
diff changeset
    64
    public void testUncomplete() throws Exception {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    65
        // Uses U+10400 which is encoded as the surrogate pair U+D801 U+DC00
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    66
        String text = "Bient\u00f4t\r\n nous plongerons\r\n dans\r les\n\n" +
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    67
                " fr\u00f4\ud801\udc00des\r\n t\u00e9n\u00e8bres\ud801\udc00";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    68
        Charset charset = UTF_8;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    69
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    70
        BodySubscriber<Stream<String>> bodySubscriber = BodySubscribers.ofLines(charset);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    71
        AtomicReference<Throwable> errorRef = new AtomicReference<>();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    72
        Runnable run = () -> {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    73
            try {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    74
                SubmissionPublisher<List<ByteBuffer>> publisher = new SubmissionPublisher<>();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    75
                byte[] sbytes = text.getBytes(charset);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    76
                byte[] bytes = Arrays.copyOfRange(sbytes, 0, sbytes.length - 1);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    77
                publisher.subscribe(bodySubscriber);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    78
                System.out.println("Publishing " + bytes.length + " bytes");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    79
                for (int i = 0; i < bytes.length; i++) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    80
                    // ensure that surrogates are split over several buffers.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    81
                    publisher.submit(List.of(ByteBuffer.wrap(bytes, i, 1)));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    82
                }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    83
                publisher.close();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    84
            } catch(Throwable t) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    85
                errorRef.set(t);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    86
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    87
        };
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    88
        Thread thread = new Thread(run,"Publishing");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    89
        thread.start();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    90
        try {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    91
            Stream<String> stream = bodySubscriber.getBody().toCompletableFuture().get();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    92
            List<String> list = stream.collect(Collectors.toList());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    93
            String resp = list.stream().collect(Collectors.joining(""));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    94
            System.out.println("***** Got: " + resp);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    95
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    96
            byte[] sbytes = text.getBytes(UTF_8);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    97
            byte[] bytes = Arrays.copyOfRange(sbytes, 0, sbytes.length - 1);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    98
            ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    99
            BufferedReader reader = new BufferedReader(new InputStreamReader(bais, charset));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   100
            String resp2 = reader.lines().collect(Collectors.joining(""));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   101
            System.out.println("***** Got2: " + resp2);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   102
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   103
            assertEquals(resp, resp2);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   104
            assertEquals(list, List.of("Bient\u00f4t",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   105
                                       " nous plongerons",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   106
                                       " dans",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   107
                                       " les",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   108
                                       "",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   109
                                       " fr\u00f4\ud801\udc00des",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   110
                                       " t\u00e9n\u00e8bres\ufffd"));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   111
        } catch (ExecutionException x) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   112
            Throwable cause = x.getCause();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   113
            if (cause instanceof MalformedInputException) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   114
                throw new RuntimeException("Unexpected MalformedInputException", cause);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   115
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   116
            throw x;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   117
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   118
        if (errorRef.get() != null) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   119
            throw new RuntimeException("Unexpected exception", errorRef.get());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   120
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   121
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   122
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   123
    @Test
57880
ff08db52ad92 8230000: some httpclients testng tests run zero test
dfuchs
parents: 49765
diff changeset
   124
    public void testStream1() throws Exception {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   125
        // Uses U+10400 which is encoded as the surrogate pair U+D801 U+DC00
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   126
        String text = "Bient\u00f4t\r\n nous plongerons\r\n dans\r\r les\n\n" +
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   127
                " fr\u00f4\ud801\udc00des\r\n t\u00e9n\u00e8bres";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   128
        Charset charset = UTF_8;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   129
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   130
        BodySubscriber<Stream<String>> bodySubscriber = BodySubscribers.ofLines(charset);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   131
        SubmissionPublisher<List<ByteBuffer>> publisher = new SubmissionPublisher<>();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   132
        byte[] bytes = text.getBytes(charset);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   133
        AtomicReference<Throwable> errorRef = new AtomicReference<>();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   134
        Runnable run = () -> {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   135
            try {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   136
                publisher.subscribe(bodySubscriber);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   137
                System.out.println("Publishing " + bytes.length + " bytes");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   138
                for (int i = 0; i < bytes.length; i++) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   139
                    // ensure that surrogates are split over several buffers.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   140
                    publisher.submit(List.of(ByteBuffer.wrap(bytes, i, 1)));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   141
                }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   142
                publisher.close();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   143
            } catch(Throwable t) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   144
                errorRef.set(t);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   145
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   146
        };
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   147
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   148
        Stream<String> stream = bodySubscriber.getBody().toCompletableFuture().get();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   149
        Thread thread = new Thread(run,"Publishing");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   150
        thread.start();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   151
        List<String> list = stream.collect(Collectors.toList());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   152
        String resp = list.stream().collect(Collectors.joining("|"));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   153
        System.out.println("***** Got: " + resp);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   154
        assertEquals(resp, text.replace("\r\n", "|")
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   155
                               .replace("\n","|")
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   156
                               .replace("\r","|"));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   157
        assertEquals(list, List.of("Bient\u00f4t",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   158
                " nous plongerons",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   159
                " dans",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   160
                "",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   161
                " les",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   162
                "",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   163
                " fr\u00f4\ud801\udc00des",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   164
                " t\u00e9n\u00e8bres"));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   165
        assertEquals(list, lines(text));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   166
        if (errorRef.get() != null) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   167
            throw new RuntimeException("Unexpected exception", errorRef.get());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   168
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   169
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   170
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   171
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   172
    @Test
57880
ff08db52ad92 8230000: some httpclients testng tests run zero test
dfuchs
parents: 49765
diff changeset
   173
    public void testStream2() throws Exception {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   174
        String text = "Bient\u00f4t\r\n nous plongerons\r\n dans\r\r" +
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   175
                " les fr\u00f4\ud801\udc00des\r\n t\u00e9n\u00e8bres\r\r";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   176
        Charset charset = UTF_8;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   177
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   178
        BodySubscriber<Stream<String>> bodySubscriber = BodySubscribers.ofLines(charset);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   179
        SubmissionPublisher<List<ByteBuffer>> publisher = new SubmissionPublisher<>();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   180
        byte[] bytes = text.getBytes(charset);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   181
        AtomicReference<Throwable> errorRef = new AtomicReference<>();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   182
        Runnable run = () -> {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   183
            try {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   184
                publisher.subscribe(bodySubscriber);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   185
                System.out.println("Publishing " + bytes.length + " bytes");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   186
                for (int i = 0; i < bytes.length; i++) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   187
                    // ensure that surrogates are split over several buffers.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   188
                    publisher.submit(List.of(ByteBuffer.wrap(bytes, i, 1)));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   189
                }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   190
                publisher.close();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   191
            } catch(Throwable t) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   192
                errorRef.set(t);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   193
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   194
        };
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   195
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   196
        Stream<String> stream = bodySubscriber.getBody().toCompletableFuture().get();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   197
        Thread thread = new Thread(run,"Publishing");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   198
        thread.start();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   199
        List<String> list = stream.collect(Collectors.toList());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   200
        String resp = list.stream().collect(Collectors.joining(""));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   201
        System.out.println("***** Got: " + resp);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   202
        String expected = Stream.of(text.split("\r\n|\r|\n"))
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   203
                .collect(Collectors.joining(""));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   204
        assertEquals(resp, expected);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   205
        assertEquals(list, List.of("Bient\u00f4t",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   206
                " nous plongerons",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   207
                " dans",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   208
                "",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   209
                " les fr\u00f4\ud801\udc00des",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   210
                " t\u00e9n\u00e8bres",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   211
                ""));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   212
        assertEquals(list, lines(text));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   213
        if (errorRef.get() != null) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   214
            throw new RuntimeException("Unexpected exception", errorRef.get());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   215
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   216
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   217
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   218
    @Test
57880
ff08db52ad92 8230000: some httpclients testng tests run zero test
dfuchs
parents: 49765
diff changeset
   219
    public void testStream3_UTF16() throws Exception {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   220
        // Uses U+10400 which is encoded as the surrogate pair U+D801 U+DC00
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   221
        String text = "Bient\u00f4t\r\n nous plongerons\r\n dans\r\r" +
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   222
                " les\n\n fr\u00f4\ud801\udc00des\r\n t\u00e9n\u00e8bres";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   223
        Charset charset = UTF_16;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   224
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   225
        BodySubscriber<Stream<String>> bodySubscriber = BodySubscribers.ofLines(charset);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   226
        SubmissionPublisher<List<ByteBuffer>> publisher = new SubmissionPublisher<>();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   227
        byte[] bytes = text.getBytes(charset);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   228
        AtomicReference<Throwable> errorRef = new AtomicReference<>();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   229
        Runnable run = () -> {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   230
            try {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   231
                publisher.subscribe(bodySubscriber);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   232
                System.out.println("Publishing " + bytes.length + " bytes");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   233
                for (int i = 0; i < bytes.length; i++) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   234
                    // ensure that surrogates are split over several buffers.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   235
                    publisher.submit(List.of(ByteBuffer.wrap(bytes, i, 1)));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   236
                }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   237
                publisher.close();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   238
            } catch(Throwable t) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   239
                errorRef.set(t);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   240
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   241
        };
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   242
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   243
        Stream<String> stream = bodySubscriber.getBody().toCompletableFuture().get();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   244
        Thread thread = new Thread(run,"Publishing");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   245
        thread.start();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   246
        List<String> list = stream.collect(Collectors.toList());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   247
        String resp = list.stream().collect(Collectors.joining(""));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   248
        System.out.println("***** Got: " + resp);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   249
        assertEquals(resp, text.replace("\n","").replace("\r",""));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   250
        assertEquals(list, List.of("Bient\u00f4t",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   251
                " nous plongerons",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   252
                " dans",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   253
                "",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   254
                " les",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   255
                "",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   256
                " fr\u00f4\ud801\udc00des",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   257
                " t\u00e9n\u00e8bres"));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   258
        assertEquals(list, lines(text));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   259
        if (errorRef.get() != null) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   260
            throw new RuntimeException("Unexpected exception", errorRef.get());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   261
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   262
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   263
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   264
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   265
    @Test
57880
ff08db52ad92 8230000: some httpclients testng tests run zero test
dfuchs
parents: 49765
diff changeset
   266
    public void testStream4_UTF16() throws Exception {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   267
        String text = "Bient\u00f4t\r\n nous plongerons\r\n dans\r\r" +
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   268
                " les fr\u00f4\ud801\udc00des\r\n t\u00e9n\u00e8bres\r\r";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   269
        Charset charset = UTF_16;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   270
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   271
        BodySubscriber<Stream<String>> bodySubscriber = BodySubscribers.ofLines(charset);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   272
        SubmissionPublisher<List<ByteBuffer>> publisher = new SubmissionPublisher<>();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   273
        byte[] bytes = text.getBytes(charset);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   274
        AtomicReference<Throwable> errorRef = new AtomicReference<>();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   275
        Runnable run = () -> {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   276
            try {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   277
                publisher.subscribe(bodySubscriber);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   278
                System.out.println("Publishing " + bytes.length + " bytes");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   279
                for (int i = 0; i < bytes.length; i++) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   280
                    // ensure that surrogates are split over several buffers.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   281
                    publisher.submit(List.of(ByteBuffer.wrap(bytes, i, 1)));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   282
                }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   283
                publisher.close();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   284
            } catch(Throwable t) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   285
                errorRef.set(t);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   286
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   287
        };
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   288
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   289
        Stream<String> stream = bodySubscriber.getBody().toCompletableFuture().get();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   290
        Thread thread = new Thread(run,"Publishing");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   291
        thread.start();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   292
        List<String> list = stream.collect(Collectors.toList());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   293
        String resp = list.stream().collect(Collectors.joining(""));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   294
        System.out.println("***** Got: " + resp);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   295
        String expected = Stream.of(text.split("\r\n|\r|\n"))
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   296
                .collect(Collectors.joining(""));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   297
        assertEquals(resp, expected);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   298
        assertEquals(list, List.of("Bient\u00f4t",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   299
                " nous plongerons",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   300
                " dans",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   301
                "",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   302
                " les fr\u00f4\ud801\udc00des",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   303
                " t\u00e9n\u00e8bres",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   304
                ""));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   305
        assertEquals(list, lines(text));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   306
        if (errorRef.get() != null) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   307
            throw new RuntimeException("Unexpected exception", errorRef.get());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   308
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   309
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   310
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   311
}