Skip to content
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

Fix asterisk encoding issue #969

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions lib/twitter/rest/form_encoder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module Twitter
module REST
class FormEncoder
UNESCAPED_CHARS = /[^a-z0-9\-\.\_\~]/i

def self.encode(data)
data.map do |k, v|
if v.nil?
::URI::DEFAULT_PARSER.escape(k.to_s, UNESCAPED_CHARS)
elsif v.respond_to?(:to_ary)
v.to_ary.map do |w|
str = ::URI::DEFAULT_PARSER.escape(k.to_s, UNESCAPED_CHARS)
unless w.nil?
str << '='
str << ::URI::DEFAULT_PARSER.escape(w.to_s, UNESCAPED_CHARS)
end
end.join('&')
else
str = ::URI::DEFAULT_PARSER.escape(k.to_s, UNESCAPED_CHARS)
str << '='
str << ::URI::DEFAULT_PARSER.escape(v.to_s, UNESCAPED_CHARS)
end
end.join('&')
end
end
end
end
8 changes: 7 additions & 1 deletion lib/twitter/rest/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
require 'twitter/headers'
require 'twitter/rate_limit'
require 'twitter/utils'
require 'twitter/rest/form_encoder'

module Twitter
module REST
Expand Down Expand Up @@ -44,7 +45,12 @@ def perform
private

def request_options
options = {@options_key => @options}
if @options_key == :form
options = {form: HTTP::FormData.create(@options, encoder: Twitter::REST::FormEncoder.method(:encode))}
else
options = {@options_key => @options}
end

if @params
if options[:params]
options[:params].merge(@params)
Expand Down
9 changes: 9 additions & 0 deletions spec/twitter/rest/form_encoder_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require 'helper'

describe Twitter::REST::FormEncoder do
describe '.encode' do
it 'encodes asterisk correctly' do
expect(described_class.encode({status: 'Update *'})).to eq('status=Update%20%2A')
end
end
end
7 changes: 7 additions & 0 deletions spec/twitter/rest/request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
describe '#request' do
it 'encodes the entire body when no uploaded media is present' do
stub_post('/1.1/statuses/update.json').with(body: {status: 'Update'}).to_return(body: fixture('status.json'), headers: {content_type: 'application/json; charset=utf-8'})
expect_any_instance_of(HTTP::Client).to receive(:post).with('https://api.twitter.com/1.1/statuses/update.json', form: instance_of(HTTP::FormData::Urlencoded)).and_call_original
@client.update('Update')
expect(a_post('/1.1/statuses/update.json').with(body: {status: 'Update'})).to have_been_made
end
Expand All @@ -18,6 +19,12 @@
expect(a_request(:post, 'https://upload.twitter.com/1.1/media/upload.json')).to have_been_made
expect(a_post('/1.1/statuses/update.json').with(body: {status: 'Update', media_ids: '470030289822314497'})).to have_been_made
end
it 'uses custom HTTP::FormData::Urlencoded instance for form requests' do
stub_post('/1.1/statuses/update.json').with(body: {status: 'Update'}).to_return(body: fixture('status.json'), headers: {content_type: 'application/json; charset=utf-8'})
expect_any_instance_of(HTTP::Client).to receive(:post).with('https://api.twitter.com/1.1/statuses/update.json', form: instance_of(HTTP::FormData::Urlencoded)).and_call_original
expect(Twitter::REST::FormEncoder).to receive(:encode).with({status: 'Update'}).and_call_original
@client.update('Update')
end

context 'when using a proxy' do
before do
Expand Down
4 changes: 2 additions & 2 deletions twitter.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ Gem::Specification.new do |spec|
spec.add_dependency 'addressable', '~> 2.3'
spec.add_dependency 'buftok', '~> 0.2.0'
spec.add_dependency 'equalizer', '~> 0.0.11'
spec.add_dependency 'http', '~> 4.0'
spec.add_dependency 'http-form_data', '~> 2.0'
spec.add_dependency 'http', '~> 4.4'
spec.add_dependency 'http-form_data', '~> 2.3'
spec.add_dependency 'http_parser.rb', '~> 0.6.0'
spec.add_dependency 'memoizable', '~> 0.4.0'
spec.add_dependency 'multipart-post', '~> 2.0'
Expand Down