You can make sure the response class is Net::HTTPOK
.
agent = Mechanize.new do |a|
a.post_connect_hooks << ->(_,_,response,_) do
break unless response.class == Net::HTTPOK
if response.content_type.empty?
response.content_type = 'text/html'
end
end
end