mirror of
https://github.com/HoneyryderChuck/httpx.git
synced 2025-08-10 00:01:27 -04:00
added the cookie plugin, with the first test: passing the cookie in the options
This commit is contained in:
parent
06667b355f
commit
6ed620caff
50
lib/httpx/plugins/cookies.rb
Normal file
50
lib/httpx/plugins/cookies.rb
Normal file
@ -0,0 +1,50 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module HTTPX
|
||||
module Plugins
|
||||
module Cookies
|
||||
def self.load_dependencies(*)
|
||||
require "http/cookie"
|
||||
end
|
||||
|
||||
module InstanceMethods
|
||||
def cookies(cookies)
|
||||
branch(default_options.with_cookies(cookies))
|
||||
end
|
||||
end
|
||||
|
||||
module RequestMethods
|
||||
def initialize(*)
|
||||
super
|
||||
@headers.cookies(@options.cookies)
|
||||
end
|
||||
end
|
||||
|
||||
module HeadersMethods
|
||||
def cookies(cookies)
|
||||
cookies.each do |k, v|
|
||||
cookie = k.is_a?(HTTP::Cookie) ? k : HTTP::Cookie.new(k.to_s, v.to_s)
|
||||
add("cookie", cookie.cookie_value)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
module ResponseMethods
|
||||
def cookies
|
||||
headers["cookie"]
|
||||
end
|
||||
end
|
||||
|
||||
module OptionsMethods
|
||||
def self.included(klass)
|
||||
super
|
||||
klass.def_option(:cookies) do |cookies|
|
||||
cookies.split(/ *; */) if cookies.is_a?(String)
|
||||
cookies
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
register_plugin :cookies, Cookies
|
||||
end
|
||||
end
|
@ -13,6 +13,8 @@ class HTTP1Test < HTTPTest
|
||||
include ResponseBody
|
||||
include IO
|
||||
|
||||
include Plugins::Cookies
|
||||
|
||||
private
|
||||
|
||||
def origin
|
||||
|
@ -11,6 +11,8 @@ class HTTP2Test < HTTPTest
|
||||
include ResponseBody
|
||||
include IO
|
||||
|
||||
include Plugins::Cookies
|
||||
|
||||
private
|
||||
|
||||
def origin
|
||||
|
29
test/support/requests/plugins/cookies.rb
Normal file
29
test/support/requests/plugins/cookies.rb
Normal file
@ -0,0 +1,29 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Requests
|
||||
module Plugins
|
||||
module Cookies
|
||||
|
||||
def test_plugin_cookies
|
||||
client = HTTPX.plugin(:cookies)
|
||||
assert client.respond_to?(:cookies), "client should be cookie-enabled"
|
||||
response = client.get(cookies_uri)
|
||||
assert response.respond_to?(:cookies), "response should have cookies"
|
||||
body = json_body(response)
|
||||
assert body.key?("cookies")
|
||||
assert body["cookies"].empty?
|
||||
|
||||
session_response = client.cookies("abc" => "def").get(cookies_uri)
|
||||
body = json_body(session_response)
|
||||
assert body.key?("cookies")
|
||||
assert body["cookies"]["abc"] == "def", "abc wasn't properly set"
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def cookies_uri
|
||||
build_uri("/cookies")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
x
Reference in New Issue
Block a user