<commit_before>require 'spec_helper'

describe Grape::Middleware::Base do
  subject { Grape::Middleware::Base.new(blank_app) }  
  let(:blank_app) { lambda{|env| [200, {}, 'Hi there.']} }
  
  before do
    # Keep it one object for testing.
    subject.stub!(:dup).and_return(subject)
  end
  
  it 'should have the app as an accessor' do
    subject.app.should == blank_app
  end
  
  it 'should be able to access the request' do
    subject.call({})
    subject.request.should be_kind_of(Rack::Request)
  end
  
  it 'should call through to the app' do
    subject.call({}).should == [200, {}, 'Hi there.']
  end
  
  context 'callbacks' do
    it 'should call #before' do
      subject.should_receive(:before)
    end
  
    it 'should call #after' do
      subject.should_receive(:after)
    end
    
    after{ subject.call!({}) }
  end
  
  it 'should be able to access the response' do
    subject.call({})
    subject.response.should be_kind_of(Rack::Response)
  end
  
  context 'options' do
    it 'should persist options passed at initialization' do
      Grape::Middleware::Base.new(blank_app, {:abc => true}).options[:abc].should be_true
    end
    
    context 'defaults' do
      class ExampleWare < Grape::Middleware::Base
        def default_options
          {:monkey => true}
        end
      end
      
      it 'should persist the default options' do
        ExampleWare.new(blank_app).options[:monkey].should be_true
      end
      
      it 'should override default options when provided' do
        ExampleWare.new(blank_app, :monkey => false).options[:monkey].should be_false
      end
    end
  end
end<commit_msg>add unit test for custom response formatter_for to base_spec<commit_after>require 'spec_helper'

describe Grape::Middleware::Base do
  subject { Grape::Middleware::Base.new(blank_app) }  
  let(:blank_app) { lambda{|env| [200, {}, 'Hi there.']} }
  
  before do
    # Keep it one object for testing.
    subject.stub!(:dup).and_return(subject)
  end
  
  it 'should have the app as an accessor' do
    subject.app.should == blank_app
  end
  
  it 'should be able to access the request' do
    subject.call({})
    subject.request.should be_kind_of(Rack::Request)
  end
  
  it 'should call through to the app' do
    subject.call({}).should == [200, {}, 'Hi there.']
  end
  
  context 'callbacks' do
    it 'should call #before' do
      subject.should_receive(:before)
    end
  
    it 'should call #after' do
      subject.should_receive(:after)
    end
    
    after{ subject.call!({}) }
  end
  
  it 'should be able to access the response' do
    subject.call({})
    subject.response.should be_kind_of(Rack::Response)
  end
  
  context 'options' do
    it 'should persist options passed at initialization' do
      Grape::Middleware::Base.new(blank_app, {:abc => true}).options[:abc].should be_true
    end
    
    context 'defaults' do
      class ExampleWare < Grape::Middleware::Base
        def default_options
          {:monkey => true}
        end
      end
      
      it 'should persist the default options' do
        ExampleWare.new(blank_app).options[:monkey].should be_true
      end
      
      it 'should override default options when provided' do
        ExampleWare.new(blank_app, :monkey => false).options[:monkey].should be_false
      end
    end
  end

  context 'formatters' do
    it 'should query the body object for a method to respond to a custom content type' do
      class Custom
        attr_accessor :options
        include Grape::Middleware::Base::Formats
        def to_custom
          'custom'
        end
      end

      c = Custom.new
      c.options = {}
      c.formatter_for('custom').call(c).should == 'custom'
    end
  end
end
{'fraction_non_alphanumeric': 0.1046137339055794, 'fraction_numerical': 0.003218884120171674, 'mean_word_length': 2.6847826086956523, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, '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': '5774750', 'n_tokens_mistral': 1177, 'n_tokens_neox': 1151, 'n_words': 339}