|
1 | 1 | package org.java_websocket;
|
2 | 2 |
|
| 3 | +import cucumber.annotation.After; |
3 | 4 | import cucumber.annotation.en.Given;
|
4 | 5 | import cucumber.annotation.en.Then;
|
5 | 6 | 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 | 7 | import org.java_websocket.client.WebSocketClient;
|
11 | 8 | import org.java_websocket.drafts.Draft;
|
12 |
| -import org.java_websocket.drafts.Draft_17; |
13 | 9 | import org.java_websocket.handshake.ClientHandshake;
|
14 | 10 | import org.java_websocket.handshake.ServerHandshake;
|
15 | 11 | import org.java_websocket.server.WebSocketServer;
|
16 |
| -import static org.junit.Assert.assertTrue; |
| 12 | +import org.junit.Assert; |
| 13 | + |
| 14 | +import java.io.IOException; |
| 15 | +import java.net.InetSocketAddress; |
| 16 | +import java.net.URI; |
| 17 | +import java.net.URISyntaxException; |
| 18 | +import java.net.UnknownHostException; |
| 19 | +import java.util.Collections; |
| 20 | +import java.util.HashMap; |
| 21 | +import java.util.Iterator; |
| 22 | +import java.util.Map; |
| 23 | +import java.util.concurrent.CountDownLatch; |
17 | 24 |
|
18 | 25 | public class AutobahnClientScenario {
|
19 | 26 |
|
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 |
| - } |
| 27 | + private class AutobahnServer extends WebSocketServer { |
| 28 | + |
| 29 | + public AutobahnServer(int port, Draft d) throws UnknownHostException { |
| 30 | + super(new InetSocketAddress(port), Collections.singletonList(d)); |
| 31 | + } |
| 32 | + |
| 33 | + @Override |
| 34 | + public void onOpen(WebSocket conn, ClientHandshake handshake) { |
| 35 | + //throw new UnsupportedOperationException("Not supported yet."); |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + public void onClose(WebSocket conn, int code, String reason, boolean remote) { |
| 40 | + //throw new UnsupportedOperationException("Not supported yet."); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public void onMessage(WebSocket conn, String message) { |
| 45 | + //throw new UnsupportedOperationException("Not supported yet."); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public void onError(WebSocket conn, Exception ex) { |
| 50 | + //throw new UnsupportedOperationException("Not supported yet."); |
| 51 | + } |
| 52 | + |
| 53 | + } |
| 54 | + |
| 55 | + private class AutobahnClient extends WebSocketClient { |
| 56 | + |
| 57 | + private final CountDownLatch connectionOpenedLatch; |
| 58 | + private final Map<String, String> openHandShakeFields; |
| 59 | + private String message; |
| 60 | + |
| 61 | + public AutobahnClient(Draft draft, URI uri) { |
| 62 | + super(uri, draft); |
| 63 | + connectionOpenedLatch = new CountDownLatch(1); |
| 64 | + openHandShakeFields = new HashMap<String, String>(); |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public void onOpen(ServerHandshake handshakedata) { |
| 69 | + Iterator<String> it = handshakedata.iterateHttpFields(); |
| 70 | + while(it.hasNext()) { |
| 71 | + String key = it.next(); |
| 72 | + System.out.printf("%s %s%n", key, handshakedata.getFieldValue(key)); // TODO Remove this |
| 73 | + openHandShakeFields.put(key, handshakedata.getFieldValue(key)); |
| 74 | + } |
| 75 | + connectionOpenedLatch.countDown(); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public void onMessage(String message) { |
| 80 | + // TODO Test message receiving |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public void onClose(int code, String reason, boolean remote) { |
| 85 | + // TODO Check connection closing |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public void onError(Exception ex) { |
| 90 | + // TODO Check error handling |
| 91 | + ex.printStackTrace(); |
| 92 | + connectionOpenedLatch.countDown(); |
| 93 | + } |
| 94 | + |
| 95 | + } |
| 96 | + |
| 97 | + private static Draft getDraft(int number) { |
| 98 | + Exception exception; |
| 99 | + try { |
| 100 | + return (Draft) Class.forName("org.java_websocket.drafts.Draft_" + number).newInstance(); |
| 101 | + } catch(InstantiationException e) { |
| 102 | + exception = e; |
| 103 | + } catch(IllegalAccessException e) { |
| 104 | + exception = e; |
| 105 | + } catch(ClassNotFoundException e) { |
| 106 | + exception = e; |
| 107 | + } |
| 108 | + throw new RuntimeException(exception); |
| 109 | + } |
| 110 | + |
| 111 | + private String protocol; |
| 112 | + private String host; |
| 113 | + private Integer port; |
| 114 | + private String query; |
| 115 | + private Draft draft; |
| 116 | + |
| 117 | + private AutobahnServer autobahnServer; |
| 118 | + |
| 119 | + @Given("^the Autobahn Server is running using Draft_(\\d+) on port (\\d+)$") |
| 120 | + public void startAutobahnServer(int draft, int port) throws UnknownHostException { |
| 121 | + autobahnServer = new AutobahnServer(port, getDraft(draft)); |
| 122 | + autobahnServer.start(); |
| 123 | + } |
| 124 | + |
| 125 | + @Given("^protocol is (.+)$") |
| 126 | + public void createProtocol(String protocol) { |
| 127 | + this.protocol = protocol; |
| 128 | + } |
| 129 | + |
| 130 | + @Given("^the host is (.+)$") |
| 131 | + public void createHost(String host) { |
| 132 | + this.host = host; |
| 133 | + } |
| 134 | + |
| 135 | + @Given("^the port is (\\d+)$") |
| 136 | + public void createPort(int port) { |
| 137 | + this.port = port; |
| 138 | + } |
| 139 | + |
| 140 | + @Given("^the query string is (.+)$") |
| 141 | + public void createQuery(String query) { |
| 142 | + this.query = query; |
| 143 | + } |
| 144 | + |
| 145 | + @Given("^the draft is Draft_(\\d+)") |
| 146 | + public void createDraft(int draft) { |
| 147 | + this.draft = getDraft(draft); |
| 148 | + } |
| 149 | + |
| 150 | + private AutobahnClient autobahnClient; |
| 151 | + |
| 152 | + @When("^the client connects to the server$") |
| 153 | + public void connectToServer() { |
| 154 | + URI uri; |
| 155 | + try { |
| 156 | + uri = new URI(this.protocol, null, this.host, this.port, null, this.query, null); |
| 157 | + } catch(URISyntaxException e) { |
| 158 | + throw new RuntimeException(e); |
| 159 | + } |
| 160 | + |
| 161 | + System.out.println(uri); |
| 162 | + autobahnClient = new AutobahnClient(this.draft, uri); |
| 163 | + try { |
| 164 | + autobahnClient.connectBlocking(); |
| 165 | + autobahnClient.connectionOpenedLatch.await(); |
| 166 | + } catch(InterruptedException e) { |
| 167 | + Assert.assertTrue(e.getMessage(), false); |
| 168 | + e.printStackTrace(); |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + @Then("^the server response should contain (.+)$") |
| 173 | + public void checkMethod(String method) { |
| 174 | + // TODO Implement check |
| 175 | + //assertTrue(method.contains("GET")); |
| 176 | + } |
| 177 | + |
| 178 | + @Then("^the response's query should contain (.+)$") |
| 179 | + public void checkQuery(String query) { |
| 180 | + // TODO Implement check |
| 181 | + //assertTrue(query.contains(this.query)); |
| 182 | + } |
| 183 | + |
| 184 | + @Then("^the response's http version should contain (.+)$") |
| 185 | + public void checkHttpVersion(String httpversion) { |
| 186 | + // TODO Implement check |
| 187 | + //assertTrue(.contains("HTTP/" + major + "." + minor)); |
| 188 | + } |
| 189 | + |
| 190 | + @Then("^the response's handshake should contain (.+)$") |
| 191 | + public void checkHandshake(String handshake) { |
| 192 | + Assert.assertEquals(handshake, autobahnClient.openHandShakeFields.get("Connection")); |
| 193 | + } |
| 194 | + |
| 195 | + @Then("^the response's host should contain (.+)$") |
| 196 | + public void checkHost(String host) { |
| 197 | + // TODO Implement check |
| 198 | + //assertTrue(host.contains(this.host)); |
| 199 | + } |
| 200 | + |
| 201 | + @Then("^the response's websocket key should contain (.+)$") |
| 202 | + public void checkWebSocketKey(String websocketKey) { |
| 203 | + // TODO Implement check |
| 204 | + Assert.assertTrue(autobahnClient.openHandShakeFields.containsKey(websocketKey)); |
| 205 | + //assertTrue(websocketKey.contains("Sec-WebSocket-Key:")); |
| 206 | + } |
| 207 | + |
| 208 | + @Then("^the response's websocket version should contain (.+)$") |
| 209 | + public void checkWebSocketVersion(String websocketVersion) { |
| 210 | + // TODO Implement check |
| 211 | + //assertTrue(websocketVersion.contains("Sec-WebSocket-Version:")); |
| 212 | + } |
| 213 | + |
| 214 | + @Then("^the response's upgraded protocol should contain (.+)$") |
| 215 | + public void checkUpgradedProtocol(String upgradedProtocol) { |
| 216 | + Assert.assertEquals(upgradedProtocol, autobahnClient.openHandShakeFields.get("Upgrade")); |
| 217 | + } |
| 218 | + |
| 219 | + @After |
| 220 | + public void cleanup() { |
| 221 | + try { |
| 222 | + autobahnClient.closeBlocking(); |
| 223 | + } catch(InterruptedException e) { |
| 224 | + e.printStackTrace(); |
| 225 | + } |
| 226 | + |
| 227 | + try { |
| 228 | + autobahnServer.stop(); |
| 229 | + } catch(IOException e) { |
| 230 | + e.printStackTrace(); |
| 231 | + } catch(InterruptedException e) { |
| 232 | + e.printStackTrace(); |
| 233 | + } |
| 234 | + } |
155 | 235 |
|
156 | 236 | }
|
0 commit comments