assert-url 0.0.1 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,15 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: cbc3fcdf6ef6d4249a7acc90d7a50400daa6e653
4
- data.tar.gz: 5c6d6af45be495d1608664f39e3d31a59343053a
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ NDY2MTI0ZTRmY2NiYWE1MjA0M2RkYWU4Y2MxZDc3YTQ2ZTgyMDhiYQ==
5
+ data.tar.gz: !binary |-
6
+ YTRkNDAzMTU3ZjAxMjkyNTQ2YzQ4NWU1ZGE3NDdhMGM3NTIwNzE0Mw==
5
7
  SHA512:
6
- metadata.gz: 93a675818fed1f17a8905e770e879e8cf1ff2f481f17a64770b1e6f0338df85a46a8da1e37695b7f02b296a5ea125905d58964b61d4842131c6887066e80fabb
7
- data.tar.gz: ab51556324b3ef568297afb10ec4263ed01320d1f3dad068b9202780413cfd19f322a7161496eec5e72900a3a4f966099f3a73a400954c0b84b6e356c3abc00c
8
+ metadata.gz: !binary |-
9
+ ZDY3OGY4ZTFmOTU4MzBmYmRlOTQxZWRjYzUyMDYwMjQyNjJjOTc0ZmFmNjUy
10
+ Y2ZmYzE1MTcxYjQzNmMxOWZmMmMzOGQ4NjUwZDAxOGQ0YTUwYzA5Y2RlZDMx
11
+ NDUyMTgxM2QxYTQ4M2U3NGM2M2FjYjYzYzhhOWY1ZGM5MWEyODA=
12
+ data.tar.gz: !binary |-
13
+ ODZjYThmNzAwM2RlYmNiNzdhZjM2NmIwNTNkZTM3NjZhOTU5Nzg4MDVjODNk
14
+ NDZhMjcyNTkxYjQzZDMzNDk0NDgxMGE2ZDZlYmNkMmYyZThjOTIzZWNlZTZl
15
+ ZDcyZjc5MDg0NThiZTg0NjIzNmFjYjA4NThhNzUzZGI1NTA4YTI=
data/README.md CHANGED
@@ -2,3 +2,65 @@ assert-url
2
2
  ==========
3
3
 
4
4
  Semantical helpers to test your URLs in Ruby
5
+
6
+ ## Usage
7
+
8
+ Ok, say you're writing a Web app with Ruby (who isn't?) and need to test your routing with `Rack::Test`.
9
+
10
+ ### The old way
11
+
12
+ ```ruby
13
+ def test_some_resource
14
+ get "/some/resource"
15
+
16
+ assert last_response.ok?
17
+ assert_equal "http://example.org/some/resource", last_response.url
18
+ end
19
+ ```
20
+
21
+ What's wrong with this, you ask? Well, in most cases when you write a test like this, you only care about testing the `/some/resource` path, but the rest (scheme, port, etc) doesn't really matter. Still we're used to use `assert_equal` on the whole url.
22
+
23
+ Now imagine that you need to change the port, scheme or host. Now imagine you have hundreds of tests. It's `grep` day, my friend.
24
+
25
+ ### Enter AssertUrl!
26
+
27
+ ```ruby
28
+ include AssertUrl
29
+
30
+ def test_some_resource
31
+ get "/some/resource"
32
+
33
+ assert last_response.ok?
34
+ assert_path_equal "/some/resource", last_response.url
35
+ end
36
+ ```
37
+
38
+ BAM! Now you're only testing what you need. You can change the port, host, even the query and the test will be fine.
39
+
40
+ `AssertUrl` provides the following assertions in the form of `expected, value`:
41
+ * `assert_scheme_equal [String or Symbol], [String or URI]`
42
+ * `assert_host_equal [String], [String or URI]`
43
+ * `assert_port_equal [Integer], [String or URI]`
44
+ * `assert_path_equal [String starting with "/"], [String or URI]`
45
+ * `assert_query_equal [Hash], [String or URI]`
46
+ * `assert_query_include [Hash], [String or URI]`
47
+ * `assert_fragment_equal [String], [String or URI]`
48
+ * `assert_url_equal [String or URI], [String or URI]`
49
+
50
+ ### But...
51
+
52
+ Can't I just do something like...?
53
+
54
+ ```ruby
55
+ assert_equal "/some/resource", URI(last_response.url).path
56
+ ```
57
+
58
+ Well, yes! sure you can. In fact, this is very close to how `AssertUrl` is implemented.
59
+
60
+ My point here is not to sell you this gem but to make a statement that we need a better habit when testing URLs. And if `AssertUrl` helps you with it, great!
61
+
62
+ ## Installation
63
+
64
+ ```
65
+ gem install assert-url
66
+ ```
data/assert_url.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "assert-url"
3
- s.version = "0.0.1"
3
+ s.version = "1.0.0"
4
4
  s.summary = "Semantical helpers to test your URLs in Ruby"
