Skip to content

Commit 17c3157

Browse files
committed
✨ Fixes handling of empty JSON responses that should not be parsed
- https://gitlab.com/oauth-xx/oauth2/-/issues/641
1 parent b0076bc commit 17c3157

File tree

3 files changed

+50
-14
lines changed

3 files changed

+50
-14
lines changed

.rubocop_gradual.lock

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
[9, 9, 25, "ThreadSafety/ClassInstanceVariable: Avoid class instance variables.", 2012823020],
1919
[13, 9, 25, "ThreadSafety/ClassInstanceVariable: Avoid class instance variables.", 2012823020]
2020
],
21-
"lib/oauth2/response.rb:355921218": [
21+
"lib/oauth2/response.rb:4048171841": [
2222
[35, 5, 204, "Style/ClassMethodsDefinitions: Use `class << self` to define a class method.", 996912427]
2323
],
2424
"oauth2.gemspec:290828046": [
@@ -48,7 +48,7 @@
4848
[69, 15, 38, "RSpec/ContextWording: Context description should match /^when\\b/, /^with\\b/, or /^without\\b/.", 1480816240],
4949
[79, 13, 23, "RSpec/ContextWording: Context description should match /^when\\b/, /^with\\b/, or /^without\\b/.", 2314399065]
5050
],
51-
"spec/oauth2/client_spec.rb:1326196445": [
51+
"spec/oauth2/client_spec.rb:348683155": [
5252
[6, 1, 29, "RSpec/SpecFilePathFormat: Spec path should end with `o_auth2/client*_spec.rb`.", 439549885],
5353
[175, 7, 492, "RSpec/NoExpectationExample: No expectation found in this example.", 1272021224],
5454
[194, 7, 592, "RSpec/NoExpectationExample: No expectation found in this example.", 3428877205],
@@ -59,13 +59,12 @@
5959
[830, 5, 360, "RSpec/NoExpectationExample: No expectation found in this example.", 536201463],
6060
[839, 5, 461, "RSpec/NoExpectationExample: No expectation found in this example.", 3392600621],
6161
[850, 5, 340, "RSpec/NoExpectationExample: No expectation found in this example.", 244592251],
62-
[895, 63, 2, "RSpec/BeEq: Prefer `be` over `eq`.", 5860785],
63-
[940, 11, 99, "Style/ClassMethodsDefinitions: Use `class << self` to define a class method.", 3084776886],
64-
[944, 11, 82, "Style/ClassMethodsDefinitions: Use `class << self` to define a class method.", 1524553529],
65-
[952, 7, 89, "RSpec/NoExpectationExample: No expectation found in this example.", 4609419],
66-
[1040, 11, 99, "Style/ClassMethodsDefinitions: Use `class << self` to define a class method.", 3084776886],
67-
[1044, 11, 82, "Style/ClassMethodsDefinitions: Use `class << self` to define a class method.", 1524553529],
68-
[1124, 17, 12, "RSpec/ContextWording: Context description should match /^when\\b/, /^with\\b/, or /^without\\b/.", 664794325]
62+
[978, 11, 99, "Style/ClassMethodsDefinitions: Use `class << self` to define a class method.", 3084776886],
63+
[982, 11, 82, "Style/ClassMethodsDefinitions: Use `class << self` to define a class method.", 1524553529],
64+
[990, 7, 89, "RSpec/NoExpectationExample: No expectation found in this example.", 4609419],
65+
[1078, 11, 99, "Style/ClassMethodsDefinitions: Use `class << self` to define a class method.", 3084776886],
66+
[1082, 11, 82, "Style/ClassMethodsDefinitions: Use `class << self` to define a class method.", 1524553529],
67+
[1162, 17, 12, "RSpec/ContextWording: Context description should match /^when\\b/, /^with\\b/, or /^without\\b/.", 664794325]
6968
],
7069
"spec/oauth2/error_spec.rb:1209122273": [
7170
[23, 1, 28, "RSpec/SpecFilePathFormat: Spec path should end with `o_auth2/error*_spec.rb`.", 3385870076],

lib/oauth2/response.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,10 @@ def parser
143143
end
144144

145145
OAuth2::Response.register_parser(:json, ["application/json", "text/javascript", "application/hal+json", "application/vnd.collection+json", "application/vnd.api+json", "application/problem+json"]) do |body|
146-
next body if body.nil?
147146
next body unless body.respond_to?(:to_str)
148147

149148
body = body.dup.force_encoding(Encoding::ASCII_8BIT) if body.respond_to?(:force_encoding)
150-
next body if body.empty?
149+
next body if body.respond_to?(:empty?) && body.empty?
151150

152151
JSON.parse(body)
153152
end

spec/oauth2/client_spec.rb

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -883,16 +883,54 @@
883883
context "when the request body is nil" do
884884
subject(:get_token) { client.get_token({}) }
885885

886-
it "raises error JSON::ParserError" do
887-
block_is_expected { get_token }.to raise_error(JSON::ParserError)
886+
it "does not raise error" do
887+
block_is_expected { get_token }.not_to raise_error
888888
end
889889

890890
context "when extract_access_token raises an exception" do
891891
let(:status_code) { 200 }
892892
let(:extract_proc) { proc { |client, hash| raise ArgumentError } }
893893

894894
it "returns a nil :access_token" do
895-
expect(client.get_token({}, {}, extract_proc)).to eq(nil)
895+
expect(client.get_token({}, {}, extract_proc)).to be_nil
896+
end
897+
end
898+
end
899+
900+
context "when the request body is empty" do
901+
subject(:get_token) { client.get_token({}) }
902+
903+
let(:body) { "" }
904+
905+
it "does not raise error" do
906+
block_is_expected { get_token }.not_to raise_error
907+
end
908+
909+
context "when extract_access_token raises an exception" do
910+
let(:status_code) { 200 }
911+
let(:extract_proc) { proc { |client, hash| raise ArgumentError } }
912+
913+
it "returns a nil :access_token" do
914+
expect(client.get_token({}, {}, extract_proc)).to be_nil
915+
end
916+
end
917+
end
918+
919+
context "when the request body is not valid JSON" do
920+
subject(:get_token) { client.get_token({}) }
921+
922+
let(:body) { "BLOOP" }
923+
924+
it "raises error" do
925+
block_is_expected { get_token }.to raise_error(JSON::ParserError, /unexpected character: 'BLOOP'/)
926+
end
927+
928+
context "when extract_access_token raises an exception" do
929+
let(:status_code) { 200 }
930+
let(:extract_proc) { proc { |client, hash| raise ArgumentError } }
931+
932+
it "returns a nil :access_token" do
933+
expect(client.get_token({}, {}, extract_proc)).to be_nil
896934
end
897935
end
898936
end

0 commit comments

Comments
 (0)