aitch 1.0.1 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (63) hide show
  1. checksums.yaml +5 -5
  2. data/.github/FUNDING.yml +3 -0
  3. data/.github/workflows/tests.yml +50 -0
  4. data/.rubocop.yml +26 -0
  5. data/Gemfile +2 -2
  6. data/README.md +54 -14
  7. data/Rakefile +5 -1
  8. data/aitch.gemspec +12 -7
  9. data/lib/aitch/configuration.rb +5 -0
  10. data/lib/aitch/dsl.rb +4 -3
  11. data/lib/aitch/engines/json.rb +15 -0
  12. data/lib/aitch/errors.rb +1 -0
  13. data/lib/aitch/ext/to_query.rb +6 -10
  14. data/lib/aitch/location.rb +4 -3
  15. data/lib/aitch/namespace.rb +38 -14
  16. data/lib/aitch/redirect.rb +1 -0
  17. data/lib/aitch/request.rb +40 -28
  18. data/lib/aitch/response/body.rb +1 -0
  19. data/lib/aitch/response/description.rb +3 -2
  20. data/lib/aitch/response/errors.rb +2 -1
  21. data/lib/aitch/response.rb +9 -5
  22. data/lib/aitch/response_parser/default_parser.rb +2 -1
  23. data/lib/aitch/response_parser/html_parser.rb +1 -0
  24. data/lib/aitch/response_parser/json_parser.rb +2 -1
  25. data/lib/aitch/response_parser/xml_parser.rb +1 -0
  26. data/lib/aitch/response_parser.rb +8 -5
  27. data/lib/aitch/uri.rb +5 -2
  28. data/lib/aitch/utils.rb +3 -1
  29. data/lib/aitch/version.rb +2 -1
  30. data/lib/aitch.rb +13 -13
  31. data/test/aitch/aitch_test.rb +1 -0
  32. data/test/aitch/configuration_test.rb +2 -1
  33. data/test/aitch/dsl_test.rb +1 -0
  34. data/test/aitch/execute_test.rb +1 -0
  35. data/test/aitch/namespace_test.rb +2 -1
  36. data/test/aitch/request/client_https_test.rb +1 -0
  37. data/test/aitch/request/follow_redirect_test.rb +4 -3
  38. data/test/aitch/request/json_request_test.rb +1 -0
  39. data/test/aitch/request/request_class_test.rb +3 -2
  40. data/test/aitch/request/status_code_validation_test.rb +3 -2
  41. data/test/aitch/request_test.rb +45 -8
  42. data/test/aitch/response/custom_response_parser_test.rb +1 -4
  43. data/test/aitch/response/default_response_parser_test.rb +14 -0
  44. data/test/aitch/response/errors_test.rb +1 -0
  45. data/test/aitch/response/html_response_test.rb +1 -0
  46. data/test/aitch/response/json_response_test.rb +2 -1
  47. data/test/aitch/response/raw_response_test.rb +1 -0
  48. data/test/aitch/response/status_3xx_test.rb +1 -0
  49. data/test/aitch/response/status_4xx_test.rb +1 -0
  50. data/test/aitch/response/status_5xx_test.rb +1 -0
  51. data/test/aitch/response/xml_response_test.rb +1 -0
  52. data/test/aitch/response_parser/html_parser_test.rb +1 -0
  53. data/test/aitch/response_parser/json_parser_test.rb +2 -1
  54. data/test/aitch/response_parser/xml_parser_test.rb +1 -1
  55. data/test/aitch/response_test.rb +1 -1
  56. data/test/aitch/to_query_test.rb +28 -0
  57. data/test/aitch/uri_test.rb +2 -1
  58. data/test/aitch/utils/symbolize_keys_test.rb +1 -0
  59. data/test/aitch/utils/underscore_test.rb +1 -0
  60. data/test/support/helpers.rb +5 -4
  61. data/test/test_helper.rb +3 -2
  62. metadata +77 -15
  63. data/.travis.yml +0 -23
data/lib/aitch/request.rb CHANGED
@@ -1,12 +1,11 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  module Aitch
3
4
  class Request
