< prev index next >

test/jdk/java/net/httpclient/websocket/jdk.incubator.httpclient/jdk/incubator/http/internal/websocket/BuildingWebSocketTest.java

Print this page


   1 /*
   2  * Copyright (c) 2016, 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 org.testng.annotations.DataProvider;
  27 import org.testng.annotations.Test;
  28 
  29 import java.net.URI;
  30 import jdk.incubator.http.HttpClient;
  31 import jdk.incubator.http.WebSocket;
  32 import java.time.Duration;
  33 import java.util.List;
  34 import java.util.concurrent.CompletionStage;
  35 import java.util.function.Function;
  36 import java.util.stream.Collectors;

  37 
  38 import static jdk.incubator.http.internal.websocket.TestSupport.assertCompletesExceptionally;
  39 import static jdk.incubator.http.internal.websocket.TestSupport.assertThrows;
  40 
  41 /*
  42  * In some places in this class a new String is created out of a string literal.
  43  * The idea is to make sure the code under test relies on something better than
  44  * the reference equality ( == ) for string equality checks.
  45  */
  46 public class BuildingWebSocketTest {
  47 


  48     @Test
  49     public void nulls() {
  50         HttpClient c = HttpClient.newHttpClient();
  51         URI uri = URI.create("ws://websocket.example.com");
  52 
  53         assertThrows(NullPointerException.class,
  54                      () -> c.newWebSocketBuilder(null, listener()));

  55         assertThrows(NullPointerException.class,
  56                      () -> c.newWebSocketBuilder(uri, null));

  57         assertThrows(NullPointerException.class,
  58                      () -> c.newWebSocketBuilder(null, null));

  59         assertThrows(NullPointerException.class,
  60                      () -> c.newWebSocketBuilder(uri, listener())
  61                             .header(null, "value"));
  62         assertThrows(NullPointerException.class,
  63                      () -> c.newWebSocketBuilder(uri, listener())
  64                             .header("name", null));
  65         assertThrows(NullPointerException.class,
  66                      () -> c.newWebSocketBuilder(uri, listener())
  67                             .header(null, null));
  68         assertThrows(NullPointerException.class,
  69                      () -> c.newWebSocketBuilder(uri, listener())
  70                             .subprotocols(null));
  71         assertThrows(NullPointerException.class,
  72                      () -> c.newWebSocketBuilder(uri, listener())
  73                             .subprotocols(null, "sub1"));
  74         assertThrows(NullPointerException.class,
  75                      () -> c.newWebSocketBuilder(uri, listener())
  76                             .subprotocols("sub1.example.com", (String) null));
  77         assertThrows(NullPointerException.class,
  78                      () -> c.newWebSocketBuilder(uri, listener())
  79                             .subprotocols("sub1.example.com", (String[]) null));
  80         assertThrows(NullPointerException.class,
  81                      () -> c.newWebSocketBuilder(uri, listener())
  82                             .subprotocols("sub1.example.com",
  83                                           "sub2.example.com",
  84                                           null));

  85         assertThrows(NullPointerException.class,
  86                      () -> c.newWebSocketBuilder(uri, listener())
  87                             .connectTimeout(null));
  88     }
  89 
  90     @Test(dataProvider = "badURIs")
  91     void illegalURI(String u) {
  92         WebSocket.Builder b = HttpClient.newHttpClient()
  93                 .newWebSocketBuilder(URI.create(u), listener());
  94         assertCompletesExceptionally(IllegalArgumentException.class, b.buildAsync());
  95     }
  96 
  97     @Test
  98     public void illegalHeaders() {
  99         List<String> headers = List.of("Authorization",
 100                                        "Connection",
 101                                        "Cookie",
 102                                        "Content-Length",
 103                                        "Date",
 104                                        "Expect",
 105                                        "From",
 106                                        "Host",
 107                                        "Origin",
 108                                        "Proxy-Authorization",
 109                                        "Referer",
 110                                        "User-agent",
 111                                        "Upgrade",
 112                                        "Via",
 113                                        "Warning",
 114                                        "Sec-WebSocket-Accept",
 115                                        "Sec-WebSocket-Extensions",
 116                                        "Sec-WebSocket-Key",
 117                                        "Sec-WebSocket-Protocol",
 118                                        "Sec-WebSocket-Version").stream()
 119                 .map(String::new).collect(Collectors.toList());


 120 
 121         Function<String, CompletionStage<?>> f =
 122                 header -> HttpClient
 123                         .newHttpClient()
 124                         .newWebSocketBuilder(URI.create("ws://websocket.example.com"),
 125                                              listener())
 126                         .buildAsync();
 127 
 128         headers.forEach(h -> assertCompletesExceptionally(IllegalArgumentException.class, f.apply(h)));
 129     }
 130 
 131     // TODO: test for bad syntax headers
 132     // TODO: test for overwrites (subprotocols) and additions (headers)
 133 
 134     @Test(dataProvider = "badSubprotocols")
 135     public void illegalSubprotocolsSyntax(String s) {
 136         WebSocket.Builder b = HttpClient.newHttpClient()
 137                 .newWebSocketBuilder(URI.create("ws://websocket.example.com"),
 138                                      listener());
 139         b.subprotocols(s);
 140         assertCompletesExceptionally(IllegalArgumentException.class, b.buildAsync());
 141     }
 142 
 143     @Test(dataProvider = "duplicatingSubprotocols")
 144     public void illegalSubprotocolsDuplicates(String mostPreferred,
 145                                               String[] lesserPreferred) {
 146         WebSocket.Builder b = HttpClient.newHttpClient()
 147                 .newWebSocketBuilder(URI.create("ws://websocket.example.com"),
 148                                      listener());
 149         b.subprotocols(mostPreferred, lesserPreferred);
 150         assertCompletesExceptionally(IllegalArgumentException.class, b.buildAsync());
 151     }
 152 
 153     @Test(dataProvider = "badConnectTimeouts")
 154     public void illegalConnectTimeout(Duration d) {
 155         WebSocket.Builder b = HttpClient.newHttpClient()
 156                 .newWebSocketBuilder(URI.create("ws://websocket.example.com"),
 157                                      listener());
 158         b.connectTimeout(d);
 159         assertCompletesExceptionally(IllegalArgumentException.class, b.buildAsync());
 160     }
 161 
 162     @DataProvider
 163     public Object[][] badURIs() {
 164         return new Object[][]{
 165                 {"http://example.com"},
 166                 {"ftp://example.com"},
 167                 {"wss://websocket.example.com/hello#fragment"},
 168                 {"ws://websocket.example.com/hello#fragment"},
 169         };
 170     }
 171 
 172     @DataProvider
 173     public Object[][] badConnectTimeouts() {
 174         return new Object[][]{
 175                 {Duration.ofDays   ( 0)},
 176                 {Duration.ofDays   (-1)},
 177                 {Duration.ofHours  ( 0)},
 178                 {Duration.ofHours  (-1)},
 179                 {Duration.ofMinutes( 0)},
 180                 {Duration.ofMinutes(-1)},
 181                 {Duration.ofSeconds( 0)},
 182                 {Duration.ofSeconds(-1)},
 183                 {Duration.ofMillis ( 0)},
 184                 {Duration.ofMillis (-1)},
 185                 {Duration.ofNanos  ( 0)},
 186                 {Duration.ofNanos  (-1)},
 187                 {Duration.ZERO},
 188         };
 189     }
 190 
 191     // https://tools.ietf.org/html/rfc7230#section-3.2.6
 192     // https://tools.ietf.org/html/rfc20
 193     @DataProvider
 194     public static Object[][] badSubprotocols() {
 195         return new Object[][]{

 196                 {new String("")},
 197                 {"round-brackets("},
 198                 {"round-brackets)"},
 199                 {"comma,"},
 200                 {"slash/"},
 201                 {"colon:"},
 202                 {"semicolon;"},
 203                 {"angle-brackets<"},
 204                 {"angle-brackets>"},
 205                 {"equals="},
 206                 {"question-mark?"},
 207                 {"at@"},
 208                 {"brackets["},
 209                 {"backslash\\"},
 210                 {"brackets]"},
 211                 {"curly-brackets{"},
 212                 {"curly-brackets}"},
 213                 {"space "},
 214                 {"non-printable-character " + Character.toString((char) 31)},
 215                 {"non-printable-character " + Character.toString((char) 127)},
   1 /*
   2  * Copyright (c) 2016, 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 org.testng.annotations.DataProvider;
  27 import org.testng.annotations.Test;
  28 
  29 import java.net.URI;
  30 import jdk.incubator.http.HttpClient;
  31 import jdk.incubator.http.WebSocket;
  32 import java.time.Duration;
  33 import java.util.List;
  34 import java.util.concurrent.CompletionStage;
  35 import java.util.function.Function;
  36 import java.util.stream.Collectors;
  37 import java.util.stream.Stream;
  38 
  39 import static jdk.incubator.http.internal.websocket.TestSupport.assertCompletesExceptionally;
  40 import static jdk.incubator.http.internal.websocket.TestSupport.assertThrows;
  41 
  42 /*
  43  * In some places in this class a new String is created out of a string literal.
  44  * The idea is to make sure the code under test relies on something better than
  45  * the reference equality ( == ) for string equality checks.
  46  */
  47 public class BuildingWebSocketTest {
  48 
  49     private final static URI VALID_URI = URI.create("ws://websocket.example.com");
  50 
  51     @Test
  52     public void nullArguments() {
  53         HttpClient c = HttpClient.newHttpClient();

  54 
  55         assertThrows(NullPointerException.class,
  56                      () -> c.newWebSocketBuilder()
  57                             .buildAsync(null, listener()));
  58         assertThrows(NullPointerException.class,
  59                      () -> c.newWebSocketBuilder()
  60                             .buildAsync(VALID_URI, null));
  61         assertThrows(NullPointerException.class,
  62                      () -> c.newWebSocketBuilder()
  63                             .buildAsync(null, null));
  64         assertThrows(NullPointerException.class,
  65                      () -> c.newWebSocketBuilder()
  66                             .header(null, "value"));
  67         assertThrows(NullPointerException.class,
  68                      () -> c.newWebSocketBuilder()
  69                             .header("name", null));
  70         assertThrows(NullPointerException.class,
  71                      () -> c.newWebSocketBuilder()
  72                             .header(null, null));
  73         assertThrows(NullPointerException.class,
  74                      () -> c.newWebSocketBuilder()
  75                             .subprotocols(null));
  76         assertThrows(NullPointerException.class,
  77                      () -> c.newWebSocketBuilder()
  78                             .subprotocols(null, "sub2.example.com"));
  79         assertThrows(NullPointerException.class,
  80                      () -> c.newWebSocketBuilder()
  81                             .subprotocols("sub1.example.com", (String) null));
  82         assertThrows(NullPointerException.class,
  83                      () -> c.newWebSocketBuilder()
  84                             .subprotocols("sub1.example.com", (String[]) null));
  85         assertThrows(NullPointerException.class,
  86                      () -> c.newWebSocketBuilder()
  87                             .subprotocols("sub1.example.com", "sub2.example.com", null));
  88         assertThrows(NullPointerException.class,
  89                      () -> c.newWebSocketBuilder()
  90                              .subprotocols("sub1.example.com", null, "sub3.example.com"));
  91         assertThrows(NullPointerException.class,
  92                      () -> c.newWebSocketBuilder()
  93                             .connectTimeout(null));
  94     }
  95 
  96     @Test(dataProvider = "badURIs")
  97     void illegalURI(URI uri) {
  98         WebSocket.Builder b = HttpClient.newHttpClient().newWebSocketBuilder();
  99         assertCompletesExceptionally(IllegalArgumentException.class,
 100                                      b.buildAsync(uri, listener()));
 101     }
 102 
 103     @Test
 104     public void illegalHeaders() {
 105         List<String> headers =
 106                 List.of("Sec-WebSocket-Accept",














 107                         "Sec-WebSocket-Extensions",
 108                         "Sec-WebSocket-Key",
 109                         "Sec-WebSocket-Protocol",
 110                         "Sec-WebSocket-Version")
 111                         .stream()
 112                         .flatMap(s -> Stream.of(s, new String(s))) // a string and a copy of it
 113                         .collect(Collectors.toList());
 114 
 115         Function<String, CompletionStage<?>> f =
 116                 header -> HttpClient.newHttpClient()
 117                         .newWebSocketBuilder()
 118                         .header(header, "value")
 119                         .buildAsync(VALID_URI, listener());

 120 
 121         headers.forEach(h -> assertCompletesExceptionally(IllegalArgumentException.class, f.apply(h)));
 122     }
 123 
 124     // TODO: test for bad syntax headers
 125     // TODO: test for overwrites (subprotocols) and additions (headers)
 126 
 127     @Test(dataProvider = "badSubprotocols")
 128     public void illegalSubprotocolsSyntax(String s) {
 129         WebSocket.Builder b = HttpClient.newHttpClient()
 130                 .newWebSocketBuilder()
 131                 .subprotocols(s);
 132         assertCompletesExceptionally(IllegalArgumentException.class,
 133                                      b.buildAsync(VALID_URI, listener()));
 134     }
 135 
 136     @Test(dataProvider = "duplicatingSubprotocols")
 137     public void illegalSubprotocolsDuplicates(String mostPreferred,
 138                                               String[] lesserPreferred) {
 139         WebSocket.Builder b = HttpClient.newHttpClient()
 140                 .newWebSocketBuilder()
 141                 .subprotocols(mostPreferred, lesserPreferred);
 142         assertCompletesExceptionally(IllegalArgumentException.class,
 143                                      b.buildAsync(VALID_URI, listener()));
 144     }
 145 
 146     @Test(dataProvider = "badConnectTimeouts")
 147     public void illegalConnectTimeout(Duration d) {
 148         WebSocket.Builder b = HttpClient.newHttpClient()
 149                 .newWebSocketBuilder()
 150                 .connectTimeout(d);
 151         assertCompletesExceptionally(IllegalArgumentException.class,
 152                                      b.buildAsync(VALID_URI, listener()));
 153     }
 154 
 155     @DataProvider
 156     public Object[][] badURIs() {
 157         return new Object[][]{
 158                 {URI.create("http://example.com")},
 159                 {URI.create("ftp://example.com")},
 160                 {URI.create("wss://websocket.example.com/hello#fragment")},
 161                 {URI.create("ws://websocket.example.com/hello#fragment")},
 162         };
 163     }
 164 
 165     @DataProvider
 166     public Object[][] badConnectTimeouts() {
 167         return new Object[][]{
 168                 {Duration.ofDays   ( 0)},
 169                 {Duration.ofDays   (-1)},
 170                 {Duration.ofHours  ( 0)},
 171                 {Duration.ofHours  (-1)},
 172                 {Duration.ofMinutes( 0)},
 173                 {Duration.ofMinutes(-1)},
 174                 {Duration.ofSeconds( 0)},
 175                 {Duration.ofSeconds(-1)},
 176                 {Duration.ofMillis ( 0)},
 177                 {Duration.ofMillis (-1)},
 178                 {Duration.ofNanos  ( 0)},
 179                 {Duration.ofNanos  (-1)},
 180                 {Duration.ZERO},
 181         };
 182     }
 183 
 184     // https://tools.ietf.org/html/rfc7230#section-3.2.6
 185     // https://tools.ietf.org/html/rfc20
 186     @DataProvider
 187     public static Object[][] badSubprotocols() {
 188         return new Object[][]{
 189                 {""},
 190                 {new String("")},
 191                 {"round-brackets("},
 192                 {"round-brackets)"},
 193                 {"comma,"},
 194                 {"slash/"},
 195                 {"colon:"},
 196                 {"semicolon;"},
 197                 {"angle-brackets<"},
 198                 {"angle-brackets>"},
 199                 {"equals="},
 200                 {"question-mark?"},
 201                 {"at@"},
 202                 {"brackets["},
 203                 {"backslash\\"},
 204                 {"brackets]"},
 205                 {"curly-brackets{"},
 206                 {"curly-brackets}"},
 207                 {"space "},
 208                 {"non-printable-character " + Character.toString((char) 31)},
 209                 {"non-printable-character " + Character.toString((char) 127)},
< prev index next >