Skip to content

Commit

Permalink
Use response.parsed_body instead of JSON.parse
Browse files Browse the repository at this point in the history
  • Loading branch information
rubhanazeem committed Feb 28, 2023
1 parent a8df411 commit 4f5781e
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def index

describe 'returns json' do
it { expect(response).to have_http_status(:forbidden) }
it { expect(JSON.parse(response.body)['errorcode']).to eq('list_fake_object_not_authorized') }
it { expect(response.parsed_body['errorcode']).to eq('list_fake_object_not_authorized') }
end
end

Expand Down
2 changes: 1 addition & 1 deletion src/api/spec/controllers/webui/comments_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@
end

it 'renders comment with Markdown properly' do
json = JSON.parse(response.body)
json = response.parsed_body
expect(json['markdown']).to eq("<h1>test comment header</h1>\n")
end
end
Expand Down
4 changes: 2 additions & 2 deletions src/api/spec/controllers/webui/monitor_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
before do
stub_request(:get, "#{CONFIG['source_url']}/build/_workerstatus").and_return(body: xml_response)
get :update_building, xhr: true
@json_response = JSON.parse(response.body)
@json_response = response.parsed_body
end

it { expect(@json_response).to have_key('simulated') }
Expand All @@ -70,7 +70,7 @@
create_list(:status_history, 5, source: 'squeue_high', architecture: 'i586')
# the factory creates the events 0..8000 hours ago
get :events, params: { arch: 'x86_64', range: 8100 }, xhr: true
@json_response = JSON.parse(response.body)
@json_response = response.parsed_body
end

it { expect(@json_response['events_max']).to be <= 800 }
Expand Down
14 changes: 7 additions & 7 deletions src/api/spec/controllers/webui/patchinfo_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,8 @@ def do_proper_post_save
let(:my_issues) { ['bgo#132412'] }

it do
expect(JSON.parse(response.body)).to eq('error' => '',
'issues' => [['bgo', '132412', 'https://bugzilla.gnome.org/show_bug.cgi?id=132412', '']])
expect(response.parsed_body).to eq('error' => '',
'issues' => [['bgo', '132412', 'https://bugzilla.gnome.org/show_bug.cgi?id=132412', '']])
end

it { expect(response).to have_http_status(:success) }
Expand All @@ -290,8 +290,8 @@ def do_proper_post_save
let(:my_issues) { ['CVE-2010-31337'] }

it do
expect(JSON.parse(response.body)).to eq('error' => '',
'issues' => [['cve', 'CVE-2010-31337', 'http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2010-31337', '']])
expect(response.parsed_body).to eq('error' => '',
'issues' => [['cve', 'CVE-2010-31337', 'http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2010-31337', '']])
end

it { expect(response).to have_http_status(:success) }
Expand All @@ -301,7 +301,7 @@ def do_proper_post_save
context 'if issues are wrongly formatted' do
let(:my_issues) { ['hell#666'] }

it { expect(JSON.parse(response.body)).to eq('error' => "hell is not a valid tracker.\n", 'issues' => []) }
it { expect(response.parsed_body).to eq('error' => "hell is not a valid tracker.\n", 'issues' => []) }
it { expect(response).to have_http_status(:success) }
end

Expand All @@ -310,9 +310,9 @@ def do_proper_post_save

it {
error_message = 'cve has no valid format. (Correct formats are e.g. boo#123456, CVE-1234-5678 and the string has to be a comma-separated list)'
expect(JSON.parse(response.body)).to eq('error' =>
expect(response.parsed_body).to eq('error' =>
error_message,
'issues' => [])
'issues' => [])
}

it { expect(response).to have_http_status(:success) }
Expand Down
18 changes: 9 additions & 9 deletions src/api/spec/controllers/webui/project_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
context 'without search term' do
before do
get :autocomplete_projects
@json_response = JSON.parse(response.body)
@json_response = response.parsed_body
end

it { expect(@json_response).to contain_exactly(apache_project.name, apache2_project.name, opensuse_project.name) }
Expand All @@ -85,7 +85,7 @@
context 'with search term' do
before do
get :autocomplete_projects, params: { term: 'Apache' }
@json_response = JSON.parse(response.body)
@json_response = response.parsed_body
end

it { expect(@json_response).to contain_exactly(apache_project.name, apache2_project.name) }
Expand All @@ -99,7 +99,7 @@
context 'and searching for parent project' do
before do
get :autocomplete_projects, params: { term: 'Apache' }
@json_response = JSON.parse(response.body)
@json_response = response.parsed_body
end

it { expect(@json_response).to include(apache_subproject.name) }
Expand All @@ -108,7 +108,7 @@
context 'and searching for parent project' do
before do
get :autocomplete_projects, params: { term: 'Apache:' }
@json_response = JSON.parse(response.body)
@json_response = response.parsed_body
end

it { expect(@json_response).to include(apache_subproject.name) }
Expand All @@ -121,7 +121,7 @@
apache_project
apache_maintenance_incident_project
get :autocomplete_incidents, params: { term: 'Apache' }
@json_response = JSON.parse(response.body)
@json_response = response.parsed_body
end

it { expect(@json_response).to contain_exactly(apache_maintenance_incident_project.name) }
Expand All @@ -138,7 +138,7 @@
context 'with a nonexistent project' do
before do
get :autocomplete_packages, params: { project: 'non:existent:project' }
@json_response = JSON.parse(response.body)
@json_response = response.parsed_body
end

it { expect(@json_response).to be_nil }
Expand All @@ -147,7 +147,7 @@
context 'without search term' do
before do
get :autocomplete_packages, params: { project: apache_project }
@json_response = JSON.parse(response.body)
@json_response = response.parsed_body
end

it { expect(@json_response).to contain_exactly('Apache_Package', 'Apache2_Package') }
Expand All @@ -157,7 +157,7 @@
context 'with search term' do
before do
get :autocomplete_packages, params: { project: apache_project, term: 'Apache2' }
@json_response = JSON.parse(response.body)
@json_response = response.parsed_body
end

it { expect(@json_response).to contain_exactly('Apache2_Package') }
Expand All @@ -170,7 +170,7 @@
before do
@repositories = create_list(:repository, 5, project: apache_project)
get :autocomplete_repositories, params: { project: apache_project }
@json_response = JSON.parse(response.body)
@json_response = response.parsed_body
end

it { expect(@json_response).to match_array(@repositories.map(&:name)) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
order: { '0' => { 'column' => '0', 'dir' => 'asc' } }, start: '0' }
end

let(:json_response) { JSON.parse(response.body) }
let(:json_response) { response.parsed_body }

before do
login user
Expand Down
4 changes: 2 additions & 2 deletions src/api/spec/controllers/webui/users_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@

it 'returns user login' do
get :autocomplete, params: { term: 'foo', format: :json }
expect(JSON.parse(response.body)).to match_array(['foobar'])
expect(response.parsed_body).to match_array(['foobar'])
end
end

Expand All @@ -383,7 +383,7 @@

it 'returns user token as array of hash' do
get :tokens, params: { q: 'foo', format: :json }
expect(JSON.parse(response.body)).to match_array([{ 'name' => 'foobaz' }])
expect(response.parsed_body).to match_array([{ 'name' => 'foobaz' }])
end
end

Expand Down

0 comments on commit 4f5781e

Please sign in to comment.