|
| 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 | +} |
0 commit comments