<commit_before>require 'rack/test'
require 'nokogiri'

class Webcat::Driver::RackTest
  class Node < Struct.new(:session, :node)
    def text
      node.text
    end
    
    def [](name)
      value = node[name.to_s]
      return value.to_s if value
    end

    def value
      node['value'].to_s
    end

    def value=(value)
      node['value'] = value.to_s
    end

    def click
      if tag_name == 'a'
        session.visit(self[:href])
      elsif tag_name == 'input' and self[:type] == 'submit'
        Form.new(session, form).submit(self)
      end
    end
    
    def tag_name
      node.node_name
    end

  private

    def form
      node.ancestors('form').first
    end
  end

  class Form < Node
    def params(button)
      params = []
      params = node.xpath("//input[@type='text']").inject(params) do |agg, input|
        agg << param(input['name'].to_s, input['value'].to_s)
        agg
      end
      params = node.xpath("//textarea").inject(params) do |agg, textarea|
        agg << param(textarea['name'].to_s, textarea.text.to_s)
        agg
      end
      params = node.xpath("//input[@type='radio']").inject(params) do |agg, input|
        agg << param(input['name'].to_s, input['value'].to_s) if input['checked']
        agg
      end
      params = node.xpath("//input[@type='checkbox']").inject(params) do |agg, input|
        agg << param(input['name'].to_s, input['value'].to_s) if input['checked']
        agg
      end
      params = node.xpath("//select").inject(params) do |agg, select|
        option = select.xpath("option[@selected]").first
        option ||= select.xpath('option').first
        agg << param(select['name'].to_s, (option['value'] || option.text).to_s) if option 
        agg
      end
      params << param(button[:name], button.value) if button[:name]
      params.join('&')
    end

    def submit(button)
      session.submit(node['action'].to_s, params(button)) 
    end

  private

    def param(key, value)
      "#{key}=#{value}"
    end
  end
  
  include ::Rack::Test::Methods
  attr_reader :app, :html, :body

  alias_method :response, :last_response
  alias_method :request, :last_request
  
  def initialize(app)
    @app = app
  end<commit_msg>Make weird form submission slightly less weird<commit_after>require 'rack/test'
require 'nokogiri'

class Webcat::Driver::RackTest
  class Node < Struct.new(:session, :node)
    def text
      node.text
    end
    
    def [](name)
      value = node[name.to_s]
      return value.to_s if value
    end

    def value
      node['value'].to_s
    end

    def value=(value)
      node['value'] = value.to_s
    end

    def click
      if tag_name == 'a'
        session.visit(self[:href])
      elsif tag_name == 'input' and self[:type] == 'submit'
        Form.new(session, form).submit(self)
      end
    end
    
    def tag_name
      node.node_name
    end

  private

    def form
      node.ancestors('form').first
    end
  end

  class Form < Node
    def params(button)
      params = []
      params << node.xpath("//input[@type='text']").inject([]) do |agg, input|
        agg << param(input['name'].to_s, input['value'].to_s)
        agg
      end
      params << node.xpath("//textarea").inject([]) do |agg, textarea|
        agg << param(textarea['name'].to_s, textarea.text.to_s)
        agg
      end
      params << node.xpath("//input[@type='radio']").inject([]) do |agg, input|
        agg << param(input['name'].to_s, input['value'].to_s) if input['checked']
        agg
      end
      params << node.xpath("//input[@type='checkbox']").inject([]) do |agg, input|
        agg << param(input['name'].to_s, input['value'].to_s) if input['checked']
        agg
      end
      params << node.xpath("//select").inject([]) do |agg, select|
        option = select.xpath("option[@selected]").first
        option ||= select.xpath('option').first
        agg << param(select['name'].to_s, (option['value'] || option.text).to_s) if option 
        agg
      end
      params << param(button[:name], button.value) if button[:name]
      params.join('&')
    end

    def submit(button)
      session.submit(node['action'].to_s, params(button)) 
    end

  private

    def param(key, value)
      "#{key}=#{value}"
    end
  end
  
  include ::Rack::Test::Methods
  attr_reader :app, :html, :body

  alias_method :response, :last_response
  alias_method :request, :last_request
  
  def initialize(app)
    @app = app
  end
{'fraction_non_alphanumeric': 0.15589950650515927, 'fraction_numerical': 0.0, 'mean_word_length': 2.4863174354964817, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 3, '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': '6026872', 'n_tokens_mistral': 1492, 'n_tokens_neox': 1445, 'n_words': 327}