Skip to content

Commit

Permalink
Merge d16ca59 into 97f075a
Browse files Browse the repository at this point in the history
  • Loading branch information
summera committed Mar 28, 2020
2 parents 97f075a + d16ca59 commit cdb8cb7
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 3 deletions.
27 changes: 27 additions & 0 deletions lib/twitter/rest/form_encoder.rb
@@ -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
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
@@ -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
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
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

0 comments on commit cdb8cb7

Please sign in to comment.