patron 0.5.0 → 0.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile.lock +2 -2
- data/ext/patron/session_ext.c +11 -4
- data/lib/patron/response.rb +8 -1
- data/lib/patron/session.rb +13 -1
- data/lib/patron/version.rb +1 -1
- data/spec/response_spec.rb +26 -0
- data/spec/session_spec.rb +41 -2
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 018a03fb6977a92e0dbf70f4e8473ef273d7576c
|
4
|
+
data.tar.gz: 611bde5b709c9f24007fa8805b369310baa8ed6d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a2e176e3648ab4293bc69cbaca8e048ba7effb544ebf49de0cd3647b99cd1105e0b52f1afaef32e3c9ab899ff5c41cc01e8fa2d97861de0f3064c405d0af4865
|
7
|
+
data.tar.gz: a1525278695ce34f891f3d06f2bb352e841d97fba8b4f50b71a21b6fe960964cd4f8f9e39ca5dab1d65ac8d30e8fd97dd80b8d3c9ff8cc63d78eb331756c35df
|
data/Gemfile.lock
CHANGED
data/ext/patron/session_ext.c
CHANGED
@@ -367,6 +367,7 @@ static void set_options_from_request(VALUE self, VALUE request) {
|
|
367
367
|
|
368
368
|
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1);
|
369
369
|
if (!NIL_P(data)) {
|
370
|
+
data = rb_funcall(data, rb_intern("to_s"), 0);
|
370
371
|
long len = RSTRING_LEN(data);
|
371
372
|
state->upload_buf = StringValuePtr(data);
|
372
373
|
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1);
|
@@ -387,8 +388,8 @@ static void set_options_from_request(VALUE self, VALUE request) {
|
|
387
388
|
VALUE multipart = rb_iv_get(request, "@multipart");
|
388
389
|
|
389
390
|
if (!NIL_P(data) && NIL_P(multipart)) {
|
391
|
+
data = rb_funcall(data, rb_intern("to_s"), 0);
|
390
392
|
long len = RSTRING_LEN(data);
|
391
|
-
|
392
393
|
state->upload_buf = StringValuePtr(data);
|
393
394
|
|
394
395
|
if (action == rb_intern("post")) {
|
@@ -537,7 +538,8 @@ static VALUE create_response(VALUE self, CURL* curl, VALUE header_buffer, VALUE
|
|
537
538
|
char* effective_url = NULL;
|
538
539
|
long code = 0;
|
539
540
|
long count = 0;
|
540
|
-
|
541
|
+
VALUE responseKlass = Qnil;
|
542
|
+
|
541
543
|
curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &effective_url);
|
542
544
|
args[0] = rb_str_new2(effective_url);
|
543
545
|
|
@@ -550,8 +552,9 @@ static VALUE create_response(VALUE self, CURL* curl, VALUE header_buffer, VALUE
|
|
550
552
|
args[3] = header_buffer;
|
551
553
|
args[4] = body_buffer;
|
552
554
|
args[5] = rb_iv_get(self, "@default_response_charset");
|
553
|
-
|
554
|
-
|
555
|
+
|
556
|
+
responseKlass = rb_funcall(self, rb_intern("response_class"), 0);
|
557
|
+
return rb_class_new_instance(6, args, responseKlass);
|
555
558
|
}
|
556
559
|
|
557
560
|
/* Raise an exception based on the Curl error code. */
|
@@ -647,6 +650,10 @@ static VALUE cleanup(VALUE self) {
|
|
647
650
|
state->upload_file = NULL;
|
648
651
|
}
|
649
652
|
|
653
|
+
if (state->post) {
|
654
|
+
curl_formfree(state->post);
|
655
|
+
}
|
656
|
+
|
650
657
|
state->upload_buf = NULL;
|
651
658
|
|
652
659
|
return Qnil;
|
data/lib/patron/response.rb
CHANGED
@@ -68,13 +68,20 @@ module Patron
|
|
68
68
|
def determine_charset(header_data, body)
|
69
69
|
header_data.match(charset_regex) || (body && body.match(charset_regex))
|
70
70
|
|
71
|
-
$1
|
71
|
+
charset = $1
|
72
|
+
validate_charset(charset) ? charset : nil
|
72
73
|
end
|
73
74
|
|
74
75
|
def charset_regex
|
75
76
|
/(?:charset|encoding)="?([a-z0-9-]+)"?/i
|
76
77
|
end
|
77
78
|
|
79
|
+
def validate_charset(charset)
|
80
|
+
charset && Encoding.find(charset) && true
|
81
|
+
rescue ArgumentError
|
82
|
+
false
|
83
|
+
end
|
84
|
+
|
78
85
|
def convert_to_default_encoding!(str)
|
79
86
|
if str.respond_to?(:encode) && Encoding.default_internal
|
80
87
|
str.force_encoding(charset).encode!(Encoding.default_internal)
|
data/lib/patron/session.rb
CHANGED
@@ -215,7 +215,19 @@ module Patron
|
|
215
215
|
req = build_request(action, url, headers, options)
|
216
216
|
handle_request(req)
|
217
217
|
end
|
218
|
-
|
218
|
+
|
219
|
+
# Returns the class that will be used to build a Response
|
220
|
+
# from a Curl call.
|
221
|
+
#
|
222
|
+
# Primarily useful if you need a very lightweight Response
|
223
|
+
# object that does not have to perform all the parsing of
|
224
|
+
# various headers/status codes. The method must return
|
225
|
+
# a module that supports the same interface for +new+
|
226
|
+
# as ++Patron::Response++
|
227
|
+
def response_class
|
228
|
+
::Patron::Response
|
229
|
+
end
|
230
|
+
|
219
231
|
# Build a request object that can be used in +handle_request+
|
220
232
|
def build_request(action, url, headers, options = {})
|
221
233
|
# If the Expect header isn't set uploads are really slow
|
data/lib/patron/version.rb
CHANGED
data/spec/response_spec.rb
CHANGED
@@ -59,4 +59,30 @@ describe Patron::Response do
|
|
59
59
|
it "should be able to serialize and deserialize itself" do
|
60
60
|
expect(Marshal.load(Marshal.dump(@request))).to eql(@request)
|
61
61
|
end
|
62
|
+
|
63
|
+
it "should encode body in the internal charset" do
|
64
|
+
allow(Encoding).to receive(:default_internal).and_return("UTF-8")
|
65
|
+
|
66
|
+
greek_encoding = Encoding.find("ISO-8859-7")
|
67
|
+
utf_encoding = Encoding.find("UTF-8")
|
68
|
+
|
69
|
+
headers = "HTTP/1.1 200 OK \r\nContent-Type: text/css\r\n"
|
70
|
+
body = "charset=ISO-8859-7 Ππ".encode(greek_encoding) # Greek alphabet
|
71
|
+
|
72
|
+
response = Patron::Response.new("url", "status", 0, headers, body, nil)
|
73
|
+
|
74
|
+
expect(response.body.encoding).to eql(utf_encoding)
|
75
|
+
end
|
76
|
+
|
77
|
+
it "should fallback to default charset when header or body charset is not valid" do
|
78
|
+
allow(Encoding).to receive(:default_internal).and_return("UTF-8")
|
79
|
+
|
80
|
+
encoding = Encoding.find("UTF-8")
|
81
|
+
headers = "HTTP/1.1 200 OK \r\nContent-Type: text/css\r\n"
|
82
|
+
body = "charset=invalid"
|
83
|
+
|
84
|
+
response = Patron::Response.new("url", "status", 0, headers, body, "UTF-8")
|
85
|
+
|
86
|
+
expect(response.body.encoding).to eql(encoding)
|
87
|
+
end
|
62
88
|
end
|
data/spec/session_spec.rb
CHANGED
@@ -168,7 +168,14 @@ describe Patron::Session do
|
|
168
168
|
expect(body.request_method).to be == "GET"
|
169
169
|
expect(body.header['content-length']).to be == [data.size.to_s]
|
170
170
|
end
|
171
|
-
|
171
|
+
|
172
|
+
it "should call to_s on the data being uploaded via GET if it is not already a String" do
|
173
|
+
data = 12345
|
174
|
+
response = @session.request(:get, "/test", {}, :data => data)
|
175
|
+
body = YAML::load(response.body)
|
176
|
+
expect(body.request_method).to be == "GET"
|
177
|
+
end
|
178
|
+
|
172
179
|
it "should upload data with :put" do
|
173
180
|
data = "upload data"
|
174
181
|
response = @session.put("/test", data)
|
@@ -204,7 +211,14 @@ describe Patron::Session do
|
|
204
211
|
body = YAML::load(response.body)
|
205
212
|
expect(body.header['transfer-encoding'].first).to be == "chunked"
|
206
213
|
end
|
207
|
-
|
214
|
+
|
215
|
+
it "should call to_s on the data being uploaded via POST if it is not already a String" do
|
216
|
+
data = 12345
|
217
|
+
response = @session.post("/testpost", data)
|
218
|
+
body = YAML::load(response.body)
|
219
|
+
expect(body['body']).to eq("12345")
|
220
|
+
end
|
221
|
+
|
208
222
|
it "should upload data with :post" do
|
209
223
|
data = "upload data"
|
210
224
|
response = @session.post("/test", data)
|
@@ -333,6 +347,31 @@ describe Patron::Session do
|
|
333
347
|
"Basic " + Base64.encode64("#{user}:#{passwd}").strip
|
334
348
|
end
|
335
349
|
|
350
|
+
describe 'when using a subclass with a custom Response' do
|
351
|
+
|
352
|
+
class CustomResponse
|
353
|
+
attr_reader :constructor_args
|
354
|
+
def initialize(*constructor_args)
|
355
|
+
@constructor_args = constructor_args
|
356
|
+
end
|
357
|
+
end
|
358
|
+
|
359
|
+
class CustomizedSession < Patron::Session
|
360
|
+
def response_class
|
361
|
+
CustomResponse
|
362
|
+
end
|
363
|
+
end
|
364
|
+
|
365
|
+
it 'instantiates the customized response object' do
|
366
|
+
@session = CustomizedSession.new
|
367
|
+
@session.base_url = "http://localhost:9001"
|
368
|
+
response = @session.request(:get, "/test", {}, :query => {:foo => "bar"})
|
369
|
+
|
370
|
+
expect(response).to be_kind_of(CustomResponse)
|
371
|
+
expect(response.constructor_args.length).to eq(6)
|
372
|
+
end
|
373
|
+
end
|
374
|
+
|
336
375
|
describe 'when instantiating with hash arguments' do
|
337
376
|
|
338
377
|
let(:args) { {
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: patron
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Phillip Toland
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-03-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -130,7 +130,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
130
130
|
version: 1.2.0
|
131
131
|
requirements: []
|
132
132
|
rubyforge_project: patron
|
133
|
-
rubygems_version: 2.
|
133
|
+
rubygems_version: 2.5.1
|
134
134
|
signing_key:
|
135
135
|
specification_version: 4
|
136
136
|
summary: Patron HTTP Client
|