1 /*
   2  * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 package jdk.incubator.http.internal.websocket;
  25 
  26 import jdk.incubator.http.WebSocket;
  27 import org.testng.annotations.Test;
  28 
  29 import java.net.URI;
  30 import java.util.List;
  31 import java.util.concurrent.CompletableFuture;
  32 import java.util.concurrent.CompletionStage;
  33 import java.util.concurrent.TimeUnit;
  34 
  35 import static java.util.concurrent.CompletableFuture.completedStage;
  36 import static jdk.incubator.http.WebSocket.MessagePart.FIRST;
  37 import static jdk.incubator.http.WebSocket.MessagePart.LAST;
  38 import static jdk.incubator.http.WebSocket.MessagePart.PART;
  39 import static jdk.incubator.http.WebSocket.MessagePart.WHOLE;
  40 import static jdk.incubator.http.WebSocket.NORMAL_CLOSURE;
  41 import static jdk.incubator.http.internal.common.Pair.pair;
  42 import static jdk.incubator.http.internal.websocket.MockListener.ListenerInvocation.onClose;
  43 import static jdk.incubator.http.internal.websocket.MockListener.ListenerInvocation.onError;
  44 import static jdk.incubator.http.internal.websocket.MockListener.ListenerInvocation.onOpen;
  45 import static jdk.incubator.http.internal.websocket.MockListener.ListenerInvocation.onText;
  46 import static org.testng.Assert.assertEquals;
  47 
  48 public class ReceivingTest {
  49 
  50     // TODO: request in onClose/onError
  51     // TODO: throw exception in onClose/onError
  52     // TODO: exception is thrown from request()
  53 
  54     @Test
  55     public void testNonPositiveRequest() throws Exception {
  56         MockListener listener = new MockListener(Long.MAX_VALUE) {
  57             @Override
  58             protected void onOpen0(WebSocket webSocket) {
  59                 webSocket.request(0);
  60             }
  61         };
  62         MockTransport transport = new MockTransport() {
  63             @Override
  64             protected Receiver newReceiver(MessageStreamConsumer consumer) {
  65                 return new MockReceiver(consumer, channel, pair(now(), m -> m.onText("1", WHOLE)));
  66             }
  67         };
  68         WebSocket ws = newInstance(listener, transport);
  69         listener.onCloseOrOnErrorCalled().get(10, TimeUnit.SECONDS);
  70         List<MockListener.ListenerInvocation> invocations = listener.invocations();
  71         assertEquals(invocations, List.of(onOpen(ws), onError(ws, IllegalArgumentException.class)));
  72     }
  73 
  74     @Test
  75     public void testText1() throws Exception {
  76         MockListener listener = new MockListener(Long.MAX_VALUE);
  77         MockTransport transport = new MockTransport() {
  78             @Override
  79             protected Receiver newReceiver(MessageStreamConsumer consumer) {
  80                 return new MockReceiver(consumer, channel,
  81                                         pair(now(), m -> m.onText("1", FIRST)),
  82                                         pair(now(), m -> m.onText("2", PART)),
  83                                         pair(now(), m -> m.onText("3", PART)),
  84                                         pair(now(), m -> m.onText("4", LAST)),
  85                                         pair(now(), m -> m.onClose(NORMAL_CLOSURE, "no reason")));
  86             }
  87         };
  88         WebSocket ws = newInstance(listener, transport);
  89         listener.onCloseOrOnErrorCalled().get(10, TimeUnit.SECONDS);
  90         List<MockListener.ListenerInvocation> invocations = listener.invocations();
  91         assertEquals(invocations, List.of(onOpen(ws),
  92                                           onText(ws, "1", FIRST),
  93                                           onText(ws, "2", PART),
  94                                           onText(ws, "3", PART),
  95                                           onText(ws, "4", LAST),
  96                                           onClose(ws, NORMAL_CLOSURE, "no reason")));
  97     }
  98 
  99     private static CompletionStage<?> seconds(long s) {
 100         return new CompletableFuture<>().completeOnTimeout(null, s, TimeUnit.SECONDS);
 101     }
 102 
 103     private static CompletionStage<?> now() {
 104         return completedStage(null);
 105     }
 106 
 107     private static WebSocket newInstance(WebSocket.Listener listener,
 108                                          TransportSupplier transport) {
 109         URI uri = URI.create("ws://localhost");
 110         String subprotocol = "";
 111         return WebSocketImpl.newInstance(uri, subprotocol, listener, transport);
 112     }
 113 }