Skip to content

Commit b7e041c

Browse files
authored
Merge pull request #573 from marci4/master
JUnit test for #390
2 parents 2a07103 + 38020c2 commit b7e041c

File tree

4 files changed

+199
-1
lines changed

4 files changed

+199
-1
lines changed

src/main/example/ExampleClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public void onError( Exception ex ) {
6767
}
6868

6969
public static void main( String[] args ) throws URISyntaxException {
70-
ExampleClient c = new ExampleClient( new URI( "ws://echo.websocket.org/asdf" ), new Draft_6455() ); // more about drafts here: http://github.com/TooTallNate/Java-WebSocket/wiki/Drafts
70+
ExampleClient c = new ExampleClient( new URI( "ws://localhost:8887" ), new Draft_6455() ); // more about drafts here: http://github.com/TooTallNate/Java-WebSocket/wiki/Drafts
7171
c.connect();
7272
}
7373

src/test/java/org/java_websocket/AllTests.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
@RunWith(Suite.class)
3333
@Suite.SuiteClasses({
3434
org.java_websocket.util.ByteBufferUtilsTest.class,
35+
org.java_websocket.misc.AllMiscTests.class,
3536
org.java_websocket.framing.AllFramingTests.class
3637
})
3738
/**
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright (c) 2010-2017 Nathan Rajlich
3+
*
4+
* Permission is hereby granted, free of charge, to any person
5+
* obtaining a copy of this software and associated documentation
6+
* files (the "Software"), to deal in the Software without
7+
* restriction, including without limitation the rights to use,
8+
* copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the
10+
* Software is furnished to do so, subject to the following
11+
* conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be
14+
* included in all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18+
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21+
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23+
* OTHER DEALINGS IN THE SOFTWARE.
24+
*/
25+
26+
package org.java_websocket.misc;
27+
import org.junit.runner.RunWith;
28+
import org.junit.runners.Suite;
29+
30+
31+
@RunWith(Suite.class)
32+
@Suite.SuiteClasses({
33+
org.java_websocket.misc.OpeningHandshakeRejectionTest.class
34+
})
35+
/**
36+
* Start all tests for mics
37+
*/
38+
public class AllMiscTests {
39+
}
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
/*
2+
* Copyright (c) 2010-2017 Nathan Rajlich
3+
*
4+
* Permission is hereby granted, free of charge, to any person
5+
* obtaining a copy of this software and associated documentation
6+
* files (the "Software"), to deal in the Software without
7+
* restriction, including without limitation the rights to use,
8+
* copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the
10+
* Software is furnished to do so, subject to the following
11+
* conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be
14+
* included in all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18+
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21+
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23+
* OTHER DEALINGS IN THE SOFTWARE.
24+
*/
25+
26+
package org.java_websocket.misc;
27+
28+
import org.java_websocket.client.WebSocketClient;
29+
import org.java_websocket.framing.CloseFrame;
30+
import org.java_websocket.handshake.ServerHandshake;
31+
import org.java_websocket.util.Charsetfunctions;
32+
import org.junit.Test;
33+
34+
import java.io.IOException;
35+
import java.io.OutputStream;
36+
import java.net.ServerSocket;
37+
import java.net.Socket;
38+
import java.net.URI;
39+
import java.util.Scanner;
40+
import java.util.concurrent.CountDownLatch;
41+
42+
import static org.junit.Assert.fail;
43+
44+
public class OpeningHandshakeRejectionTest {
45+
46+
/**
47+
* How many testcases do we have
48+
*/
49+
private static final int testCases = 10;
50+
51+
public OpeningHandshakeRejectionTest() {
52+
Thread thread = new Thread(
53+
new Runnable() {
54+
public void run() {
55+
try {
56+
ServerSocket server = new ServerSocket( 8887 );
57+
while( true ) {
58+
Socket client = null;
59+
try {
60+
client = server.accept();
61+
Scanner in = new Scanner( client.getInputStream() );
62+
String input = in.nextLine();
63+
String testCase = input.split( " " )[1];
64+
OutputStream os = client.getOutputStream();
65+
if( "/0".equals( testCase ) ) {
66+
os.write( Charsetfunctions.asciiBytes( "HTTP/1.1 100 Switching Protocols\r\n" ) );
67+
os.flush();
68+
}
69+
if( "/1".equals( testCase ) ) {
70+
os.write( Charsetfunctions.asciiBytes( "HTTP/1.0 100 Switching Protocols\r\n" ) );
71+
os.flush();
72+
}
73+
if( "/2".equals( testCase ) ) {
74+
os.write( Charsetfunctions.asciiBytes( "HTTP 100 Switching Protocols\r\n" ) );
75+
os.flush();
76+
}
77+
if( "/3".equals( testCase ) ) {
78+
os.write( Charsetfunctions.asciiBytes( "HTTP/1.1 200 Switching Protocols\r\n" ) );
79+
os.flush();
80+
}
81+
if( "/4".equals( testCase ) ) {
82+
os.write( Charsetfunctions.asciiBytes( "HTTP 101 Switching Protocols\r\n" ) );
83+
os.flush();
84+
}
85+
if( "/5".equals( testCase ) ) {
86+
os.write( Charsetfunctions.asciiBytes( "HTTP/1.1 404 Switching Protocols\r\n" ) );
87+
os.flush();
88+
}
89+
if( "/6".equals( testCase ) ) {
90+
os.write( Charsetfunctions.asciiBytes( "HTTP/2.0 404 Switching Protocols\r\n" ) );
91+
os.flush();
92+
}
93+
if( "/7".equals( testCase ) ) {
94+
os.write( Charsetfunctions.asciiBytes( "HTTP/1.1 500 Switching Protocols\r\n" ) );
95+
os.flush();
96+
}
97+
if( "/8".equals( testCase ) ) {
98+
os.write( Charsetfunctions.asciiBytes( "GET 302 Switching Protocols\r\n" ) );
99+
os.flush();
100+
}
101+
if( "/9".equals( testCase ) ) {
102+
os.write( Charsetfunctions.asciiBytes( "GET HTTP/1.1 101 Switching Protocols\r\n" ) );
103+
os.flush();
104+
}
105+
client.close();
106+
} catch ( IOException e ) {
107+
fail( "There should be no exception" );
108+
} finally {
109+
if( client != null )
110+
client.close();
111+
}
112+
}
113+
} catch ( Exception e ) {
114+
e.printStackTrace();
115+
fail( "There should be no exception" );
116+
}
117+
}
118+
} );
119+
thread.start();
120+
}
121+
122+
@Test(timeout=10000)
123+
public void testClient() {
124+
try {
125+
for( int i = 0; i < testCases; i++ ) {
126+
final CountDownLatch latch = new CountDownLatch( 1 );
127+
WebSocketClient webSocketClient = new WebSocketClient( new URI( "ws://localhost:8887/" + i ) ) {
128+
@Override
129+
public void onOpen( ServerHandshake handshakedata ) {
130+
fail( "There should not be a connection" );
131+
latch.countDown();
132+
}
133+
@Override
134+
public void onMessage( String message ) {
135+
136+
}
137+
@Override
138+
public void onClose( int code, String reason, boolean remote ) {
139+
if( code != CloseFrame.PROTOCOL_ERROR ) {
140+
fail( "There should be a protocol error" );
141+
}
142+
close();
143+
latch.countDown();
144+
}
145+
146+
@Override
147+
public void onError( Exception ex ) {
148+
fail( "There should not be an exception" );
149+
}
150+
};
151+
webSocketClient.connect();
152+
latch.await();
153+
}
154+
} catch ( Exception e ) {
155+
fail( "There should be no exception" );
156+
}
157+
}
158+
}

0 commit comments

Comments
 (0)