Skip to content

Commit ade2d26

Browse files
committed
Merge pull request ruby-grape#117 from dblock/frontier-rbx
Fixes ruby-grape#113, build broken on Rubinius
2 parents 5a3bc1e + b3c4b58 commit ade2d26

File tree

8 files changed

+51
-39
lines changed

8 files changed

+51
-39
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,7 @@ pkg
2727
dist
2828
Gemfile.lock
2929

30+
## Rubinius
31+
.rbx
32+
3033
## PROJECT::SPECIFIC

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ group :development, :test do
99
gem 'guard-bundler'
1010
gem 'rb-fsevent'
1111
gem 'growl'
12+
gem 'json'
1213
end

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: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -14,44 +14,14 @@
1414
require 'pry'
1515

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

2120
RSpec.configure do |config|
2221
config.include Rack::Test::Methods
2322
end
2423

25-
# Versioning
26-
27-
# Returns the path with options[:version] prefixed if options[:using] is :path.
28-
# Returns normal path otherwise.
29-
def versioned_path(options = {})
30-
case options[:using]
31-
when :path
32-
File.join('/', options[:prefix] || '', options[:version], options[:path])
33-
when :header
34-
File.join('/', options[:prefix] || '', options[:path])
35-
else
36-
raise ArgumentError.new("unknown versioning strategy: #{options[:using]}")
37-
end
24+
Dir["#{File.dirname(__FILE__)}/support/*.rb"].each do |file|
25+
require file
3826
end
3927

40-
def versioned_headers(options)
41-
case options[:using]
42-
when :path
43-
{} # no-op
44-
when :header
45-
{
46-
'HTTP_ACCEPT' => "application/vnd.#{options[:vendor]}-#{options[:version]}+#{options[:format]}"
47-
}
48-
else
49-
raise ArgumentError.new("unknown versioning strategy: #{options[:using]}")
50-
end
51-
end
52-
53-
def versioned_get(path, version_name, version_options = {})
54-
path = versioned_path(version_options.merge(:version => version_name, :path => path))
55-
headers = versioned_headers(version_options.merge(:version => version_name))
56-
get path, {}, headers
57-
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)