Skip to content

Commit

Permalink
Change html response body check (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
JorgeLeites authored Jun 26, 2024
1 parent 318fb2f commit f1cc1c6
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 8 deletions.
16 changes: 11 additions & 5 deletions lib/rock_rms/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,23 @@ def on_complete(env)
private

def html_body?(body)
body.lstrip.start_with?('<!DOCTYPE html>')
/(<!DOCTYPE html>)|(<html>)/ =~ body
end

def check_html_error(env)
return unless html_body?(env[:body])

return unless /An error has occurred while processing your request/ =~ env[:body]
if /An error has occurred while processing your request/ =~ env[:body]
raise RockRMS::InternalServerError, error_message(
status: 500, url: env[:url], body: 'Unknown API error.'
)
end

raise RockRMS::InternalServerError, error_message(
status: 500, url: env[:url], body: 'Unknown API error.'
)
if /Page Not Found/ =~ env[:body]
raise RockRMS::NotFound, error_message(
status: 404, url: env[:url], body: 'Page not found.'
)
end
end

def error_message(env)
Expand Down
2 changes: 1 addition & 1 deletion lib/rock_rms/parse_oj.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def on_complete(env)
private

def html_body?(body)
body.start_with?('<!DOCTYPE html>')
/(<!DOCTYPE html>)|(<html>)/ =~ body
end

def empty_body?(body)
Expand Down
10 changes: 8 additions & 2 deletions spec/rock_rms/error_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,16 @@ def expect_success
end

context 'html error responses' do
it 'raises exception for html error' do
body = File.read('spec/rock_rms/fixtures/html_error.html')
it 'raises exception for internal error' do
body = File.read('spec/rock_rms/fixtures/internal_error.html')
stub(200, body)
expect_failure(500, 'Unknown API error.')
end

it 'raises exception for not found error' do
body = File.read('spec/rock_rms/fixtures/not_found_error.html')
stub(200, body)
expect_failure(404, 'Page not found.')
end
end
end
File renamed without changes.
79 changes: 79 additions & 0 deletions spec/rock_rms/fixtures/not_found_error.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<html>
<head>
<title>Object moved</title>
</head>
<body>
<h2>Object moved to <a href="/page/123">here</a>.</h2>
</body>
</html>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Rock - Page Not Found</title>

<link rel="stylesheet" href="/Themes/Rock/Styles/bootstrap.css" />
<link rel="stylesheet" href="/Themes/Rock/Styles/theme.css" />

<!-- Icons -->
<link rel="shortcut icon" href="/Assets/Icons/favicon.ico" />
<link
rel="apple-touch-icon-precomposed"
sizes="144x144"
href="/Assets/Icons/touch-icon-ipad-retina.png"
/>
<link
rel="apple-touch-icon-precomposed"
sizes="114x114"
href="/Assets/Icons/touch-icon-iphone-retina.png"
/>
<link
rel="apple-touch-icon-precomposed"
sizes="72x72"
href="/Assets/Icons/touch-icon-ipad.png"
/>
<link
rel="apple-touch-icon-precomposed"
href="/Assets/Icons/touch-icon-iphone.png"
/>

<script src="/Scripts/Bundles/RockJQueryLatest?v=RWC4egkRBNjCo9_aBrw2jUeb13vsJBPHaIryUym02aM1"></script>
</head>
<body id="splash" class="error">
<form method="post" action="/" id="form1">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="" />

<input
type="hidden"
name="__VIEWSTATEGENERATOR"
id="__VIEWSTATEGENERATOR"
value=""
/>

<div id="content">
<div id="logo"></div>

<div id="content-box">
<div class="row">
<div class="col-md-12">
<div class="error-wrap">
<h1>We Can't Find That Page</h1>

<p class="error-icon info">
<i class="fa fa-question-circle"></i>
</p>

<p>
Sorry, but the page you are looking for can not be found.
Check the address of the page and see your administrator if
you still need assistance.
</p>
</div>
</div>
</div>
</div>
</div>
</form>
</body>
</html>

0 comments on commit f1cc1c6

Please sign in to comment.