File: relation_test.rb

package info (click to toggle)
ruby-sawyer 0.8.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 248 kB
  • sloc: ruby: 1,032; sh: 17; makefile: 3
file content (174 lines) | stat: -rw-r--r-- 5,323 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
require File.expand_path("../helper", __FILE__)

module Sawyer
  class RelationTest < TestCase
    def test_builds_relation_from_hash
      hash = {:href => '/users/1', :method => 'post'}
      rel  = Sawyer::Relation.from_link(nil, :self, hash)

      assert_equal :self,      rel.name
      assert_equal '/users/1', rel.href
      assert_equal :post,      rel.method
      assert_equal [:post],    rel.available_methods.to_a
    end

    def test_builds_multiple_rels_from_multiple_methods
      index = {
        'comments' => {:href => '/comments', :method => 'get,post'}
      }

      rels = Sawyer::Relation.from_links(nil, index)
      assert_equal 1, rels.size
      assert_equal [:comments], rels.keys

      assert rel = rels[:comments]
      assert_equal '/comments',   rel.href
      assert_equal :get,          rel.method
      assert_equal [:get, :post], rel.available_methods.to_a
      assert_kind_of Addressable::Template, rel.href_template
    end

    def test_builds_rels_from_hash
      index = {
        'self' => '/users/1'
      }

      rels = Sawyer::Relation.from_links(nil, index)

      assert_equal 1, rels.size
      assert_equal [:self], rels.keys
      assert rel = rels[:self]
      assert_equal :self,      rel.name
      assert_equal '/users/1', rel.href
      assert_equal :get,       rel.method
      assert_equal [:get],     rel.available_methods.to_a
      assert_kind_of Addressable::Template, rel.href_template
    end

    def test_builds_rels_from_hash_index
      index = {
        'self' => {:href => '/users/1'}
      }

      rels = Sawyer::Relation.from_links(nil, index)

      assert_equal 1, rels.size
      assert_equal [:self], rels.keys
      assert rel = rels[:self]
      assert_equal :self,      rel.name
      assert_equal '/users/1', rel.href
      assert_equal :get,       rel.method
      assert_equal [:get],     rel.available_methods.to_a
      assert_kind_of Addressable::Template, rel.href_template
    end

    def test_builds_rels_from_nil
      rels = Sawyer::Relation.from_links nil, nil
      assert_equal 0,  rels.size
      assert_equal [], rels.keys
    end

    def test_relation_api_calls
      agent = Sawyer::Agent.new "http://foo.com/a/" do |conn|
        conn.builder.handlers.delete(Faraday::Adapter::NetHttp)
        conn.adapter :test do |stubs|
          stubs.get '/a/1' do
            [200, {}, '{}']
          end
          stubs.delete '/a/1' do
            [204, {}, '{}']
          end
        end
      end

      rel = Sawyer::Relation.new agent, :self, "/a/1", "get,put,delete"
      assert_equal :get, rel.method
      [:get, :put, :delete].each do |m|
        assert rel.available_methods.include?(m), "#{m.inspect} is not available: #{rel.available_methods.inspect}"
      end

      assert_equal 200, rel.call.status
      assert_equal 200, rel.call(:method => :head).status
      assert_equal 204, rel.call(nil, :method => :delete).status
      assert_raises ArgumentError do
        rel.call nil, :method => :post
      end

      assert_equal 200, rel.head.status
      assert_equal 200, rel.get.status
      assert_equal 204, rel.delete.status

      assert_raises ArgumentError do
        rel.post
      end
    end

    def test_relation_api_calls_with_uri_tempate
      agent = Sawyer::Agent.new "http://foo.com/a" do |conn|
        conn.builder.handlers.delete(Faraday::Adapter::NetHttp)
        conn.adapter :test do |stubs|
          stubs.get '/octocat/hello' do |env|
            assert_equal "a=1&b=2", env[:url].query
            [200, {}, '{}']
          end

          stubs.get '/a' do
            [404, {}, '{}']
          end
        end
      end

      rel = Sawyer::Relation.new agent, :repo, "{/user,repo}{?a,b}"

      assert_equal '', rel.href
      assert_equal '/octocat', rel.href(:user => :octocat)

      assert_equal 404, rel.get.status
      assert_equal 200, rel.get(:uri => {'user' => 'octocat', 'repo' => 'hello', 'a' => 1, 'b' => 2}).status
    end

    def test_handles_invalid_uri
      hash = {:href => '/this has spaces', :method => 'post'}
      rel  = Sawyer::Relation.from_link(nil, :self, hash)

      assert_equal :self,      rel.name
      assert_equal '/this has spaces', rel.href
    end

    def test_allows_all_methods_when_not_in_strict_mode

      agent = Sawyer::Agent.new "http://foo.com/a/", :allow_undefined_methods => true do |conn|
        conn.builder.handlers.delete(Faraday::Adapter::NetHttp)
        conn.adapter :test do |stubs|
          stubs.get '/a/1' do
            [200, {}, '{}']
          end
          stubs.delete '/a/1' do
            [204, {}, '{}']
          end
          stubs.post '/a/1' do
            [200, {}, '{}']
          end
          stubs.put '/a/1' do
            [204, {}, '{}']
          end
        end
      end

      rel = Sawyer::Relation.new agent, :self, "/a/1"
      assert_equal 200, rel.get.status
      assert_equal 200, rel.post.status
      assert_equal 204, rel.put.status
      assert_equal 204, rel.delete.status
    end

    def test_map_inspect
      map = Sawyer::Relation::Map.new
      hash = {:href => '/users/1', :method => 'post'}
      rel  = Sawyer::Relation.from_link(nil, :self, hash)
      map << rel

      assert_equal "{:self_url=>\"/users/1\"}".force_encoding("UTF-8"), map.inspect
    end
  end
end