4
- attr_accessor :request_method
5
- attr_accessor :url
6
- attr_accessor :data
7
- attr_accessor :headers
8
- attr_accessor :options
9
- attr_accessor :redirects
5
+ attr_accessor :request_method, :url, :data, :headers, :options, :redirects
6
+
7
+ alias params= data=
8
+ alias body= data=
10
9
 
11
10
  def initialize(options)
12
11
  self.headers = {}
@@ -35,7 +34,8 @@ module Aitch
35
34
  end
36
35
 
37
36
  def content_type
38
- headers["Content-Type"] || options.fetch(:default_headers, {})["Content-Type"]
37
+ headers["Content-Type"] ||
38
+ options.fetch(:default_headers, {})["Content-Type"]
39
39
  end
40
40
 
41
41
  def request
@@ -57,29 +57,40 @@ module Aitch
57
57
  end
58
58
 
59
59
  def uri
60
- @uri ||= URI.new(url, data, http_method_class::REQUEST_HAS_BODY)
60
+ @uri ||= begin
61
+ normalized_url = if url.start_with?("/") && options[:base_url]
62
+ File.join(options[:base_url], url)
63
+ else
64
+ url
65
+ end
66
+
67
+ URI.new(normalized_url, data, http_method_class::REQUEST_HAS_BODY)
68
+ end
61
69
  end
62
70
 
63
71
  def http_method_class
64
72
  Net::HTTP.const_get(request_method.to_s.capitalize)
65
73
  rescue NameError
66
- raise InvalidHTTPMethodError, "unexpected HTTP verb: #{request_method.inspect}"
74
+ raise InvalidHTTPMethodError,
75
+ "unexpected HTTP verb: #{request_method.inspect}"
67
76
  end
68
77
 
69
- private
70
- def set_body(request)
78
+ private def set_body(request)
71
79
  body_data = data
72
80
  body_data = data.to_h if data.respond_to?(:to_h)
73
- body_data = ResponseParser::JSONParser.engine.dump(body_data) if content_type.to_s =~ /\bjson\b/
74
81
 
75
- if body_data.kind_of?(Hash)
76
- request.body = Utils.build_query(body_data)
77
- else
78
- request.body = body_data.to_s
82
+ if content_type.to_s.match?(/\bjson\b/)
83
+ body_data = ResponseParser::JSONParser.engine.dump(body_data)
79
84
  end
85
+
86
+ request.body = if body_data.is_a?(Hash)
87
+ Utils.build_query(body_data)
88
+ else
89
+ body_data.to_s
90
+ end
80
91
  end
81
92
 
82
- def set_headers(request)
93
+ private def set_headers(request)
83
94
  all_headers = options.fetch(:default_headers, {}).merge(headers)
84
95
 
85
96
  all_headers.each do |name, value|
@@ -88,30 +99,31 @@ module Aitch
88
99
  end
89
100
  end
90
101
 
91
- def set_credentials(request)
102
+ private def set_credentials(request)
92
103
  return unless options[:user] || options[:password]
104
+
93
105
  request.basic_auth(options[:user], options[:password])
94
106
  end
95
107
 
96
- def set_https(client)
108
+ private def set_https(client)
97
109
  client.use_ssl = uri.scheme == "https"
98
110
  client.verify_mode = OpenSSL::SSL::VERIFY_PEER
99
111
  end
100
112
 
101
- def set_timeout(client)
113
+ private def set_timeout(client)
102
114
  client.read_timeout = options[:timeout]
103
115
  end
104
116
 
105
- def set_logger(client)
117
+ private def set_logger(client)
106
118
  logger = options[:logger]
107
119
  client.set_debug_output(logger) if logger
108
120
  end
109
121
 
110
- def set_user_agent(request)
122
+ private def set_user_agent(request)
111
123
  request["User-Agent"] = options[:user_agent]
112
124
  end
113
125
 
114
- def set_gzip(request)
126
+ private def set_gzip(request)
115
127
  request["Accept-Encoding"] = "gzip,deflate"
116
128
  end
117
129
 
@@ -119,7 +131,7 @@ module Aitch
119
131
  defined?(Net::ReadTimeout) ? Net::ReadTimeout : Timeout::Error
120
132
  end
121
133
 
122
- def follow_redirect(response, redirected_from = [])
134
+ private def follow_redirect(response)
123
135
  return response unless response.redirect?
124
136
 
125
137
  redirect = Redirect.new(options)
