Skip to content

Commit 0e53c0e

Browse files
committed
Setting request body to JSON fixes webhook API JSON decoding error
includes Tests for JSON conversion
1 parent 683df90 commit 0e53c0e

File tree

4 files changed

+33
-24
lines changed

4 files changed

+33
-24
lines changed

lib/bitbucket_rest_api/connection.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,9 @@ def connection(options = {})
8989
clear_cache unless options.empty?
9090
puts "OPTIONS:#{conn_options.inspect}" if ENV['DEBUG']
9191

92-
@connection ||= Faraday.new(conn_options.merge(:builder => stack(options)))
92+
@connection ||= Faraday.new(conn_options.merge(:builder => stack(options))) do |faraday|
93+
faraday.response :logger if ENV['DEBUG']
94+
end
9395
end
9496

9597
end # Connection

lib/bitbucket_rest_api/request.rb

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,25 @@ def request(method, path, params, options)
4747
request.url(path, params)
4848
when *METHODS_WITH_BODIES
4949
request.path = path
50-
request.body = extract_data_from_params(params) unless params.empty?
50+
unless params.empty?
51+
# data = extract_data_from_params(params)
52+
# request.body = MultiJson.dump(data)
53+
request.body = MultiJson.dump(params)
54+
end
5155
end
5256
end
5357
response.body
5458
end
5559

5660
private
5761

58-
def extract_data_from_params(params) # :nodoc:
59-
return params['data'] if params.has_key?('data') and !params['data'].nil?
60-
return params
61-
end
62+
# def extract_data_from_params(params) # :nodoc:
63+
# if params.has_key?('data') and !params['data'].nil?
64+
# params['data']
65+
# else
66+
# params
67+
# end
68+
# end
6269

6370
def _extract_mime_type(params, options) # :nodoc:
6471
options['resource'] = params['resource'] ? params.delete('resource') : ''

spec/bitbucket_rest_api/request_spec.rb

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
describe "request" do
99
it "raises an ArgumentError if an unsupported HTTP verb is used" do
10-
expect { fake_api.new.request(:i_am_a_teapot, '/', {}, {}) }.to raise_error(ArgumentError)
10+
expect { fake_api.new.request(:i_am_a_teapot, '/') }.to raise_error(ArgumentError)
1111
end
1212

1313
context "with a connection" do
@@ -17,64 +17,64 @@
1717
end
1818

1919
it "supports get" do
20-
stub_request(:get, "https://api.bitbucket.org/1.0/repositories").
20+
stub_request(:get, "https://api.bitbucket.org/1.0/endpoint").
2121
with(:headers => {
2222
'Accept' => '*/*',
2323
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
2424
'Authorization' => 'Bearer 12345',
2525
'User-Agent' => 'Faraday v0.9.2'
2626
})
2727

28-
fake_api.new.request(:get, '/1.0/repositories', {}, {})
28+
fake_api.new.request(:get, '/1.0/endpoint', {}, {})
2929
end
3030

3131
it "supports put" do
32-
stub_request(:put, "https://api.bitbucket.org/1.0/repositories").
33-
with(:body => { "data" => "payload" },
32+
stub_request(:put, "https://api.bitbucket.org/1.0/endpoint").
33+
with(:body => "{\"data\":{\"key\":\"value\"}}",
3434
:headers => {
3535
'Accept' => '*/*',
3636
'Content-Type'=>'application/x-www-form-urlencoded',
3737
'Authorization' => 'Bearer 12345',
3838
'User-Agent' => 'Faraday v0.9.2'
3939
})
4040

41-
fake_api.new.request(:put, '/1.0/repositories', { :data => "payload" }, {})
41+
fake_api.new.request(:put, '/1.0/endpoint', { 'data' => { 'key' => 'value'} }, {})
4242
end
4343

4444
it "supports patch" do
45-
stub_request(:patch, "https://api.bitbucket.org/1.0/repositories").
46-
with(:body => { "data" => "payload" },
45+
stub_request(:patch, "https://api.bitbucket.org/1.0/endpoint").
46+
with(:body => "{\"data\":{\"key\":\"value\"}}",
4747
:headers => {
4848
'Accept' => '*/*',
4949
'Content-Type'=>'application/x-www-form-urlencoded',
5050
'Authorization' => 'Bearer 12345',
5151
'User-Agent' => 'Faraday v0.9.2'
5252
})
5353

54-
fake_api.new.request(:patch, '/1.0/repositories', { :data => "payload" }, {})
54+
fake_api.new.request(:patch, '/1.0/endpoint', { 'data' => { 'key' => 'value'} }, {})
5555
end
5656

5757
it "supports delete" do
58-
stub_request(:delete, "https://api.bitbucket.org/1.0/repositories").
58+
stub_request(:delete, "https://api.bitbucket.org/1.0/endpoint").
5959
with(:headers => {
6060
'Accept' => '*/*',
6161
'Authorization' => 'Bearer 12345',
6262
'User-Agent' => 'Faraday v0.9.2'
6363
})
64-
fake_api.new.request(:delete, '/1.0/repositories', {}, {})
64+
fake_api.new.request(:delete, '/1.0/endpoint', {}, {})
6565
end
6666

6767
it "supports post" do
68-
stub_request(:post, "https://api.bitbucket.org/1.0/repositories").
69-
with(:body => { "data" => "payload" },
68+
stub_request(:post, "https://api.bitbucket.org/1.0/endpoint").
69+
with(:body => "{\"data\":{\"key\":\"value\"}}",
7070
:headers => {
7171
'Accept' => '*/*',
7272
'Content-Type'=>'application/x-www-form-urlencoded',
7373
'Authorization' => 'Bearer 12345',
7474
'User-Agent' => 'Faraday v0.9.2'
7575
})
7676

77-
fake_api.new.request(:post, '/1.0/repositories', { :data => "payload" }, {})
77+
fake_api.new.request(:post, '/1.0/endpoint', { 'data' => { 'key' => 'value'} }, {})
7878
end
7979
end
8080
end

spec/spec_helper.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
end
1919
end
2020

21-
VCR.configure do |config|
22-
config.cassette_library_dir = 'spec/fixtures/vcr_cassettes'
23-
config.hook_into :webmock
24-
end
21+
# VCR.configure do |config|
22+
# config.cassette_library_dir = 'spec/fixtures/vcr_cassettes'
23+
# config.hook_into :webmock
24+
# end

0 commit comments

Comments
 (0)