mirror of
https://github.com/HoneyryderChuck/httpx.git
synced 2025-10-05 00:02:38 -04:00
Merge branch 'issue-137' into 'master'
Cookie Jar management Closes #137 See merge request honeyryderchuck/httpx!151
This commit is contained in:
commit
fa7b61a852
@ -189,9 +189,8 @@ module HTTPX
|
||||
|
||||
return self if h1 == h2
|
||||
|
||||
merged = h1.merge(h2) do |k, v1, v2|
|
||||
case k
|
||||
when :headers, :ssl, :http2_settings, :timeout
|
||||
merged = h1.merge(h2) do |_k, v1, v2|
|
||||
if v1.respond_to?(:merge) && v2.respond_to?(:merge)
|
||||
v1.merge(v2)
|
||||
else
|
||||
v2
|
||||
|
@ -61,7 +61,7 @@ module HTTPX
|
||||
|
||||
def build_request(*, _)
|
||||
request = super
|
||||
request.headers.set_cookie(@options.cookies[request.uri])
|
||||
request.headers.set_cookie(request.options.cookies[request.uri])
|
||||
request
|
||||
end
|
||||
end
|
||||
@ -75,6 +75,21 @@ module HTTPX
|
||||
add("cookie", header_value)
|
||||
end
|
||||
end
|
||||
|
||||
module OptionsMethods
|
||||
def initialize(*)
|
||||
super
|
||||
|
||||
return unless @headers.key?("cookie")
|
||||
|
||||
@headers.delete("cookie").each do |ck|
|
||||
ck.split(/ *; */).each do |cookie|
|
||||
name, value = cookie.split("=", 2)
|
||||
@cookies.add(Cookie.new(name, value))
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
register_plugin :cookies, Cookies
|
||||
end
|
||||
|
@ -107,6 +107,8 @@ module HTTPX
|
||||
|
||||
@path ||= "/"
|
||||
raise ArgumentError, "name must be specified" if @name.nil?
|
||||
|
||||
@name = @name.to_s
|
||||
end
|
||||
|
||||
def expires
|
||||
@ -122,7 +124,7 @@ module HTTPX
|
||||
# Returns a string for use in the Cookie header, i.e. `name=value`
|
||||
# or `name="value"`.
|
||||
def cookie_value
|
||||
"#{@name}=#{Scanner.quote(@value)}"
|
||||
"#{@name}=#{Scanner.quote(@value.to_s)}"
|
||||
end
|
||||
alias_method :to_s, :cookie_value
|
||||
|
||||
|
@ -57,7 +57,7 @@ module HTTPX
|
||||
def each(uri = nil, &blk)
|
||||
return enum_for(__method__, uri) unless block_given?
|
||||
|
||||
return @store.each(&blk) unless uri
|
||||
return @cookies.each(&blk) unless uri
|
||||
|
||||
uri = URI(uri)
|
||||
|
||||
@ -73,6 +73,25 @@ module HTTPX
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def merge(other)
|
||||
cookies_dup = dup
|
||||
|
||||
other.each do |elem|
|
||||
cookie = case elem
|
||||
when Cookie
|
||||
elem
|
||||
when Array
|
||||
Cookie.new(*elem)
|
||||
else
|
||||
Cookie.new(elem)
|
||||
end
|
||||
|
||||
cookies_dup.add(cookie)
|
||||
end
|
||||
|
||||
cookies_dup
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -12,7 +12,7 @@ module HTTPX
|
||||
def []=: (headers_key field, headers_value value) -> void
|
||||
|
||||
def add: (headers_key field, string value) -> void
|
||||
def delete: (headers_key field) -> void
|
||||
def delete: (headers_key field) -> Array[String]?
|
||||
|
||||
def each: (?_Each[[headers_key, String]]? extra_headers) { (headers_key, String) -> void } -> void
|
||||
| (?_Each[[headers_key, String]]? extra_headers) -> Enumerable[[headers_key, String]]
|
||||
@ -28,7 +28,7 @@ module HTTPX
|
||||
def to_hash: () -> Hash[headers_key, String]
|
||||
alias to_h to_hash
|
||||
|
||||
def inspect: () -> String
|
||||
def inspect: () -> String
|
||||
|
||||
private
|
||||
|
||||
|
@ -31,19 +31,17 @@ module HTTPX
|
||||
|
||||
def valid_for_uri?: (uri) -> bool
|
||||
|
||||
def self.new: (Cookie) -> untyped
|
||||
| (cookie_attributes) -> untyped
|
||||
| (String, String) -> untyped
|
||||
| (String, String, cookie_attributes) -> untyped
|
||||
def self.new: (Cookie) -> instance
|
||||
| (cookie_attributes) -> instance
|
||||
| (_ToS, _ToS, ?cookie_attributes) -> instance
|
||||
|
||||
def self.path_match?: (String, String) -> bool
|
||||
|
||||
private
|
||||
|
||||
def initialize: (cookie_attributes) -> untyped
|
||||
| (String, String) -> untyped
|
||||
| (String, String, cookie_attributes?) -> untyped
|
||||
|
||||
| (_ToS, _ToS, ?cookie_attributes) -> untyped
|
||||
|
||||
def acceptable_from_uri?: (uri) -> bool
|
||||
end
|
||||
end
|
||||
|
@ -7,21 +7,20 @@ module HTTPX
|
||||
|
||||
@cookies: Array[Cookie]
|
||||
|
||||
def parse: (String) -> void
|
||||
def parse: (String set_cookie) -> void
|
||||
|
||||
def add: (Cookie) -> void
|
||||
| (Cookie, String) -> void
|
||||
def add: (Cookie name, ?String path) -> void
|
||||
|
||||
def []: (uri) -> Array[Cookie]
|
||||
|
||||
def each: (uri) { (Cookie) -> void } -> void
|
||||
| (uri) -> Enumerable[Cookie]
|
||||
| () { (Cookie) -> void } -> void
|
||||
| () -> Enumerable[Cookie]
|
||||
def each: (?uri) { (Cookie) -> void } -> void
|
||||
| (?uri) -> Enumerable[Cookie]
|
||||
|
||||
def merge: (_Each[cookie] cookies) -> instance
|
||||
|
||||
private
|
||||
|
||||
def initialize: () -> void
|
||||
| (_Each[cookie]) -> untyped
|
||||
def initialize: (?_Each[cookie] cookies) -> untyped
|
||||
end
|
||||
end
|
||||
end
|
@ -88,7 +88,7 @@ class OptionsTest < Minitest::Test
|
||||
:body => nil,
|
||||
:window_size => 16_384,
|
||||
:body_threshold_size => 114_688,
|
||||
:form => { :bar => "bar" },
|
||||
:form => { foo: "foo", :bar => "bar" },
|
||||
:timeout => {
|
||||
connect_timeout: 60,
|
||||
settings_timeout: 10,
|
||||
|
@ -12,7 +12,7 @@ module Requests
|
||||
assert body.key?("cookies")
|
||||
assert body["cookies"].empty?
|
||||
|
||||
session_response = session.with_cookies("abc" => "def").get(cookies_uri)
|
||||
session_response = session.with(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"
|
||||
@ -20,7 +20,7 @@ module Requests
|
||||
|
||||
def test_plugin_cookies_get_with_hash
|
||||
session = HTTPX.plugin(:cookies)
|
||||
session_response = session.with_cookies([{ "name" => "abc", "value" => "def" }]).get(cookies_uri)
|
||||
session_response = session.with(cookies: [{ "name" => "abc", "value" => "def" }]).get(cookies_uri)
|
||||
body = json_body(session_response)
|
||||
assert body.key?("cookies")
|
||||
assert body["cookies"]["abc"] == "def", "abc wasn't properly set"
|
||||
@ -28,7 +28,7 @@ module Requests
|
||||
|
||||
def test_plugin_cookies_get_with_cookie
|
||||
session = HTTPX.plugin(:cookies)
|
||||
session_response = session.with_cookies([HTTPX::Plugins::Cookies::Cookie.new("abc", "def")]).get(cookies_uri)
|
||||
session_response = session.with(cookies: [HTTPX::Plugins::Cookies::Cookie.new("abc", "def")]).get(cookies_uri)
|
||||
body = json_body(session_response)
|
||||
assert body.key?("cookies")
|
||||
assert body["cookies"]["abc"] == "def", "abc wasn't properly set"
|
||||
@ -49,19 +49,19 @@ module Requests
|
||||
verify_cookies(body["cookies"], session_cookies)
|
||||
|
||||
# second request reuses the session
|
||||
extra_cookie_response = session.with_cookies("e" => "f").get(cookies_uri)
|
||||
extra_cookie_response = session.with(cookies: { "e" => "f" }).get(cookies_uri)
|
||||
body = json_body(extra_cookie_response)
|
||||
assert body.key?("cookies")
|
||||
verify_cookies(body["cookies"], session_cookies.merge("e" => "f"))
|
||||
|
||||
# redirect to a different origin only uses the option cookies
|
||||
other_origin_response = session.with_cookies("e" => "f").get(redirect_uri(origin("google.com")))
|
||||
other_origin_response = session.with(cookies: { "e" => "f" }).get(redirect_uri(origin("google.com")))
|
||||
verify_status(other_origin_response, 302)
|
||||
assert !other_origin_response.headers.key?("set-cookie"), "cookies should not transition to next origin"
|
||||
end
|
||||
|
||||
def test_cookies_wrap
|
||||
session = HTTPX.plugin(:cookies).with_cookies("abc" => "def")
|
||||
session = HTTPX.plugin(:cookies).with(cookies: { "abc" => "def" })
|
||||
|
||||
session.wrap do |_http|
|
||||
set_cookie_uri = cookies_set_uri("123" => "456")
|
||||
@ -201,6 +201,26 @@ module Requests
|
||||
assert [c4, c3, c2, c1].sort == [c3, c4, c1, c2]
|
||||
end
|
||||
|
||||
def test_plugin_cookies_jar_management
|
||||
cookie_header = lambda do |response|
|
||||
JSON.parse(response.to_s)["headers"]
|
||||
end
|
||||
uri = build_uri("/headers")
|
||||
|
||||
http = HTTPX.plugin(:cookies).with(cookies: { :a => 1, :b => 2 })
|
||||
verify_header(cookie_header.call(http.get(uri)), "Cookie", "a=1; b=2")
|
||||
|
||||
http = http.with(cookies: { :a => 3 })
|
||||
verify_header(cookie_header.call(http.get(uri)), "Cookie", "a=3; b=2")
|
||||
|
||||
verify_header(cookie_header.call(http.get(uri, cookies: { :a => 4 })), "Cookie", "a=4; b=2")
|
||||
|
||||
http = http.with(headers: { "Cookie" => "a=1;f=6" })
|
||||
verify_header(cookie_header.call(http.get(uri)), "Cookie", "a=1; b=2; f=6")
|
||||
|
||||
verify_header(cookie_header.call(http.get(uri, cookies: { :a => 4 })), "Cookie", "a=4; b=2; f=6")
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def jar_cookies_uri(path = "/cookies")
|
||||
|
Loading…
x
Reference in New Issue
Block a user