|
1 | 1 | packageorg.java_websocket; |
2 | 2 |
|
| 3 | +importcucumber.annotation.After; |
3 | 4 | importcucumber.annotation.en.Given; |
4 | 5 | importcucumber.annotation.en.Then; |
5 | 6 | importcucumber.annotation.en.When; |
6 | | -importjava.net.InetSocketAddress; |
7 | | -importjava.net.URI; |
8 | | -importjava.net.UnknownHostException; |
9 | | -importjava.util.Collections; |
10 | 7 | importorg.java_websocket.client.WebSocketClient; |
11 | 8 | importorg.java_websocket.drafts.Draft; |
12 | | -importorg.java_websocket.drafts.Draft_17; |
13 | 9 | importorg.java_websocket.handshake.ClientHandshake; |
14 | 10 | importorg.java_websocket.handshake.ServerHandshake; |
15 | 11 | importorg.java_websocket.server.WebSocketServer; |
16 | | -importstaticorg.junit.Assert.assertTrue; |
| 12 | +importorg.junit.Assert; |
| 13 | + |
| 14 | +importjava.io.IOException; |
| 15 | +importjava.net.InetSocketAddress; |
| 16 | +importjava.net.URI; |
| 17 | +importjava.net.URISyntaxException; |
| 18 | +importjava.net.UnknownHostException; |
| 19 | +importjava.util.Collections; |
| 20 | +importjava.util.HashMap; |
| 21 | +importjava.util.Iterator; |
| 22 | +importjava.util.Map; |
| 23 | +importjava.util.concurrent.CountDownLatch; |
17 | 24 |
|
18 | 25 | publicclassAutobahnClientScenario{ |
19 | 26 |
|
20 | | -privateclassAutobahnServerextendsWebSocketServer{ |
21 | | - |
22 | | -publicAutobahnServer(intport, Draftd) throwsUnknownHostException{ |
23 | | -super(newInetSocketAddress(port), Collections.singletonList(d)); |
24 | | - } |
25 | | - |
26 | | -@Override |
27 | | -publicvoidonOpen(WebSocketconn, ClientHandshakehandshake){ |
28 | | -thrownewUnsupportedOperationException("Not supported yet."); |
29 | | - } |
30 | | - |
31 | | -@Override |
32 | | -publicvoidonClose(WebSocketconn, intcode, Stringreason, booleanremote){ |
33 | | -thrownewUnsupportedOperationException("Not supported yet."); |
34 | | - } |
35 | | - |
36 | | -@Override |
37 | | -publicvoidonMessage(WebSocketconn, Stringmessage){ |
38 | | -thrownewUnsupportedOperationException("Not supported yet."); |
39 | | - } |
40 | | - |
41 | | -@Override |
42 | | -publicvoidonError(WebSocketconn, Exceptionex){ |
43 | | -thrownewUnsupportedOperationException("Not supported yet."); |
44 | | - } |
45 | | - |
46 | | - } |
47 | | - |
48 | | -privateclassAutobahnClientextendsWebSocketClient{ |
49 | | - |
50 | | -publicAutobahnClient(Draftdraft, URIuri){ |
51 | | -super(uri, draft); |
52 | | - } |
53 | | - |
54 | | -@Override |
55 | | -publicvoidonOpen(ServerHandshakehandshakedata){ |
56 | | -thrownewUnsupportedOperationException("Not supported yet."); |
57 | | - } |
58 | | - |
59 | | -@Override |
60 | | -publicvoidonMessage(Stringmessage){ |
61 | | -thrownewUnsupportedOperationException("Not supported yet."); |
62 | | - } |
63 | | - |
64 | | -@Override |
65 | | -publicvoidonClose(intcode, Stringreason, booleanremote){ |
66 | | -thrownewUnsupportedOperationException("Not supported yet."); |
67 | | - } |
68 | | - |
69 | | -@Override |
70 | | -publicvoidonError(Exceptionex){ |
71 | | -thrownewUnsupportedOperationException("Not supported yet."); |
72 | | - } |
73 | | - } |
74 | | -privateStringprotocol; |
75 | | -privateStringhost; |
76 | | -privateIntegerport; |
77 | | -privateStringquery; |
78 | | -privateDraftdraft; |
79 | | - |
80 | | -@Given("^the Autobahn Server is running using Draft_(\\d+) on port (\\d+)$") |
81 | | -publicvoidstartAutobahnServer() throwsUnknownHostException{ |
82 | | -newAutobahnServer(9003, newDraft_17()).start(); |
83 | | - } |
84 | | - |
85 | | -@Given("^protocol is ws://$") |
86 | | -publicvoidcreateProtocol(Stringprotocol){ |
87 | | -this.protocol = protocol; |
88 | | - } |
89 | | - |
90 | | -@Given("^the host is localhost:$") |
91 | | -publicvoidcreateHost(Stringhost){ |
92 | | -this.host = host; |
93 | | - } |
94 | | - |
95 | | -@Given("^the port is (\\d*)$") |
96 | | -publicvoidcreatePort(Integerport){ |
97 | | -this.port = port; |
98 | | - } |
99 | | - |
100 | | -@Given("^the query string is case=(\\d+)&agent=tootallnate/websocket$") |
101 | | -publicvoidcreateQuery(Stringquery){ |
102 | | -this.query = query; |
103 | | - } |
104 | | - |
105 | | -@Given("^the draft is Draft_17") |
106 | | -publicvoidcreateDraft(Draft_17draft_17){ |
107 | | -this.draft = draft_17; |
108 | | - } |
109 | | - |
110 | | -@When("^the client connects to the server") |
111 | | -publicvoidconnectToServer(){ |
112 | | -AutobahnClientautobahnClient = newAutobahnClient(this.draft, URI.create(this.protocol + this.host + this.port + this.query)); |
113 | | -Threadthread = newThread(autobahnClient); |
114 | | -thread.start(); |
115 | | - } |
116 | | - |
117 | | -@Then("^the server response should contain (\\w*)$") |
118 | | -publicvoidcheckMethod(Stringmethod){ |
119 | | -assertTrue(method.contains("GET")); |
120 | | - } |
121 | | - |
122 | | -@Then("^the response should contain case=(\\d+)&agent=tootallnate/websocket$") |
123 | | -publicvoidcheckQuery(Stringquery){ |
124 | | -assertTrue(query.contains(this.query)); |
125 | | - } |
126 | | - |
127 | | -@Then("^the response should contain HTTP/(\\d+).(\\d+)$") |
128 | | -publicvoidcheckHttpVersion(Stringhttp_version){ |
129 | | -assertTrue(http_version.contains("HTTP/1.1")); |
130 | | - } |
131 | | - |
132 | | -@Then("^the response should contain Connection: Upgrade$") |
133 | | -publicvoidcheckHandshake(Stringhandshake){ |
134 | | -assertTrue(handshake.contains("Connection: Upgrade")); |
135 | | - } |
136 | | - |
137 | | -@Then("^the response should contain localhost:$") |
138 | | -publicvoidcheckHost(Stringhost){ |
139 | | -assertTrue(host.contains(this.host)); |
140 | | - } |
141 | | - |
142 | | -@Then("^the response should contain Sec-WebSocket-Key:$") |
143 | | -publicvoidcheckWebSocketKey(StringwebsocketKey){ |
144 | | -assertTrue(websocketKey.contains("Sec-WebSocket-Key:")); |
145 | | - } |
146 | | - |
147 | | -@Then("^the response should contain Sec-WebSocket-Version:$") |
148 | | -publicvoidcheckWebSocketVersion(StringwebsocketVersion){ |
149 | | -assertTrue(websocketVersion.contains("Sec-WebSocket-Version:")); |
150 | | - } |
151 | | -@Then("^the response should contain Upgrade: websocket$") |
152 | | -publicvoidcheckUpgradedProtocol(StringupgradedProtocol){ |
153 | | -assertTrue(upgradedProtocol.contains("Upgrade: websocket")); |
154 | | - } |
| 27 | +privateclassAutobahnServerextendsWebSocketServer{ |
| 28 | + |
| 29 | +publicAutobahnServer(intport, Draftd) throwsUnknownHostException{ |
| 30 | +super(newInetSocketAddress(port), Collections.singletonList(d)); |
| 31 | + } |
| 32 | + |
| 33 | +@Override |
| 34 | +publicvoidonOpen(WebSocketconn, ClientHandshakehandshake){ |
| 35 | +//throw new UnsupportedOperationException("Not supported yet."); |
| 36 | + } |
| 37 | + |
| 38 | +@Override |
| 39 | +publicvoidonClose(WebSocketconn, intcode, Stringreason, booleanremote){ |
| 40 | +//throw new UnsupportedOperationException("Not supported yet."); |
| 41 | + } |
| 42 | + |
| 43 | +@Override |
| 44 | +publicvoidonMessage(WebSocketconn, Stringmessage){ |
| 45 | +//throw new UnsupportedOperationException("Not supported yet."); |
| 46 | + } |
| 47 | + |
| 48 | +@Override |
| 49 | +publicvoidonError(WebSocketconn, Exceptionex){ |
| 50 | +//throw new UnsupportedOperationException("Not supported yet."); |
| 51 | + } |
| 52 | + |
| 53 | + } |
| 54 | + |
| 55 | +privateclassAutobahnClientextendsWebSocketClient{ |
| 56 | + |
| 57 | +privatefinalCountDownLatchconnectionOpenedLatch; |
| 58 | +privatefinalMap<String, String> openHandShakeFields; |
| 59 | +privateStringmessage; |
| 60 | + |
| 61 | +publicAutobahnClient(Draftdraft, URIuri){ |
| 62 | +super(uri, draft); |
| 63 | +connectionOpenedLatch = newCountDownLatch(1); |
| 64 | +openHandShakeFields = newHashMap<String, String>(); |
| 65 | + } |
| 66 | + |
| 67 | +@Override |
| 68 | +publicvoidonOpen(ServerHandshakehandshakedata){ |
| 69 | +Iterator<String> it = handshakedata.iterateHttpFields(); |
| 70 | +while(it.hasNext()){ |
| 71 | +Stringkey = 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 | +publicvoidonMessage(Stringmessage){ |
| 80 | +// TODO Test message receiving |
| 81 | + } |
| 82 | + |
| 83 | +@Override |
| 84 | +publicvoidonClose(intcode, Stringreason, booleanremote){ |
| 85 | +// TODO Check connection closing |
| 86 | + } |
| 87 | + |
| 88 | +@Override |
| 89 | +publicvoidonError(Exceptionex){ |
| 90 | +// TODO Check error handling |
| 91 | +ex.printStackTrace(); |
| 92 | +connectionOpenedLatch.countDown(); |
| 93 | + } |
| 94 | + |
| 95 | + } |
| 96 | + |
| 97 | +privatestaticDraftgetDraft(intnumber){ |
| 98 | +Exceptionexception; |
| 99 | +try{ |
| 100 | +return (Draft) Class.forName("org.java_websocket.drafts.Draft_" + number).newInstance(); |
| 101 | + } catch(InstantiationExceptione){ |
| 102 | +exception = e; |
| 103 | + } catch(IllegalAccessExceptione){ |
| 104 | +exception = e; |
| 105 | + } catch(ClassNotFoundExceptione){ |
| 106 | +exception = e; |
| 107 | + } |
| 108 | +thrownewRuntimeException(exception); |
| 109 | + } |
| 110 | + |
| 111 | +privateStringprotocol; |
| 112 | +privateStringhost; |
| 113 | +privateIntegerport; |
| 114 | +privateStringquery; |
| 115 | +privateDraftdraft; |
| 116 | + |
| 117 | +privateAutobahnServerautobahnServer; |
| 118 | + |
| 119 | +@Given("^the Autobahn Server is running using Draft_(\\d+) on port (\\d+)$") |
| 120 | +publicvoidstartAutobahnServer(intdraft, intport) throwsUnknownHostException{ |
| 121 | +autobahnServer = newAutobahnServer(port, getDraft(draft)); |
| 122 | +autobahnServer.start(); |
| 123 | + } |
| 124 | + |
| 125 | +@Given("^protocol is (.+)$") |
| 126 | +publicvoidcreateProtocol(Stringprotocol){ |
| 127 | +this.protocol = protocol; |
| 128 | + } |
| 129 | + |
| 130 | +@Given("^the host is (.+)$") |
| 131 | +publicvoidcreateHost(Stringhost){ |
| 132 | +this.host = host; |
| 133 | + } |
| 134 | + |
| 135 | +@Given("^the port is (\\d+)$") |
| 136 | +publicvoidcreatePort(intport){ |
| 137 | +this.port = port; |
| 138 | + } |
| 139 | + |
| 140 | +@Given("^the query string is (.+)$") |
| 141 | +publicvoidcreateQuery(Stringquery){ |
| 142 | +this.query = query; |
| 143 | + } |
| 144 | + |
| 145 | +@Given("^the draft is Draft_(\\d+)") |
| 146 | +publicvoidcreateDraft(intdraft){ |
| 147 | +this.draft = getDraft(draft); |
| 148 | + } |
| 149 | + |
| 150 | +privateAutobahnClientautobahnClient; |
| 151 | + |
| 152 | +@When("^the client connects to the server$") |
| 153 | +publicvoidconnectToServer(){ |
| 154 | +URIuri; |
| 155 | +try{ |
| 156 | +uri = newURI(this.protocol, null, this.host, this.port, null, this.query, null); |
| 157 | + } catch(URISyntaxExceptione){ |
| 158 | +thrownewRuntimeException(e); |
| 159 | + } |
| 160 | + |
| 161 | +System.out.println(uri); |
| 162 | +autobahnClient = newAutobahnClient(this.draft, uri); |
| 163 | +try{ |
| 164 | +autobahnClient.connectBlocking(); |
| 165 | +autobahnClient.connectionOpenedLatch.await(); |
| 166 | + } catch(InterruptedExceptione){ |
| 167 | +Assert.assertTrue(e.getMessage(), false); |
| 168 | +e.printStackTrace(); |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | +@Then("^the server response should contain (.+)$") |
| 173 | +publicvoidcheckMethod(Stringmethod){ |
| 174 | +// TODO Implement check |
| 175 | +//assertTrue(method.contains("GET")); |
| 176 | + } |
| 177 | + |
| 178 | +@Then("^the response's query should contain (.+)$") |
| 179 | +publicvoidcheckQuery(Stringquery){ |
| 180 | +// TODO Implement check |
| 181 | +//assertTrue(query.contains(this.query)); |
| 182 | + } |
| 183 | + |
| 184 | +@Then("^the response's http version should contain (.+)$") |
| 185 | +publicvoidcheckHttpVersion(Stringhttpversion){ |
| 186 | +// TODO Implement check |
| 187 | +//assertTrue(.contains("HTTP/" + major + "." + minor)); |
| 188 | + } |
| 189 | + |
| 190 | +@Then("^the response's handshake should contain (.+)$") |
| 191 | +publicvoidcheckHandshake(Stringhandshake){ |
| 192 | +Assert.assertEquals(handshake, autobahnClient.openHandShakeFields.get("Connection")); |
| 193 | + } |
| 194 | + |
| 195 | +@Then("^the response's host should contain (.+)$") |
| 196 | +publicvoidcheckHost(Stringhost){ |
| 197 | +// TODO Implement check |
| 198 | +//assertTrue(host.contains(this.host)); |
| 199 | + } |
| 200 | + |
| 201 | +@Then("^the response's websocket key should contain (.+)$") |
| 202 | +publicvoidcheckWebSocketKey(StringwebsocketKey){ |
| 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 | +publicvoidcheckWebSocketVersion(StringwebsocketVersion){ |
| 210 | +// TODO Implement check |
| 211 | +//assertTrue(websocketVersion.contains("Sec-WebSocket-Version:")); |
| 212 | + } |
| 213 | + |
| 214 | +@Then("^the response's upgraded protocol should contain (.+)$") |
| 215 | +publicvoidcheckUpgradedProtocol(StringupgradedProtocol){ |
| 216 | +Assert.assertEquals(upgradedProtocol, autobahnClient.openHandShakeFields.get("Upgrade")); |
| 217 | + } |
| 218 | + |
| 219 | +@After |
| 220 | +publicvoidcleanup(){ |
| 221 | +try{ |
| 222 | +autobahnClient.closeBlocking(); |
| 223 | + } catch(InterruptedExceptione){ |
| 224 | +e.printStackTrace(); |
| 225 | + } |
| 226 | + |
| 227 | +try{ |
| 228 | +autobahnServer.stop(); |
| 229 | + } catch(IOExceptione){ |
| 230 | +e.printStackTrace(); |
| 231 | + } catch(InterruptedExceptione){ |
| 232 | +e.printStackTrace(); |
| 233 | + } |
| 234 | + } |
155 | 235 |
|
156 | 236 | } |
0 commit comments