diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 455412f..cb9eede 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -7,6 +7,7 @@ require 'rspec' require File.expand_path '../../lib/yodatra.rb', __FILE__ +require File.expand_path '../../lib/yodatra/api_formatter.rb', __FILE__ require File.expand_path '../../lib/yodatra/models_controller.rb', __FILE__ require File.expand_path '../data/ar_models_controller.rb', __FILE__ @@ -15,6 +16,7 @@ module RSpecMixin def app Sinatra.new { use Yodatra::Base + use Yodatra::ApiFormatter use Yodatra::ModelsController use ArModelsController } diff --git a/spec/unit/api_formatter_spec.rb b/spec/unit/api_formatter_spec.rb new file mode 100644 index 0000000..858adf1 --- /dev/null +++ b/spec/unit/api_formatter_spec.rb @@ -0,0 +1,33 @@ +require File.expand_path '../../spec_helper.rb', __FILE__ + +describe Yodatra::ApiFormatter do + let(:app) { proc{[200,{},['Hello, world.']]} } + + before do + @stack = Yodatra::ApiFormatter.new(app, &bloc) + @request = Rack::MockRequest.new(@stack) + end + + context 'with a unity bloc' do + let(:bloc) { proc{ |s,h,r| [s,h,r] } } + + it 'transforms the response with the given block' do + response = @request.get('/') + expect(response.body).to eq 'Hello, world.' + end + end + + context 'with an object wrapper bloc' do + let(:bloc) do + proc do |s,h,r| + transformed = r.map{ |rr| {data: rr}.to_json } + [s,h,transformed] + end + end + + it 'transforms the response with the given block' do + response = @request.get('/') + expect(JSON.parse(response.body)['data']).to eq 'Hello, world.' + end + end +end