Skip to content

Commit a3dc776

Browse files
Merge pull request #1 from nathanvda/master
Merged tests in master. Now completely decoupled from rails.
2 parents 4de802f + 597bd3f commit a3dc776

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+195
-798
lines changed

Gemfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,9 @@ group :development do
1616
gem "jquery-rails"
1717
gem "rails", "~> 3.2"
1818
gem "sqlite3"
19+
20+
gem "rack-test"
21+
22+
gem "rspec-rails"
23+
gem "rspec"
1924
end

Gemfile.lock

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ GEM
3030
multi_json (~> 1.0)
3131
arel (3.0.2)
3232
builder (3.0.0)
33+
diff-lcs (1.1.3)
3334
erubis (2.7.0)
3435
git (1.2.5)
3536
grape (0.2.1)
@@ -86,6 +87,19 @@ GEM
8687
rake (0.9.2.2)
8788
rdoc (3.12)
8889
json (~> 1.4)
90+
rspec (2.11.0)
91+
rspec-core (~> 2.11.0)
92+
rspec-expectations (~> 2.11.0)
93+
rspec-mocks (~> 2.11.0)
94+
rspec-core (2.11.1)
95+
rspec-expectations (2.11.1)
96+
diff-lcs (~> 1.1.3)
97+
rspec-mocks (2.11.1)
98+
rspec-rails (2.11.0)
99+
actionpack (>= 3.0)
100+
activesupport (>= 3.0)
101+
railties (>= 3.0)
102+
rspec (~> 2.11.0)
89103
shoulda (3.1.1)
90104
shoulda-context (~> 1.0)
91105
shoulda-matchers (~> 1.2)
@@ -112,7 +126,10 @@ DEPENDENCIES
112126
grape
113127
jeweler (~> 1.8.4)
114128
jquery-rails
129+
rack-test
115130
rails (~> 3.2)
116131
rdoc (~> 3.12)
132+
rspec
133+
rspec-rails
117134
shoulda
118135
sqlite3

Rakefile

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,9 @@ Jeweler::RubygemsDotOrgTasks.new
2929

3030
Bundler::GemHelper.install_tasks
3131

32-
require 'rake/testtask'
33-
34-
Rake::TestTask.new(:test) do |t|
35-
t.libs << 'lib'
36-
t.libs << 'test'
37-
t.pattern = 'test/**/*_test.rb'
38-
t.verbose = false
39-
end
32+
require 'rspec/core'
33+
require 'rspec/core/rake_task'
4034

35+
RSpec::Core::RakeTask.new(:spec)
4136

42-
task :default => :test
37+
task :default => :spec

lib/grape-swagger.rb

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ def add_swagger_documentation(options={})
2121
private
2222

2323
def create_documentation_class
24+
2425
Class.new(Grape::API) do
2526
class << self
2627
def name
@@ -40,8 +41,8 @@ def self.setup(options)
4041
@@target_class = options[:target_class]
4142
@@mount_path = options[:mount_path]
4243
@@class_name = options[:class_name] || options[:mount_path].gsub('/','')
43-
@@api_version = options[:api_version]
44-
@@base_path = options[:base_path]
44+
api_version = options[:api_version]
45+
base_path = options[:base_path]
4546

4647
desc 'Swagger compatible API description'
4748
get @@mount_path do
@@ -53,9 +54,9 @@ def self.setup(options)
5354
{ :path => "#{@@mount_path}/#{route}.{format}" }
5455
end
5556
{
56-
apiVersion: @@api_version,
57+
apiVersion: api_version,
5758
swaggerVersion: "1.1",
58-
basePath: @@base_path || "http://#{env['HTTP_HOST']}",
59+
basePath: base_path || "http://#{env['HTTP_HOST']}",
5960
operations:[],
6061
apis: routes_array
6162
}
@@ -83,9 +84,9 @@ def self.setup(options)
8384
end
8485

