test/excon/hypermedia_test.rb in excon-hypermedia-0.1.0 vs test/excon/hypermedia_test.rb in excon-hypermedia-0.2.0
- old
+ new
@@ -1,84 +1,90 @@
# frozen_string_literal: true
-# rubocop:disable Metrics/AbcSize, Metrics/LineLength
+# rubocop:disable Metrics/LineLength
require_relative '../test_helper'
module Excon
# HypermediaTest
#
# Verifies the Excon connection consuming HyperMedia APIs.
#
class HypermediaTest < Minitest::Test
def entrypoint
- <<~EOF
- {
- "_links": {
- "hello": {
- "href":"http://www.example.com/hello/{location}"
- }
- }
- }
- EOF
+ '{ "_links": { "hello": { "href":"http://www.example.com/hello/{location}" } } }'
end
- def hello_world
+ def hello_world # rubocop:disable Metrics/MethodLength
<<~EOF
{
"_links": {
"goodbye": {
"href":"http://www.example.com/hello/world/goodbye{?message}"
}
- }
+ },
+ "uid": "hello",
+ "message": "goodbye!"
}
- EOF
+ EOF
end
- def hello_universe
- <<~EOF
- {
- "_links": {
- "goodbye": {
- "href":"http://www.example.com/hello/universe/goodbye{?message}"
- }
- }
- }
- EOF
- end
-
def setup
Excon.defaults[:mock] = true
+ Excon.defaults[:middlewares].push(Excon::HyperMedia::Middleware)
- Excon.stub({ method: :get, path: '/api' }, body: entrypoint, status: 200)
- Excon.stub({ method: :get, path: '/hello/world' }, body: hello_world, status: 200)
- Excon.stub({ method: :get, path: '/hello/world/goodbye', query: nil }, body: 'bye!', status: 200)
- Excon.stub({ method: :get, path: '/hello/world/goodbye', query: 'message=farewell' }, body: 'farewell', status: 200)
- Excon.stub({ method: :get, path: '/hello/universe' }, body: hello_universe, status: 200)
+ response = { headers: { 'Content-Type' => 'application/hal+json' } }
+ Excon.stub({ method: :get, path: '/api' }, response.merge(body: entrypoint))
+ Excon.stub({ method: :get, path: '/hello/world' }, response.merge(body: hello_world))
+ Excon.stub({ method: :get, path: '/hello/world/goodbye', query: nil }, response.merge(body: 'bye!'))
+ Excon.stub({ method: :get, path: '/hello/world/goodbye', query: 'message=farewell' }, response.merge(body: 'farewell'))
end
- def test_hypermedia_request
- conn = Excon.new('http://www.example.com/api', hypermedia: true)
- conn2 = conn.hello(expand: { location: 'world' })
- conn3 = conn.hello(expand: { location: 'universe' })
+ def client
+ Excon.get('http://www.example.com/api')
+ end
- assert_equal '/hello/world', conn2.data[:path]
- assert conn2.get.body.include?('http://www.example.com/hello/world/goodbye{?message}')
+ def test_request
+ response = client.hello(expand: { location: 'world' }).get
- assert_equal '/hello/universe', conn3.data[:path]
- assert conn3.get.body.include?('http://www.example.com/hello/universe/goodbye{?message}')
+ assert response.body.include?('http://www.example.com/hello/world/goodbye{?message}')
end
- def test_nested_hypermedia_request
- conn = Excon.new('http://www.example.com/api', hypermedia: true)
- conn2 = conn.hello(expand: { location: 'world' }).goodbye
- conn3 = conn.hello(expand: { location: 'world' }).goodbye(expand: { message: 'farewell' })
+ def test_nested_request
+ hello = client.hello(expand: { location: 'world' })
+ response = hello.get.goodbye.get
- assert_equal '/hello/world/goodbye', conn2.data[:path]
- assert_nil conn2.data[:query]
- assert_equal 'bye!', conn2.get.body
+ assert_equal 'bye!', response.body
+ end
- assert_equal '/hello/world/goodbye', conn3.data[:path]
- assert_equal 'message=farewell', conn3.data[:query]
- assert_equal 'farewell', conn3.get.body
+ def test_nested_query_parameters
+ hello = client.hello(expand: { location: 'world' })
+ response = hello.get.goodbye(expand: { message: 'farewell' }).get
+
+ assert_equal 'farewell', response.body
+ end
+
+ def test_expand_in_get
+ response = client.hello.get(expand: { location: 'world' })
+
+ assert response.body.include?('http://www.example.com/hello/world/goodbye{?message}')
+ end
+
+ def test_attribute
+ response = client.hello(expand: { location: 'world' }).get
+
+ assert_equal response.uid, 'hello'
+ assert_equal response.message, 'goodbye!'
+ end
+
+ def test_links
+ response = client.hello(expand: { location: 'world' }).get
+
+ assert_equal response.links.first.name, 'goodbye'
+ end
+
+ def test_attributes
+ response = client.hello(expand: { location: 'world' }).get
+
+ assert_equal response.attributes.uid, 'hello'
end
def teardown
Excon.stubs.clear
end