diff --git a/CHANGELOG.md b/CHANGELOG.md index 47820eae0e..f1c093d233 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ * [#703](https://github.com/intridea/grape/pull/703): Removed `Grape::Middleware::Auth::Digest` - [@dspaeth-faber](https://github.com/dspaeth-faber). * [#703](https://github.com/intridea/grape/pull/703): Removed `Grape::Middleware::Auth::OAuth2` - [@dspaeth-faber](https://github.com/dspaeth-faber). * [#719](https://github.com/intridea/grape/pull/719): Allow passing options hash to a custom validator - [@elado](https://github.com/elado). +* [#716](https://github.com/intridea/grape/pull/716): Calling `content-type` will now return the current content-type - [@dblock](https://github.com/dblock). * Your contribution here. 0.8.0 (7/10/2014) diff --git a/lib/grape/dsl/inside_route.rb b/lib/grape/dsl/inside_route.rb index 2d1b08eb10..283f126ca6 100644 --- a/lib/grape/dsl/inside_route.rb +++ b/lib/grape/dsl/inside_route.rb @@ -5,10 +5,6 @@ module DSL module InsideRoute extend ActiveSupport::Concern - included do - - end - # A filtering method that will return a hash # consisting only of keys that have been declared by a # `params` statement against the current/target endpoint or parent @@ -117,8 +113,12 @@ def header(key = nil, val = nil) end # Set response content-type - def content_type(val) - header('Content-Type', val) + def content_type(val = nil) + if val + header('Content-Type', val) + else + header['Content-Type'] + end end # Set or get a cookie @@ -216,9 +216,6 @@ def present(*args) def route env["rack.routing_args"][:route_info] end - - module ClassMethods - end end end end diff --git a/spec/grape/dsl/inside_route_spec.rb b/spec/grape/dsl/inside_route_spec.rb index 7e2a4209ef..61cd3a9e3d 100644 --- a/spec/grape/dsl/inside_route_spec.rb +++ b/spec/grape/dsl/inside_route_spec.rb @@ -5,56 +5,218 @@ module DSL module InsideRouteSpec class Dummy include Grape::DSL::InsideRoute + + attr_reader :env, :request, :settings + + def initialize + @env = {} + @header = {} + @settings = Grape::Util::HashStack.new + end end end describe Endpoint do - subject { EndpointSpec::Dummy.new } + subject { InsideRouteSpec::Dummy.new } - xdescribe '#declared' do - it 'does some thing' - end + describe '#version' do + it 'defaults to nil' do + expect(subject.version).to be nil + end - xdescribe '#version' do - it 'does some thing' + it 'returns env[api.version]' do + subject.env['api.version'] = 'dummy' + expect(subject.version).to eq 'dummy' + end end - xdescribe '#error!' do - it 'does some thing' + describe '#error!' do + it 'throws :error' do + expect { subject.error! 'Not Found', 404 }.to throw_symbol(:error) + end + + describe 'thrown' do + before do + catch(:error) { subject.error! 'Not Found', 404 } + end + it 'sets status' do + expect(subject.status).to eq 404 + end + end + + describe 'default_error_status' do + before do + subject.settings[:default_error_status] = 500 + catch(:error) { subject.error! 'Unknown' } + end + it 'sets status to default_error_status' do + expect(subject.status).to eq 500 + end + end + + # self.status(status || settings[:default_error_status]) + # throw :error, message: message, status: self.status, headers: headers end - xdescribe '#redirect' do - it 'does some thing' + describe '#redirect' do + describe 'default' do + before do + subject.redirect '/' + end + + it 'sets status to 302' do + expect(subject.status).to eq 302 + end + + it 'sets location header' do + expect(subject.header['Location']).to eq '/' + end + end + + describe 'permanent' do + before do + subject.redirect '/', permanent: true + end + + it 'sets status to 301' do + expect(subject.status).to eq 301 + end + + it 'sets location header' do + expect(subject.header['Location']).to eq '/' + end + end end - xdescribe '#status' do - it 'does some thing' + describe '#status' do + ['GET', 'PUT', 'DELETE', 'OPTIONS'].each do |method| + it 'defaults to 200 on GET' do + request = Grape::Request.new(Rack::MockRequest.env_for('/', method: method)) + expect(subject).to receive(:request).and_return(request) + expect(subject.status).to eq 200 + end + end + + it 'defaults to 201 on POST' do + request = Grape::Request.new(Rack::MockRequest.env_for('/', method: 'POST')) + expect(subject).to receive(:request).and_return(request) + expect(subject.status).to eq 201 + end + + it 'returns status set' do + subject.status 501 + expect(subject.status).to eq 501 + end end - xdescribe '#header' do - it 'does some thing' + describe '#header' do + describe 'set' do + before do + subject.header 'Name', 'Value' + end + + it 'returns value' do + expect(subject.header['Name']).to eq 'Value' + expect(subject.header('Name')).to eq 'Value' + end + end + + it 'returns nil' do + expect(subject.header['Name']).to be nil + expect(subject.header('Name')).to be nil + end end - xdescribe '#content_type' do - it 'does some thing' + describe '#content_type' do + describe 'set' do + before do + subject.content_type 'text/plain' + end + + it 'returns value' do + expect(subject.content_type).to eq 'text/plain' + end + end + + it 'returns default' do + expect(subject.content_type).to be nil + end end - xdescribe '#cookies' do - it 'does some thing' + describe '#cookies' do + it 'returns an instance of Cookies' do + expect(subject.cookies).to be_a Grape::Cookies + end end - xdescribe '#body' do - it 'does some thing' + describe '#body' do + describe 'set' do + before do + subject.body 'body' + end + + it 'returns value' do + expect(subject.body).to eq 'body' + end + end + + it 'returns default' do + expect(subject.body).to be nil + end end - xdescribe '#route' do - it 'does some thing' + describe '#route' do + before do + subject.env['rack.routing_args'] = {} + subject.env['rack.routing_args'][:route_info] = 'dummy' + end + + it 'returns route_info' do + expect(subject.route).to eq 'dummy' + end end - xdescribe '#present' do - it 'does some thing' + describe '#present' do + # see entity_spec.rb for entity representation spec coverage + + describe 'dummy' do + before do + subject.present 'dummy' + end + + it 'presents dummy object' do + expect(subject.body).to eq 'dummy' + end + end + + describe 'with' do + describe 'entity' do + let(:entity_mock) do + entity_mock = Object.new + allow(entity_mock).to receive(:represent).and_return('dummy') + entity_mock + end + + describe 'instance' do + before do + subject.present 'dummy', with: entity_mock + end + + it 'presents dummy object' do + expect(subject.body).to eq 'dummy' + end + end + end + end end + describe '#declared' do + # see endpoint_spec.rb#declared for spec coverage + + it 'returns an empty hash' do + expect(subject.declared({})).to eq({}) + end + end end end end