<commit_before>require 'rails/test_help'

class Flunk < ActionDispatch::IntegrationTest

  def self.test(resource, action, &block)

    if action.class == Hash
      hash          = action
      name          = hash[:name]
      action        = hash[:action]
      flunk_reason  = hash[:flunk_reason]
    end

    new_proc = Proc.new do
      @resource     ||= resource
      @action       ||= action
      @flunk_reason ||= flunk_reason
      instance_eval(&block)
      result
      instance_eval(&@after) unless @after.nil?
    end

    name = resource + ": " + action if name.nil?
    super name, &new_proc
  end

  def self.flunk(resource, action, failure_reason, &block)
    name = "Flunked #{resource}: #{action} (#{failure_reason})"
    test(resource, { action: action, name: name, flunk_reason: failure_reason }, &block)
  end


  def result
    if !@result_fetched
      @result_fetched = true

      @username   ||= self.class.config.read_username
      @password   ||= self.class.config.read_password
      @auth_token ||= self.class.config.read_auth_token
      @headers    ||= self.class.config.read_headers
      @method     ||= self.class.config.read_method
      @ssl        ||= self.class.config.read_ssl

      @headers = {
        "CONTENT_TYPE" => "application/json",
        "HTTP_ACCEPT" => "application/json"
      }.merge!(@headers || {})
      # @headers ||= {}

      if @username || @password
        @headers["HTTP_AUTHORIZATION"] = "Basic #{Base64.encode64(@username.to_s + ":" + @password.to_s)}".strip
      elsif @auth_token
        @headers["HTTP_AUTHORIZATION"] = "Token token=\"#{@auth_token}\"".strip
      end

      @body = @body.to_json if @body.present?

      send @method, @path, @body, @headers

      @response = response


      expected_status_code = Rack::Utils::SYMBOL_TO_STATUS_CODE[@status]
<commit_msg>updating for rails 5<commit_after>require 'rails/test_help'

class Flunk < ActionDispatch::IntegrationTest

  def self.test(resource, action, &block)

    if action.class == Hash
      hash          = action
      name          = hash[:name]
      action        = hash[:action]
      flunk_reason  = hash[:flunk_reason]
    end

    new_proc = Proc.new do
      @resource     ||= resource
      @action       ||= action
      @flunk_reason ||= flunk_reason
      instance_eval(&block)
      result
      instance_eval(&@after) unless @after.nil?
    end

    name = resource + ": " + action if name.nil?
    super name, &new_proc
  end

  def self.flunk(resource, action, failure_reason, &block)
    name = "Flunked #{resource}: #{action} (#{failure_reason})"
    test(resource, { action: action, name: name, flunk_reason: failure_reason }, &block)
  end


  def result
    if !@result_fetched
      @result_fetched = true

      @username   ||= self.class.config.read_username
      @password   ||= self.class.config.read_password
      @auth_token ||= self.class.config.read_auth_token
      @headers    ||= self.class.config.read_headers
      @method     ||= self.class.config.read_method
      @ssl        ||= self.class.config.read_ssl

      @headers = {
        "CONTENT_TYPE" => "application/json",
        "HTTP_ACCEPT" => "application/json"
      }.merge!(@headers || {})
      # @headers ||= {}

      if @username || @password
        @headers["HTTP_AUTHORIZATION"] = "Basic #{Base64.encode64(@username.to_s + ":" + @password.to_s)}".strip
      elsif @auth_token
        @headers["HTTP_AUTHORIZATION"] = "Token token=\"#{@auth_token}\"".strip
      end

      @body = @body.to_json if @body.present?

      send @method, @path, params: @body, headers: @headers

      @response = response


      expected_status_code = Rack::Utils::SYMBOL_TO_STATUS_CODE[@status]

{'fraction_non_alphanumeric': 0.13504823151125403, 'fraction_numerical': 0.002411575562700965, 'mean_word_length': 2.6742125984251968, 'pattern_counts': {'":': 4, '<': 5, '<?xml version=': 0, '>': 7, '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': '595704', 'n_tokens_mistral': 1205, 'n_tokens_neox': 1162, 'n_words': 235}