Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 108c77a2306074e018df268a84600f8132e43ad6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
//
//  ========================================================================
//  Copyright (c) 1995-2012 Mort Bay Consulting Pty. Ltd.
//  ------------------------------------------------------------------------
//  All rights reserved. This program and the accompanying materials
//  are made available under the terms of the Eclipse Public License v1.0
//  and Apache License v2.0 which accompanies this distribution.
//
//      The Eclipse Public License is available at
//      http://www.eclipse.org/legal/epl-v10.html
//
//      The Apache License v2.0 is available at
//      http://www.opensource.org/licenses/apache2.0.php
//
//  You may elect to redistribute this code under either of these licenses.
//  ========================================================================
//

package org.eclipse.jetty.client;

import static org.junit.Assert.assertEquals;

import java.io.IOException;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.TimeUnit;

import javax.servlet.http.HttpServletRequest;

import org.eclipse.jetty.http.HttpMethods;
import org.eclipse.jetty.io.Buffer;
import org.eclipse.jetty.io.Connection;
import org.eclipse.jetty.io.EndPoint;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.nio.SelectChannelConnector;
import org.eclipse.jetty.websocket.WebSocket;
import org.eclipse.jetty.websocket.WebSocketBuffers;
import org.eclipse.jetty.websocket.WebSocketConnectionD00;
import org.eclipse.jetty.websocket.WebSocketHandler;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/* ------------------------------------------------------------ */
/**
 * Functional testing for HttpExchange.
 */
public class WebSocketUpgradeTest
{
    protected Server _server;
    protected int _port;
    protected HttpClient _httpClient;
    protected Connector _connector;
    protected ConcurrentLinkedQueue<TestWebSocket> _webSockets= new ConcurrentLinkedQueue<TestWebSocket>();
    protected WebSocketHandler _handler;
    protected TestWebSocket _websocket;
    final BlockingQueue<Object> _results = new ArrayBlockingQueue<Object>(100);

    /* ------------------------------------------------------------ */
    @Before
    public void setUp() throws Exception
    {
        startServer();
        _httpClient=new HttpClient();
        _httpClient.setIdleTimeout(2000);
        _httpClient.setTimeout(2500);
        _httpClient.setConnectTimeout(1000);
        _httpClient.setConnectorType(HttpClient.CONNECTOR_SELECT_CHANNEL);
        _httpClient.setMaxConnectionsPerAddress(10);
        _httpClient.start();
    }

    /* ------------------------------------------------------------ */
    @After
    public void tearDown() throws Exception
    {
        _httpClient.stop();
        Thread.sleep(500);
        stopServer();
    }

