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

require "spec_helper"

require "sockjs"
require "sockjs/transports/jsonp"

class SockJS::Transports::JSONP
  def session_class
    FakeSession
  end
end

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

  describe "#handle(request)" do
    let(:transport) do
      described_class.new(SockJS::Connection.new {}, Hash.new)
    end

    let(:request) do
      @request ||= FakeRequest.new.tap do |request|
        request.path_info = "/echo/a/b/jsonp"
      end
    end

    let(:response) do
      transport.handle(request)
    end

    context "with callback specified" do
      let(:request) do
        @request ||= FakeRequest.new.tap do |request|
          request.path_info = "/echo/a/b/jsonp"
          request.callback = "clbk"
        end
      end

      context "with a session" do
        let(:transport) do
          connection = SockJS::Connection.new {}
          connection.sessions["b"] = FakeSession.new(self, Hash.new)

          described_class.new(connection, Hash.new)
        end

        it "should respond with HTTP 200" do
          response.status.should eql(200)
        end

        it "should respond with plain text MIME type" do
          response.headers["Content-Type"].should match("text/plain")
        end

        it "should respond with a body"
      end

      context "without any session" do
        it "should respond with HTTP 200" do
          response.status.should eql(200)
        end

        it "should respond with javascript MIME type" do
          response.headers["Content-Type"].should match("application/javascript")
        end

        it "should set access control" do
          response.headers["Access-Control-Allow-Origin"].should eql(request.origin)
          response.headers["Access-Control-Allow-Credentials"].should eql("true")
        end

        it "should disable caching" do
          response.headers["Cache-Control"].should eql("no-store, no-cache, must-revalidate, max-age=0")
        end

        it "should open a new session"
      end
    end

    context "without callback specified" do
      it "should respond with HTTP 500" do
        response.status.should eql(500)
      end

      it "should respond with HTML MIME type" do
        response.headers["Content-Type"].should match("text/html")
      end

      it "should return error message in the body" do
        response # Run the handler.
        request.chunks.last.should match(/"callback" parameter required/)
      end
    end
  end

  describe "#format_frame(payload)" do
    it "should format payload"
  end
end



describe SockJS::Transports::JSONPSend do
  it_should_match_path  "server/session/jsonp_send"
  it_should_have_method "POST"
  transport_handler_eql "a/b/jsonp_send", "POST"

  describe "#handle(request)" do
    let(:transport) do
      connection = SockJS::Connection.new {}
      connection.sessions["b"] = FakeSession.new(self, Hash.new)

      described_class.new(connection, Hash.new)
    end

    let(:request) do
      @request ||= FakeRequest.new.tap do |request|
        request.path_info = "/a/b/jsonp_send"
      end
    end

    let(:response) do
      transport.handle(request)
    end

    context "with data" do
      context "with application/x-www-form-urlencoded" do
        context "with valid data" do
          let(:request) do
            @request ||= FakeRequest.new.tap do |request|
              request.path_info = "/a/b/jsonp_send"
              request.content_type = "application/x-www-form-urlencoded"
              request.data = "d=sth"
            end
          end

          context "with a valid session" do
            # TODO
          end

          context "without a valid session" do
            # TODO
          end
        end
      end

      context "with any other MIME type" do
        let(:request) do
          FakeRequest.new.tap do |request|
            request.path_info = "/a/b/jsonp_send"
            request.data = "data"
          end
        end

        context "with a valid session" do
          # TODO
        end

        context "without a valid session" do
          # TODO
        end
      end
    end

    [nil, "", "d=", "f=test"].each do |data|
      context "without data = #{data.inspect}" do
        let(:request) do
          FakeRequest.new.tap do |request|
            request.path_info = "/a/b/jsonp_send"
            request.content_type = "application/x-www-form-urlencoded"
            request.data = data
          end
        end
<commit_msg>Some stuff is no necessary anymore ...<commit_after>#!/usr/bin/env bundle exec rspec
# encoding: utf-8

require "spec_helper"

require "sockjs"
require "sockjs/transports/jsonp"