5
5
  s.description = s.summary
6
6
  s.authors = ["Lucas Tolchinsky"]
data/lib/assert_url.rb CHANGED
@@ -1,29 +1,54 @@
1
1
  require "uri"
2
2
 
3
3
  module AssertUrl
4
- PARTS = %W[scheme host port path query]
4
+ PARTS = %W[scheme host port path query fragment]
5
5
 
6
6
  PARTS.each do |part|
7
7
  error = const_set("#{part.capitalize}Error", Class.new(StandardError))
8
8
 
9
- define_method(:"assert_#{part}") do |expected, value|
10
- expected, value = urify(expected, value).map(&:"#{part}")
9
+ define_method(:"assert_#{part}_equal") do |expected, value|
10
+ expected = normalize(part, expected)
11
+ value = urify(value).send(part.to_sym)
11
12
 
12
13
  expected == value || (raise error, "expected #{expected}, got #{value}")
13
14
  end
14
15
  end
15
16
 
16
- alias assert_hostname assert_host
17
+ def assert_url_equal(expected, value)
18
+ expected, value = urify(expected), urify(value)
17
19
 
18
- def assert_url(expected, value)
19
- expected, value = urify(expected, value)
20
+ PARTS.map { |part| send(:"assert_#{part}_equal", expected.send(part.to_sym), value) }.reduce(:&)
21
+ end
22
+
23
+ def assert_query_include(expected, value)
24
+ value = Hash[URI.decode_www_form(urify(value).query)]
25
+
26
+ includes?(expected.to_a, value) || (raise QueryError, "expected #{value} to include #{expected}")
27
+ end
20
28
 
21
- PARTS.map { |part| send(:"assert_#{part}", expected, value) }.reduce(:&)
29
+ def includes?(expected, got)
30
+ expected.all? do |(key, value)|
31
+ key = key.to_s
32
+ got.has_key?(key) && got[key] == value
33
+ end
22
34
  end
35
+ private :includes?
23
36
 
24
- def urify(*args)
25
- args.each_with_object([]) { |arg, accum| accum << (arg.kind_of?(URI) ? arg : URI(arg)) }
37
+ def urify(arg)
38
+ arg.kind_of?(URI) ? arg : URI(arg)
26
39
  end
27
40
  private :urify
41
+
42
+ def normalize(part, expected)
43
+ case part
44
+ when "scheme"
45
+ expected.kind_of?(Symbol) ? expected.to_s : expected
46
+ when "query"
47
+ URI.encode_www_form(expected) rescue expected
48
+ else
49
+ expected
50
+ end
51
+ end
52
+ private :normalize
28
53
  end
29
54
 
@@ -5,54 +5,70 @@ require_relative "../lib/assert_url"
5
5
  include AssertUrl
6
6
 
7
7
  setup do
8
- "http://sub.example.org:8080/path/to/resource?foo=bar"
8
+ "http://sub.example.org:8080/path/to/resource?foo=bar#fragment"
9
9
  end
10
10
 
11
- test "assert_url" do |url|
12
- assert assert_url(url, url)
13
-
14
- assert_raise do
15
- assert_scheme("https://example.org", url)
16
- end
17
- end
18
-
19
- test "assert_scheme" do |url|
20
- assert assert_scheme("http://example.org", url)
11
+ test "assert_scheme_equal" do |url|
12
+ assert assert_scheme_equal("http", url)
13
+ assert assert_scheme_equal(:http, url)
21
14
 