@@ -143,18 +155,18 @@ module Aitch
143
155
  response
144
156
  end
145
157
 
146
- def validate_response!(response)
158
+ private def validate_response!(response)
147
159
  return unless options[:expect]
148
160
 
149
161
  expected = [options[:expect]].flatten
150
162
  return if expected.include?(response.code)
151
163
 
152
164
  descriptions = expected
153
- .map {|code| Response.description_for_code(code) }
154
- .join(", ")
165
+ .map {|code| Response.description_for_code(code) }
166
+ .join(", ")
155
167
 
156
168
  raise StatusCodeError,
157
- "Expected(#{descriptions}) <=> Actual(#{response.description})"
169
+ "Expected(#{descriptions}) <=> Actual(#{response.description})"
158
170
  end
159
171
  end
160
172
  end
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  module Aitch
3
4
  class Response
4
5
  class Body
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  module Aitch
3
4
  class Response
4
5
  DESCRIPTION = {
@@ -56,7 +57,7 @@ module Aitch
56
57
  504 => "Gateway Time Out",
57
58
  505 => "Version Not Supported",
58
59
  507 => "Insufficient Storage",
59
- 511 => "Network Authentication Required",
60
- }
60
+ 511 => "Network Authentication Required"
61
+ }.freeze
61
62
  end
62
63
  end
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  module Aitch
3
4
  class Response
4
5
  ERRORS = {
@@ -35,6 +36,6 @@ module Aitch
35
36
  505 => VersionNotSupportedError,
36
37
  507 => InsufficientStorageError,
37
38
  511 => NetworkAuthenticationRequiredError
38
- }
39
+ }.freeze
39
40
  end
40
41
  end
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  module Aitch
3
4
  class Response
4
5
  extend Forwardable
@@ -17,7 +18,9 @@ module Aitch
17
18
  end
18
19
 
19
20
  ERRORS.each do |status_code, exception|
20
- method_name = Utils.underscore(exception.name.split("::").last).gsub("_error", "")
21
+ method_name = Utils
22
+ .underscore(exception.name.split("::").last)
23
+ .gsub("_error", "")
21
24
 
22
25
  define_method "#{method_name}?" do
23
26
  code == status_code
@@ -35,7 +38,7 @@ module Aitch
35
38
  def success?
36
39
  code >= 200 && code <= 399
37
40
  end
38
- alias_method :ok?, :success?
41
+ alias ok? success?
39
42
 
40
43
  def redirect?
41
44
  code >= 300 && code <= 399
@@ -75,11 +78,12 @@ module Aitch
75
78
 
76
79
  def method_missing(name, *args, &block)
77
80
  return headers[name.to_s] if headers.key?(name.to_s)
81
+
78
82
  super
79
83
  end
80
84
 
81
- def respond_to_missing?(name, include_private = false)
82
- headers.key?(name.to_s)
85
+ def respond_to_missing?(name, _include_private = false)
86
+ headers.key?(name.to_s) || super
83
87
  end
84
88
 
85
89
  def description
@@ -90,6 +94,6 @@ module Aitch
90
94
  "#<#{self.class} #{description} (#{content_type})>"
91
95
  end
92
96
 
93
- alias_method :to_s, :inspect
97
+ alias to_s inspect
94
98
  end
95
99
  end
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  module Aitch
3
4
  module ResponseParser
4
5
  module DefaultParser
@@ -6,7 +7,7 @@ module Aitch
6
7
  :default
7
8
  end
8
9
 
9
- def self.match?(content_type)
10
+ def self.match?(_content_type)
10
11
  true
11
12
  end
12
13
 
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  module Aitch
3
4
  module ResponseParser
4
5
  module HTMLParser
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  module Aitch
3
4
  module ResponseParser
4
5
  module JSONParser
@@ -6,7 +7,7 @@ module Aitch
6
7
  attr_accessor :engine
7
8
  end
8
9
 
9
- self.engine = ::JSON
10
+ self.engine = Engines::JSON
10
11
 
11
12
  def self.type
12
13
  :json
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  module Aitch
3
4
  module ResponseParser
4
5
  module XMLParser
@@ -1,24 +1,27 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  module Aitch
3
4
  module ResponseParser
4
- PARSERS = []
5
+ def self.parsers
6
+ @parsers ||= []
7
+ end
5
8
 
6
9
  def self.prepend(name, parser)
7
10
  unregister(name)
8
- PARSERS.unshift parser
11
+ parsers.unshift parser
9
12
  end
10
13
 
11
14
  def self.append(name, parser)
12
15
  unregister(name)
13
- PARSERS << parser
16
+ parsers << parser
14
17
  end
15
18
 
16
19
  def self.unregister(name)
17
- PARSERS.delete_if {|parser| parser.type == name }
20
+ parsers.delete_if {|parser| parser.type == name }
18
21
  end
19
22
 
20
23
  def self.find(content_type)
21
- PARSERS.find {|parser| parser.match?(content_type) }
24
+ parsers.find {|parser| parser.match?(content_type) }
22
25
  end
23
26
 
24
27
  append :json, JSONParser
data/lib/aitch/uri.rb CHANGED
@@ -1,11 +1,12 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  module Aitch
3
4
  class URI
4
5
  extend Forwardable
5
6
 
6
7
  def_delegators :@uri, :host, :port, :scheme
7
8
 
8
- def initialize(url, data = {}, request_has_body = false)
9
+ def initialize(url, data = {}, request_has_body = false) # rubocop:disable Style/OptionalBooleanParameter
9
10
  @url = url
10
11
  @data = data
11
12
  @request_has_body = request_has_body
@@ -35,7 +36,9 @@ module Aitch
35
36
 
36
37
  def query
37
38
  query = [@uri.query]
38
- query << ::URI.encode_www_form(@data.to_a) if !request_has_body? && @data.respond_to?(:to_a)
39
+ if !request_has_body? && @data.respond_to?(:to_a)
40
+ query << ::URI.encode_www_form(@data.to_a)
41
+ end
39
42
  query = query.compact.reject(&:empty?).join("&")
40
43
 
41
44
  "?#{query}" unless query == ""
data/lib/aitch/utils.rb CHANGED
@@ -1,6 +1,8 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  module Aitch
3
- module Utils extend self
4
+ module Utils
5
+ extend self
4
6
  def underscore(string)
5
7
  string = string.gsub(/(?<=.)(URI|[A-Z])/) do |char|
6
8
  "_#{char}"
data/lib/aitch/version.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  module Aitch
3
- VERSION = "1.0.1"
4
+ VERSION = "1.2.0"
4
5
  end
data/lib/aitch.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  require "net/https"
3
4
  require "forwardable"
4
5
  require "json"
@@ -19,6 +20,7 @@ require "aitch/namespace"
19
20
  require "aitch/location"
20
21
  require "aitch/configuration"
21
22
  require "aitch/errors"
23
+ require "aitch/engines/json"
22
24
  require "aitch/request"
23
25
  require "aitch/redirect"
24
26
  require "aitch/response/errors"
@@ -37,21 +39,19 @@ module Aitch
37
39
  extend Forwardable
38
40
 
39
41
  def_delegators :namespace,
40
- :configuration, :config,
41
- :get, :get!,
42
- :post, :post!,
43
- :put, :put!,
44
- :patch, :patch!,
45
- :options, :options!,
46
- :trace, :trace!,
47
- :head, :head!,
48
- :delete, :delete!,
49
- :execute, :execute!,
50
- :configure
42
+ :configuration, :config,
43
+ :get, :get!,
44
+ :post, :post!,
45
+ :put, :put!,
46
+ :patch, :patch!,
47
+ :options, :options!,
48
+ :trace, :trace!,
49
+ :head, :head!,
50
+ :delete, :delete!,
51
+ :execute, :execute!,
52
+ :configure
51
53
  end
52
54
 
53
- private
54
-
55
55
  def self.namespace
56
56
  @namespace ||= Namespace.new
57
57
  end
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  require "test_helper"
3
4
 
4
5
  class AitchTest < Minitest::Test
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  require "test_helper"
3
4
 
4
5
  class ConfigurationTest < Minitest::Test
@@ -16,7 +17,7 @@ class ConfigurationTest < Minitest::Test
16
17
  end
17
18
 
18
19
  test "sets default headers" do
19
- assert_equal Hash.new, Aitch::Configuration.new.default_headers
20
+ assert_equal({}, Aitch::Configuration.new.default_headers)
20
21
  end
