public void ShouldCreateAWellFormedResponse() { var expectedResponse = "HTTP/1.1 101 Switching Protocols\r\n" + "Upgrade: websocket\r\n" + "Connection: Upgrade\r\n" + "Sec-WebSocket-Accept: 3zK22MhUhryr5kF4G6IwUR0kqa4=\r\n" + "Content-Type: text/html\r\n" + "Content-Length: 0\r\n" + "\r\n"; var response = new WebSocketAcceptResponse("key", null); var responseString = response.ToString(); Assert.AreEqual(expectedResponse, responseString); }
public void ShouldSetStatusCodeToSwitchingProtocols() { var response = new WebSocketAcceptResponse("key", null); Assert.AreEqual(HttpStatusCode.SwitchingProtocols, response.StatusCode); }
public void ShouldNotAddProtocolToHeadersIfNotSpecified() { var response = new WebSocketAcceptResponse("key", null); Assert.That(!response.Headers.ContainsKey("Sec-WebSocket-Protocol")); }
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; }