|
| 1 | +packageorg.java_websocket; |
| 2 | + |
| 3 | +importcucumber.annotation.en.Given; |
| 4 | +importcucumber.annotation.en.Then; |
| 5 | +importcucumber.annotation.en.When; |
| 6 | +importjava.net.InetSocketAddress; |
| 7 | +importjava.net.URI; |
| 8 | +importjava.net.UnknownHostException; |
| 9 | +importjava.util.Collections; |
| 10 | +importorg.java_websocket.client.WebSocketClient; |
| 11 | +importorg.java_websocket.drafts.Draft; |
| 12 | +importorg.java_websocket.drafts.Draft_17; |
| 13 | +importorg.java_websocket.handshake.ClientHandshake; |
| 14 | +importorg.java_websocket.handshake.ServerHandshake; |
| 15 | +importorg.java_websocket.server.WebSocketServer; |
| 16 | +importstaticorg.junit.Assert.assertTrue; |
| 17 | + |
| 18 | +publicclassAutobahnClientScenario{ |
| 19 | + |
| 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 | + } |
| 155 | + |
| 156 | +} |
0 commit comments