-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Set response headers based on Rack version #2355
Changes from all commits
608bd18
1b6b7a4
cd71b14
6bfc616
ff4c6b5
5ecb7c7
32ca429
2d87076
0a88e94
7b51e0f
d0850d5
ba8c0e3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,6 +42,10 @@ def self.deprecator | |
@deprecator ||= ActiveSupport::Deprecation.new('2.0', 'Grape') | ||
end | ||
|
||
def self.rack3? | ||
Gem::Version.new(::Rack.release) >= Gem::Version.new('3') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you are just trying to detect whether to use lower case headers or not, I suggest a slightly different approach: LOWER_CASE_HEADERS = Rack::CONTENT_TYPE == "content-type" You could then use that directly or in a method. |
||
end | ||
|
||
eager_autoload do | ||
autoload :API | ||
autoload :Endpoint | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -185,7 +185,7 @@ def redirect(url, permanent: false, body: nil, **_options) | |
status 302 | ||
body_message ||= "This resource has been moved temporarily to #{url}." | ||
end | ||
header 'Location', url | ||
header Grape::Http::Headers::LOCATION, url | ||
content_type 'text/plain' | ||
body body_message | ||
end | ||
|
@@ -328,9 +328,9 @@ def sendfile(value = nil) | |
def stream(value = nil) | ||
return if value.nil? && @stream.nil? | ||
|
||
header 'Content-Length', nil | ||
header 'Transfer-Encoding', nil | ||
header 'Cache-Control', 'no-cache' # Skips ETag generation (reading the response up front) | ||
header Grape::Http::Headers::CONTENT_LENGTH, nil | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can use |
||
header Grape::Http::Headers::TRANSFER_ENCODING, nil | ||
header Grape::Http::Headers::CACHE_CONTROL, 'no-cache' # Skips ETag generation (reading the response up front) | ||
if value.is_a?(String) | ||
file_body = Grape::ServeStream::FileBody.new(value) | ||
@stream = Grape::ServeStream::StreamResponse.new(file_body) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,5 +2,5 @@ | |
|
||
module Grape | ||
# The current version of Grape. | ||
VERSION = '1.8.1' | ||
VERSION = '1.9.0' | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -162,13 +162,19 @@ def validate(request) | |
return unless request.params.key? @attrs.first | ||
# check if admin flag is set to true | ||
return unless @option | ||
|
||
# check if user is admin or not | ||
# as an example get a token from request and check if it's admin or not | ||
raise Grape::Exceptions::Validation.new(params: @attrs, message: 'Can not set Admin only field.') unless request.headers['X-Access-Token'] == 'admin' | ||
raise Grape::Exceptions::Validation.new(params: @attrs, message: 'Can not set Admin only field.') unless request.headers[access_header] == 'admin' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, tried that, but got...
which was annoying, as don't like the defining it twice either. |
||
end | ||
|
||
def access_header | ||
Grape.rack3? ? 'x-access-token' : 'X-Access-Token' | ||
end | ||
end | ||
end | ||
let(:app) { Rack::Builder.new(subject) } | ||
let(:x_access_token_header) { Grape.rack3? ? 'x-access-token' : 'X-Access-Token' } | ||
|
||
before do | ||
described_class.register_validator('admin', admin_validator) | ||
|
@@ -197,14 +203,14 @@ def validate(request) | |
end | ||
|
||
it 'does not fail when we send admin fields and we are admin' do | ||
header 'X-Access-Token', 'admin' | ||
header x_access_token_header, 'admin' | ||
get '/', admin_field: 'tester', non_admin_field: 'toaster', admin_false_field: 'test' | ||
expect(last_response.status).to eq 200 | ||
expect(last_response.body).to eq 'bacon' | ||
end | ||
|
||
it 'fails when we send admin fields and we are not admin' do | ||
header 'X-Access-Token', 'user' | ||
header x_access_token_header, 'user' | ||
get '/', admin_field: 'tester', non_admin_field: 'toaster', admin_false_field: 'test' | ||
expect(last_response.status).to eq 400 | ||
expect(last_response.body).to include 'Can not set Admin only field.' | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rack 3 is following the HTTP/2+ semantics which require header names to be lower case. To avoid compatibility issues, starting with Grape 1.9.0, headers will be cased based on what version of Rack you are using.