Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

filter nested params

  • Loading branch information...
commit 6f81c433aeefad75accee6469e2db5623eebd616 1 parent 814f875
@shime shime authored
Showing with 42 additions and 12 deletions.
  1. +12 −10 lib/sinatra/exceptional.rb
  2. +30 −2 spec/sinatra_exceptional_spec.rb
View
22 lib/sinatra/exceptional.rb
@@ -29,26 +29,28 @@ def framework
"sinatra"
end
- def params
- params = {}
-
+ def filtered_params(params)
+ final_params = {}
if @opts && @opts[:params_filter]
- @request.params.each do |key,original_value|
- params[key] = key.to_s.match(@opts[:params_filter]) ? FILTERED_TEXT : original_value
+ params.each do |key,original_value|
+ if key.to_s === @opts[:params_filter]
+ final_params[key] = FILTERED_TEXT
+ elsif original_value.respond_to?(:to_hash)
+ final_params[key] = filtered_params(params[key])
+ else
+ final_params[key] = params[key]
+ end
end
- else
- params = @request.params
end
- params
+ final_params
end
def extra_stuff
-
return {} if @request.nil?
{
'request' => {
'url' => "#{@request.url}",
- 'parameters' => params,
+ 'parameters' => filtered_params(@request.params),
'request_method' => @request.request_method.to_s,
'remote_ip' => @request.ip,
'headers' => extract_http_headers(@environment),
View
32 spec/sinatra_exceptional_spec.rb
@@ -2,8 +2,9 @@
require File.join(File.join(File.expand_path(File.dirname(__FILE__))), '..', 'lib', 'sinatra', 'exceptional')
require 'rack/test'
require 'minitest/autorun'
-require 'webmock'
+require 'webmock/minitest'
require 'json'
+require "sinatra/exceptional"
include Rack::Test::Methods
include WebMock::API
@@ -12,6 +13,7 @@ def mock_app(&block)
@app = Sinatra.new PartyHard, &block
end
+
class PartyHard < Sinatra::Base
class AndrewWKPartyError < StandardError; end
@@ -52,7 +54,7 @@ def app
last_response.body.must_equal 'HELL YEAH'
end
- it 'throws an party error like a boss' do
+ it 'throws a party error like a boss' do
lambda {
get '/partyfail'
}.must_raise PartyHard::AndrewWKPartyError
@@ -67,3 +69,29 @@ def app
last_response.errors.must_match /AndrewWKPartyError/
end
end
+
+describe "SinatraExceptionData" do
+ describe "#params" do
+ before :each do
+ @data = Exceptional::SinatraExceptionData.new(nil,nil,nil,{:params_filter => "test"})
+ end
+
+ it "should filter params" do
+ @data.
+ filtered_params({"test" => "hidden","not_test" => "not_hidden"})["test"].
+ must_match Exceptional::SinatraExceptionData::FILTERED_TEXT
+ @data.
+ filtered_params({"test" => "hidden","not_test" => "not_hidden"})["not_test"].
+ must_match "not_hidden"
+ end
+
+ it "should filter nested params" do
+ @data.
+ filtered_params({"a" => {"test" => "hidden"},"not_test" => "not_hidden"})["a"]["test"].
+ must_match Exceptional::SinatraExceptionData::FILTERED_TEXT
+ @data.
+ filtered_params({"a" => {"test" => "hidden"},"not_test" => "not_hidden"})["not_test"].
+ must_match "not_hidden"
+ end
+ end
+end

0 comments on commit 6f81c43

Please sign in to comment.
Something went wrong with that request. Please try again.