-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a0e8619
commit 7c1dd20
Showing
3 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |