|
| 1 | +/* |
| 2 | + * Copyright (c) 2010 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.cometd.javascript; |
| 18 | + |
| 19 | +import org.junit.Assert; |
| 20 | +import org.junit.Test; |
| 21 | + |
| 22 | +public class CometDWebSocketClientProtocolTest extends AbstractCometDWebSocketTest |
| 23 | +{ |
| 24 | + private static final String PROTOCOL = "bayeux/1.0"; |
| 25 | + |
| 26 | + @Test |
| 27 | + public void testClientWithoutWebSocketProtocolServerWithoutWebSocketProtocol() throws Exception |
| 28 | + { |
| 29 | + evaluateScript("cometd.configure({" + |
| 30 | + "url: '" + cometdURL + "', " + |
| 31 | + "logLevel: '" + getLogLevel() + "'" + |
| 32 | + "});"); |
| 33 | + |
| 34 | + defineClass(Latch.class); |
| 35 | + |
| 36 | + evaluateScript("var latch = new Latch(1);"); |
| 37 | + Latch latch = get("latch"); |
| 38 | + String channelName = "/bar"; |
| 39 | + evaluateScript("cometd.addListener('/meta/handshake', function(message)" + |
| 40 | + "{" + |
| 41 | + " if (message.successful)" + |
| 42 | + " {" + |
| 43 | + " cometd.batch(function()" + |
| 44 | + " {" + |
| 45 | + " cometd.subscribe('" + channelName + "', latch, 'countDown');" + |
| 46 | + " cometd.publish('" + channelName + "', {});" + |
| 47 | + " });" + |
| 48 | + " }" + |
| 49 | + "});"); |
| 50 | + |
| 51 | + evaluateScript("cometd.handshake();"); |
| 52 | + Assert.assertTrue(latch.await(5000)); |
| 53 | + |
| 54 | + evaluateScript("var disconnectLatch = new Latch(1);"); |
| 55 | + Latch disconnectLatch = get("disconnectLatch"); |
| 56 | + evaluateScript("cometd.addListener('/meta/disconnect', disconnectLatch, disconnectLatch.countDown);"); |
| 57 | + evaluateScript("cometd.disconnect();"); |
| 58 | + Assert.assertTrue(disconnectLatch.await(5000)); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void testClientWithWebSocketProtocolServerWithoutWebSocketProtocol() throws Exception |
| 63 | + { |
| 64 | + evaluateScript("cometd.configure({" + |
| 65 | + "url: '" + cometdURL + "', " + |
| 66 | + "protocol: '" + PROTOCOL + "', " + |
| 67 | + "logLevel: '" + getLogLevel() + "'" + |
| 68 | + "});"); |
| 69 | + |
| 70 | + defineClass(Latch.class); |
| 71 | + |
| 72 | + evaluateScript("var latch = new Latch(1);"); |
| 73 | + Latch latch = get("latch"); |
| 74 | + String channelName = "/bar"; |
| 75 | + evaluateScript("cometd.addListener('/meta/handshake', function(message)" + |
| 76 | + "{" + |
| 77 | + " if (message.successful)" + |
| 78 | + " {" + |
| 79 | + " cometd.batch(function()" + |
| 80 | + " {" + |
| 81 | + " cometd.subscribe('" + channelName + "', latch, 'countDown');" + |
| 82 | + " cometd.publish('" + channelName + "', {});" + |
| 83 | + " });" + |
| 84 | + " }" + |
| 85 | + "});"); |
| 86 | + |
| 87 | + evaluateScript("cometd.handshake();"); |
| 88 | + // The server tries to match the client protocol but if it can't tries |
| 89 | + // as if the client sent no protocol, which in this case will match |
| 90 | + Assert.assertTrue(latch.await(5000)); |
| 91 | + |
| 92 | + evaluateScript("var disconnectLatch = new Latch(1);"); |
| 93 | + Latch disconnectLatch = get("disconnectLatch"); |
| 94 | + evaluateScript("cometd.addListener('/meta/disconnect', disconnectLatch, disconnectLatch.countDown);"); |
| 95 | + evaluateScript("cometd.disconnect();"); |
| 96 | + Assert.assertTrue(disconnectLatch.await(5000)); |
| 97 | + } |
| 98 | +} |
0 commit comments