8586
{
86-
apiVersion: @@api_version,
87+
apiVersion: api_version,
8788
swaggerVersion: "1.1",
88-
basePath: @@base_path || "http://#{env['HTTP_HOST']}",
89+
basePath: base_path || "http://#{env['HTTP_HOST']}",
8990
resourcePath: "",
9091
apis: routes_array
9192
}

spec/grape-swagger_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
require 'spec_helper'
2+
3+
describe Grape::API do
4+
5+
it "added combined-routes" do
6+
Grape::API.should respond_to :combined_routes
7+
end
8+
9+
it "added add_swagger_documentation" do
10+
Grape::API.should respond_to :add_swagger_documentation
11+
end
12+
13+
14+
15+
end

spec/non_default_api_spec.rb

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
require 'spec_helper'
2+
3+
describe "overruling the defaults for the api documentation generation" do
4+
5+
class MountedApi < Grape::API
6+
desc 'this gets something'
7+
get '/something' do
8+
{:bla => 'something'}
9+
end
10+
end
11+
12+
13+
context "overruling the basepath" do
14+
class SimpleApiWithBasePath < Grape::API
15+
NON_DEFAULT_BASE_PATH= "http://www.breakcoregivesmewood.com"
16+
17+
mount MountedApi
18+
add_swagger_documentation :base_path => NON_DEFAULT_BASE_PATH
19+
end
20+
21+
subject { SimpleApiWithBasePath.new }
22+
def app; subject end
23+
24+
it "retrieves the given base-path on /swagger_doc" do
25+
get '/swagger_doc'
26+
last_response.body.should == "{:apiVersion=>\"0.1\", :swaggerVersion=>\"1.1\", :basePath=>\"#{SimpleApiWithBasePath::NON_DEFAULT_BASE_PATH}\", :operations=>[], :apis=>[{:path=>\"/swagger_doc/mountedapi.{format}\"}, {:path=>\"/swagger_doc/swagger_doc.{format}\"}]}"
27+
end
28+
29+
it "retrieves the same given base-path for mounted-api" do
30+
Random.stub(:rand) { 0 }
31+
get '/swagger_doc/mountedapi'
32+
last_response.body.should == "{:apiVersion=>\"0.1\", :swaggerVersion=>\"1.1\", :basePath=>\"#{SimpleApiWithBasePath::NON_DEFAULT_BASE_PATH}\", :resourcePath=>\"\", :apis=>[{:path=>\"/something.{format}\", :operations=>[{:notes=>nil, :summary=>\"this gets something\", :nickname=>0, :httpMethod=>\"GET\", :parameters=>[]}]}]}"
33+
end
34+
end
35+
36+
context "overruling the basepath" do
37+
class SimpleApiWithAPiVersion < Grape::API
38+
API_VERSION = "101"
39+
40+
mount MountedApi
41+
add_swagger_documentation :api_version => API_VERSION
42+
end
43+
44+
subject { SimpleApiWithAPiVersion.new }
45+
def app; subject end
46+
47+
it "retrieves the given base-path on /swagger_doc" do
48+
get '/swagger_doc'
49+
last_response.body.should == "{:apiVersion=>\"#{SimpleApiWithAPiVersion::API_VERSION}\", :swaggerVersion=>\"1.1\", :basePath=>\"http://example.org\", :operations=>[], :apis=>[{:path=>\"/swagger_doc/mountedapi.{format}\"}, {:path=>\"/swagger_doc/swagger_doc.{format}\"}]}"
50+
end
51+
52+
it "retrieves the same given base-path for mounted-api" do
53+
Random.stub(:rand) { 0 }
54+
get '/swagger_doc/mountedapi'
55+
last_response.body.should == "{:apiVersion=>\"#{SimpleApiWithAPiVersion::API_VERSION}\", :swaggerVersion=>\"1.1\", :basePath=>\"http://example.org\", :resourcePath=>\"\", :apis=>[{:path=>\"/something.{format}\", :operations=>[{:notes=>nil, :summary=>\"this gets something\", :nickname=>0, :httpMethod=>\"GET\", :parameters=>[]}]}]}"
56+
end
57+
end
58+
59+
context "overruling the mount-path" do
60+
class SimpleApiWithDifferentMount < Grape::API
61+
MOUNT_PATH = "api_doc"
62+
63+
mount MountedApi
64+
add_swagger_documentation :mount_path => MOUNT_PATH
65+
end
66+
67+
subject { SimpleApiWithDifferentMount.new }
68+
def app; subject end
69+
70+
it "retrieves the given base-path on /swagger_doc" do
71+
get '/api_doc'
72+
last_response.body.should == "{:apiVersion=>\"0.1\", :swaggerVersion=>\"1.1\", :basePath=>\"http://example.org\", :operations=>[], :apis=>[{:path=>\"/swagger_doc/mountedapi.{format}\"}, {:path=>\"/swagger_doc/swagger_doc.{format}\"}]}"
73+
end
74+
75+
it "retrieves the same given base-path for mounted-api" do
76+
Random.stub(:rand) { 0 }
77+
get '/api_doc/mountedapi'
78+
last_response.body.should == "{:apiVersion=>\"0.1\", :swaggerVersion=>\"1.1\", :basePath=>\"http://example.org\", :resourcePath=>\"\", :apis=>[{:path=>\"/something.{format}\", :operations=>[{:notes=>nil, :summary=>\"this gets something\", :nickname=>0, :httpMethod=>\"GET\", :parameters=>[]}]}]}"
79+
end
80+
81+
it "does not respond to swagger_doc" do
82+
get '/swagger_doc'
83+
last_response.status.should be == 404
84+
end
85+
end
86+
87+
88+
end

