Skip to content

Commit 126c395

Browse files
committed
Add an additional test, improve the error check to fix the tests
1 parent 6aedd90 commit 126c395

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

lib/importmap/npm.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def get_json(uri)
8080
http.request(request)
8181
}
8282

83-
unless response.kind_of? Net::HTTPSuccess
83+
unless response.code.to_i < 300
8484
raise HTTPError, "Unexpected error response #{response.code}: #{response.body}"
8585
end
8686

@@ -115,7 +115,7 @@ def get_audit
115115
return {} if body.empty?
116116

117117
response = post_json(uri, body)
118-
unless response.kind_of? Net::HTTPSuccess
118+
unless response.code.to_i < 300
119119
raise HTTPError, "Unexpected error response #{response.code}: #{response.body}"
120120
end
121121
JSON.parse(response.body)

test/npm_test.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,22 @@ class Importmap::NpmTest < ActiveSupport::TestCase
6767
end
6868
end
6969

70+
test "failed vulnerable packages with mock" do
71+
response = Class.new do
72+
def body
73+
{ "message" => "Service unavailable" }.to_json
74+
end
75+
76+
def code() "500" end
77+
end.new
78+
79+
@npm.stub(:post_json, response) do
80+
assert_raises(Importmap::Npm::HTTPError) do
81+
vulnerable_packages = @npm.vulnerable_packages
82+
end
83+
end
84+
end
85+
7086
test "successful vulnerable packages with mock" do
7187
response = Class.new do
7288
def body

0 commit comments

Comments
 (0)