http_validator 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.autotest +23 -0
- data/.gemtest +0 -0
- data/History.txt +6 -0
- data/Manifest.txt +8 -0
- data/README.txt +44 -0
- data/Rakefile +18 -0
- data/lib/http_validator.rb +120 -0
- data/test/features/resource_size.feature +13 -0
- data/test/features/step_defn/verify.rb +16 -0
- metadata +89 -0
data/.autotest
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- ruby -*-
|
2
|
+
|
3
|
+
require 'autotest/restart'
|
4
|
+
|
5
|
+
# Autotest.add_hook :initialize do |at|
|
6
|
+
# at.extra_files << "../some/external/dependency.rb"
|
7
|
+
#
|
8
|
+
# at.libs << ":../some/external"
|
9
|
+
#
|
10
|
+
# at.add_exception 'vendor'
|
11
|
+
#
|
12
|
+
# at.add_mapping(/dependency.rb/) do |f, _|
|
13
|
+
# at.files_matching(/test_.*rb$/)
|
14
|
+
# end
|
15
|
+
#
|
16
|
+
# %w(TestA TestB).each do |klass|
|
17
|
+
# at.extra_class_map[klass] = "test/test_misc.rb"
|
18
|
+
# end
|
19
|
+
# end
|
20
|
+
|
21
|
+
# Autotest.add_hook :run_command do |at|
|
22
|
+
# system "rake build"
|
23
|
+
# end
|
data/.gemtest
ADDED
File without changes
|
data/History.txt
ADDED
data/Manifest.txt
ADDED
data/README.txt
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
= http_validator
|
2
|
+
|
3
|
+
* http://github.com/ccyphers
|
4
|
+
|
5
|
+
== DESCRIPTION:
|
6
|
+
|
7
|
+
Validate http request content length vs actual for
|
8
|
+
elements such as img, link, etc..
|
9
|
+
|
10
|
+
== SYNOPSIS:
|
11
|
+
|
12
|
+
HTTP::Validator.run('http://slashdot.org', :display_report => nil)
|
13
|
+
|
14
|
+
== REQUIREMENTS:
|
15
|
+
|
16
|
+
* curb
|
17
|
+
|
18
|
+
== INSTALL:
|
19
|
+
|
20
|
+
*
|
21
|
+
|
22
|
+
== LICENSE:
|
23
|
+
GPLv3: http://www.gnu.org/licenses/gpl.html
|
24
|
+
|
25
|
+
Copyright (c) 2011 Cliff Cyphers
|
26
|
+
|
27
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
28
|
+
a copy of this software and associated documentation files (the
|
29
|
+
'Software'), to deal in the Software without restriction, including
|
30
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
31
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
32
|
+
permit persons to whom the Software is furnished to do so, subject to
|
33
|
+
the following conditions:
|
34
|
+
|
35
|
+
The above copyright notice and this permission notice shall be
|
36
|
+
included in all copies or substantial portions of the Software.
|
37
|
+
|
38
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
39
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
40
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
41
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
42
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
43
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
44
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# -*- ruby -*-
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'hoe'
|
5
|
+
|
6
|
+
# Hoe.plugin :compiler
|
7
|
+
# Hoe.plugin :gem_prelude_sucks
|
8
|
+
# Hoe.plugin :inline
|
9
|
+
# Hoe.plugin :minitest
|
10
|
+
# Hoe.plugin :racc
|
11
|
+
# Hoe.plugin :rubyforge
|
12
|
+
|
13
|
+
Hoe.spec 'http_validator' do
|
14
|
+
developer('Cliff Cyphers', 'cliff.cyphers@gmail.com')
|
15
|
+
extra_deps << ['localized_gems', '0.7.15']
|
16
|
+
end
|
17
|
+
|
18
|
+
# vim: syntax=ruby
|
@@ -0,0 +1,120 @@
|
|
1
|
+
require 'curb'
|
2
|
+
require 'uri'
|
3
|
+
require 'nokogiri'
|
4
|
+
class HttpValidator
|
5
|
+
VERSION = '0.1.0'
|
6
|
+
end
|
7
|
+
|
8
|
+
module HTTP
|
9
|
+
module Header
|
10
|
+
def self.parse(str)
|
11
|
+
headers = {}
|
12
|
+
str.gsub(/\r/, '').split(/\n/).each { |i|
|
13
|
+
k, v = i.split(':')
|
14
|
+
headers[k] = v
|
15
|
+
}
|
16
|
+
headers
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
class Browser
|
21
|
+
attr_accessor :base_url, :elements, :element_info
|
22
|
+
def initialize(url='')
|
23
|
+
@base_url = url
|
24
|
+
@browser = Curl::Easy.new
|
25
|
+
#@browser.enable_cookies = true
|
26
|
+
@browser.cookiejar = 'cookies.txt'
|
27
|
+
@browser.headers["User-Agent"] = "Mozilla/5.0 (X11; Linux i686) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.186 Safari/535.1"
|
28
|
+
@element_types = %w(script img link)
|
29
|
+
clear
|
30
|
+
end
|
31
|
+
|
32
|
+
def clear
|
33
|
+
@element_info = {}
|
34
|
+
@elements = []
|
35
|
+
end
|
36
|
+
|
37
|
+
def element_details(e)
|
38
|
+
return if %w(text/css text/javascript before-content data after-content stylesheet top).include?(e)
|
39
|
+
if e =~ /^http:\/\/|^https:\/\//
|
40
|
+
@browser.url = e
|
41
|
+
else
|
42
|
+
@browser.url = "#{@base_url.gsub(/\/$/, '')}/#{e}"
|
43
|
+
end
|
44
|
+
@browser.perform
|
45
|
+
headers = HTTP::Header.parse(@browser.header_str)
|
46
|
+
if @element_info.has_key?(e)
|
47
|
+
@element_info[e] << {:expected_size => headers['Content-Length'].strip.to_i,
|
48
|
+
:actual_size => @browser.body_str.length, :headers => headers}
|
49
|
+
else
|
50
|
+
begin
|
51
|
+
@element_info[e] = [{:expected_size => headers['Content-Length'].strip.to_i,
|
52
|
+
:actual_size => @browser.body_str.length, :headers => headers}]
|
53
|
+
rescue => ex
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
|
59
|
+
def get_elements
|
60
|
+
@browser.url = @base_url
|
61
|
+
@browser.perform
|
62
|
+
doc = Nokogiri::HTML(@browser.body_str)
|
63
|
+
@element_types.each { |elem|
|
64
|
+
e = nil
|
65
|
+
doc.search("//#{elem}").each { |item|
|
66
|
+
next unless item.attribute_nodes.length > 0
|
67
|
+
item.attribute_nodes.each { |node|
|
68
|
+
next unless node.value =~ /\//
|
69
|
+
e = node.value
|
70
|
+
break
|
71
|
+
}
|
72
|
+
@elements << e
|
73
|
+
}
|
74
|
+
}
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
module Validator
|
79
|
+
@browser = HTTP::Browser.new
|
80
|
+
|
81
|
+
def self.display_report(element_info, format='plain')
|
82
|
+
if format == 'plain'
|
83
|
+
printf "%-100s%15s%15s\n\n", "Element", "Expected Size", "Actual Size"
|
84
|
+
element_info.each_pair { |resource, values|
|
85
|
+
values.each { |item|
|
86
|
+
printf "%-100s%15s%15s\n", resource, item[:expected_size], item[:actual_size]
|
87
|
+
}
|
88
|
+
}
|
89
|
+
elsif format == 'cucumber_example'
|
90
|
+
element_info.each_pair { |resource, values|
|
91
|
+
values.each { |item|
|
92
|
+
printf "%-100s%15s%15s\n", "#{resource}|", "#{item[:expected_size]}|", "#{item[:actual_size]}|"
|
93
|
+
}
|
94
|
+
}
|
95
|
+
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
def self.run(url='', params={})
|
100
|
+
p params
|
101
|
+
raise ArgumentError unless url =~ /^http:\/\/|https:\/\//
|
102
|
+
URI.parse(url) # see if parsable, blow up if not
|
103
|
+
@browser.base_url = url
|
104
|
+
@browser.clear
|
105
|
+
@browser.get_elements
|
106
|
+
@browser.elements.each { |e| @browser.element_details(e) }
|
107
|
+
if params.has_key?(:display_report)
|
108
|
+
params[:display_report][:format] ||= 'plain'
|
109
|
+
display_report(@browser.element_info, params[:display_report][:format])
|
110
|
+
end
|
111
|
+
@browser.element_info
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
#HTTP::Validator.run('http://slashdot.org')
|
117
|
+
#HTTP::Validator.run('https://github.com')
|
118
|
+
|
119
|
+
#HTTP::Validator.run('http://127.0.0.1:9090', :display_report => {:format => 'cucumber_example'})
|
120
|
+
|
@@ -0,0 +1,13 @@
|
|
1
|
+
Feature: Download elements from source html
|
2
|
+
In order to release all images, external javascript and
|
3
|
+
css files must download and be the correct size
|
4
|
+
|
5
|
+
Scenario Outline: Download
|
6
|
+
Given a user has landed on a <page>
|
7
|
+
Then all resource elements should download with the correct size
|
8
|
+
|
9
|
+
Examples:
|
10
|
+
|page |
|
11
|
+
|http://slashdot.org |
|
12
|
+
|https://github.com |
|
13
|
+
|http://www.yahoo.com |
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'rspec'
|
2
|
+
require 'http_validator'
|
3
|
+
|
4
|
+
Given /a user has landed on a (.+)/ do |url|
|
5
|
+
@element_info = HTTP::Validator.run(url)
|
6
|
+
end
|
7
|
+
|
8
|
+
Then /all resource elements should download with the correct size/ do
|
9
|
+
@element_info.each_pair { |resource, values|
|
10
|
+
values.each { |item|
|
11
|
+
puts "Verifying #{resource} downloaded size #{item[:actual_size]} matches Content-Length #{item[:expected_size]}\n"
|
12
|
+
item[:actual_size].should equal item[:expected_size]
|
13
|
+
}
|
14
|
+
}
|
15
|
+
end
|
16
|
+
|
metadata
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: http_validator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.1.0
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Cliff Cyphers
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-10-12 00:00:00 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: localized_gems
|
17
|
+
prerelease: false
|
18
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
19
|
+
none: false
|
20
|
+
requirements:
|
21
|
+
- - "="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.7.15
|
24
|
+
type: :runtime
|
25
|
+
version_requirements: *id001
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: hoe
|
28
|
+
prerelease: false
|
29
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
30
|
+
none: false
|
31
|
+
requirements:
|
32
|
+
- - ~>
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: "2.12"
|
35
|
+
type: :development
|
36
|
+
version_requirements: *id002
|
37
|
+
description: |-
|
38
|
+
Validate http request content length vs actual for
|
39
|
+
elements such as img, link, etc..
|
40
|
+
email:
|
41
|
+
- cliff.cyphers@gmail.com
|
42
|
+
executables: []
|
43
|
+
|
44
|
+
extensions: []
|
45
|
+
|
46
|
+
extra_rdoc_files:
|
47
|
+
- History.txt
|
48
|
+
- Manifest.txt
|
49
|
+
- README.txt
|
50
|
+
files:
|
51
|
+
- .autotest
|
52
|
+
- History.txt
|
53
|
+
- Manifest.txt
|
54
|
+
- README.txt
|
55
|
+
- Rakefile
|
56
|
+
- lib/http_validator.rb
|
57
|
+
- test/features/step_defn/verify.rb
|
58
|
+
- test/features/resource_size.feature
|
59
|
+
- .gemtest
|
60
|
+
homepage: http://github.com/ccyphers
|
61
|
+
licenses: []
|
62
|
+
|
63
|
+
post_install_message:
|
64
|
+
rdoc_options:
|
65
|
+
- --main
|
66
|
+
- README.txt
|
67
|
+
require_paths:
|
68
|
+
- lib
|
69
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
70
|
+
none: false
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: "0"
|
75
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
76
|
+
none: false
|
77
|
+
requirements:
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: "0"
|
81
|
+
requirements: []
|
82
|
+
|
83
|
+
rubyforge_project: http_validator
|
84
|
+
rubygems_version: 1.8.10
|
85
|
+
signing_key:
|
86
|
+
specification_version: 3
|
87
|
+
summary: Validate http request content length vs actual for elements such as img, link, etc..
|
88
|
+
test_files: []
|
89
|
+
|