spec/simple_mounted_api_spec.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
require 'spec_helper'
2+
3+
describe "a simple mounted api" do
4+
5+
class MountedApi < Grape::API
6+
desc 'this gets something'
7+
get '/something' do
8+
{:bla => 'something'}
9+
end
10+
end
11+
12+
class SimpleApi < Grape::API
13+
mount MountedApi
14+
add_swagger_documentation
15+
end
16+
17+
subject { SimpleApi.new }
18+
def app; subject end
19+
20+
it "retrieves swagger-documentation on /swagger_doc" do
21+
get '/swagger_doc'
22+
last_response.body.should == "{:apiVersion=>\"0.1\", :swaggerVersion=>\"1.1\", :basePath=>\"http://example.org\", :operations=>[], :apis=>[{:path=>\"/swagger_doc/mountedapi.{format}\"}, {:path=>\"/swagger_doc/swagger_doc.{format}\"}]}"
23+
end
24+
25+
it "retrieves the documentation for mounted-api" do
26+
Random.stub(:rand) { 0 }
27+
get '/swagger_doc/mountedapi'
28+
last_response.body.should == "{:apiVersion=>\"0.1\", :swaggerVersion=>\"1.1\", :basePath=>\"http://example.org\", :resourcePath=>\"\", :apis=>[{:path=>\"/something.{format}\", :operations=>[{:notes=>nil, :summary=>\"this gets something\", :nickname=>0, :httpMethod=>\"GET\", :parameters=>[]}]}]}"
29+
end
30+
end

spec/spec_helper.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
$LOAD_PATH.unshift(File.dirname(__FILE__))
2+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), 'support'))
4+
5+
require 'grape'
6+
require 'grape-swagger'
7+
8+
require 'rubygems'
9+
require 'bundler'
10+
Bundler.setup :default, :test
11+
12+
13+
require 'rack/test'
14+
15+
# Load support files
16+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
17+
18+
RSpec.configure do |config|
19+
# Remove this line if you don't want RSpec's should and should_not
20+
# methods or matchers
21+
require 'rspec/expectations'
22+
config.include RSpec::Matchers
23+
24+
# == Mock Framework
25+
config.mock_with :rspec
26+
27+
config.include Rack::Test::Methods
28+
#config.include Rack::Test::Methods::Patch
29+
end

0 commit comments

Comments
 (0)