<commit_before>require 'spec_helper'
require 'rack/app/test'
require 'securerandom'
require 'tmpdir'

out_file = File.join(Dir.tmpdir,SecureRandom.hex)
in_file = File.join(Dir.tmpdir,SecureRandom.hex)

Kernel.at_exit do
  [in_file, out_file].each { |fp| File.delete(fp) if File.exist?(fp) }
end

InstanceMethodsSpec = Class.new
describe Rack::App::InstanceMethods do

  include Rack::App::Test

  rack_app do

    get '/serve_file' do
      serve_file Rack::App::Utils.pwd('spec', 'fixtures', 'raw.txt')
    end

    get '/serve_file_break_execution' do
      serve_file Rack::App::Utils.pwd('spec', 'fixtures', 'raw.txt')
      raise 'not raised'
    end

    get '/redirect_to' do
      redirect_to '/hello'
    end

    get '/redirect_to_with_params' do
      redirect_to '/hello', :hello => 'world'
    end

    get '/redirect_to_break_execution' do
      redirect_to '/hello'
      raise 'never happen'
    end

    get '/payload_to_file' do
      payload_to_file(out_file)
    end

  end

  describe '#serve_file' do

    it 'should serve file content with Rack::File' do
      response = get(:url => '/serve_file')

      expect(response.status).to eq 200
      expect(response.body).to eq "hello world!\nhow you doing?"
      expect(response.body).to eq "hello world!\nhow you doing?"

      new_response = get(:url => '/serve_file',
                         :headers => {'IF_MODIFIED_SINCE' => response.headers["Last-Modified"]})

      expect(new_response.status).to eq 304

    end

    it 'should break the flow of the code processing' do
      expect{get(:url => '/serve_file_break_execution')}.to_not raise_error
      response = get(:url => '/serve_file_break_execution')
      expect(response.status).to eq 200
      expect(response.body).to eq "hello world!\nhow you doing?"
    end

  end

  describe '#stream_payload_to' do

    file_size_in_mb = (ENV['STREAM_FILE_SIZE'] || 128).to_i
<commit_msg>chore: decrease file size for stream payload test<commit_after>require 'spec_helper'
require 'rack/app/test'
require 'securerandom'
require 'tmpdir'

out_file = File.join(Dir.tmpdir,SecureRandom.hex)
in_file = File.join(Dir.tmpdir,SecureRandom.hex)

Kernel.at_exit do
  [in_file, out_file].each { |fp| File.delete(fp) if File.exist?(fp) }
end

InstanceMethodsSpec = Class.new
describe Rack::App::InstanceMethods do

  include Rack::App::Test

  rack_app do

    get '/serve_file' do
      serve_file Rack::App::Utils.pwd('spec', 'fixtures', 'raw.txt')
    end

    get '/serve_file_break_execution' do
      serve_file Rack::App::Utils.pwd('spec', 'fixtures', 'raw.txt')
      raise 'not raised'
    end

    get '/redirect_to' do
      redirect_to '/hello'
    end

    get '/redirect_to_with_params' do
      redirect_to '/hello', :hello => 'world'
    end

    get '/redirect_to_break_execution' do
      redirect_to '/hello'
      raise 'never happen'
    end

    get '/payload_to_file' do
      payload_to_file(out_file)
    end

  end

  describe '#serve_file' do

    it 'should serve file content with Rack::File' do
      response = get(:url => '/serve_file')

      expect(response.status).to eq 200
      expect(response.body).to eq "hello world!\nhow you doing?"
      expect(response.body).to eq "hello world!\nhow you doing?"

      new_response = get(:url => '/serve_file',
                         :headers => {'IF_MODIFIED_SINCE' => response.headers["Last-Modified"]})

      expect(new_response.status).to eq 304

    end

    it 'should break the flow of the code processing' do
      expect{get(:url => '/serve_file_break_execution')}.to_not raise_error
      response = get(:url => '/serve_file_break_execution')
      expect(response.status).to eq 200
      expect(response.body).to eq "hello world!\nhow you doing?"
    end

  end

  describe '#stream_payload_to' do

    file_size_in_mb = (ENV['STREAM_FILE_SIZE'] || 64).to_i

{'fraction_non_alphanumeric': 0.1238686320144815, 'fraction_numerical': 0.005947763123868632, 'mean_word_length': 3.2319474835886215, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, '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': '4996914', 'n_tokens_mistral': 1325, 'n_tokens_neox': 1251, 'n_words': 307}