<commit_before>#!/usr/bin/env bundle exec rspec
# encoding: utf-8

require "spec_helper"

require "sockjs"
require "sockjs/transports/websocket"

describe SockJS::Transports::WebSocket do
  it_should_match_path  "server/session/websocket"
  it_should_have_method "GET"
  transport_handler_eql "a/b/websocket", "GET"

  # TODO: This should be a mixin.
  def transport(options = Hash.new)
    connection = SockJS::Connection.new {}
    connection.session_open {}
    # TODO: In fact the first argument should always be the transport,
    # we should replace 'self' by the following code in all the specs.
    described_class.new(connection, options).tap do |transport|
      # TODO: Use Connection#create_session instead of
      # instantiating FakeSession manually.
      connection.create_session("b", transport, FakeSession)
    end
  end

  def request(opts = Hash.new)
    env = {
      "HTTP_CONNECTION" => "Upgrade",
      "HTTP_UPGRADE" => "WebSocket"}
    FakeRequest.new(env.merge(opts)).tap do |request|
      request.path_info = "/a/b/websocket"
    end
  end

  def response(transport = transport, request = request)
    transport.handle(request)
  end


  describe "#handle_open(request)" do
    it "should send the opening frame"
    it "should open a new session"
  end

  describe "#handle_message(request, event)" do<commit_msg>Working on specs for WebSocket#handle.<commit_after>#!/usr/bin/env bundle exec rspec
# encoding: utf-8

require "spec_helper"

require "sockjs"
require "sockjs/transports/websocket"

describe SockJS::Transports::WebSocket do
  it_should_match_path  "server/session/websocket"
  it_should_have_method "GET"
  transport_handler_eql "a/b/websocket", "GET"

  # TODO: This should be a mixin.
  def transport(options = Hash.new)
    connection = SockJS::Connection.new {}
    connection.session_open {}
    # TODO: In fact the first argument should always be the transport,
    # we should replace 'self' by the following code in all the specs.
    described_class.new(connection, options).tap do |transport|
      # TODO: Use Connection#create_session instead of
      # instantiating FakeSession manually.
      connection.create_session("b", transport, FakeSession)
    end
  end

  def request(opts = Hash.new)
    env = {
      "HTTP_CONNECTION" => "Upgrade",
      "HTTP_UPGRADE" => "WebSocket"}
    FakeRequest.new(env.merge(opts)).tap do |request|
      request.path_info = "/a/b/websocket"
    end
  end

  def response(transport = transport, request = request)
    transport.handle(request)
  end

  describe "#handle(request)" do
    it "should respond with 404 and an error message if the transport is disabled" do
      options   = {disabled_transports: [described_class]}
      transport = transport(options)
      response  = response(transport, request)

      transport.should be_disabled

      response.status.should eql(404)
      response.chunks.last.should eql("WebSockets Are Disabled")
    end

    it "should respond with 400 and an error message if HTTP_UPGRADE isn't WebSocket" do
      request  = request("HTTP_UPGRADE" => "something")
      response = response(transport, request)

      response.status.should eql(400)
      response.chunks.last.should eql('Can "Upgrade" only to "WebSocket".')
    end
  end

  describe "#handle_open(request)" do
    it "should send the opening frame"
    it "should open a new session"
  end

  describe "#handle_message(request, event)" do
{'fraction_non_alphanumeric': 0.09866511897852583, 'fraction_numerical': 0.004062681369704005, 'mean_word_length': 3.664411366711773, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2752871', 'n_tokens_mistral': 1080, 'n_tokens_neox': 1024, 'n_words': 300}