    /* ------------------------------------------------------------ */
    @Test
    public void testGetWithContentExchange() throws Exception
    {
        final WebSocket clientWS = new WebSocket.OnTextMessage()
        {
            Connection _connection;
            
            /* ------------------------------------------------------------ */
            public void onClose(int closeCode, String message)
            {
            }
            
            /* ------------------------------------------------------------ */
            public void onOpen(Connection connection)
            {
                _connection=connection;
                _results.add("clientWS.onConnect");
                _results.add(_connection);
            }
            
            /* ------------------------------------------------------------ */
            public void onMessage(String data)
            {
                _results.add("clientWS.onMessage");
                _results.add(data);
            }
        };


        HttpExchange httpExchange=new HttpExchange()
        {
            /* ------------------------------------------------------------ */
            /**
             * @see org.eclipse.jetty.client.HttpExchange#onResponseStatus(org.eclipse.jetty.io.Buffer, int, org.eclipse.jetty.io.Buffer)
             */
            @Override
            protected void onResponseStatus(Buffer version, int status, Buffer reason) throws IOException
            {
                waitFor(2);
                _results.add(new Integer(status));
                super.onResponseStatus(version,status,reason);
            }

            /* ------------------------------------------------------------ */
            /**
             * @see org.eclipse.jetty.client.HttpExchange#onSwitchProtocol(org.eclipse.jetty.io.EndPoint)
             */
            @Override
            protected Connection onSwitchProtocol(EndPoint endp) throws IOException
            {
                waitFor(3);
                WebSocketConnectionD00 connection = new WebSocketConnectionD00(clientWS,endp,new WebSocketBuffers(4096),System.currentTimeMillis(),1000,"");

                _results.add("onSwitchProtocol");
                _results.add(connection);
                clientWS.onOpen(connection);
                return connection;
            }

            /* ------------------------------------------------------------ */
            private void waitFor(int results)
            {
                try
                {
                    int c=10;
                    while(_results.size()<results && c-->0)
                        Thread.sleep(10);
                }
                catch(InterruptedException e)
                {
                    e.printStackTrace();
                }
            }
        };

        httpExchange.setURL("http://localhost:"+_port+"/");
        httpExchange.setMethod(HttpMethods.GET);

        httpExchange.addRequestHeader("Upgrade","WebSocket");
        httpExchange.addRequestHeader("Connection","Upgrade");

        _httpClient.send(httpExchange);
        int status = httpExchange.waitForDone();
        assertEquals(HttpExchange.STATUS_COMPLETED, status);

        assertEquals("serverWS.onConnect", _results.poll(1,TimeUnit.SECONDS));
        TestWebSocket serverWS = (TestWebSocket)_results.poll(1,TimeUnit.SECONDS);

        assertEquals(new Integer(101), _results.poll(1,TimeUnit.SECONDS));

        assertEquals("onSwitchProtocol", _results.poll(1,TimeUnit.SECONDS));
        WebSocketConnectionD00 client_conn=(WebSocketConnectionD00)_results.poll(1,TimeUnit.SECONDS);

        assertEquals("clientWS.onConnect", _results.poll(1,TimeUnit.SECONDS));
        assertEquals(client_conn, _results.poll(1,TimeUnit.SECONDS));

        client_conn.sendMessage("hello world");

        assertEquals("serverWS.onMessage", _results.poll(1,TimeUnit.SECONDS));
        assertEquals("hello world", _results.poll(1,TimeUnit.SECONDS));

        serverWS.sendMessage("buongiorno");

        assertEquals("clientWS.onMessage", _results.poll(1,TimeUnit.SECONDS));
        assertEquals("buongiorno", _results.poll(1,TimeUnit.SECONDS));

    }

    /* ------------------------------------------------------------ */
    protected void newServer() throws Exception
    {
        _server=new Server();
        _server.setGracefulShutdown(500);
        _connector=new SelectChannelConnector();

        _connector.setPort(0);
        _server.setConnectors(new Connector[] { _connector });
    }

    /* ------------------------------------------------------------ */
    protected void startServer() throws Exception
    {
        newServer();
        _handler= new WebSocketHandler()
        {
            /* ------------------------------------------------------------ */
            public WebSocket doWebSocketConnect(HttpServletRequest request, String protocol)
            {
                _websocket = new TestWebSocket();
                return _websocket;
            }
        };
        _handler.getWebSocketFactory().setMinVersion(-1);

        _server.setHandler(_handler);
        _server.start();
        _port=_connector.getLocalPort();
    }

    /* ------------------------------------------------------------ */
    private void stopServer() throws Exception
    {
        _server.stop();
        _server.join();
    }

    /* ------------------------------------------------------------ */
    class TestWebSocket implements WebSocket.OnTextMessage
    {
        Connection _connection;

        /* ------------------------------------------------------------ */
        public void onOpen(Connection connection)
        {
            _connection=connection;
            _webSockets.add(this);
            _results.add("serverWS.onConnect");
            _results.add(this);
        }

        /* ------------------------------------------------------------ */
        public void onMessage(final String data)
        {
            _results.add("serverWS.onMessage");
            _results.add(data);
        }
        
        /* ------------------------------------------------------------ */
        public void onClose(int code, String message)
        {
            _results.add("onClose");
            _webSockets.remove(this);
        }

        /* ------------------------------------------------------------ */
        public void sendMessage(String msg) throws IOException
        {
            _connection.sendMessage(msg);
        }
    }
}

Back to the top