public void LiteralRoutesShouldRankHigherThanCaptureRoutes()
        {
            var logger = MockRepository.GenerateMock<ILogger>();
            var handler = MockRepository.GenerateMock<Func<IWebSocketHandler>>();

            IRankedRoutePredicate capturePredicate = new CaptureRankedRoutePredicate();
            IRankedRoutePredicate literalPredicate = new LiteralRankedRoutePredicate();
            var predicates = new[] {capturePredicate, literalPredicate};

            var routeRegister = new ListBasedRouteRegister();
            routeRegister.Register("/test/{capture}", handler);
            routeRegister.Register("/test/literal", handler);

            var routeResolver = new RankedRouteResolver(routeRegister, predicates, logger);

            var path = new PathBuilder("/test/literal").Build();

            Assert.AreEqual("/test/literal", routeResolver.Resolve(path).Path.FullPath);

            var path2 = new PathBuilder("/test/anything").Build();

            Assert.AreEqual("/test/{capture}", routeResolver.Resolve(path2).Path.FullPath);

            var path3 = new PathBuilder("/test").Build();

            Assert.AreEqual("", routeResolver.Resolve(path3).Path.FullPath);
        }
        public void Register(string fullPath, Func<IWebSocketHandler> handler)
        {
            var path = new PathBuilder(fullPath).Build();

            var route = new Route(path, handler);

            _routes.Add(route);
        }
        public IHandler Negotiate(IRequest request, Func<byte[], Task> sendAction)
        {
            var path = new PathBuilder(request.Path).Build();

            var route = _routeResolver.Resolve(path);
            if (route.Equals(Route.Empty))
                throw new HandshakeException(HttpStatusCode.NotFound);

            var webSocketHandler = route.Handler();

            var version = request.GetWebSocketVersion();

            var handler = _protocolSelector.Select(version, m => webSocketHandler.OnMessage(m), () => webSocketHandler.OnClose(), b => webSocketHandler.OnBinary(b), b => webSocketHandler.OnPing(b), b => webSocketHandler.OnPong(b), ex => webSocketHandler.OnError(ex));

            var routeParameters = _routeParametersBuilder.Build(route.Path, path);

            var queryParameters = _queryParametersBuilder.Build(path);

            var subProtocol = _subProtocolNegotiator.Negotiate(_hostConfiguration.SupportedSubProtocols, request.GetWebSocketProtocols());

            webSocketHandler.Query = queryParameters;
            webSocketHandler.Route = routeParameters;
            webSocketHandler.Context = request.ToContext(subProtocol);
            webSocketHandler.SendAction = (message, frameType) =>
            {
                switch (frameType)
                {
                    case FrameType.Binary:
                        sendAction(handler.FrameBinary(message));
                        break;
                    case FrameType.Ping:
                        sendAction(handler.FramePing(message));
                        break;
                    case FrameType.Pong:
                        sendAction(handler.FramePong(message));
                        break;
                    case FrameType.Text:
                        sendAction(handler.FrameText(message));
                        break;
                    case FrameType.Close:
                        sendAction(handler.FrameClose(WebSocketStatusCode.NormalClosure));
                        break;
                }
            };

            var requestKey = request.Headers["Sec-WebSocket-Key"];

            var response = new WebSocketAcceptResponse(requestKey, subProtocol);
            sendAction(response.ToBytes()).ContinueWith(t => webSocketHandler.OnOpen());

            return handler;
        }
        public void DictionaryShouldContainAllTheCaptures()
        {
            var incomingPath = new PathBuilder("/uk/london/richmond").Build();
            var routePath = new PathBuilder("/{country}/{city}/{town}").Build();

            var parametersBuilder = new RouteParametersBuilder();

            dynamic dictionary = parametersBuilder.Build(routePath, incomingPath);

            Assert.AreEqual("uk", dictionary.Country);
            Assert.AreEqual("london", dictionary.City);
            Assert.AreEqual("richmond", dictionary.Town);
        }
        public void PathTests()
        {
            var routePath = new PathBuilder("/first/{capture}");

            var route = new Route(routePath, null);

            var predicate = new CaptureRankedRoutePredicate();

            Assert.IsTrue(predicate.Matches(route, new PathBuilder("/first/1").Build()));
            Assert.IsTrue(predicate.Matches(route, new PathBuilder("/first/test").Build()));
            Assert.IsFalse(predicate.Matches(route, new PathBuilder("/first").Build()));
            Assert.IsFalse(predicate.Matches(route, new PathBuilder("/first/").Build()));
        }