dredd-rack 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ module Dredd
2
+ module Rack
3
+ VERSION = '0.3.0'
4
+ end
5
+ end
@@ -0,0 +1,28 @@
1
+ require 'spec_helper'
2
+
3
+ describe Dredd::Rack::Configuration do
4
+
5
+ context 'when extending any class' do
6
+
7
+ before(:each) do
8
+ Dredd::Rack.const_set(:ConfigurableClass, Class.new)
9
+ @klass = Dredd::Rack::ConfigurableClass
10
+ @klass.send :extend, Dredd::Rack::Configuration
11
+ end
12
+
13
+ after(:each) do
14
+ Dredd::Rack.send(:remove_const, :ConfigurableClass)
15
+ end
16
+
17
+ let(:subject) { @klass }
18
+
19
+ describe 'provides #app which' do
20
+
21
+ it_behaves_like 'a configuration option', 'app'
22
+
23
+ it "defauts to nil", private: true do
24
+ expect(subject.app).to be_nil
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,61 @@
1
+ require 'spec_helper'
2
+
3
+ describe Dredd::Rack::RakeTask do
4
+
5
+ it 'is a kind of Rake::TaskLib', protected: true do
6
+ expect(subject).to be_kind_of ::Rake::TaskLib
7
+ end
8
+
9
+ it 'responds to :description', public: true do
10
+ expect(subject).to respond_to :description
11
+ end
12
+
13
+ it 'responds to :runner', public: true do
14
+ expect(subject).to respond_to :runner
15
+ end
16
+
17
+ it 'responds to :name', public: true do
18
+ expect(subject).to respond_to :name
19
+ end
20
+
21
+ describe '#runner', public: true do
22
+
23
+ it 'is a default Dredd::Rack runner' do
24
+ expect(subject.runner).to be_kind_of Dredd::Rack::Runner
25
+ expect(subject.runner.command).to match(/\Adredd doc\/\*\.apib doc\/\*\.apib\.md \z/)
26
+ end
27
+ end
28
+
29
+ describe '#description', public: true do
30
+
31
+ it 'defaults to "Run Dredd::Rack API blueprint verification"' do
32
+ expect(subject.description).to eq 'Run Dredd::Rack API blueprint verification'
33
+ end
34
+
35
+ context 'and a custom description was provided' do
36
+
37
+ it 'returns the custom description' do
38
+ pending "I don't know how to setup the custom description."
39
+ # set the custom description here
40
+
41
+ subject = Dredd::Rack::RakeTask.new # should be created after setting the description
42
+ expect(subject.description).to eq 'Validate an API against its blueprint'
43
+ end
44
+ end
45
+ end
46
+
47
+ describe '#name', public: true do
48
+
49
+ it 'defaults to :dredd' do
50
+ expect(subject.name).to eq :dredd
51
+ end
52
+
53
+ context 'when a custom name was provided' do
54
+
55
+ it 'returns the custom name' do
56
+ subject = Dredd::Rack::RakeTask.new(:anderson)
57
+ expect(subject.name).to eq :anderson
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,223 @@
1
+ require 'spec_helper'
2
+
3
+ describe Dredd::Rack::Runner do
4
+
5
+ it 'responds to :api_endpoint', public: true do
6
+ expect(subject).to respond_to :api_endpoint
7
+ end
8
+
9
+ it 'responds to :command', public: true do
10
+ expect(subject).to respond_to :command
11
+ end
12
+
13
+ it 'responds to :command_parts', private: true do
14
+ expect(subject).to respond_to :command_parts
15
+ end
16
+
17
+ it 'responds to :command_parts=', private: true do
18
+ expect(subject).to respond_to :command_parts=
19
+ end
20
+
21
+ it 'respond_to :paths_to_blueprints', public: true do
22
+ expect(subject).to respond_to :paths_to_blueprints
23
+ end
24
+
25
+ it 'responds to :run', public: true do
26
+ expect(subject).to respond_to :run
27
+ end
28
+
29
+ it 'generates a valid command by default', public: true do
30
+ expect(subject.send(:command_valid?)).to be_truthy
31
+ end
32
+
33
+ describe 'responds to all the Dredd options', public: true do
34
+
35
+ Dredd::Rack::Runner::OPTIONS.each do |option|
36
+ it "responds to :#{option}" do
37
+ expect(subject).to respond_to option
38
+ end
39
+ end
40
+ end
41
+
42
+ describe 'responds to the negated form of the Dredd boolean options', public: true do
43
+
44
+ Dredd::Rack::Runner::NEGATABLE_BOOLEAN_OPTIONS.each do |option|
45
+ it "responds to :no_#{option}" do
46
+ expect(subject).to respond_to "no_#{option}".to_sym
47
+ end
48
+ end
49
+ end
50
+
51
+ describe '#command_valid?', private: true do
52
+
53
+ context 'when the generated command has less than two arguments' do
54
+
55
+ it 'returns false' do
56
+ allow(subject).to receive_message_chain(:command, :has_at_least_two_arguments?).and_return(false)
57
+ expect(subject.send(:command_valid?)).not_to be_truthy
58
+ end
59
+ end
60
+ end
61
+
62
+ describe '#initialize', public: true do
63
+
64
+ context 'with an API endpoint as argument' do
65
+
66
+ it 'defines a custom API endpoint' do
67
+ expect(Dredd::Rack::Runner.new('https://api.example.com').command).to end_with 'https://api.example.com'
68
+ expect(Dredd::Rack::Runner.new('https://api.example.com').command).not_to match /http:\/\/localhost:3000/
69
+ end
70
+ end
71
+
72
+ context 'with a block as argument' do
73
+
74
+ it 'returns a configured Dredd runner' do
75
+
76
+ subject = Dredd::Rack::Runner.new 'http://localhost:4567' do |options|
77
+ options.paths_to_blueprints 'blueprints/*.apib', 'doc/*.apib'
78
+ options.level :silly
79
+ options.no_color!
80
+ end
81
+
82
+ expect(subject.command).to match /http:\/\/localhost:4567/
83
+ expect(subject.command).to match /blueprints\/\*\.apib doc\/\*\.apib/
84
+ expect(subject.command).to match /--level silly/
85
+ expect(subject.command).to match /--no-color/
86
+
87
+ expect(subject.command).not_to match /http:\/\/localhost:3000/
88
+ expect(subject.command).not_to match /doc\/\*\.apib.md/
89
+ end
90
+ end
91
+ end
92
+
93
+ describe '#paths_to_blueprints', public: true do
94
+
95
+ it 'is chainable' do
96
+ expect(subject.paths_to_blueprints('some/path/*.apib')).to eq subject
97
+ end
98
+
99
+ context 'with one or more paths to blueprints as arguments' do
100
+
101
+ it 'defines custom paths to blueprints' do
102
+ expect(subject.paths_to_blueprints('blueprints/*.md', 'blueprints/*.apib').command).to match /blueprints\/\*\.md blueprints\/\*\.apib/
103
+ expect(subject.paths_to_blueprints('blueprints/*.md').command).not_to match /doc/
104
+ end
105
+ end
106
+
107
+ context 'with a blank path as argument', public: true do
108
+
109
+ it 'raises ArgumentError' do
110
+ expect{ subject.paths_to_blueprints('') }.to raise_error ArgumentError, 'invalid path to blueprints'
111
+ end
112
+ end
113
+ end
114
+
115
+ describe '#run', public: true do
116
+
117
+ context 'when the command is valid' do
118
+
119
+ before(:each) do
120
+ allow(subject).to receive(:command_valid?).and_return(true)
121
+ end
122
+
123
+ it 'runs Dredd!' do
124
+ command = double()
125
+ allow(subject).to receive(:command).and_return(command)
126
+
127
+ expect(Kernel).to receive(:system).with(command)
128
+ subject.run
129
+ end
130
+
131
+ context 'when configured to test the API locally' do
132
+
133
+ before(:each) do
134
+ allow(subject).to receive(:api_remote?).and_return(false)
135
+ end
136
+
137
+ context 'with Dredd::Rack.app properly configured' do
138
+
139
+ before(:each) do
140
+ @app_under_test = double()
141
+ allow(Dredd::Rack).to receive(:app).and_return(@app_under_test)
142
+
143
+ @capybara_server = double()
144
+ allow(@capybara_server).to receive(:new).and_return(@capybara_server)
145
+ allow(@capybara_server).to receive(:host).and_return('localhost')
146
+ allow(@capybara_server).to receive(:port).and_return('4567')
147
+ allow(Kernel).to receive(:system)
148
+
149
+ stub_const("Capybara::Server", @capybara_server)
150
+ end
151
+
152
+ it 'creates a Capybara::Server instance and serves the API' do
153
+ expect(@capybara_server).to receive(:new).with(@app_under_test)
154
+ expect(@capybara_server).to receive(:boot)
155
+ subject.run
156
+ end
157
+
158
+ it 'gets its API endpoint from the Capybara::Server instance' do
159
+ allow(@capybara_server).to receive(:boot)
160
+ expect { subject.run }.to change { subject.api_endpoint }.to("http://#{@capybara_server.host}:#{@capybara_server.port.to_s}")
161
+ end
162
+ end
163
+ end
164
+ end
165
+ end
166
+
167
+ Dredd::Rack::Runner::BOOLEAN_OPTIONS.each do |option|
168
+ describe "##{option}", public: true do
169
+ it_behaves_like 'a boolean option', option, ['some argument']
170
+ end
171
+ end
172
+
173
+ Dredd::Rack::Runner::NEGATABLE_BOOLEAN_OPTIONS.each do |option|
174
+ describe "#no_#{option}", public: true do
175
+ it_behaves_like 'a boolean option', "no_#{option}".to_sym, ['some argument']
176
+ end
177
+ end
178
+
179
+ Dredd::Rack::Runner::SINGLE_ARGUMENT_OPTIONS.each do |option|
180
+ describe "##{option}", public: true do
181
+ it_behaves_like 'a single-argument option', option, ['first argument', 'second argument']
182
+ end
183
+ end
184
+ end
185
+
186
+ describe String, public: true do
187
+
188
+ it 'responds to :has_at_least_two_arguments?' do
189
+ expect(subject).to respond_to :has_at_least_two_arguments?
190
+ end
191
+
192
+
193
+ describe '#has_at_least_two_arguments?' do
194
+
195
+ context 'when the String can be interpreted as a command with at least two arguments' do
196
+ context 'returns true (reliable)' do
197
+
198
+ ['dredd doc/*.apib http://api.example.com',
199
+ 'dredd doc/*.apib doc/*apib.md http://api.example.com',
200
+ 'dredd doc/*.apib http://api.example.com --level verbose'].each do |subject|
201
+
202
+ it "e.g. '#{subject}'" do
203
+ expect(subject.has_at_least_two_arguments?).to be_truthy
204
+ end
205
+ end
206
+ end
207
+ end
208
+
209
+ context 'when the String can be interpreted as a command with less than two arguments' do
210
+ context 'returns false (unreliable)' do
211
+
212
+ ['dredd http://api.example.com',
213
+ 'dredd doc/*.apib --dry-run',
214
+ 'dredd --dry-run --level verbose'].each do |subject|
215
+
216
+ it "e.g. '#{subject}'" do
217
+ expect(subject.has_at_least_two_arguments?).to be_falsey
218
+ end
219
+ end
220
+ end
221
+ end
222
+ end
223
+ end
@@ -0,0 +1,8 @@
1
+ require 'spec_helper'
2
+
3
+ describe Dredd::Rack do
4
+
5
+ it 'is configurable', public: true do
6
+ expect(subject).to be_kind_of Dredd::Rack::Configuration
7
+ end
8
+ end
@@ -0,0 +1,35 @@
1
+ require 'bundler/setup'
2
+ Bundler.setup
3
+
4
+ require 'dredd/rack'
5
+
6
+ Dir["./spec/support/**/*.rb"].sort.each { |f| require f; puts f }
7
+
8
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
9
+ RSpec.configure do |config|
10
+ # make sure the deprecated RSpec 2 syntax is not used
11
+ config.raise_errors_for_deprecations!
12
+
13
+ # rspec-expectations config goes here. You can use an alternate
14
+ # assertion/expectation library such as wrong or the stdlib/minitest
15
+ # assertions if you prefer.
16
+ config.expect_with :rspec do |expectations|
17
+ # This option will default to `true` in RSpec 4. It makes the `description`
18
+ # and `failure_message` of custom matchers include text for helper methods
19
+ # defined using `chain`, e.g.:
20
+ # be_bigger_than(2).and_smaller_than(4).description
21
+ # # => "be bigger than 2 and smaller than 4"
22
+ # ...rather than:
23
+ # # => "be bigger than 2"
24
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
25
+ end
26
+
27
+ # rspec-mocks config goes here. You can use an alternate test double
28
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
29
+ config.mock_with :rspec do |mocks|
30
+ # Prevents you from mocking or stubbing a method that does not exist on
31
+ # a real object. This is generally recommended, and will default to
32
+ # `true` in RSpec 4.
33
+ mocks.verify_partial_doubles = true
34
+ end
35
+ end
@@ -0,0 +1,28 @@
1
+ RSpec.shared_examples 'a configuration option' do |option_name|
2
+
3
+ before(:each) do
4
+ @get_option = option_name.to_sym # e.g. :sign_in_token
5
+ @set_option = option_name.+('=').to_sym # e.g. :sign_in_token=
6
+ end
7
+
8
+ it 'is accessible', private: true do
9
+ expect(subject).to respond_to @get_option
10
+ expect(subject).to respond_to @set_option
11
+ end
12
+
13
+ context 'once set' do
14
+
15
+ before(:each) do
16
+ @_original_value = subject.send @get_option
17
+ subject.send @set_option, 'custom header'
18
+ end
19
+
20
+ after(:each) do
21
+ subject.send @set_option, @_original_value
22
+ end
23
+
24
+ it 'can be retrieved', private: true do
25
+ expect(subject.send @get_option).to eq 'custom header'
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,18 @@
1
+ RSpec.shared_examples 'a boolean option' do |option|
2
+
3
+ it_behaves_like 'an option', option
4
+
5
+ let(:option_flag) { option.to_s.gsub('_', '-').gsub('!', '').prepend('--') }
6
+
7
+ it 'appends its flag to the command' do
8
+ expect{ subject.send(option) }.to change{
9
+ subject.command
10
+ }.to(include "#{option_flag}")
11
+ end
12
+
13
+ it 'ignores any option argument' do
14
+ expect{ subject.send(option, 'first argument', 'second argument') }.to change{
15
+ subject.command
16
+ }.to(end_with "#{option_flag}")
17
+ end
18
+ end
@@ -0,0 +1,6 @@
1
+ RSpec.shared_examples 'an option' do |option|
2
+
3
+ it 'is chainable' do
4
+ expect(subject.send(option)).to eq subject
5
+ end
6
+ end
@@ -0,0 +1,12 @@
1
+ RSpec.shared_examples 'a single-argument option' do |option, args|
2
+
3
+ it_behaves_like 'an option', option
4
+
5
+ it 'appends its flag and argument to the command' do
6
+ option_flag = option.to_s.gsub('_', '-').prepend('--')
7
+
8
+ expect{ subject.send(option, args.slice(0), args.slice(1)) }.to change{
9
+ subject.command
10
+ }.to(end_with "#{option_flag} #{args.slice(0)}")
11
+ end
12
+ end
metadata ADDED
@@ -0,0 +1,142 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dredd-rack
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.0
5
+ platform: ruby
6
+ authors:
7
+ - Gonzalo Bulnes Guilpain
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: capybara
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.4'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.4'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.4'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.4'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rainbow
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: inch
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.5.10
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.5.10
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ description:
84
+ email:
85
+ - gon.bulnes@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - Gemfile
91
+ - LICENSE
92
+ - README.md
93
+ - Rakefile
94
+ - lib/dredd/rack.rb
95
+ - lib/dredd/rack/configuration.rb
96
+ - lib/dredd/rack/rake_task.rb
97
+ - lib/dredd/rack/runner.rb
98
+ - lib/dredd/rack/version.rb
99
+ - spec/lib/dredd/rack/configuration_spec.rb
100
+ - spec/lib/dredd/rack/rake_task_spec.rb
101
+ - spec/lib/dredd/rack/runner_spec.rb
102
+ - spec/lib/dredd/rack_spec.rb
103
+ - spec/spec_helper.rb
104
+ - spec/support/spec_for_configuration_option_interface.rb
105
+ - spec/support/specs_for_boolean_options.rb
106
+ - spec/support/specs_for_options.rb
107
+ - spec/support/specs_for_single_argument_options.rb
108
+ homepage: https://github.com/gonzalo-bulnes/dredd-rack
109
+ licenses:
110
+ - GPLv3
111
+ metadata: {}
112
+ post_install_message:
113
+ rdoc_options: []
114
+ require_paths:
115
+ - lib
116
+ required_ruby_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ required_rubygems_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ requirements: []
127
+ rubyforge_project:
128
+ rubygems_version: 2.4.6
129
+ signing_key:
130
+ specification_version: 4
131
+ summary: Convenient API blueprint testing with Apiary Dredd for Rack applications.
132
+ test_files:
133
+ - spec/lib/dredd/rack/rake_task_spec.rb
134
+ - spec/lib/dredd/rack/configuration_spec.rb
135
+ - spec/lib/dredd/rack/runner_spec.rb
136
+ - spec/lib/dredd/rack_spec.rb
137
+ - spec/spec_helper.rb
138
+ - spec/support/specs_for_single_argument_options.rb
139
+ - spec/support/specs_for_boolean_options.rb
140
+ - spec/support/specs_for_options.rb
141
+ - spec/support/spec_for_configuration_option_interface.rb
142
+ has_rdoc: