File: agent_test.rb

package info (click to toggle)
ruby-sawyer 0.8.1-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 248 kB
  • sloc: ruby: 1,032; sh: 17; makefile: 3
file content (197 lines) | stat: -rw-r--r-- 5,847 bytes parent folder | download | duplicates (2)
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
require File.expand_path("../helper", __FILE__)

require 'faraday/adapter/test'

module Sawyer
  class AgentTest < TestCase

    class InlineRelsParser
      def parse(data)
        links = {}
        data.keys.select {|k| k[/_url$/] }.each {|k| links[k.to_s.gsub(/_url$/, '')] = data.delete(k) }

        return data, links
      end
    end

    def setup
      @stubs = Faraday::Adapter::Test::Stubs.new
      @agent = Sawyer::Agent.new "http://foo.com/a/" do |conn|
        conn.builder.handlers.delete(Faraday::Adapter::NetHttp)
        conn.adapter :test, @stubs
      end
    end

    def test_accesses_root_relations
      @stubs.get '/a/' do |env|
        assert_equal 'foo.com', env[:url].host

        [200, {'Content-Type' => 'application/json'}, Sawyer::Agent.encode(
          :_links => {
            :users => {:href => '/users'}})]
      end

      assert_equal 200, @agent.root.status

      assert_equal '/users', @agent.rels[:users].href
      assert_equal :get,     @agent.rels[:users].method
    end

    def test_allows_custom_rel_parsing
      @stubs.get '/a/' do |env|
        assert_equal 'foo.com', env[:url].host

        [200, {'Content-Type' => 'application/json'}, Sawyer::Agent.encode(
          :url => '/',
          :users_url => '/users',
          :repos_url => '/repos')]
      end

      agent = Sawyer::Agent.new "http://foo.com/a/" do |conn|
        conn.builder.handlers.delete(Faraday::Adapter::NetHttp)
        conn.adapter :test, @stubs
      end
      agent.links_parser = InlineRelsParser.new

      assert_equal 200, agent.root.status

      assert_equal '/users', agent.rels[:users].href
      assert_equal :get,     agent.rels[:users].method
      assert_equal '/repos', agent.rels[:repos].href
      assert_equal :get,     agent.rels[:repos].method

    end

    def test_saves_root_endpoint
      @stubs.get '/a/' do |env|
        [200, {}, '{}']
      end

      assert_kind_of Sawyer::Response, @agent.root
      refute_equal @agent.root.time, @agent.start.time
    end

    def test_starts_a_session
      @stubs.get '/a/' do |env|
        assert_equal 'foo.com', env[:url].host

        [200, {'Content-Type' => 'application/json'}, Sawyer::Agent.encode(
          :_links => {
            :users => {:href => '/users'}})]
      end

      res = @agent.start

      assert_equal 200, res.status
      assert_kind_of Sawyer::Resource, resource = res.data

      assert_equal '/users', resource.rels[:users].href
      assert_equal :get,     resource.rels[:users].method
    end

    def test_requests_with_body_and_options
      @stubs.post '/a/b/c' do |env|
        assert_equal '{"a":1}', env[:body]
        assert_equal 'abc',     env[:request_headers]['x-test']
        assert_equal 'foo=bar', env[:url].query
        [200, {}, "{}"]
      end

      res = @agent.call :post, 'b/c' , {:a => 1},
        :headers => {"X-Test" => "abc"},
        :query   => {:foo => 'bar'}
      assert_equal 200, res.status
    end

    def test_requests_with_body_and_options_to_get
      @stubs.get '/a/b/c' do |env|
        assert_nil env[:body]
        assert_equal 'abc',     env[:request_headers]['x-test']
        assert_equal 'foo=bar', env[:url].query
        [200, {}, "{}"]
      end

      res = @agent.call :get, 'b/c' , {:a => 1},
        :headers => {"X-Test" => "abc"},
        :query   => {:foo => 'bar'}
      assert_equal 200, res.status
    end

    def test_encodes_and_decodes_times
      time = Time.at(Time.now.to_i)
      data = {
        :a => 1,
        :b => true,
        :c => 'c',
        :created_at => time,
        :published_at => nil,
        :updated_at => "An invalid date",
        :pub_date => time,
        :subscribed_at => time.to_i,
        :lost_at => time.to_f,
        :first_date => false,
        :validate => true
      }
      data = [data.merge(:foo => [data])]
      encoded = Sawyer::Agent.encode(data)
      decoded = Sawyer::Agent.decode(encoded)

      2.times do
        assert_equal 1, decoded.size
        decoded = decoded.shift

        assert_equal 1, decoded[:a]
        assert_equal true, decoded[:b]
        assert_equal 'c', decoded[:c]
        assert_equal time, decoded[:created_at], "Did not parse created_at as Time"
        assert_nil decoded[:published_at]
        assert_equal "An invalid date", decoded[:updated_at]
        assert_equal time, decoded[:pub_date], "Did not parse pub_date as Time"
        assert_equal true, decoded[:validate]
        assert_equal time, decoded[:subscribed_at], "Did not parse subscribed_at as Time"
        assert_equal time, decoded[:lost_at], "Did not parse lost_at as Time"
        assert_equal false, decoded[:first_date], "Parsed first_date"
        decoded = decoded[:foo]
      end
    end

    def test_does_not_encode_non_json_content_types
      @stubs.get '/a/' do |env|
        assert_equal 'foo.com', env[:url].host

        [200, {'Content-Type' => 'text/plain'}, "This is plain text"]
      end
      res = @agent.call :get, '/a/',
        :headers => {"Accept" => "text/plain"}
      assert_equal 200, res.status

      assert_equal "This is plain text", res.data
    end

    # https://bugs.debian.org/888173
    #
    # def test_handle_yaml_dump_and_load
    #   require 'yaml'
    #   res = Agent.new 'http://example.com', :a => 1
    #   YAML.load(YAML.dump(res))
    # end

    def test_handle_marshal_dump_and_load
      res = Agent.new 'http://example.com', :a => 1
      Marshal.load(Marshal.dump(res))
    end

    def test_blank_response_doesnt_raise
      @stubs.get "/a/" do |env|
        assert_equal "foo.com", env[:url].host
        [200, { "Content-Type" => "application/json" }, " "]
      end

      agent = Sawyer::Agent.new "http://foo.com/a/" do |conn|
        conn.adapter :test, @stubs
      end

#      assert_equal 200, agent.root.status
    end
  end
end