mirror of
https://github.com/HoneyryderChuck/httpx.git
synced 2025-09-30 00:01:02 -04:00
added tests to the req/rep/head factory classes, and to the plugin module
This commit is contained in:
parent
b975f7e3c5
commit
15a972df32
@ -18,4 +18,88 @@ class ClientTest < Minitest::Test
|
||||
request3 = client2.request(:get, "http://google.com", headers: {"accept" => "text/javascript"})
|
||||
assert request3.headers["accept"] == "text/javascript", "header hasn't been properly set"
|
||||
end
|
||||
|
||||
def test_client_plugin
|
||||
klient_class = Class.new(Client)
|
||||
klient_class.plugin(TestPlugin)
|
||||
client = klient_class.new
|
||||
assert client.respond_to?(:foo), "instance methods weren't added"
|
||||
assert client.foo == "client-foo", "instance method is unexpected"
|
||||
assert client.respond_to?(:bar), "load and configure didn't work"
|
||||
assert client.bar == "config-load-bar", "load and configure didn't work"
|
||||
request = client.request(:get, "/")
|
||||
assert request.respond_to?(:foo), "request methods haven't been added"
|
||||
assert request.foo == "request-foo", "request method is unexpected"
|
||||
assert request.headers.respond_to?(:foo), "headers methods haven't been added"
|
||||
assert request.headers.foo == "headers-foo", "headers method is unexpected"
|
||||
assert client.respond_to?(:response), "response constructor was added"
|
||||
response = client.response(nil, 200, {})
|
||||
assert response.respond_to?(:foo), "response methods haven't been added"
|
||||
assert response.foo == "response-foo", "response method is unexpected"
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
TestPlugin = Module.new do
|
||||
self::ClassMethods = Module.new do
|
||||
def foo
|
||||
"client-foo"
|
||||
end
|
||||
end
|
||||
self::InstanceMethods = Module.new do
|
||||
def foo
|
||||
self.class.foo
|
||||
end
|
||||
|
||||
def response(*args)
|
||||
@default_options.response_class.new(*args)
|
||||
end
|
||||
end
|
||||
self::RequestClassMethods = Module.new do
|
||||
def foo
|
||||
'request-foo'
|
||||
end
|
||||
end
|
||||
self::RequestMethods = Module.new do
|
||||
def foo
|
||||
self.class.foo
|
||||
end
|
||||
end
|
||||
self::ResponseClassMethods = Module.new do
|
||||
def foo
|
||||
"response-foo"
|
||||
end
|
||||
end
|
||||
self::ResponseMethods = Module.new do
|
||||
def foo
|
||||
self.class.foo
|
||||
end
|
||||
end
|
||||
self::HeadersClassMethods = Module.new do
|
||||
def foo
|
||||
"headers-foo"
|
||||
end
|
||||
end
|
||||
self::HeadersMethods = Module.new do
|
||||
def foo
|
||||
self.class.foo
|
||||
end
|
||||
end
|
||||
|
||||
def self.load_dependencies(mod)
|
||||
mod.__send__(:include, Module.new do
|
||||
def bar
|
||||
"load-bar"
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
def self.configure(mod)
|
||||
mod.__send__(:include, Module.new do
|
||||
def bar
|
||||
"config-#{super}"
|
||||
end
|
||||
end)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -69,6 +69,9 @@ class OptionsSpec < Minitest::Test
|
||||
:cookies => {},
|
||||
:max_concurrent_requests => 100,
|
||||
:max_retries => 3,
|
||||
:request_class => bar.request_class,
|
||||
:response_class => bar.response_class,
|
||||
:headers_class => bar.headers_class,
|
||||
}, "options haven't merged correctly"
|
||||
end
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user