21
22
 
22
23
  test "configures aitch" do
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  require "test_helper"
3
4
 
4
5
  class DslTest < Minitest::Test
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  require "test_helper"
3
4
 
4
5
  class ExecuteTest < Minitest::Test
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  require "test_helper"
3
4
 
4
5
  class NamespaceTest < Minitest::Test
@@ -7,6 +8,6 @@ class NamespaceTest < Minitest::Test
7
8
  ns.config.user_agent = "MyLib/1.0.0"
8
9
 
9
10
  assert_equal "MyLib/1.0.0", ns.config.user_agent
10
- assert_match %r[^Aitch], Aitch.config.user_agent
11
+ assert_match(/^Aitch/, Aitch.config.user_agent)
11
12
  end
12
13
  end
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  require "test_helper"
3
4
 
4
5
  class ClientHttpsTest < Minitest::Test
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  require "test_helper"
3
4
 
4
5
  class FollowRedirectTest < Minitest::Test
@@ -25,9 +26,9 @@ class FollowRedirectTest < Minitest::Test
25
26
  register_uri(:get, "http://example.org/", location: "http://example.com/", status: 301)
26
27
  register_uri(:get, "http://example.com/", location: "https://example.com/", status: 301)
27
28
 
28
- assert_raises(Aitch::TooManyRedirectsError) {
29
+ assert_raises(Aitch::TooManyRedirectsError) do
29
30
  Aitch.get("http://example.org/")
30
- }
31
+ end
31
32
  end
32
33
 
33
34
  test "returns only redirection urls" do
@@ -50,7 +51,7 @@ class FollowRedirectTest < Minitest::Test
50
51
  register_uri(:post, "http://example.org/hi", status: 307, location: "/hello")
51
52
  register_uri(:post, "http://example.org/hello", status: 200)
52
53
 
53
- response = Aitch.post("http://example.org/", {a: 1}, {Range: "1..100"})
54
+ response = Aitch.post("http://example.org/", {a: 1}, Range: "1..100")
54
55
 
55
56
  assert_equal "http://example.org/hello", response.url
56
57
  assert_equal 200, response.code
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  require "test_helper"
3
4
 
4
5
  class JsonRequestTest < Minitest::Test
@@ -1,11 +1,12 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  require "test_helper"
3
4
 
4
5
  class RequestClassTest < Minitest::Test
5
6
  test "raises with invalid method" do
6
- error = assert_raises(Aitch::InvalidHTTPMethodError) {
7
+ error = assert_raises(Aitch::InvalidHTTPMethodError) do
7
8
  build_request(request_method: "invalid").request
8
- }
9
+ end
9
10
 
10
11
  assert_equal %[unexpected HTTP verb: "invalid"], error.message
11
12
  end
@@ -1,13 +1,14 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  require "test_helper"
3
4
 
4
5
  class StatusCodeValidationTest < Minitest::Test
5
6
  test "raises exception when status code isn't valid" do
6
7
  register_uri(:get, "http://example.org/", status: 404)
7
8
 
8
- error = assert_raises(Aitch::StatusCodeError) {
9
+ error = assert_raises(Aitch::StatusCodeError) do
9
10
  Aitch.get("http://example.org/", {}, {}, expect: [200])
10
- }
11
+ end
11
12
 
12
13
  assert_equal "Expected(200 OK) <=> Actual(404 Not Found)", error.message
13
14
  end
@@ -1,10 +1,11 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  require "test_helper"
3
4
 
4
5
  class RequestTest < Minitest::Test
5
6
  test "sets content type" do
6
- request = build_request(content_type: 'application/json')
7
- assert_equal 'application/json', request.content_type
7
+ request = build_request(content_type: "application/json")
8
+ assert_equal "application/json", request.content_type
8
9
  end
9
10
 
10
11
  test "raises with invalid uri" do
@@ -25,9 +26,11 @@ class RequestTest < Minitest::Test
25
26
  end
26
27
 
27
28
  test "sets user agent" do
28
- requester = build_request
29
+ requester = build_request(headers: {"User-Agent" => "CUSTOM"})
29
30
  request = requester.request
