}
public void testStompMessagesAreReceivedByAjaxClient() throws Exception {
LOG.debug( "*** testStompMessagesAreRecievedByAjaxClient ***" );
HttpClient httpClient = new HttpClient();
httpClient.start();
httpClient.setConnectorType(HttpClient.CONNECTOR_SELECT_CHANNEL);
// client 1 subscribes to a queue
LOG.debug( "SENDING LISTEN" );
AjaxTestContentExchange contentExchange = new AjaxTestContentExchange();
contentExchange.setMethod( "POST" );
contentExchange.setURL("http://localhost:8080/amq");
contentExchange.setRequestContent( new ByteArrayBuffer("destination=queue://test&type=listen&message=handler") );
contentExchange.setRequestContentType( "application/x-www-form-urlencoded; charset=UTF-8" );
httpClient.send(contentExchange);
contentExchange.waitForDone();
String jsessionid = contentExchange.getJsessionId();
// client 1 polls for messages
LOG.debug( "SENDING POLL" );
AjaxTestContentExchange poll = new AjaxTestContentExchange();
poll.setMethod( "GET" );
poll.setURL("http://localhost:8080/amq?timeout=5000");
poll.setRequestHeader( "Cookie", jsessionid );
httpClient.send( poll );
// stomp client queues some messages
StompConnection connection = new StompConnection();
connection.open(stompUri.getHost(), stompUri.getPort());
connection.connect("user", "password");
HashMap<String, String> headers = new HashMap<String, String>();
headers.put( "amq-msg-type", "text" );
connection.send( "/queue/test", "message1", (String)null, headers );
connection.send( "/queue/test", "message2", (String)null, headers );
connection.send( "/queue/test", "message3", (String)null, headers );
connection.send( "/queue/test", "message4", (String)null, headers );
connection.send( "/queue/test", "message5", (String)null, headers );
String frame = "DISCONNECT\n" + "\n\n" + Stomp.NULL;
connection.sendFrame(frame);
// Need to let the transport have enough time to dispatch the incoming messages from
// the socket before we break the connection.
TimeUnit.SECONDS.sleep(5);
connection.disconnect();
// wait for poll to finish
poll.waitForDone();
String response = poll.getResponseContent();
// not all messages might be delivered during the 1st poll. We need to check again.
poll = new AjaxTestContentExchange();
poll.setMethod( "GET" );
poll.setURL("http://localhost:8080/amq?timeout=5000");
poll.setRequestHeader( "Cookie", jsessionid );
httpClient.send( poll );
poll.waitForDone();
String fullResponse = response + poll.getResponseContent();
assertContains( "<response id='handler' destination='queue://test' >message1</response>\n", fullResponse );