check_http_response 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +13 -0
- data/Rakefile +2 -0
- data/check_http_response.gemspec +18 -0
- data/lib/check_http_response/request.rb +73 -0
- data/lib/check_http_response/version.rb +3 -0
- data/lib/check_http_response.rb +10 -0
- metadata +71 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Robert Gogolok
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# CheckHttpResponse
|
2
|
+
|
3
|
+
## Usage
|
4
|
+
|
5
|
+
response = CheckHttpResponse.for(
|
6
|
+
'http://localhost:8080/page/1',
|
7
|
+
:headers => { 'Host' => 'www.mydomain.com' }
|
8
|
+
)
|
9
|
+
|
10
|
+
response.redirects_to?("http://www.mydomain.com/page1") #=> true
|
11
|
+
|
12
|
+
response.redirects_to?("http://nowhere.com/page2") #=> false
|
13
|
+
response.errors #=> { :redirect => "Wrong location" }
|
data/Rakefile
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/check_http_response/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Robert Gogolok"]
|
6
|
+
gem.email = ["gogolok@gmail.com"]
|
7
|
+
gem.summary = %q{checks web server response}
|
8
|
+
gem.homepage = ""
|
9
|
+
|
10
|
+
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
11
|
+
gem.files = `git ls-files`.split("\n")
|
12
|
+
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
13
|
+
gem.name = "check_http_response"
|
14
|
+
gem.require_paths = ["lib"]
|
15
|
+
gem.version = CheckHttpResponse::VERSION
|
16
|
+
|
17
|
+
gem.add_dependency "httparty", "~> 0.8.1"
|
18
|
+
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
require 'httparty'
|
2
|
+
|
3
|
+
module CheckHttpResponse
|
4
|
+
class Request
|
5
|
+
|
6
|
+
class HttpRequest
|
7
|
+
include ::HTTParty
|
8
|
+
end
|
9
|
+
|
10
|
+
attr_reader :errors, :url, :headers
|
11
|
+
|
12
|
+
def initialize(url, options)
|
13
|
+
@headers = options[:headers] || {}
|
14
|
+
@url = url
|
15
|
+
|
16
|
+
clear_errors
|
17
|
+
end
|
18
|
+
|
19
|
+
# Returns true if the response redirects to the given location.
|
20
|
+
#
|
21
|
+
# Warning: '303 see other' is not supported
|
22
|
+
#
|
23
|
+
# response = CheckHttpResponse.for(
|
24
|
+
# 'http://localhost:8080/page/1',
|
25
|
+
# :headers => { 'Host' => 'www.mydomain.com' }
|
26
|
+
# )
|
27
|
+
# response.redirects_to?('http://www.mydomain.com/page/1/index.html') #=> true
|
28
|
+
#
|
29
|
+
# response.redirects_to?('http://nowhere.com/') #=> false
|
30
|
+
# response.errors #=> { :redirect => "Wrong location" }
|
31
|
+
#
|
32
|
+
# ==== Options
|
33
|
+
# <tt>:permanent</tt> Specifies whether the redirect should be 'permanent', default: true
|
34
|
+
def redirects_to?(location, options = {})
|
35
|
+
clear_errors
|
36
|
+
|
37
|
+
check_permanent = true
|
38
|
+
check_permanent = options[:permanent] if not options[:permanent].nil?
|
39
|
+
|
40
|
+
redirected = false
|
41
|
+
begin
|
42
|
+
HttpRequest.get(self.url,
|
43
|
+
:headers => self.headers,
|
44
|
+
:no_follow => true
|
45
|
+
)
|
46
|
+
|
47
|
+
@errors[:base] = "No redirect initiated"
|
48
|
+
rescue HTTParty::RedirectionTooDeep => e
|
49
|
+
is_correct_location = location == e.response['location']
|
50
|
+
|
51
|
+
is_correct_redirect = if check_permanent
|
52
|
+
e.response.class == Net::HTTPMovedPermanently
|
53
|
+
else
|
54
|
+
e.response.class == Net::HTTPFound
|
55
|
+
end
|
56
|
+
|
57
|
+
redirected = is_correct_location && is_correct_redirect
|
58
|
+
@errors[:redirect] = "Wrong redirect type" if not is_correct_redirect
|
59
|
+
@errors[:redirect] = "Wrong location" if not is_correct_location
|
60
|
+
end
|
61
|
+
|
62
|
+
return redirected
|
63
|
+
end
|
64
|
+
|
65
|
+
private
|
66
|
+
|
67
|
+
def clear_errors
|
68
|
+
@errors = {}
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
metadata
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: check_http_response
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Robert Gogolok
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-02-06 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: httparty
|
16
|
+
requirement: &70362549392860 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 0.8.1
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70362549392860
|
25
|
+
description:
|
26
|
+
email:
|
27
|
+
- gogolok@gmail.com
|
28
|
+
executables: []
|
29
|
+
extensions: []
|
30
|
+
extra_rdoc_files: []
|
31
|
+
files:
|
32
|
+
- .gitignore
|
33
|
+
- Gemfile
|
34
|
+
- LICENSE
|
35
|
+
- README.md
|
36
|
+
- Rakefile
|
37
|
+
- check_http_response.gemspec
|
38
|
+
- lib/check_http_response.rb
|
39
|
+
- lib/check_http_response/request.rb
|
40
|
+
- lib/check_http_response/version.rb
|
41
|
+
homepage: ''
|
42
|
+
licenses: []
|
43
|
+
post_install_message:
|
44
|
+
rdoc_options: []
|
45
|
+
require_paths:
|
46
|
+
- lib
|
47
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
48
|
+
none: false
|
49
|
+
requirements:
|
50
|
+
- - ! '>='
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '0'
|
53
|
+
segments:
|
54
|
+
- 0
|
55
|
+
hash: -1229798005457806409
|
56
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
segments:
|
63
|
+
- 0
|
64
|
+
hash: -1229798005457806409
|
65
|
+
requirements: []
|
66
|
+
rubyforge_project:
|
67
|
+
rubygems_version: 1.8.10
|
68
|
+
signing_key:
|
69
|
+
specification_version: 3
|
70
|
+
summary: checks web server response
|
71
|
+
test_files: []
|