test/aitch/request_test.rb in aitch-1.0.2 vs test/aitch/request_test.rb in aitch-1.1.0
- old
+ new
@@ -1,12 +1,13 @@
# frozen_string_literal: true
+
require "test_helper"
class RequestTest < Minitest::Test
test "sets content type" do
- request = build_request(content_type: 'application/json')
- assert_equal 'application/json', request.content_type
+ request = build_request(content_type: "application/json")
+ assert_equal "application/json", request.content_type
end
test "raises with invalid uri" do
assert_raises(Aitch::InvalidURIError) { build_request(url: "\\").uri }
end
@@ -23,13 +24,15 @@
assert_raises(Aitch::InvalidHTTPMethodError) { request.perform }
end
test "sets user agent" do
- requester = build_request
+ requester = build_request(headers: {"User-Agent" => "CUSTOM"})
request = requester.request
- assert_equal requester.options[:user_agent], request["User-Agent"]
+
+ assert_equal "CUSTOM", requester.headers["User-Agent"]
+ assert_equal "CUSTOM", request["User-Agent"]
end
test "requests gzip encoding" do
request = build_request.request
assert_equal "gzip,deflate", request["Accept-Encoding"]
@@ -48,10 +51,15 @@
test "sets request body from string" do
request = build_request(request_method: "post", data: "some body").request
assert_equal "some body", request.body
end
+ test "sets request body from params key" do
+ request = build_request(request_method: "post", params: "some body").request
+ assert_equal "some body", request.body
+ end
+
test "sets json body from object" do
request = build_request(
request_method: "post",
data: {a: 1},
content_type: "application/json",
@@ -64,11 +72,11 @@
test "sets json body from object (default headers)" do
request = build_request(
request_method: "post",
data: {a: 1},
- options: {json_parser: JSON, default_headers: {'Content-Type' => 'application/json'}}
+ options: {json_parser: JSON, default_headers: {"Content-Type" => "application/json"}}
).request
expected = {a: 1}.to_json
assert_equal expected, request.body
end
@@ -119,15 +127,32 @@
end
test "performs request when using dsl" do
register_uri(:post, /.+/)
- response = Aitch.post do
+ Aitch.post do
url "http://example.org/some/path"
params a: 1, b: 2
headers Rendering: "0.1"
options user: "user", password: "pass"
end
+
+ assert_equal "/some/path", last_request.uri.request_uri
+ assert_equal :post, last_request.method
+ assert_equal "a=1&b=2", last_request.body
+ assert_equal "0.1", last_request.headers["Rendering"]
+ assert_equal "user:pass", Base64.decode64(last_request.headers["Authorization"].split(" ").last)
+ end
+
+ test "performs request when using kwargs" do
+ register_uri(:post, /.+/)
+
+ Aitch.post(
+ url: "http://example.org/some/path",
+ data: {a: 1, b: 2},
+ headers: {Rendering: "0.1"},
+ options: {user: "user", password: "pass"}
+ )
assert_equal "/some/path", last_request.uri.request_uri
assert_equal :post, last_request.method
assert_equal "a=1&b=2", last_request.body
assert_equal "0.1", last_request.headers["Rendering"]