mirror of
https://github.com/HoneyryderChuck/httpx.git
synced 2025-10-05 00:02:38 -04:00
Merge branch 'cookies'
This commit is contained in:
commit
fbdd2391fd
@ -59,7 +59,7 @@ module HTTPX
|
||||
end
|
||||
|
||||
def plugin(*plugins)
|
||||
Class.new(Client).plugins(plugins).new(default_options)
|
||||
Class.new(Client).plugins(plugins).new
|
||||
end
|
||||
alias :plugins :plugin
|
||||
|
||||
|
63
lib/httpx/plugins/cookies.rb
Normal file
63
lib/httpx/plugins/cookies.rb
Normal file
@ -0,0 +1,63 @@
|
||||
# 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, self)
|
||||
end
|
||||
end
|
||||
|
||||
module HeadersMethods
|
||||
def cookies(jar, request)
|
||||
return unless jar
|
||||
unless jar.is_a?(HTTP::CookieJar)
|
||||
jar = jar.each_with_object(HTTP::CookieJar.new) do |(k, v), j|
|
||||
cookie = k.is_a?(HTTP::Cookie) ? v : HTTP::Cookie.new(k.to_s, v.to_s)
|
||||
cookie.domain = request.authority
|
||||
cookie.path = request.path
|
||||
j.add(cookie)
|
||||
end
|
||||
end
|
||||
self["cookie"] = HTTP::Cookie.cookie_value(jar.cookies)
|
||||
end
|
||||
end
|
||||
|
||||
module ResponseMethods
|
||||
def cookie_jar
|
||||
return @cookies if defined?(@cookies)
|
||||
return nil unless headers.key?("set-cookie")
|
||||
@cookies ||= begin
|
||||
jar = HTTP::CookieJar.new
|
||||
jar.parse(headers["set-cookie"], @request.uri)
|
||||
jar
|
||||
end
|
||||
end
|
||||
alias :cookies :cookie_jar
|
||||
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
|
@ -16,6 +16,8 @@ class HTTP1Test < HTTPTest
|
||||
include Plugins::Proxy
|
||||
include Plugins::Authentication
|
||||
include Plugins::FollowRedirects
|
||||
include Plugins::Cookies
|
||||
|
||||
private
|
||||
|
||||
def origin
|
||||
|
@ -14,6 +14,7 @@ class HTTP2Test < HTTPTest
|
||||
include Plugins::Proxy
|
||||
include Plugins::Authentication
|
||||
include Plugins::FollowRedirects
|
||||
include Plugins::Cookies
|
||||
|
||||
private
|
||||
|
||||
|
54
test/support/requests/plugins/cookies.rb
Normal file
54
test/support/requests/plugins/cookies.rb
Normal file
@ -0,0 +1,54 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Requests
|
||||
module Plugins
|
||||
module Cookies
|
||||
|
||||
def test_plugin_cookies_get
|
||||
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
|
||||
|
||||
def test_plugin_cookies_set
|
||||
client = HTTPX.plugin(:cookies)
|
||||
session_cookies = { "a" => "b", "c" => "d" }
|
||||
session_uri = set_cookies_uri(session_cookies)
|
||||
session_response = client.get(set_cookies_uri(session_cookies))
|
||||
assert session_response.status == 302, "response should redirect"
|
||||
|
||||
assert !session_response.cookies.nil?, "there should be cookies in the response"
|
||||
response_cookies = session_response.cookie_jar
|
||||
assert !response_cookies.empty?
|
||||
response_cookies.cookies(session_uri).each do |cookie|
|
||||
assert session_cookies.one? { |k, v| k == cookie.name && v == cookie.value }
|
||||
end
|
||||
|
||||
response = client.cookies(response_cookies).get(cookies_uri)
|
||||
body = json_body(response)
|
||||
assert body.key?("cookies")
|
||||
assert body["cookies"]["a"] == "b"
|
||||
assert body["cookies"]["c"] == "d"
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def cookies_uri
|
||||
build_uri("/cookies")
|
||||
end
|
||||
|
||||
def set_cookies_uri(cookies)
|
||||
build_uri("/cookies/set?" + URI.encode_www_form(cookies))
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
x
Reference in New Issue
Block a user