assert_valid_markup_nu 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/assert_valid_markup_nu.rb +43 -0
  2. metadata +67 -0
@@ -0,0 +1,43 @@
1
+ require 'digest/md5'
2
+ require 'net/http'
3
+
4
+ module AssertValidMarkupNu
5
+
6
+ # Assert that HTML 5 markup is valid according to http://validator.nu/
7
+ #
8
+ # By default, assert_valid_markup validates the contents of @response.body which is set automatically when
9
+ # you call get, post, put or delete in functional and integration tests. For example:
10
+ #
11
+ # test "index page should be valid" do
12
+ # get :index
13
+ # assert_valid_markup
14
+ # end
15
+ #
16
+ # You can also pass assert_valid_markup a string to be validated like this:
17
+ #
18
+ # test "fragment should be valid" do
19
+ # assert_valid_markup "<div>Hello world!</div>"
20
+ # end
21
+ #
22
+
23
+ def assert_valid_markup (markup = @response.body)
24
+ response = nil
25
+ cache = File.join Dir::tmpdir, "markup.#{Digest::MD5.hexdigest(markup)}"
26
+ if File.exist? cache
27
+ response = File.open(cache) { |file| Marshal.load(file) }
28
+ else
29
+ Net::HTTP.start("validator.nu") do |http|
30
+ response = http.request_post "/?out=json", markup, "CONTENT-TYPE" => "text/html;"
31
+ File.open(cache, "w+") { |file| Marshal.dump(response, file) }
32
+ end
33
+ end
34
+ decoded = ActiveSupport::JSON.decode response.entity
35
+ errors = decoded["messages"].select { |entry| entry["type"] == "error" }
36
+ message = errors.inject "Invalid markup:\n\n" do |message, error|
37
+ message << "#{error["message"]}\n#{error["extract"]}\n\n"
38
+ end
39
+ assert errors.empty?, message
40
+ rescue SocketError
41
+ # No assertion if we can’t connect to the validator.
42
+ end
43
+ end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: assert_valid_markup_nu
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Craig Davey
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-01-19 00:00:00 -05:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description:
23
+ email: me@craigdavey.ca
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files: []
29
+
30
+ files:
31
+ - lib/assert_valid_markup_nu.rb
32
+ has_rdoc: true
33
+ homepage: http://github.com/craigdavey/assert_valid_markup_nu
34
+ licenses: []
35
+
36
+ post_install_message:
37
+ rdoc_options: []
38
+
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ hash: 3
47
+ segments:
48
+ - 0
49
+ version: "0"
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ hash: 3
56
+ segments:
57
+ - 0
58
+ version: "0"
59
+ requirements: []
60
+
61
+ rubyforge_project:
62
+ rubygems_version: 1.6.2
63
+ signing_key:
64
+ specification_version: 3
65
+ summary: Validate HTML 5 markup in Rails tests.
66
+ test_files: []
67
+