/**
* Copyright 2012 Ronen Hamias, Anton Kharenko
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package io.scalecube.socketio.pipeline;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.handler.codec.http.DefaultFullHttpRequest;
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpMethod;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.HttpResponse;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.HttpVersion;
import io.netty.util.CharsetUtil;
import org.junit.Before;
import org.junit.Test;
public class HandshakeHandlerTest {
private static final int PROTOCOL = 1;
private static final String CONTEXT_PATH = "/socket.io";
private static final String HANDSHAKE_PATH = CONTEXT_PATH + "/" + PROTOCOL + "/";
private HandshakeHandler handshakeHandler;
@Before
public void setUp() throws Exception {
handshakeHandler = new HandshakeHandler(HANDSHAKE_PATH, 60, 60, "websocket,flashsocket,xhr-polling,jsonp-polling");
}
@Test
public void testChannelRead() throws Exception {
HttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/socket.io/1/");
LastOutboundHandler lastOutboundHandler = new LastOutboundHandler();
EmbeddedChannel channel = new EmbeddedChannel(lastOutboundHandler, handshakeHandler);
channel.writeInbound(request);
Object outboundMessage = lastOutboundHandler.getOutboundMessages().poll();
assertTrue(outboundMessage instanceof FullHttpResponse);
FullHttpResponse res = (FullHttpResponse) outboundMessage;
assertEquals(HttpVersion.HTTP_1_1, res.protocolVersion());
assertEquals(HttpResponseStatus.OK, res.status());
ByteBuf content = res.content();
assertTrue(content.toString(CharsetUtil.UTF_8).endsWith("60:60:websocket,flashsocket,xhr-polling,jsonp-polling"));
channel.finish();
}
@Test
public void testChannelReadError() throws Exception {
HttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/wrongPath/");
LastOutboundHandler lastOutboundHandler = new LastOutboundHandler();
EmbeddedChannel channel = new EmbeddedChannel(lastOutboundHandler, handshakeHandler);
channel.writeInbound(request);
Object outboundMessage = lastOutboundHandler.getOutboundMessages().poll();
assertTrue(outboundMessage instanceof HttpResponse);
HttpResponse res = (HttpResponse) outboundMessage;
assertEquals(HttpVersion.HTTP_1_1, res.protocolVersion());
assertEquals(HttpResponseStatus.BAD_REQUEST, res.status());
channel.finish();
}
@Test
public void testChannelReadPost() throws Exception {
HttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/socket.io/1/");
LastOutboundHandler lastOutboundHandler = new LastOutboundHandler();
EmbeddedChannel channel = new EmbeddedChannel(lastOutboundHandler, handshakeHandler);
channel.writeInbound(request);
Object object = channel.readInbound();
assertTrue(object instanceof HttpRequest);
assertEquals(request, object);
channel.finish();
}
@Test
public void testChannelReadNonHttp() throws Exception {
LastOutboundHandler lastOutboundHandler = new LastOutboundHandler();
EmbeddedChannel channel = new EmbeddedChannel(lastOutboundHandler, handshakeHandler);
channel.writeInbound(Unpooled.EMPTY_BUFFER);
Object object = channel.readInbound();
assertTrue(object instanceof ByteBuf);
assertEquals(Unpooled.EMPTY_BUFFER, object);
channel.finish();
}
}