Skip to content

Commit

Permalink
Added Network::URL (closes #536).
Browse files Browse the repository at this point in the history
  • Loading branch information
postmodern committed Aug 17, 2024
1 parent a0e8619 commit 7c1dd20
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/ronin/support/network.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@
require 'ronin/support/network/tld'
require 'ronin/support/network/tls'
require 'ronin/support/network/udp'
require 'ronin/support/network/url'
require 'ronin/support/network/mixin'
require 'ronin/support/network/core_ext'
85 changes: 85 additions & 0 deletions lib/ronin/support/network/url.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# frozen_string_literal: true
#
# Copyright (c) 2006-2024 Hal Brodigan (postmodern.mod3 at gmail.com)
#
# ronin-support is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# ronin-support is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with ronin-support. If not, see <https://www.gnu.org/licenses/>.
#

require 'ronin/support/network/http'

require 'addressable/uri'
require 'uri/query_params/mixin'

module Ronin
module Support
module Network
#
# Represents a URL.
#
# ## Features
#
# * Supports parsing URLs with IDN domains.
#
# @api public
#
# @since 1.2.0
#
class URL < Addressable::URI

include URI::QueryParams::Mixin

#
# Returns the Status Code of the HTTP Response for the URL.
#
# @param [Hash{Symbol => Object}] kwargs
# Additional keyword arguments for {HTTP.response_status}.
#
# @return [Integer]
# The HTTP Response Status.
#
# @example
# url = Network::URL.parse('http://github.com/')
# url.status
# # => 301
#
# @see HTTP.response_status
#
def status(**kwargs)
HTTP.response_status(self,**kwargs)
end

#
# Checks if the HTTP response for the URL has an HTTP `OK` status code.
#
# @param [Hash{Symbol => Object}] kwargs
# Additional keyword arguments for {HTTP.ok?}.
#
# @return [Boolean]
# Specifies whether the response had an HTTP OK status code or not.
#
# @example
# url = Network::URL.parse('https://example.com/')
# url.ok?
# # => true
#
# @see HTTP.ok?
#
def ok?(**kwargs)
HTTP.ok?(self,**kwargs)
end

end
end
end
end
46 changes: 46 additions & 0 deletions spec/network/url_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
require 'spec_helper'
require 'ronin/support/network/url'

require 'webmock/rspec'

describe Ronin::Support::Network::URL do
it "must inherit from Addressable::URI" do
expect(described_class).to be < Addressable::URI
end

it "must include URI::QueryParams::Mixin" do
expect(described_class).to include(URI::QueryParams::Mixin)
end

subject { described_class.parse('https://example.com/') }

describe "#status" do
context "integration", :network do
before(:all) { WebMock.allow_net_connect! }

it "must request the HTTP status for the URI" do
expect(subject.status).to eq(200)
end
end
end

describe "#ok?" do
context "integration", :network do
before(:all) { WebMock.allow_net_connect! }

context "when the URI returns has a HTTP 200 response" do
it "must return true" do
expect(subject.ok?).to be(true)
end
end

context "when the URI does not return a HTTP 200 response" do
subject { described_class.parse('https://example.com/foo') }

it "must return false" do
expect(subject.ok?).to be(false)
end
end
end
end
end

0 comments on commit 7c1dd20

Please sign in to comment.