Skip to content

Commit b3c4b58

Browse files
committed
Reorganized shared examples and helpers.
1 parent e3af6c3 commit b3c4b58

File tree

6 files changed

+45
-39
lines changed

6 files changed

+45
-39
lines changed

spec/grape/api_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
require 'spec_helper'
2-
require 'shared_versioning_examples'
2+
require 'shared/versioning_examples'
33

44
describe Grape::API do
55
subject { Class.new(Grape::API) }
@@ -448,7 +448,7 @@ def call(env)
448448
subject.get(:hello){ "Hello, world."}
449449
get '/hello'
450450
last_response.status.should eql 401
451-
get '/hello', {}, 'HTTP_AUTHORIZATION' => encode_basic('allow','whatever')
451+
get '/hello', {}, 'HTTP_AUTHORIZATION' => encode_basic_auth('allow','whatever')
452452
last_response.status.should eql 200
453453
end
454454

@@ -476,7 +476,7 @@ def call(env)
476476
subject.get(:hello){ "Hello, world."}
477477
get '/hello'
478478
last_response.status.should eql 401
479-
get '/hello', {}, 'HTTP_AUTHORIZATION' => encode_basic('allow','whatever')
479+
get '/hello', {}, 'HTTP_AUTHORIZATION' => encode_basic_auth('allow','whatever')
480480
last_response.status.should eql 200
481481
end
482482
end

spec/grape/middleware/auth/basic_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ def app
2020
end
2121

2222
it 'should authenticate if given valid creds' do
23-
get '/whatever', {}, 'HTTP_AUTHORIZATION' => encode_basic('admin','admin')
23+
get '/whatever', {}, 'HTTP_AUTHORIZATION' => encode_basic_auth('admin','admin')
2424
last_response.status.should == 200
2525
end
2626

2727
it 'should throw a 401 is wrong auth is given' do
28-
get '/whatever', {}, 'HTTP_AUTHORIZATION' => encode_basic('admin','wrong')
28+
get '/whatever', {}, 'HTTP_AUTHORIZATION' => encode_basic_auth('admin','wrong')
2929
last_response.status.should == 401
3030
end
3131
end
File renamed without changes.

spec/spec_helper.rb

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -14,46 +14,14 @@
1414
require 'pry'
1515

1616
require 'base64'
17-
def encode_basic(username, password)
18-
"Basic " + Base64.encode64("#{username}:#{password}")
19-
end
2017

2118
require 'hashie/hash'
2219

2320
RSpec.configure do |config|
2421
config.include Rack::Test::Methods
2522
end
2623

27-
# Versioning
28-
29-
# Returns the path with options[:version] prefixed if options[:using] is :path.
30-
# Returns normal path otherwise.
31-
def versioned_path(options = {})
32-
case options[:using]
33-
when :path
34-
File.join('/', options[:prefix] || '', options[:version], options[:path])
35-
when :header
36-
File.join('/', options[:prefix] || '', options[:path])
37-
else
38-
raise ArgumentError.new("unknown versioning strategy: #{options[:using]}")
39-
end
40-
end
41-
42-
def versioned_headers(options)
43-
case options[:using]
44-
when :path
45-
{} # no-op
46-
when :header
47-
{
48-
'HTTP_ACCEPT' => "application/vnd.#{options[:vendor]}-#{options[:version]}+#{options[:format]}"
49-
}
50-
else
51-
raise ArgumentError.new("unknown versioning strategy: #{options[:using]}")
52-
end
24+
Dir["#{File.dirname(__FILE__)}/support/*.rb"].each do |file|
25+
require file
5326
end
5427

55-
def versioned_get(path, version_name, version_options = {})
56-
path = versioned_path(version_options.merge(:version => version_name, :path => path))
57-
headers = versioned_headers(version_options.merge(:version => version_name))
58-
get path, {}, headers
59-
end
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
def encode_basic_auth(username, password)
2+
"Basic " + Base64.encode64("#{username}:#{password}")
3+
end
4+

spec/support/versioned_helpers.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Versioning
2+
3+
# Returns the path with options[:version] prefixed if options[:using] is :path.
4+
# Returns normal path otherwise.
5+
def versioned_path(options = {})
6+
case options[:using]
7+
when :path
8+
File.join('/', options[:prefix] || '', options[:version], options[:path])
9+
when :header
10+
File.join('/', options[:prefix] || '', options[:path])
11+
else
12+
raise ArgumentError.new("unknown versioning strategy: #{options[:using]}")
13+
end
14+
end
15+
16+
def versioned_headers(options)
17+
case options[:using]
18+
when :path
19+
{} # no-op
20+
when :header
21+
{
22+
'HTTP_ACCEPT' => "application/vnd.#{options[:vendor]}-#{options[:version]}+#{options[:format]}"
23+
}
24+
else
25+
raise ArgumentError.new("unknown versioning strategy: #{options[:using]}")
26+
end
27+
end
28+
29+
def versioned_get(path, version_name, version_options = {})
30+
path = versioned_path(version_options.merge(:version => version_name, :path => path))
31+
headers = versioned_headers(version_options.merge(:version => version_name))
32+
get path, {}, headers
33+
end
34+

0 commit comments

Comments
 (0)