class SockJS::Transports::JSONP
  def session_class
    FakeSession
  end
end

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

  describe "#handle(request)" do
    let(:transport) do
      described_class.new(SockJS::Connection.new {}, Hash.new)
    end

    let(:request) do
      @request ||= FakeRequest.new.tap do |request|
        request.path_info = "/echo/a/b/jsonp"
      end
    end

    let(:response) do
      transport.handle(request)
    end

    context "with callback specified" do
      let(:request) do
        @request ||= FakeRequest.new.tap do |request|
          request.path_info = "/echo/a/b/jsonp"
          request.callback = "clbk"
        end
      end

      context "with a session" do
        let(:transport) do
          connection = SockJS::Connection.new {}
          connection.sessions["b"] = FakeSession.new(self, Hash.new)

          described_class.new(connection, Hash.new)
        end

        it "should respond with HTTP 200" do
          response.status.should eql(200)
        end

        it "should respond with plain text MIME type" do
          response.headers["Content-Type"].should match("text/plain")
        end

        it "should respond with a body"
      end

      context "without any session" do
        it "should respond with HTTP 200" do
          response.status.should eql(200)
        end

        it "should respond with javascript MIME type" do
          response.headers["Content-Type"].should match("application/javascript")
        end

        it "should set access control" do
          response.headers["Access-Control-Allow-Origin"].should eql(request.origin)
          response.headers["Access-Control-Allow-Credentials"].should eql("true")
        end

        it "should disable caching" do
          response.headers["Cache-Control"].should eql("no-store, no-cache, must-revalidate, max-age=0")
        end

        it "should open a new session"
      end
    end

    context "without callback specified" do
      it "should respond with HTTP 500" do
        response.status.should eql(500)
      end

      it "should respond with HTML MIME type" do
        response.headers["Content-Type"].should match("text/html")
      end

      it "should return error message in the body" do
        response # Run the handler.
        request.chunks.last.should match(/"callback" parameter required/)
      end
    end
  end

  describe "#format_frame(payload)" do
    it "should format payload"
  end
end



describe SockJS::Transports::JSONPSend do
  it_should_match_path  "server/session/jsonp_send"
  it_should_have_method "POST"
  transport_handler_eql "a/b/jsonp_send", "POST"

  describe "#handle(request)" do
    let(:transport) do
      connection = SockJS::Connection.new {}
      connection.sessions["b"] = FakeSession.new(self, Hash.new)

      described_class.new(connection, Hash.new)
    end

    let(:request) do
      @request ||= FakeRequest.new.tap do |request|
        request.path_info = "/a/b/jsonp_send"
      end
    end

    let(:response) do
      transport.handle(request)
    end

    context "with valid data" do
      context "with application/x-www-form-urlencoded" do
        let(:request) do
          @request ||= FakeRequest.new.tap do |request|
            request.path_info = "/a/b/jsonp_send"
            request.content_type = "application/x-www-form-urlencoded"
            request.data = "d=sth"
          end
        end

        context "with a valid session" do
          # TODO
        end

        context "without a valid session" do
          # TODO
        end
      end

      context "with any other MIME type" do
        let(:request) do
          FakeRequest.new.tap do |request|
            request.path_info = "/a/b/jsonp_send"
            request.data = "data"
          end
        end

        context "with a valid session" do
          # TODO
        end

        context "without a valid session" do
          # TODO
        end
      end
    end

    [nil, "", "d=", "f=test"].each do |data|
      context "without data = #{data.inspect}" do
        let(:request) do
          FakeRequest.new.tap do |request|
            request.path_info = "/a/b/jsonp_send"
            request.content_type = "application/x-www-form-urlencoded"
            request.data = data
          end
        end

{'fraction_non_alphanumeric': 0.09976272648835202, 'fraction_numerical': 0.004314063848144953, 'mean_word_length': 2.133828996282528, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, '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': '6421913', 'n_tokens_mistral': 2802, 'n_tokens_neox': 2677, 'n_words': 729}