<commit_before>proc { |p| $:.unshift(p) unless $:.any? { |lp| File.expand_path(lp) == p } }.call(File.expand_path('.', File.dirname(__FILE__)))
require 'helper'
require 'logger'
require 'stringio'

describe ApiHammer::RequestLogger do
  let(:logio) { StringIO.new }
  let(:logger) { Logger.new(logio) }

  it 'logs' do
    app = ApiHammer::RequestLogger.new(proc { |env| [200, {}, []] }, logger)
    app.call(Rack::MockRequest.env_for('/')).last.close
    assert_match(/200/, logio.string)
  end

  it 'colors by status' do
    {200 => :intense_green, 400 => :intense_yellow, 500 => :intense_red, 300 => :white}.each do |status, color|
      app = ApiHammer::RequestLogger.new(proc { |env| [status, {}, []] }, logger)
      app.call(Rack::MockRequest.env_for('/')).last.close
      assert(logio.string.include?(Term::ANSIColor.send(color, status.to_s)))
    end
  end

  it 'logs id and uuid (json)' do
    body = %q({"uuid": "theuuid", "foo_uuid": "thefoouuid", "id": "theid", "id_for_x": "theidforx", "bar.id": "thebarid", "baz-guid": "bazzz"})
    app = ApiHammer::RequestLogger.new(proc { |env| [200, {"Content-Type" => 'application/json; charset=UTF8'}, [body]] }, logger)
    app.call(Rack::MockRequest.env_for('/')).last.close
    assert_match(%q("body_ids":{"uuid":"theuuid","foo_uuid":"thefoouuid","id":"theid","id_for_x":"theidforx","bar.id":"thebarid","baz-guid":"bazzz"}), logio.string)
  end

  it 'logs id and uuid (form encoded)' do
    body = %q(uuid=theuuid&foo_uuid=thefoouuid&id=theid&id_for_x=theidforx&bar.id=thebarid&baz-guid=bazzz)
    app = ApiHammer::RequestLogger.new(proc { |env| [200, {"Content-Type" => 'application/x-www-form-urlencoded; charset=UTF8'}, [body]] }, logger)
    app.call(Rack::MockRequest.env_for('/')).last.close
    assert_match(%q("body_ids":{"uuid":"theuuid","foo_uuid":"thefoouuid","id":"theid","id_for_x":"theidforx","bar.id":"thebarid","baz-guid":"bazzz"}), logio.string)
  end

  it 'logs request and response body on error' do
    app = ApiHammer::RequestLogger.new(proc { |env| [400, {}, ['the_response_body']] }, logger)
    app.call(Rack::MockRequest.env_for('/', :input => 'the_request_body')).last.close
    assert_match(/"request":\{[^\{]*"body":"the_request_body"/, logio.string)
    assert_match(/"response":\{[^\{]*"body":"the_response_body"/, logio.string)
  end
end<commit_msg>fix test<commit_after>proc { |p| $:.unshift(p) unless $:.any? { |lp| File.expand_path(lp) == p } }.call(File.expand_path('.', File.dirname(__FILE__)))
require 'helper'
require 'logger'
require 'stringio'

describe ApiHammer::RequestLogger do
  let(:logio) { StringIO.new }
  let(:logger) { Logger.new(logio) }

  it 'logs' do
    app = ApiHammer::RequestLogger.new(proc { |env| [200, {}, []] }, logger)
    app.call(Rack::MockRequest.env_for('/')).last.close
    assert_match(/200/, logio.string)
  end

  it 'colors by status' do
    {200 => :intense_green, 400 => :intense_yellow, 500 => :intense_red, 300 => :white}.each do |status, color|
      app = ApiHammer::RequestLogger.new(proc { |env| [status, {}, []] }, logger)
      app.call(Rack::MockRequest.env_for('/')).last.close
      assert(logio.string.include?(Term::ANSIColor.send(color, status.to_s)))
    end
  end

  it 'logs id and uuid (json)' do
    body = %q({"uuid": "theuuid", "foo_uuid": "thefoouuid", "id": "theid", "id_for_x": "theidforx", "bar.id": "thebarid", "baz-guid": "bazzz"})
    app = ApiHammer::RequestLogger.new(proc { |env| [200, {"Content-Type" => 'application/json; charset=UTF8'}, [body]] }, logger)
    app.call(Rack::MockRequest.env_for('/')).last.close
    assert_match(%q("body_ids":{"uuid":"theuuid","foo_uuid":"thefoouuid","id":"theid","id_for_x":"theidforx","bar.id":"thebarid","baz-guid":"bazzz"}), logio.string)
  end

  it 'logs id and uuid (form encoded)' do
    body = %q(uuid=theuuid&foo_uuid=thefoouuid&id=theid&id_for_x=theidforx&bar.id=thebarid&baz-guid=bazzz)
    app = ApiHammer::RequestLogger.new(proc { |env| [200, {"Content-Type" => 'application/x-www-form-urlencoded; charset=UTF8'}, [body]] }, logger)
    app.call(Rack::MockRequest.env_for('/')).last.close
    assert_match(%q("body_ids":{"uuid":"theuuid","foo_uuid":"thefoouuid","id":"theid","id_for_x":"theidforx","bar.id":"thebarid","baz-guid":"bazzz"}), logio.string)
  end

  it 'logs request and response body on error' do
    app = ApiHammer::RequestLogger.new(proc { |env| [400, {}, ['the_response_body']] }, logger)
    app.call(Rack::MockRequest.env_for('/', :input => 'the_request_body')).last.close
    assert_match(/"request":\{.*"body":"the_request_body"/, logio.string)
    assert_match(/"response":\{.*"body":"the_response_body"/, logio.string)
  end
end
{'fraction_non_alphanumeric': 0.21579961464354527, 'fraction_numerical': 0.012417041318775423, 'mean_word_length': 5.3478260869565215, 'pattern_counts': {'":': 48, '<': 3, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '636845', 'n_tokens_mistral': 1804, 'n_tokens_neox': 1729, 'n_words': 282}