30
- assert_equal requester.options[:user_agent], request["User-Agent"]
31
+
32
+ assert_equal "CUSTOM", requester.headers["User-Agent"]
33
+ assert_equal "CUSTOM", request["User-Agent"]
31
34
  end
32
35
 
33
36
  test "requests gzip encoding" do
@@ -50,6 +53,11 @@ class RequestTest < Minitest::Test
50
53
  assert_equal "some body", request.body
51
54
  end
52
55
 
56
+ test "sets request body from params key" do
57
+ request = build_request(request_method: "post", params: "some body").request
58
+ assert_equal "some body", request.body
59
+ end
60
+
53
61
  test "sets json body from object" do
54
62
  request = build_request(
55
63
  request_method: "post",
@@ -66,7 +74,7 @@ class RequestTest < Minitest::Test
66
74
  request = build_request(
67
75
  request_method: "post",
68
76
  data: {a: 1},
69
- options: {json_parser: JSON, default_headers: {'Content-Type' => 'application/json'}}
77
+ options: {json_parser: JSON, default_headers: {"Content-Type" => "application/json"}}
70
78
  ).request
71
79
 
72
80
  expected = {a: 1}.to_json
@@ -121,7 +129,7 @@ class RequestTest < Minitest::Test
121
129
  test "performs request when using dsl" do
122
130
  register_uri(:post, /.+/)
123
131
 
124
- response = Aitch.post do
132
+ Aitch.post do
125
133
  url "http://example.org/some/path"
126
134
  params a: 1, b: 2
127
135
  headers Rendering: "0.1"
@@ -132,7 +140,36 @@ class RequestTest < Minitest::Test
132
140
  assert_equal :post, last_request.method
133
141
  assert_equal "a=1&b=2", last_request.body
134
142
  assert_equal "0.1", last_request.headers["Rendering"]
135
- assert_equal "user", last_request.uri.user
136
- assert_equal "pass", last_request.uri.password
143
+ assert_equal "user:pass", Base64.decode64(last_request.headers["Authorization"].split(" ").last)
144
+ end
145
+
146
+ test "performs request when using kwargs" do
147
+ register_uri(:post, /.+/)
148
+
149
+ Aitch.post(
150
+ url: "http://example.org/some/path",
151
+ data: {a: 1, b: 2},
152
+ headers: {Rendering: "0.1"},
153
+ options: {user: "user", password: "pass"}
154
+ )
155
+
156
+ assert_equal "/some/path", last_request.uri.request_uri
157
+ assert_equal :post, last_request.method
158
+ assert_equal "a=1&b=2", last_request.body
159
+ assert_equal "0.1", last_request.headers["Rendering"]
160
+ assert_equal "user:pass", Base64.decode64(last_request.headers["Authorization"].split(" ").last)
161
+ end
162
+
163
+ test "uses base url" do
164
+ register_uri(:get, /.+/)
165
+
166
+ client = Aitch::Namespace.new
167
+ client.configure {|c| c.base_url = "https://example.com" }
168
+
169
+ client.get("/some/path")
170
+
171
+ assert_equal "/some/path", last_request.uri.request_uri
172
+ assert_equal "example.com", last_request.uri.host
173
+ assert_equal "https", last_request.uri.scheme
137
174
  end
138
175
  end
@@ -1,14 +1,11 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  require "test_helper"
3
4
  require "csv"
4
5
 
5
6
  class CustomResponseParserTest < Minitest::Test
6
7
  setup do
7
8
  parser = Class.new do
8
- def self.type
9
- :csv
10
- end
11
-
12
9
  def self.match?(content_type)
13
10
  content_type =~ /csv/
14
11
  end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "test_helper"
4
+ require "csv"
5
+
6
+ class DefaultResponseParserTest < Minitest::Test
7
+ test "returns application/custom" do
8
+ register_uri(:get, "http://example.org/file.custom", body: "1,2,3", content_type: "application/custom")
9
+ response = Aitch.get("http://example.org/file.custom")
10
+
11
+ assert_instance_of String, response.data
12
+ assert_equal %[1,2,3], response.data
13
+ end
14
+ end
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  require "test_helper"
3
4
 
4
5
  class ErrorsTest < Minitest::Test
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  require "test_helper"
3
4
 
4
5
  class HtmlResponseTest < Minitest::Test