test/jdk/java/net/httpclient/LineSubscribersAndSurrogatesTest.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.ByteArrayOutputStream;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    27
import java.io.IOException;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    28
import java.io.InputStreamReader;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    29
import java.io.StringReader;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    30
import java.io.UncheckedIOException;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    31
import java.net.http.HttpResponse.BodySubscriber;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    32
import java.net.http.HttpResponse.BodySubscribers;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    33
import java.nio.ByteBuffer;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    34
import java.nio.charset.MalformedInputException;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    35
import java.util.ArrayList;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    36
import java.util.Arrays;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    37
import java.util.List;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    38
import java.util.concurrent.CopyOnWriteArrayList;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    39
import java.util.concurrent.ExecutionException;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    40
import java.util.concurrent.Flow;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    41
import java.util.concurrent.SubmissionPublisher;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    42
import java.util.function.Supplier;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    43
import java.util.stream.Collectors;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    44
import java.util.stream.Stream;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    45
import org.testng.annotations.Test;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    46
import static java.nio.charset.StandardCharsets.UTF_8;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    47
import static java.nio.charset.StandardCharsets.UTF_16;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    48
import static org.testng.Assert.assertEquals;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    49
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    50
/*
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    51
 * @test
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    52
 * @summary tests for BodySubscribers returned by fromLineSubscriber.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    53
 *       In particular tests that surrogate characters are handled
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    54
 *       correctly.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    55
 * @modules java.net.http java.logging
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    56
 * @run testng/othervm LineSubscribersAndSurrogatesTest
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    57
 */
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    58
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    59
public class LineSubscribersAndSurrogatesTest {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    60
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    61
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    62
    static final Class<NullPointerException> NPE = NullPointerException.class;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    63
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    64
    private static final List<String> lines(String text, String eol) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    65
        if (eol == null) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    66
            return new BufferedReader(new StringReader(text)).lines().collect(Collectors.toList());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    67
        } else {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    68
            String replaced = text.replace(eol, "|");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    69
            int i=0;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    70
            while(replaced.endsWith("||")) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    71
                replaced = replaced.substring(0,replaced.length()-1);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    72
                i++;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    73
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    74
            List<String> res = List.of(replaced.split("\\|"));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    75
            if (i > 0) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    76
                res = new ArrayList<>(res);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    77
                for (int j=0; j<i; j++) res.add("");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    78
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    79
            return res;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    80
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    81
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    82
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    83
    @Test
57880
ff08db52ad92 8230000: some httpclients testng tests run zero test
dfuchs
parents: 49765
diff changeset
    84
    public void testIncomplete() throws Exception {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    85
        // Uses U+10400 which is encoded as the surrogate pair U+D801 U+DC00
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    86
        String text = "Bient\u00f4t\r\n nous plongerons\r\n dans\r" +
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    87
                " les\n\n fr\u00f4\ud801\udc00des\r\n t\u00e9n\u00e8bres\ud801\udc00";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    88
        ObjectSubscriber subscriber = new ObjectSubscriber();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    89
        BodySubscriber<String> bodySubscriber = BodySubscribers.fromLineSubscriber(
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    90
                subscriber, Supplier::get, UTF_8, null);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    91
        SubmissionPublisher<List<ByteBuffer>> publisher = new SubmissionPublisher<>();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    92
        byte[] sbytes = text.getBytes(UTF_8);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    93
        byte[] bytes = Arrays.copyOfRange(sbytes,0, sbytes.length - 1);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    94
        publisher.subscribe(bodySubscriber);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    95
        System.out.println("Publishing " + bytes.length + " bytes");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    96
        for (int i=0; i<bytes.length; i++) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    97
            // ensure that surrogates are split over several buffers.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    98
            publisher.submit(List.of(ByteBuffer.wrap(bytes, i, 1)));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    99
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   100
        publisher.close();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   101
        try {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   102
            String resp = bodySubscriber.getBody().toCompletableFuture().get();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   103
            System.out.println("***** Got: " + resp);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   104
            ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   105
            BufferedReader reader = new BufferedReader(new InputStreamReader(bais, UTF_8));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   106
            String resp2 = reader.lines().collect(Collectors.joining(""));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   107
            assertEquals(resp, resp2);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   108
            assertEquals(subscriber.list, List.of("Bient\u00f4t",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   109
                    " nous plongerons",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   110
                    " dans",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   111
                    " les",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   112
                    "",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   113
                    " fr\u00f4\ud801\udc00des",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   114
                    " t\u00e9n\u00e8bres\ufffd"));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   115
        } catch (ExecutionException x) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   116
            Throwable cause = x.getCause();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   117
            if (cause instanceof MalformedInputException) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   118
                throw new RuntimeException("Unexpected MalformedInputException thrown", cause);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   119
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   120
            throw x;
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
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   124
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   125
    @Test
57880
ff08db52ad92 8230000: some httpclients testng tests run zero test
dfuchs
parents: 49765
diff changeset
   126
    public void testStringWithFinisherLF() throws Exception {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   127
        // Uses U+10400 which is encoded as the surrogate pair U+D801 U+DC00
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   128
        String text = "Bient\u00f4t\r\n nous plongerons\r\n dans\r" +
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   129
                " les\n\n fr\u00f4\ud801\udc00des\r\n t\u00e9n\u00e8bres\r";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   130
        ObjectSubscriber subscriber = new ObjectSubscriber();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   131
        BodySubscriber<String> bodySubscriber = BodySubscribers.fromLineSubscriber(
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   132
                subscriber, Supplier::get, UTF_8, "\n");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   133
        SubmissionPublisher<List<ByteBuffer>> publisher = new SubmissionPublisher<>();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   134
        byte[] bytes = text.getBytes(UTF_8);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   135
        publisher.subscribe(bodySubscriber);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   136
        System.out.println("Publishing " + bytes.length + " bytes");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   137
        for (int i=0; i<bytes.length; i++) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   138
            // ensure that surrogates are split over several buffers.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   139
            publisher.submit(List.of(ByteBuffer.wrap(bytes, i, 1)));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   140
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   141
        publisher.close();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   142
        String resp = bodySubscriber.getBody().toCompletableFuture().get();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   143
        System.out.println("***** Got: " + resp);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   144
        List<String> expected = List.of("Bient\u00f4t\r",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   145
                " nous plongerons\r",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   146
                " dans\r les",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   147
                "",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   148
                " fr\u00f4\ud801\udc00des\r",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   149
                " t\u00e9n\u00e8bres\r");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   150
        assertEquals(subscriber.list, expected);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   151
        assertEquals(resp, Stream.of(text.split("\n")).collect(Collectors.joining("")));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   152
        assertEquals(resp, expected.stream().collect(Collectors.joining("")));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   153
        assertEquals(subscriber.list, lines(text, "\n"));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   154
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   155
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   156
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   157
    @Test
57880
ff08db52ad92 8230000: some httpclients testng tests run zero test
dfuchs
parents: 49765
diff changeset
   158
    public void testStringWithFinisherCR() throws Exception {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   159
        String text = "Bient\u00f4t\r\n nous plongerons\r\n dans\r" +
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   160
                " les fr\u00f4\ud801\udc00des\r\n t\u00e9n\u00e8bres\r\r";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   161
        ObjectSubscriber subscriber = new ObjectSubscriber();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   162
        BodySubscriber<String> bodySubscriber = BodySubscribers.fromLineSubscriber(
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   163
                subscriber, Supplier::get, UTF_8, "\r");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   164
        SubmissionPublisher<List<ByteBuffer>> publisher = new SubmissionPublisher<>();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   165
        byte[] bytes = text.getBytes(UTF_8);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   166
        publisher.subscribe(bodySubscriber);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   167
        System.out.println("Publishing " + bytes.length + " bytes");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   168
        for (int i=0; i<bytes.length; i++) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   169
            // ensure that surrogates are split over several buffers.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   170
            publisher.submit(List.of(ByteBuffer.wrap(bytes, i, 1)));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   171
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   172
        publisher.close();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   173
        String resp = bodySubscriber.getBody().toCompletableFuture().get();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   174
        System.out.println("***** Got: " + resp);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   175
        assertEquals(resp, text.replace("\r", ""));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   176
        assertEquals(subscriber.list, List.of("Bient\u00f4t",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   177
                "\n nous plongerons",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   178
                "\n dans",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   179
                " les fr\u00f4\ud801\udc00des",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   180
                "\n t\u00e9n\u00e8bres",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   181
                ""));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   182
        assertEquals(subscriber.list, lines(text, "\r"));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   183
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   184
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   185
    @Test
57880
ff08db52ad92 8230000: some httpclients testng tests run zero test
dfuchs
parents: 49765
diff changeset
   186
    public void testStringWithFinisherCRLF() throws Exception {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   187
        String text = "Bient\u00f4t\r\n nous plongerons\r\n dans\r" +
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   188
                " les fr\u00f4\ud801\udc00des\r\n t\u00e9n\u00e8bres";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   189
        ObjectSubscriber subscriber = new ObjectSubscriber();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   190
        BodySubscriber<String> bodySubscriber = BodySubscribers.fromLineSubscriber(
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   191
                subscriber, Supplier::get, UTF_8, "\r\n");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   192
        SubmissionPublisher<List<ByteBuffer>> publisher = new SubmissionPublisher<>();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   193
        byte[] bytes = text.getBytes(UTF_8);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   194
        publisher.subscribe(bodySubscriber);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   195
        System.out.println("Publishing " + bytes.length + " bytes");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   196
        for (int i=0; i<bytes.length; i++) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   197
            // ensure that surrogates are split over several buffers.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   198
            publisher.submit(List.of(ByteBuffer.wrap(bytes, i, 1)));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   199
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   200
        publisher.close();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   201
        String resp = bodySubscriber.getBody().toCompletableFuture().get();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   202
        System.out.println("***** Got: " + resp);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   203
        assertEquals(resp, text.replace("\r\n",""));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   204
        assertEquals(subscriber.list, List.of("Bient\u00f4t",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   205
                " nous plongerons",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   206
                " dans\r les fr\u00f4\ud801\udc00des",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   207
                " t\u00e9n\u00e8bres"));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   208
        assertEquals(subscriber.list, lines(text, "\r\n"));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   209
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   210
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   211
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   212
    @Test
57880
ff08db52ad92 8230000: some httpclients testng tests run zero test
dfuchs
parents: 49765
diff changeset
   213
    public void testStringWithFinisherBR() throws Exception {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   214
        String text = "Bient\u00f4t\r\n nous plongerons\r\n dans\r" +
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   215
                " les\r\r fr\u00f4\ud801\udc00des\r\n t\u00e9n\u00e8bres";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   216
        ObjectSubscriber subscriber = new ObjectSubscriber();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   217
        BodySubscriber<String> bodySubscriber = BodySubscribers.fromLineSubscriber(
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   218
                subscriber, Supplier::get, UTF_8, null);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   219
        SubmissionPublisher<List<ByteBuffer>> publisher = new SubmissionPublisher<>();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   220
        byte[] bytes = text.getBytes(UTF_8);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   221
        publisher.subscribe(bodySubscriber);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   222
        System.out.println("Publishing " + bytes.length + " bytes");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   223
        for (int i=0; i<bytes.length; i++) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   224
            // ensure that surrogates are split over several buffers.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   225
            publisher.submit(List.of(ByteBuffer.wrap(bytes, i, 1)));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   226
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   227
        publisher.close();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   228
        String resp = bodySubscriber.getBody().toCompletableFuture().get();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   229
        System.out.println("***** Got: " + resp);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   230
        List<String> expected = List.of("Bient\u00f4t",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   231
                " nous plongerons",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   232
                " dans",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   233
                " les",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   234
                "",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   235
                " fr\u00f4\ud801\udc00des",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   236
                " t\u00e9n\u00e8bres");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   237
        assertEquals(subscriber.list, expected);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   238
        assertEquals(resp, expected.stream().collect(Collectors.joining("")));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   239
        assertEquals(subscriber.list, lines(text, null));
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
    @Test
57880
ff08db52ad92 8230000: some httpclients testng tests run zero test
dfuchs
parents: 49765
diff changeset
   243
    public void testStringWithFinisherBR_UTF_16() throws Exception {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   244
        String text = "Bient\u00f4t\r\n nous plongerons\r\n dans\r" +
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   245
                " les\r\r fr\u00f4\ud801\udc00des\r\n t\u00e9n\u00e8bres\r\r";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   246
        ObjectSubscriber subscriber = new ObjectSubscriber();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   247
        BodySubscriber<String> bodySubscriber = BodySubscribers.fromLineSubscriber(
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   248
                subscriber, Supplier::get, UTF_16, null);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   249
        SubmissionPublisher<List<ByteBuffer>> publisher = new SubmissionPublisher<>();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   250
        byte[] bytes = text.getBytes(UTF_16);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   251
        publisher.subscribe(bodySubscriber);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   252
        System.out.println("Publishing " + bytes.length + " bytes");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   253
        for (int i=0; i<bytes.length; i++) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   254
            // ensure that surrogates are split over several buffers.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   255
            publisher.submit(List.of(ByteBuffer.wrap(bytes, i, 1)));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   256
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   257
        publisher.close();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   258
        String resp = bodySubscriber.getBody().toCompletableFuture().get();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   259
        System.out.println("***** Got: " + resp);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   260
        List<String> expected = List.of("Bient\u00f4t",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   261
                " nous plongerons",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   262
                " dans",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   263
                " les",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   264
                "",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   265
                " fr\u00f4\ud801\udc00des",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   266
                " t\u00e9n\u00e8bres",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   267
                "");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   268
        assertEquals(resp, expected.stream().collect(Collectors.joining("")));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   269
        assertEquals(subscriber.list, expected);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   270
        assertEquals(subscriber.list, lines(text, null));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   271
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   272
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   273
    void testStringWithoutFinisherBR() throws Exception {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   274
        String text = "Bient\u00f4t\r\n nous plongerons\r\n dans\r" +
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   275
                " les\r\r fr\u00f4\ud801\udc00des\r\n t\u00e9n\u00e8bres";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   276
        ObjectSubscriber subscriber = new ObjectSubscriber();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   277
        BodySubscriber<Void> bodySubscriber = BodySubscribers.fromLineSubscriber(subscriber);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   278
        SubmissionPublisher<List<ByteBuffer>> publisher = new SubmissionPublisher<>();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   279
        byte[] bytes = text.getBytes(UTF_8);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   280
        publisher.subscribe(bodySubscriber);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   281
        System.out.println("Publishing " + bytes.length + " bytes");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   282
        for (int i = 0; i < bytes.length; i++) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   283
            // ensure that surrogates are split over several buffers.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   284
            publisher.submit(List.of(ByteBuffer.wrap(bytes, i, 1)));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   285
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   286
        publisher.close();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   287
        Void resp = bodySubscriber.getBody().toCompletableFuture().get();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   288
        System.out.println("***** Got: " + resp);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   289
        List<String> expected = List.of("Bient\u00f4t",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   290
                " nous plongerons",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   291
                " dans",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   292
                " les",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   293
                "",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   294
                " fr\u00f4\ud801\udc00des",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   295
                " t\u00e9n\u00e8bres");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   296
        assertEquals(subscriber.text, expected.stream().collect(Collectors.joining("")));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   297
        assertEquals(subscriber.list, expected);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   298
        assertEquals(subscriber.list, lines(text, null));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   299
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   300
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   301
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   302
    /** An abstract Subscriber that converts all received data into a String. */
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   303
    static abstract class AbstractSubscriber implements Supplier<String> {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   304
        protected final List<Object> list = new CopyOnWriteArrayList<>();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   305
        protected volatile Flow.Subscription subscription;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   306
        protected final StringBuilder baos = new StringBuilder();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   307
        protected volatile String text;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   308
        protected volatile RuntimeException error;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   309
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   310
        public void onSubscribe(Flow.Subscription subscription) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   311
            this.subscription = subscription;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   312
            subscription.request(Long.MAX_VALUE);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   313
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   314
        public void onError(Throwable throwable) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   315
            System.out.println(this + " onError: " + throwable);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   316
            error = new RuntimeException(throwable);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   317
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   318
        public void onComplete() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   319
            System.out.println(this + " onComplete");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   320
            text = baos.toString();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   321
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   322
        @Override public String get() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   323
            if (error != null) throw error;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   324
            return text;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   325
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   326
        public final List<?> list() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   327
            return list;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   328
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   329
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   330
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   331
    static class StringSubscriber extends AbstractSubscriber
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   332
            implements Flow.Subscriber<String>, Supplier<String>
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   333
    {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   334
        @Override public void onNext(String item) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   335
            System.out.println(this + " onNext: \""
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   336
                    + item.replace("\n","\\n")
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   337
                          .replace("\r", "\\r")
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   338
                    + "\"");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   339
            baos.append(item);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   340
            list.add(item);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   341
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   342
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   343
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   344
    static class CharSequenceSubscriber extends AbstractSubscriber
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   345
            implements Flow.Subscriber<CharSequence>, Supplier<String>
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   346
    {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   347
        @Override public void onNext(CharSequence item) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   348
            System.out.println(this + " onNext: \""
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   349
                    + item.toString().replace("\n","\\n")
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   350
                    .replace("\r", "\\r")
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   351
                    + "\"");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   352
            baos.append(item);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   353
            list.add(item);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   354
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   355
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   356
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   357
    static class ObjectSubscriber extends AbstractSubscriber
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   358
            implements Flow.Subscriber<Object>, Supplier<String>
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   359
    {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   360
        @Override public void onNext(Object item) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   361
            System.out.println(this + " onNext: \""
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   362
                    + item.toString().replace("\n","\\n")
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   363
                    .replace("\r", "\\r")
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   364
                    + "\"");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   365
            baos.append(item);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   366
            list.add(item);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   367
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   368
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   369
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   370
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   371
    static void uncheckedWrite(ByteArrayOutputStream baos, byte[] ba) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   372
        try {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   373
            baos.write(ba);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   374
        } catch (IOException e) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   375
            throw new UncheckedIOException(e);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   376
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   377
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   378
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   379
}