Skip to content

Commit 5c19bd5

Browse files
ThreaTThreaT
authored andcommitted
Added acceptance tests prototype. Requires code review and bug fixing.
1 parent f431e4b commit 5c19bd5

File tree

4 files changed

+249
-28
lines changed

4 files changed

+249
-28
lines changed

pom.xml

Lines changed: 54 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<properties>
2222
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
2323
<java.version>1.6</java.version>
24+
<cucumber.version>1.0.0.RC24</cucumber.version>
2425
</properties>
2526
<build>
2627
<plugins>
@@ -61,6 +62,28 @@
6162
</plugin>
6263
</plugins>
6364
</build>
65+
<dependencies>
66+
<!-- JUnit -->
67+
<dependency>
68+
<groupId>junit</groupId>
69+
<artifactId>junit</artifactId>
70+
<version>4.8.2</version>
71+
<scope>test</scope>
72+
</dependency>
73+
<!-- Cucumber -->
74+
<dependency>
75+
<groupId>info.cukes</groupId>
76+
<artifactId>cucumber-junit</artifactId>
77+
<version>1.0.0</version>
78+
<scope>test</scope>
79+
</dependency>
80+
<dependency>
81+
<groupId>info.cukes</groupId>
82+
<artifactId>cucumber-java</artifactId>
83+
<version>${cucumber.version}</version>
84+
<scope>test</scope>
85+
</dependency>
86+
</dependencies>
6487
<developers>
6588
<developer>
6689
<id>TooTallNate</id>
@@ -88,32 +111,35 @@
88111
</license>
89112
</licenses>
90113
<profiles>
91-
<profile>
92-
<id>release-sign-artifacts</id>
93-
<activation>
94-
<property><name>performRelease</name><value>true</value></property>
95-
</activation>
96-
<build>
97-
<plugins>
98-
<plugin>
99-
<groupId>org.apache.maven.plugins</groupId>
100-
<artifactId>maven-gpg-plugin</artifactId>
101-
<version>1.1</version>
102-
<executions>
103-
<execution>
104-
<id>sign-artifacts</id>
105-
<phase>verify</phase>
106-
<goals>
107-
<goal>sign</goal>
108-
</goals>
109-
</execution>
110-
</executions>
111-
<configuration>
112-
<keyname>rohmer.david@gmail.com</keyname>
113-
</configuration>
114-
</plugin>
115-
</plugins>
116-
</build>
117-
</profile>
114+
<profile>
115+
<id>release-sign-artifacts</id>
116+
<activation>
117+
<property>
118+
<name>performRelease</name>
119+
<value>true</value>
120+
</property>
121+
</activation>
122+
<build>
123+
<plugins>
124+
<plugin>
125+
<groupId>org.apache.maven.plugins</groupId>
126+
<artifactId>maven-gpg-plugin</artifactId>
127+
<version>1.1</version>
128+
<executions>
129+
<execution>
130+
<id>sign-artifacts</id>
131+
<phase>verify</phase>
132+
<goals>
133+
<goal>sign</goal>
134+
</goals>
135+
</execution>
136+
</executions>
137+
<configuration>
138+
<keyname>rohmer.david@gmail.com</keyname>
139+
</configuration>
140+
</plugin>
141+
</plugins>
142+
</build>
143+
</profile>
118144
</profiles>
119-
</project>
145+
</project>
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
package org.java_websocket;
2+
3+
import cucumber.annotation.en.Given;
4+
import cucumber.annotation.en.Then;
5+
import cucumber.annotation.en.When;
6+
import java.net.InetSocketAddress;
7+
import java.net.URI;
8+
import java.net.UnknownHostException;
9+
import java.util.Collections;
10+
import org.java_websocket.client.WebSocketClient;
11+
import org.java_websocket.drafts.Draft;
12+
import org.java_websocket.drafts.Draft_17;
13+
import org.java_websocket.handshake.ClientHandshake;
14+
import org.java_websocket.handshake.ServerHandshake;
15+
import org.java_websocket.server.WebSocketServer;
16+
import static org.junit.Assert.assertTrue;
17+
18+
public class AutobahnClientScenario {
19+
20+
private class AutobahnServer extends WebSocketServer {
21+
22+
public AutobahnServer(int port, Draft d) throws UnknownHostException {
23+
super(new InetSocketAddress(port), Collections.singletonList(d));
24+
}
25+
26+
@Override
27+
public void onOpen(WebSocket conn, ClientHandshake handshake) {
28+
throw new UnsupportedOperationException("Not supported yet.");
29+
}
30+
31+
@Override
32+
public void onClose(WebSocket conn, int code, String reason, boolean remote) {
33+
throw new UnsupportedOperationException("Not supported yet.");
34+
}
35+
36+
@Override
37+
public void onMessage(WebSocket conn, String message) {
38+
throw new UnsupportedOperationException("Not supported yet.");
39+
}
40+
41+
@Override
42+
public void onError(WebSocket conn, Exception ex) {
43+
throw new UnsupportedOperationException("Not supported yet.");
44+
}
45+
46+
}
47+
48+
private class AutobahnClient extends WebSocketClient {
49+
50+
public AutobahnClient(Draft draft, URI uri) {
51+
super(uri, draft);
52+
}
53+
54+
@Override
55+
public void onOpen(ServerHandshake handshakedata) {
56+
throw new UnsupportedOperationException("Not supported yet.");
57+
}
58+
59+
@Override
60+
public void onMessage(String message) {
61+
throw new UnsupportedOperationException("Not supported yet.");
62+
}
63+
64+
@Override
65+
public void onClose(int code, String reason, boolean remote) {
66+
throw new UnsupportedOperationException("Not supported yet.");
67+
}
68+
69+
@Override
70+
public void onError(Exception ex) {
71+
throw new UnsupportedOperationException("Not supported yet.");
72+
}
73+
}
74+
private String protocol;
75+
private String host;
76+
private Integer port;
77+
private String query;
78+
private Draft draft;
79+
80+
@Given("^the Autobahn Server is running using Draft_(\\d+) on port (\\d+)$")
81+
public void startAutobahnServer() throws UnknownHostException {
82+
new AutobahnServer(9003, new Draft_17()).start();
83+
}
84+
85+
@Given("^protocol is ws://$")
86+
public void createProtocol(String protocol) {
87+
this.protocol = protocol;
88+
}
89+
90+
@Given("^the host is localhost:$")
91+
public void createHost(String host) {
92+
this.host = host;
93+
}
94+
95+
@Given("^the port is (\\d*)$")
96+
public void createPort(Integer port) {
97+
this.port = port;
98+
}
99+
100+
@Given("^the query string is case=(\\d+)&agent=tootallnate/websocket$")
101+
public void createQuery(String query) {
102+
this.query = query;
103+
}
104+
105+
@Given("^the draft is Draft_17")
106+
public void createDraft(Draft_17 draft_17) {
107+
this.draft = draft_17;
108+
}
109+
110+
@When("^the client connects to the server")
111+
public void connectToServer() {
112+
AutobahnClient autobahnClient = new AutobahnClient(this.draft, URI.create(this.protocol + this.host + this.port + this.query));
113+
Thread thread = new Thread(autobahnClient);
114+
thread.start();
115+
}
116+
117+
@Then("^the server response should contain (\\w*)$")
118+
public void checkMethod(String method) {
119+
assertTrue(method.contains("GET"));
120+
}
121+
122+
@Then("^the response should contain case=(\\d+)&agent=tootallnate/websocket$")
123+
public void checkQuery(String query) {
124+
assertTrue(query.contains(this.query));
125+
}
126+
127+
@Then("^the response should contain HTTP/(\\d+).(\\d+)$")
128+
public void checkHttpVersion(String http_version) {
129+
assertTrue(http_version.contains("HTTP/1.1"));
130+
}
131+
132+
@Then("^the response should contain Connection: Upgrade$")
133+
public void checkHandshake(String handshake) {
134+
assertTrue(handshake.contains("Connection: Upgrade"));
135+
}
136+
137+
@Then("^the response should contain localhost:$")
138+
public void checkHost(String host) {
139+
assertTrue(host.contains(this.host));
140+
}
141+
142+
@Then("^the response should contain Sec-WebSocket-Key:$")
143+
public void checkWebSocketKey(String websocketKey) {
144+
assertTrue(websocketKey.contains("Sec-WebSocket-Key:"));
145+
}
146+
147+
@Then("^the response should contain Sec-WebSocket-Version:$")
148+
public void checkWebSocketVersion(String websocketVersion) {
149+
assertTrue(websocketVersion.contains("Sec-WebSocket-Version:"));
150+
}
151+
@Then("^the response should contain Upgrade: websocket$")
152+
public void checkUpgradedProtocol(String upgradedProtocol) {
153+
assertTrue(upgradedProtocol.contains("Upgrade: websocket"));
154+
}
155+
156+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.java_websocket;
2+
3+
import org.junit.runner.RunWith;
4+
5+
import cucumber.junit.Cucumber;
6+
7+
@RunWith(Cucumber.class)
8+
public class AutobahnClientTest {
9+
10+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
Feature: Client connects using Draft 17
2+
As an autobahn client
3+
I want to connect to a websocket server using draft 17
4+
So that I can send and receive messages
5+
6+
Scenario Outline: Client connection parameters are valid
7+
Given the Autobahn Server is running using Draft_17 on port 9003
8+
And protocol is <protocol>
9+
And the host is <host>
10+
And the port is <port>
11+
And the query string is <query>
12+
And the draft is Draft_17
13+
When the client connects to the server
14+
Then the server response should contain <method>
15+
And the response should contain <query>
16+
And the response should contain <http_version>
17+
And the response should contain <handshake>
18+
And the response should contain <host>
19+
And the response should contain <websocket_key>
20+
And the response should contain <websocket_version>
21+
And the response should contain <upgraded_protocol>
22+
23+
Examples:
24+
|protocol|host |port|query |method |http_version|handshake |websocket_key |websocket_version |upgraded_protocol |
25+
|ws:// |localhost:|9001|case=1&agent=tootallnate/websocket|GET |HTTP/1.1 |Connection: Upgrade|Sec-WebSocket-Key:|Sec-WebSocket-Version:|Upgrade: websocket|
26+
27+
28+
29+

0 commit comments

Comments
 (0)