< prev index next >

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

Print this page

        

*** 1,7 **** /* ! * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. --- 1,7 ---- /* ! * Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation.
*** 32,41 **** --- 32,42 ---- import java.time.Duration; import java.util.List; import java.util.concurrent.CompletionStage; import java.util.function.Function; import java.util.stream.Collectors; + import java.util.stream.Stream; import static jdk.incubator.http.internal.websocket.TestSupport.assertCompletesExceptionally; import static jdk.incubator.http.internal.websocket.TestSupport.assertThrows; /*
*** 43,173 **** * The idea is to make sure the code under test relies on something better than * the reference equality ( == ) for string equality checks. */ public class BuildingWebSocketTest { @Test ! public void nulls() { HttpClient c = HttpClient.newHttpClient(); - URI uri = URI.create("ws://websocket.example.com"); assertThrows(NullPointerException.class, ! () -> c.newWebSocketBuilder(null, listener())); assertThrows(NullPointerException.class, ! () -> c.newWebSocketBuilder(uri, null)); assertThrows(NullPointerException.class, ! () -> c.newWebSocketBuilder(null, null)); assertThrows(NullPointerException.class, ! () -> c.newWebSocketBuilder(uri, listener()) .header(null, "value")); assertThrows(NullPointerException.class, ! () -> c.newWebSocketBuilder(uri, listener()) .header("name", null)); assertThrows(NullPointerException.class, ! () -> c.newWebSocketBuilder(uri, listener()) .header(null, null)); assertThrows(NullPointerException.class, ! () -> c.newWebSocketBuilder(uri, listener()) .subprotocols(null)); assertThrows(NullPointerException.class, ! () -> c.newWebSocketBuilder(uri, listener()) ! .subprotocols(null, "sub1")); assertThrows(NullPointerException.class, ! () -> c.newWebSocketBuilder(uri, listener()) .subprotocols("sub1.example.com", (String) null)); assertThrows(NullPointerException.class, ! () -> c.newWebSocketBuilder(uri, listener()) .subprotocols("sub1.example.com", (String[]) null)); assertThrows(NullPointerException.class, ! () -> c.newWebSocketBuilder(uri, listener()) ! .subprotocols("sub1.example.com", ! "sub2.example.com", ! null)); assertThrows(NullPointerException.class, ! () -> c.newWebSocketBuilder(uri, listener()) .connectTimeout(null)); } @Test(dataProvider = "badURIs") ! void illegalURI(String u) { ! WebSocket.Builder b = HttpClient.newHttpClient() ! .newWebSocketBuilder(URI.create(u), listener()); ! assertCompletesExceptionally(IllegalArgumentException.class, b.buildAsync()); } @Test public void illegalHeaders() { ! List<String> headers = List.of("Authorization", ! "Connection", ! "Cookie", ! "Content-Length", ! "Date", ! "Expect", ! "From", ! "Host", ! "Origin", ! "Proxy-Authorization", ! "Referer", ! "User-agent", ! "Upgrade", ! "Via", ! "Warning", ! "Sec-WebSocket-Accept", "Sec-WebSocket-Extensions", "Sec-WebSocket-Key", "Sec-WebSocket-Protocol", ! "Sec-WebSocket-Version").stream() ! .map(String::new).collect(Collectors.toList()); Function<String, CompletionStage<?>> f = ! header -> HttpClient ! .newHttpClient() ! .newWebSocketBuilder(URI.create("ws://websocket.example.com"), ! listener()) ! .buildAsync(); headers.forEach(h -> assertCompletesExceptionally(IllegalArgumentException.class, f.apply(h))); } // TODO: test for bad syntax headers // TODO: test for overwrites (subprotocols) and additions (headers) @Test(dataProvider = "badSubprotocols") public void illegalSubprotocolsSyntax(String s) { WebSocket.Builder b = HttpClient.newHttpClient() ! .newWebSocketBuilder(URI.create("ws://websocket.example.com"), ! listener()); ! b.subprotocols(s); ! assertCompletesExceptionally(IllegalArgumentException.class, b.buildAsync()); } @Test(dataProvider = "duplicatingSubprotocols") public void illegalSubprotocolsDuplicates(String mostPreferred, String[] lesserPreferred) { WebSocket.Builder b = HttpClient.newHttpClient() ! .newWebSocketBuilder(URI.create("ws://websocket.example.com"), ! listener()); ! b.subprotocols(mostPreferred, lesserPreferred); ! assertCompletesExceptionally(IllegalArgumentException.class, b.buildAsync()); } @Test(dataProvider = "badConnectTimeouts") public void illegalConnectTimeout(Duration d) { WebSocket.Builder b = HttpClient.newHttpClient() ! .newWebSocketBuilder(URI.create("ws://websocket.example.com"), ! listener()); ! b.connectTimeout(d); ! assertCompletesExceptionally(IllegalArgumentException.class, b.buildAsync()); } @DataProvider public Object[][] badURIs() { return new Object[][]{ ! {"http://example.com"}, ! {"ftp://example.com"}, ! {"wss://websocket.example.com/hello#fragment"}, ! {"ws://websocket.example.com/hello#fragment"}, }; } @DataProvider public Object[][] badConnectTimeouts() { --- 44,166 ---- * The idea is to make sure the code under test relies on something better than * the reference equality ( == ) for string equality checks. */ public class BuildingWebSocketTest { + private final static URI VALID_URI = URI.create("ws://websocket.example.com"); + @Test ! public void nullArguments() { HttpClient c = HttpClient.newHttpClient(); assertThrows(NullPointerException.class, ! () -> c.newWebSocketBuilder() ! .buildAsync(null, listener())); assertThrows(NullPointerException.class, ! () -> c.newWebSocketBuilder() ! .buildAsync(VALID_URI, null)); assertThrows(NullPointerException.class, ! () -> c.newWebSocketBuilder() ! .buildAsync(null, null)); assertThrows(NullPointerException.class, ! () -> c.newWebSocketBuilder() .header(null, "value")); assertThrows(NullPointerException.class, ! () -> c.newWebSocketBuilder() .header("name", null)); assertThrows(NullPointerException.class, ! () -> c.newWebSocketBuilder() .header(null, null)); assertThrows(NullPointerException.class, ! () -> c.newWebSocketBuilder() .subprotocols(null)); assertThrows(NullPointerException.class, ! () -> c.newWebSocketBuilder() ! .subprotocols(null, "sub2.example.com")); assertThrows(NullPointerException.class, ! () -> c.newWebSocketBuilder() .subprotocols("sub1.example.com", (String) null)); assertThrows(NullPointerException.class, ! () -> c.newWebSocketBuilder() .subprotocols("sub1.example.com", (String[]) null)); assertThrows(NullPointerException.class, ! () -> c.newWebSocketBuilder() ! .subprotocols("sub1.example.com", "sub2.example.com", null)); ! assertThrows(NullPointerException.class, ! () -> c.newWebSocketBuilder() ! .subprotocols("sub1.example.com", null, "sub3.example.com")); assertThrows(NullPointerException.class, ! () -> c.newWebSocketBuilder() .connectTimeout(null)); } @Test(dataProvider = "badURIs") ! void illegalURI(URI uri) { ! WebSocket.Builder b = HttpClient.newHttpClient().newWebSocketBuilder(); ! assertCompletesExceptionally(IllegalArgumentException.class, ! b.buildAsync(uri, listener())); } @Test public void illegalHeaders() { ! List<String> headers = ! List.of("Sec-WebSocket-Accept", "Sec-WebSocket-Extensions", "Sec-WebSocket-Key", "Sec-WebSocket-Protocol", ! "Sec-WebSocket-Version") ! .stream() ! .flatMap(s -> Stream.of(s, new String(s))) // a string and a copy of it ! .collect(Collectors.toList()); Function<String, CompletionStage<?>> f = ! header -> HttpClient.newHttpClient() ! .newWebSocketBuilder() ! .header(header, "value") ! .buildAsync(VALID_URI, listener()); headers.forEach(h -> assertCompletesExceptionally(IllegalArgumentException.class, f.apply(h))); } // TODO: test for bad syntax headers // TODO: test for overwrites (subprotocols) and additions (headers) @Test(dataProvider = "badSubprotocols") public void illegalSubprotocolsSyntax(String s) { WebSocket.Builder b = HttpClient.newHttpClient() ! .newWebSocketBuilder() ! .subprotocols(s); ! assertCompletesExceptionally(IllegalArgumentException.class, ! b.buildAsync(VALID_URI, listener())); } @Test(dataProvider = "duplicatingSubprotocols") public void illegalSubprotocolsDuplicates(String mostPreferred, String[] lesserPreferred) { WebSocket.Builder b = HttpClient.newHttpClient() ! .newWebSocketBuilder() ! .subprotocols(mostPreferred, lesserPreferred); ! assertCompletesExceptionally(IllegalArgumentException.class, ! b.buildAsync(VALID_URI, listener())); } @Test(dataProvider = "badConnectTimeouts") public void illegalConnectTimeout(Duration d) { WebSocket.Builder b = HttpClient.newHttpClient() ! .newWebSocketBuilder() ! .connectTimeout(d); ! assertCompletesExceptionally(IllegalArgumentException.class, ! b.buildAsync(VALID_URI, listener())); } @DataProvider public Object[][] badURIs() { return new Object[][]{ ! {URI.create("http://example.com")}, ! {URI.create("ftp://example.com")}, ! {URI.create("wss://websocket.example.com/hello#fragment")}, ! {URI.create("ws://websocket.example.com/hello#fragment")}, }; } @DataProvider public Object[][] badConnectTimeouts() {
*** 191,200 **** --- 184,194 ---- // https://tools.ietf.org/html/rfc7230#section-3.2.6 // https://tools.ietf.org/html/rfc20 @DataProvider public static Object[][] badSubprotocols() { return new Object[][]{ + {""}, {new String("")}, {"round-brackets("}, {"round-brackets)"}, {"comma,"}, {"slash/"},
< prev index next >