| 
									
										
										
										
											2015-12-09 20:40:33 +08:00
										 |  |  | require 'spec_helper' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | describe 'exposing' do | 
					
						
							| 
									
										
										
										
											2015-12-18 05:12:00 +08:00
										 |  |  |   include_context "the api entities" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-09 20:40:33 +08:00
										 |  |  |   before :all do | 
					
						
							|  |  |  |     module TheApi | 
					
						
							|  |  |  |       class ResponseApi < Grape::API | 
					
						
							|  |  |  |         format :json | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         desc 'This returns something', | 
					
						
							|  |  |  |           params: Entities::UseResponse.documentation, | 
					
						
							|  |  |  |           failure: [{code: 400, message: 'NotFound', model: Entities::ApiError}] | 
					
						
							|  |  |  |         get '/params_response' do | 
					
						
							|  |  |  |           { "declared_params" => declared(params) } | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         desc 'This returns something', | 
					
						
							|  |  |  |           entity: Entities::UseResponse, | 
					
						
							|  |  |  |           failure: [{code: 400, message: 'NotFound', model: Entities::ApiError}] | 
					
						
							|  |  |  |         get '/entity_response' do | 
					
						
							|  |  |  |           { "declared_params" => declared(params) } | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         add_swagger_documentation | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def app | 
					
						
							|  |  |  |     TheApi::ResponseApi | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe "response from entity" do | 
					
						
							|  |  |  |     subject do | 
					
						
							|  |  |  |       get '/swagger_doc/entity_response' | 
					
						
							|  |  |  |       JSON.parse(last_response.body) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     describe "uses entity as response object" do | 
					
						
							|  |  |  |       specify do | 
					
						
							|  |  |  |         expect(subject).to eql({ | 
					
						
							|  |  |  |           "info"=>{"title"=>"API title", "version"=>"v1"}, | 
					
						
							|  |  |  |           "swagger"=>"2.0", | 
					
						
							|  |  |  |           "produces"=>["application/json"], | 
					
						
							|  |  |  |           "host"=>"example.org", | 
					
						
							|  |  |  |           "schemes"=>["https", "http"], | 
					
						
							|  |  |  |           "paths"=>{ | 
					
						
							|  |  |  |             "/entity_response"=>{ | 
					
						
							|  |  |  |               "get"=>{ | 
					
						
							|  |  |  |                 "produces"=>["application/json"], | 
					
						
							|  |  |  |                 "responses"=>{ | 
					
						
							|  |  |  |                   "200"=>{"description"=>"This returns something", "schema"=>{"$ref"=>"#/definitions/UseResponse"}}, | 
					
						
							|  |  |  |                   "400"=>{"description"=>"NotFound", "schema"=>{"$ref"=>"#/definitions/ApiError"}}}}}}, | 
					
						
							|  |  |  |           "definitions"=>{ | 
					
						
							|  |  |  |             "ResponseItem"=>{ | 
					
						
							|  |  |  |               "type"=>"object", | 
					
						
							|  |  |  |               "properties"=>{"id"=>{"type"=>"integer"}, "name"=>{"type"=>"string"}}}, | 
					
						
							|  |  |  |             "UseResponse"=>{ | 
					
						
							|  |  |  |               "type"=>"object", | 
					
						
							|  |  |  |               "properties"=>{"description"=>{"type"=>"string"}, "$responses"=>{"$ref"=>"#/definitions/ResponseItem"}}}, | 
					
						
							|  |  |  |             "ApiError"=>{ | 
					
						
							|  |  |  |               "type"=>"object", | 
					
						
							|  |  |  |               "properties"=>{"code"=>{"type"=>"integer"}, "message"=>{"type"=>"string"}}} | 
					
						
							|  |  |  |         }}) | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe "response params" do | 
					
						
							|  |  |  |     subject do | 
					
						
							|  |  |  |       get '/swagger_doc/params_response' | 
					
						
							|  |  |  |       JSON.parse(last_response.body) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     describe "uses params as response object" do | 
					
						
							|  |  |  |       specify do | 
					
						
							|  |  |  |         expect(subject).to eql({ | 
					
						
							|  |  |  |           "info"=>{"title"=>"API title", "version"=>"v1"}, | 
					
						
							|  |  |  |           "swagger"=>"2.0", | 
					
						
							|  |  |  |           "produces"=>["application/json"], | 
					
						
							|  |  |  |           "host"=>"example.org", | 
					
						
							|  |  |  |           "schemes"=>["https", "http"], | 
					
						
							|  |  |  |           "paths"=>{ | 
					
						
							|  |  |  |             "/params_response"=>{ | 
					
						
							|  |  |  |               "get"=>{ | 
					
						
							|  |  |  |                 "produces"=>["application/json"], | 
					
						
							|  |  |  |                 "responses"=>{ | 
					
						
							|  |  |  |                   "200"=>{"description"=>"This returns something", "schema"=>{"$ref"=>"#/definitions/ParamsResponse"}}, | 
					
						
							|  |  |  |                   "400"=>{"description"=>"NotFound", "schema"=>{"$ref"=>"#/definitions/ApiError"}}}}}}, | 
					
						
							|  |  |  |           "definitions"=>{ | 
					
						
							|  |  |  |             "ParamsResponse"=>{"properties"=>{"description"=>{"type"=>"string"}}}, | 
					
						
							|  |  |  |             "ApiError"=>{ | 
					
						
							|  |  |  |               "type"=>"object", | 
					
						
							|  |  |  |               "properties"=>{"code"=>{"type"=>"integer"}, "message"=>{"type"=>"string"}}}} | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | end |