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/handle invalid wp json response #1818

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions app/finders/users/oembed_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ def user_details_from_oembed_data(oembed_data)

oembed_data = oembed_data.first if oembed_data.is_a?(Array)

oembed_data = {} unless oembed_data.is_a?(Hash)

if oembed_data['author_url'] =~ %r{/author/([^/]+)/?\z}
details = [Regexp.last_match[1], 'Author URL', 90]
elsif oembed_data['author_name'] && !oembed_data['author_name'].empty?
Expand Down
16 changes: 10 additions & 6 deletions app/finders/users/wp_json_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,16 @@ def aggressive(_opts = {})
def users_from_response(response)
found = []

JSON.parse(response.body)&.each do |user|
found << Model::User.new(user['slug'],
id: user['id'],
found_by: found_by,
confidence: 100,
interesting_entries: [response.effective_url])
json = JSON.parse(response.body)

if json.is_a?(Enumerable)
json.each do |user|
found << Model::User.new(user['slug'],
id: user['id'],
found_by: found_by,
confidence: 100,
interesting_entries: [response.effective_url])
end
end

found
Expand Down
12 changes: 10 additions & 2 deletions spec/app/finders/users/oembed_api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,17 @@
end

context 'when not a JSON response' do
let(:body) { '' }
context 'when empty' do
let(:body) { '' }

its(:aggressive) { should eql([]) }
its(:aggressive) { should eql([]) }
end

context 'when a string' do
let(:body) { '404' }

its(:aggressive) { should eql([]) }
end
end

context 'when a JSON response' do
Expand Down
12 changes: 10 additions & 2 deletions spec/app/finders/users/wp_json_api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,17 @@
end

context 'when not a JSON response' do
let(:body) { '' }
context 'when empty' do
let(:body) { '' }

its(:aggressive) { should eql([]) }
its(:aggressive) { should eql([]) }
end

context 'when a string' do
let(:body) { '404' }

its(:aggressive) { should eql([]) }
end
end

context 'when a JSON response' do
Expand Down
Loading