<commit_before>require File.expand_path("../../../../spec_helper", __FILE__)
require 'nokogiri'

module Bcsec::Modes::Support
  describe LoginFormAssetProvider do
    before do
      @provider = LoginFormAssetProvider.new
    end

    describe "#login_html" do
      before do
        env = { 'SCRIPT_NAME' => '/foo' }
        @doc = Nokogiri.HTML(@provider.login_html(env))
      end

      it "includes SCRIPT_NAME in the postback URL" do
        (@doc/'form').first.attributes["action"].value.should == "/foo/login"
      end

      it "includes SCRIPT_NAME in the CSS URL" do
        (@doc/'link[rel="stylesheet"]').first.attributes["href"].value.should == "/foo/login/login.css"
      end

      it "can render a 'login failed' message" do
        @doc = Nokogiri.HTML(@provider.login_html({}, { :login_failed => true }))

        (@doc/'.error').first.inner_html.should == 'Login failed'
      end

      it "can render a 'logged out' message" do
        @doc = Nokogiri.HTML(@provider.login_html({}, { :logged_out => true }))

        (@doc/'h1').first.inner_html.should == 'Logged out'
      end

      it "can render text in the username text field" do
        @doc = Nokogiri.HTML(@provider.login_html({}, { :username => "user" }))

        (@doc/'input[name="username"]').first['value'].should == 'user'
      end

      it "can store a URL to go to after login succeeds" do
        @doc = Nokogiri.HTML(@provider.login_html({}, { :url => 'http://www.example.edu' }))

        (@doc/'input[name="url"]').first['value'].should == 'http://www.example.edu'
      end

      it "escapes HTML in usernames" do
        html = @provider.login_html({}, { :username => "user<a/>" })

        # Annoyingly, Nokogiri.HTML automatically unescapes escaped entities in
        # attribute values.
        html.should include("user&lt;a/&gt;")
        html.should_not include("user<a/>")
      end
    end

    describe "#login_css" do
      it "provides CSS for the login form" do
        expected_css = File.read(File.join(File.dirname(__FILE__),
                                           %w(.. .. .. .. assets bcsec modes middleware form login.css)))

        @provider.login_css.should == expected_css
      end
    end
  end
end<commit_msg>Update HTML escape output to match what Rack 1.3 does.<commit_after>require File.expand_path("../../../../spec_helper", __FILE__)
require 'nokogiri'

module Bcsec::Modes::Support
  describe LoginFormAssetProvider do
    before do
      @provider = LoginFormAssetProvider.new
    end

    describe "#login_html" do
      before do
        env = { 'SCRIPT_NAME' => '/foo' }
        @doc = Nokogiri.HTML(@provider.login_html(env))
      end

      it "includes SCRIPT_NAME in the postback URL" do
        (@doc/'form').first.attributes["action"].value.should == "/foo/login"
      end

      it "includes SCRIPT_NAME in the CSS URL" do
        (@doc/'link[rel="stylesheet"]').first.attributes["href"].value.should == "/foo/login/login.css"
      end

      it "can render a 'login failed' message" do
        @doc = Nokogiri.HTML(@provider.login_html({}, { :login_failed => true }))

        (@doc/'.error').first.inner_html.should == 'Login failed'
      end

      it "can render a 'logged out' message" do
        @doc = Nokogiri.HTML(@provider.login_html({}, { :logged_out => true }))

        (@doc/'h1').first.inner_html.should == 'Logged out'
      end

      it "can render text in the username text field" do
        @doc = Nokogiri.HTML(@provider.login_html({}, { :username => "user" }))

        (@doc/'input[name="username"]').first['value'].should == 'user'
      end

      it "can store a URL to go to after login succeeds" do
        @doc = Nokogiri.HTML(@provider.login_html({}, { :url => 'http://www.example.edu' }))

        (@doc/'input[name="url"]').first['value'].should == 'http://www.example.edu'
      end

      it "escapes HTML in usernames" do
        html = @provider.login_html({}, { :username => "user<a/>" })

        # Annoyingly, Nokogiri.HTML automatically unescapes escaped entities in
        # attribute values.
        html.should include("user&lt;a&#x2F;&gt;");
        html.should_not include("user<a/>")
      end
    end

    describe "#login_css" do
      it "provides CSS for the login form" do
        expected_css = File.read(File.join(File.dirname(__FILE__),
                                           %w(.. .. .. .. assets bcsec modes middleware form login.css)))

        @provider.login_css.should == expected_css
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.15206356212756567, 'fraction_numerical': 0.0011035091591260207, 'mean_word_length': 2.488837567359507, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3106311', 'n_tokens_mistral': 1413, 'n_tokens_neox': 1367, 'n_words': 342}