Make petstore tests pass

This commit is contained in:
xhh 2015-04-19 00:42:55 +08:00
parent 8dd7d3aacd
commit 194e9e6f05
6 changed files with 59 additions and 59 deletions

View File

@ -37,8 +37,8 @@ describe "Pet" do
end end
it "should fetch a pet object" do it "should fetch a pet object" do
pet = PetApi.get_pet_by_id(10002) pet = SwaggerClient::PetApi.get_pet_by_id(10002)
pet.should be_a(Pet) pet.should be_a(SwaggerClient::Pet)
pet.id.should == 10002 pet.id.should == 10002
pet.name.should == "RUBY UNIT TESTING" pet.name.should == "RUBY UNIT TESTING"
pet.tags[0].name.should == "tag test" pet.tags[0].name.should == "tag test"
@ -46,17 +46,17 @@ describe "Pet" do
end end
it "should find pets by status" do it "should find pets by status" do
pets = PetApi.find_pets_by_status(:status => 'available') pets = SwaggerClient::PetApi.find_pets_by_status(:status => 'available')
pets.length.should >= 3 pets.length.should >= 3
end end
it "should not find a pet with invalid status" do it "should not find a pet with invalid status" do
pets = PetApi.find_pets_by_status(:status => 'invalid-status') pets = SwaggerClient::PetApi.find_pets_by_status(:status => 'invalid-status')
pets.length.should == 0 pets.length.should == 0
end end
it "should find a pet by status" do it "should find a pet by status" do
pets = PetApi.find_pets_by_status(:status => "available,sold") pets = SwaggerClient::PetApi.find_pets_by_status(:status => "available,sold")
pets.map {|pet| pets.map {|pet|
if(pet.status != 'available' && pet.status != 'sold') if(pet.status != 'available' && pet.status != 'sold')
raise "pet status wasn't right" raise "pet status wasn't right"
@ -65,19 +65,19 @@ describe "Pet" do
end end
it "should update a pet" do it "should update a pet" do
pet = Pet.new({'id' => 10002, 'status' => 'sold'}) pet = SwaggerClient::Pet.new({'id' => 10002, 'status' => 'sold'})
PetApi.add_pet(:body => pet) SwaggerClient::PetApi.add_pet(:body => pet)
fetched = PetApi.get_pet_by_id(10002) fetched = SwaggerClient::PetApi.get_pet_by_id(10002)
fetched.id.should == 10002 fetched.id.should == 10002
fetched.status.should == 'sold' fetched.status.should == 'sold'
end end
it "should create a pet" do it "should create a pet" do
pet = Pet.new('id' => 10002, 'name' => "RUBY UNIT TESTING") pet = SwaggerClient::Pet.new('id' => 10002, 'name' => "RUBY UNIT TESTING")
PetApi.add_pet(:body => pet) SwaggerClient::PetApi.add_pet(:body => pet)
pet = PetApi.get_pet_by_id(10002) pet = SwaggerClient::PetApi.get_pet_by_id(10002)
pet.id.should == 10002 pet.id.should == 10002
pet.name.should == "RUBY UNIT TESTING" pet.name.should == "RUBY UNIT TESTING"
end end

View File

@ -1,9 +1,9 @@
require 'spec_helper' require 'spec_helper'
describe Swagger::Request do describe SwaggerClient::Swagger::Request do
before(:each) do before(:each) do
Swagger.configure do |config| SwaggerClient::Swagger.configure do |config|
inject_format = true inject_format = true
config.api_key = 'special-key' config.api_key = 'special-key'
config.host = 'petstore.swagger.io' config.host = 'petstore.swagger.io'
@ -15,7 +15,7 @@ describe Swagger::Request do
@default_params = { @default_params = {
:params => {:foo => "1", :bar => "2"} :params => {:foo => "1", :bar => "2"}
} }
@request = Swagger::Request.new(@default_http_method, @default_path, @default_params) @request = SwaggerClient::Swagger::Request.new(@default_http_method, @default_path, @default_params)
end end
describe "initialization" do describe "initialization" do
@ -24,7 +24,7 @@ describe Swagger::Request do
end end
it "allows params to be nil" do it "allows params to be nil" do
@request = Swagger::Request.new(@default_http_method, @default_path, :params => nil) @request = SwaggerClient::Swagger::Request.new(@default_http_method, @default_path, :params => nil)
@request.query_string.should == "" @request.query_string.should == ""
end end
@ -59,7 +59,7 @@ describe Swagger::Request do
describe "body" do describe "body" do
it "camelCases parameters" do it "camelCases parameters" do
@request = Swagger::Request.new(@default_http_method, @default_path, @default_params.merge({ @request = SwaggerClient::Swagger::Request.new(@default_http_method, @default_path, @default_params.merge({
:body => { :body => {
:bad_dog => 'bud', :bad_dog => 'bud',
:goodDog => "dud" :goodDog => "dud"
@ -73,7 +73,7 @@ describe Swagger::Request do
describe "path" do describe "path" do
it "accounts for a total absence of format in the path string" do it "accounts for a total absence of format in the path string" do
@request = Swagger::Request.new(:get, "/word.{format}/cat/entries", @default_params.merge({ @request = SwaggerClient::Swagger::Request.new(:get, "/word.{format}/cat/entries", @default_params.merge({
:format => "xml", :format => "xml",
:params => { :params => {
} }
@ -82,7 +82,7 @@ describe Swagger::Request do
end end
it "does string substitution (format) on path params" do it "does string substitution (format) on path params" do
@request = Swagger::Request.new(:get, "/word.{format}/cat/entries", @default_params.merge({ @request = SwaggerClient::Swagger::Request.new(:get, "/word.{format}/cat/entries", @default_params.merge({
:format => "xml", :format => "xml",
:params => { :params => {
} }
@ -91,7 +91,7 @@ describe Swagger::Request do
end end
it "leaves path-bound params out of the query string" do it "leaves path-bound params out of the query string" do
@request = Swagger::Request.new(:get, "/word.{format}/{word}/entries", @default_params.merge({ @request = SwaggerClient::Swagger::Request.new(:get, "/word.{format}/{word}/entries", @default_params.merge({
:params => { :params => {
:word => "cat", :word => "cat",
:limit => 20 :limit => 20
@ -101,7 +101,7 @@ describe Swagger::Request do
end end
it "returns a question-mark free (blank) query string if no query params are present" do it "returns a question-mark free (blank) query string if no query params are present" do
@request = Swagger::Request.new(:get, "/word.{format}/{word}/entries", @default_params.merge({ @request = SwaggerClient::Swagger::Request.new(:get, "/word.{format}/{word}/entries", @default_params.merge({
:params => { :params => {
:word => "cat", :word => "cat",
} }
@ -110,7 +110,7 @@ describe Swagger::Request do
end end
it "removes blank params" do it "removes blank params" do
@request = Swagger::Request.new(:get, "words/fancy", @default_params.merge({ @request = SwaggerClient::Swagger::Request.new(:get, "words/fancy", @default_params.merge({
:params => { :params => {
:word => "dog", :word => "dog",
:limit => "", :limit => "",
@ -121,7 +121,7 @@ describe Swagger::Request do
end end
it "URI encodes the path" do it "URI encodes the path" do
@request = Swagger::Request.new(:get, "word.{format}/bill gates/definitions", @default_params.merge({ @request = SwaggerClient::Swagger::Request.new(:get, "word.{format}/bill gates/definitions", @default_params.merge({
:params => { :params => {
:word => "bill gates" :word => "bill gates"
} }
@ -130,7 +130,7 @@ describe Swagger::Request do
end end
it "converts numeric params to strings" do it "converts numeric params to strings" do
@request = Swagger::Request.new(@default_http_method, @default_path, @default_params.merge({ @request = SwaggerClient::Swagger::Request.new(@default_http_method, @default_path, @default_params.merge({
:params => { :params => {
:limit => 100 :limit => 100
} }
@ -142,7 +142,7 @@ describe Swagger::Request do
end end
it "camelCases parameters" do it "camelCases parameters" do
@request = Swagger::Request.new(@default_http_method, @default_path, @default_params.merge({ @request = SwaggerClient::Swagger::Request.new(@default_http_method, @default_path, @default_params.merge({
:params => { :params => {
:bad_dog => 'bud', :bad_dog => 'bud',
:goodDog => "dud" :goodDog => "dud"
@ -153,7 +153,7 @@ describe Swagger::Request do
it "converts boolean values to their string representation" do it "converts boolean values to their string representation" do
params = {:stringy => "fish", :truthy => true, :falsey => false} params = {:stringy => "fish", :truthy => true, :falsey => false}
@request = Swagger::Request.new(:get, 'fakeMethod', :params => params) @request = SwaggerClient::Swagger::Request.new(:get, 'fakeMethod', :params => params)
@request.query_string.should == "?falsey=false&stringy=fish&truthy=true" @request.query_string.should == "?falsey=false&stringy=fish&truthy=true"
end end
@ -162,12 +162,12 @@ describe Swagger::Request do
describe "API key" do describe "API key" do
it "is inferred from the Swagger base configuration by default" do it "is inferred from the Swagger base configuration by default" do
Swagger.configure {|c| c.api_key = "xyz" } SwaggerClient::Swagger.configure {|c| c.api_key = "xyz" }
Swagger::Request.new(:get, "word/json").headers[:api_key].should == "xyz" SwaggerClient::Swagger::Request.new(:get, "word/json").headers[:api_key].should == "xyz"
end end
it "can be obfuscated for public display" do it "can be obfuscated for public display" do
@request = Swagger::Request.new(:get, "words/fancy", @default_params.merge({ @request = SwaggerClient::Swagger::Request.new(:get, "words/fancy", @default_params.merge({
:params => { :params => {
:word => "dog", :word => "dog",
:api_key => "123456" :api_key => "123456"
@ -179,21 +179,21 @@ describe Swagger::Request do
end end
it "allows a key in the params to override the configuration-level key, even if it's blank" do it "allows a key in the params to override the configuration-level key, even if it's blank" do
Swagger.configure {|c| c.api_key = "abc" } SwaggerClient::Swagger.configure {|c| c.api_key = "abc" }
@request_with_key = Swagger::Request.new(:get, "word/json", :params => {:api_key => "jkl"}) @request_with_key = SwaggerClient::Swagger::Request.new(:get, "word/json", :params => {:api_key => "jkl"})
@request_with_key.headers[:api_key].should be_nil @request_with_key.headers[:api_key].should be_nil
@request_with_key.params[:api_key].should == "jkl" @request_with_key.params[:api_key].should == "jkl"
@request_without_key = Swagger::Request.new(:get, "word/json", :params => {:api_key => nil}) @request_without_key = SwaggerClient::Swagger::Request.new(:get, "word/json", :params => {:api_key => nil})
@request_without_key.headers[:api_key].should be_nil @request_without_key.headers[:api_key].should be_nil
@request_without_key.params[:api_key].should be_nil @request_without_key.params[:api_key].should be_nil
end end
it "allows a key in the headers to override the configuration-level key, even if it's blank" do it "allows a key in the headers to override the configuration-level key, even if it's blank" do
Swagger.configure {|c| c.api_key = "hij" } SwaggerClient::Swagger.configure {|c| c.api_key = "hij" }
Swagger::Request.new(:get, "word/json").headers[:api_key].should == "hij" SwaggerClient::Swagger::Request.new(:get, "word/json").headers[:api_key].should == "hij"
Swagger::Request.new(:get, "word/json", :headers => {:api_key => "jkl"}).headers[:api_key].should == "jkl" SwaggerClient::Swagger::Request.new(:get, "word/json", :headers => {:api_key => "jkl"}).headers[:api_key].should == "jkl"
Swagger::Request.new(:get, "word/json", :headers => {:api_key => nil}).headers[:api_key].should be_nil SwaggerClient::Swagger::Request.new(:get, "word/json", :headers => {:api_key => nil}).headers[:api_key].should be_nil
end end
end end

View File

@ -1,6 +1,6 @@
require 'spec_helper' require 'spec_helper'
describe Swagger::Response do describe SwaggerClient::Swagger::Response do
before do before do
configure_swagger configure_swagger
@ -13,7 +13,7 @@ describe Swagger::Response do
@raw = Typhoeus::Request.get("http://petstore.swagger.io/v2/pet/10002") @raw = Typhoeus::Request.get("http://petstore.swagger.io/v2/pet/10002")
end end
@response = Swagger::Response.new(@raw) @response = SwaggerClient::Swagger::Response.new(@raw)
end end
describe "initialization" do describe "initialization" do
@ -43,7 +43,7 @@ describe Swagger::Response do
@raw = Typhoeus::Request.get("http://petstore.swagger.io/v2/pet/10002", @raw = Typhoeus::Request.get("http://petstore.swagger.io/v2/pet/10002",
:headers => {'Accept'=> "application/xml"}) :headers => {'Accept'=> "application/xml"})
end end
@response = Swagger::Response.new(@raw) @response = SwaggerClient::Swagger::Response.new(@raw)
@response.format.should == 'xml' @response.format.should == 'xml'
@response.xml?.should == true @response.xml?.should == true
end end

View File

@ -37,7 +37,7 @@ end
#end #end
def configure_swagger def configure_swagger
Swagger.configure do |config| SwaggerClient::Swagger.configure do |config|
config.api_key = 'special-key' config.api_key = 'special-key'
config.host = 'petstore.swagger.io' config.host = 'petstore.swagger.io'
config.base_path = '/v2' config.base_path = '/v2'
@ -47,7 +47,7 @@ end
# always delete and then re-create the pet object with 10002 # always delete and then re-create the pet object with 10002
def prepare_pet def prepare_pet
# remove the pet # remove the pet
PetApi.delete_pet(10002) SwaggerClient::PetApi.delete_pet(10002)
# recreate the pet # recreate the pet
category = Category.new('id' => 20002, 'name' => 'category test') category = Category.new('id' => 20002, 'name' => 'category test')
tag = Tag.new('id' => 30002, 'name' => 'tag test') tag = Tag.new('id' => 30002, 'name' => 'tag test')
@ -59,13 +59,13 @@ end
# always delete and then re-create the store order # always delete and then re-create the store order
def prepare_store def prepare_store
order = Order.new("id" => 10002, order = SwaggerClient::Order.new("id" => 10002,
"petId" => 10002, "petId" => 10002,
"quantity" => 789, "quantity" => 789,
"shipDate" => "2015-04-06T23:42:01.678Z", "shipDate" => "2015-04-06T23:42:01.678Z",
"status" => "placed", "status" => "placed",
"complete" => false) "complete" => false)
StoreApi.place_order(:body => order) SwaggerClient::StoreApi.place_order(:body => order)
end end
configure_swagger configure_swagger

View File

@ -7,7 +7,7 @@ describe "Store" do
end end
it "should fetch an order" do it "should fetch an order" do
item = StoreApi.get_order_by_id(10002) item = SwaggerClient::StoreApi.get_order_by_id(10002)
item.id.should == 10002 item.id.should == 10002
end end
end end

View File

@ -1,7 +1,7 @@
# require 'spec_helper' # require 'spec_helper'
require File.dirname(__FILE__) + '/spec_helper' require File.dirname(__FILE__) + '/spec_helper'
describe Swagger do describe SwaggerClient::Swagger do
before(:each) do before(:each) do
configure_swagger configure_swagger
@ -16,35 +16,35 @@ describe Swagger do
context 'host' do context 'host' do
it 'removes http from host' do it 'removes http from host' do
Swagger.configure {|c| c.host = 'http://example.com' } SwaggerClient::Swagger.configure {|c| c.host = 'http://example.com' }
Swagger.configuration.host.should == 'example.com' SwaggerClient::Swagger.configuration.host.should == 'example.com'
end end
it 'removes https from host' do it 'removes https from host' do
Swagger.configure {|c| c.host = 'https://wookiee.com' } SwaggerClient::Swagger.configure {|c| c.host = 'https://wookiee.com' }
Swagger.configuration.host.should == 'wookiee.com' SwaggerClient::Swagger.configuration.host.should == 'wookiee.com'
end end
it 'removes trailing path from host' do it 'removes trailing path from host' do
Swagger.configure {|c| c.host = 'hobo.com/v4' } SwaggerClient::Swagger.configure {|c| c.host = 'hobo.com/v4' }
Swagger.configuration.host.should == 'hobo.com' SwaggerClient::Swagger.configuration.host.should == 'hobo.com'
end end
end end
context 'base_path' do context 'base_path' do
it "prepends a slash to base_path" do it "prepends a slash to base_path" do
Swagger.configure {|c| c.base_path = 'v4/dog' } SwaggerClient::Swagger.configure {|c| c.base_path = 'v4/dog' }
Swagger.configuration.base_path.should == '/v4/dog' SwaggerClient::Swagger.configuration.base_path.should == '/v4/dog'
end end
it "doesn't prepend a slash if one is already there" do it "doesn't prepend a slash if one is already there" do
Swagger.configure {|c| c.base_path = '/v4/dog' } SwaggerClient::Swagger.configure {|c| c.base_path = '/v4/dog' }
Swagger.configuration.base_path.should == '/v4/dog' SwaggerClient::Swagger.configuration.base_path.should == '/v4/dog'
end end
it "ends up as a blank string if nil" do it "ends up as a blank string if nil" do
Swagger.configure {|c| c.base_path = nil } SwaggerClient::Swagger.configure {|c| c.base_path = nil }
Swagger.configuration.base_path.should == '' SwaggerClient::Swagger.configuration.base_path.should == ''
end end
end end