22
15
  assert_raise(AssertUrl::SchemeError) do
23
- assert_scheme("ftp://example.org", url)
16
+ assert_scheme_equal("ftp", url)
24
17
  end
25
18
  end
26
19
 
27
- test "assert_host" do |url|
28
- assert assert_host("https://sub.example.org", url)
20
+ test "assert_host_equal" do |url|
21
+ assert assert_host_equal("sub.example.org", url)
29
22
 
30
23
  assert_raise(AssertUrl::HostError) do
31
- assert_host("http://example.org", url)
24
+ assert_host_equal("example.org", url)
32
25
  end
33
26
  end
34
27
 
35
- test "assert_port" do |url|
36
- assert assert_port("http://place.org:8080", url)
28
+ test "assert_port_equal" do |url|
29
+ assert assert_port_equal(8080, url)
37
30
 
38
31
  assert_raise(AssertUrl::PortError) do
39
- assert_port("ftp://example.org", url)
32
+ assert_port_equal(80, url)
40
33
  end
41
34
  end
42
35
 
43
- test "assert_path" do |url|
44
- assert assert_path("http://wat.org/path/to/resource", url)
36
+ test "assert_path_equal" do |url|
37
+ assert assert_path_equal("/path/to/resource", url)
45
38
 
46
39
  assert_raise(AssertUrl::PathError) do
47
- assert_path("ftp://example.org", url)
40
+ assert_path_equal("/not/the/resource", url)
41
+ end
42
+ end
43
+
44
+ test "assert_query_equal" do |url|
45
+ assert assert_query_equal({foo: "bar"}, url)
46
+
47
+ assert_raise(AssertUrl::QueryError) do
48
+ assert_query_equal("", url)
48
49
  end
49
50
  end
50
51
 
51
- test "assert_query" do |url|
52
- assert assert_query("http://foo.bar/q?foo=bar", url)
52
+ test "assert_query_include" do
53
+ assert assert_query_include({foo: "bar"}, "http://example.org/?foo=bar&baz=wat")
53
54
 
54
55
  assert_raise(AssertUrl::QueryError) do
55
- assert_query("ftp://example.org", url)
56
+ assert_query_include({foo: "wat"}, "http://example.org/?foo=bar&baz=wat")
57
+ end
58
+ end
59
+
60
+ test "assert_fragment_equal" do |url|
61
+ assert assert_fragment_equal("fragment", url)
62
+
63
+ assert_raise(AssertUrl::FragmentError) do
64
+ assert_fragment_equal("not", url)
56
65
  end
57
66
  end
58
67
 
68
+ test "assert_url_equal" do |url|
69
+ assert assert_url_equal(url, url)
70
+
71
+ assert_raise do
72
+ assert_url_equal("https://example.org", url)
73
+ end
74
+ end
metadata CHANGED
@@ -1,27 +1,27 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: assert-url
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lucas Tolchinsky
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-21 00:00:00.000000000 Z
11
+ date: 2014-08-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: cutest
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
17
+ - - ! '>='
18
18
  - !ruby/object:Gem::Version
19
19
  version: '0'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ">="
24
+ - - ! '>='
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
27
  description: Semantical helpers to test your URLs in Ruby
@@ -46,17 +46,17 @@ require_paths:
46
46
  - lib
47
47
  required_ruby_version: !ruby/object:Gem::Requirement
48
48
  requirements:
49
- - - ">="
49
+ - - ! '>='
50
50
  - !ruby/object:Gem::Version
51
51
  version: '0'
52
52
  required_rubygems_version: !ruby/object:Gem::Requirement
53
53
  requirements:
54
- - - ">="
54
+ - - ! '>='
55
55
  - !ruby/object:Gem::Version
56
56
  version: '0'
57
57
  requirements: []
58
58
  rubyforge_project:
59
- rubygems_version: 2.2.0
59
+ rubygems_version: 2.2.2
60
60
  signing_key:
61
61
  specification_version: 4
62
62
  summary: Semantical helpers to test your URLs in Ruby