jruby-httpclient 0.3.0-java → 0.4.0-java
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.
- data/VERSION +1 -1
- data/jruby-httpclient.gemspec +7 -4
- data/lib/http_client.rb +2 -1
- data/lib/http_client/client.rb +12 -31
- data/lib/http_client/client_configuration.rb +136 -0
- data/lib/http_client/methods.rb +65 -70
- data/lib/http_client/response.rb +93 -0
- data/test/http_client/test_basic_auth.rb +1 -1
- data/test/http_client/test_client_configuration.rb +142 -0
- data/test/http_client/test_client_headers.rb +7 -7
- data/test/http_client/test_cookies.rb +7 -0
- data/test/http_client/test_request_entity.rb +4 -4
- data/test/http_client/test_response.rb +24 -0
- data/test/http_client/test_server_headers.rb +18 -0
- data/test/http_test_server.rb +7 -0
- metadata +7 -4
- data/lib/http_client/parameters.rb +0 -121
- data/test/http_client/test_client_parameters.rb +0 -201
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.4.0
|
data/jruby-httpclient.gemspec
CHANGED
@@ -5,12 +5,12 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{jruby-httpclient}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.4.0"
|
9
9
|
s.platform = Gem::Platform.new([nil, "java", nil])
|
10
10
|
|
11
11
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
12
12
|
s.authors = ["Adam Esterline"]
|
13
|
-
s.date = %q{2011-
|
13
|
+
s.date = %q{2011-07-01}
|
14
14
|
s.description = %q{An HTTP client designed for use with JRuby in a threaded environment}
|
15
15
|
s.email = %q{adam@esterlines.com}
|
16
16
|
s.extra_rdoc_files = [
|
@@ -28,16 +28,19 @@ Gem::Specification.new do |s|
|
|
28
28
|
"jruby-httpclient.gemspec",
|
29
29
|
"lib/http_client.rb",
|
30
30
|
"lib/http_client/client.rb",
|
31
|
+
"lib/http_client/client_configuration.rb",
|
31
32
|
"lib/http_client/methods.rb",
|
32
|
-
"lib/http_client/
|
33
|
+
"lib/http_client/response.rb",
|
33
34
|
"test/helper.rb",
|
34
35
|
"test/http_client/test_basic_auth.rb",
|
35
36
|
"test/http_client/test_basic_client_operations.rb",
|
37
|
+
"test/http_client/test_client_configuration.rb",
|
36
38
|
"test/http_client/test_client_headers.rb",
|
37
|
-
"test/http_client/test_client_parameters.rb",
|
38
39
|
"test/http_client/test_cookies.rb",
|
39
40
|
"test/http_client/test_redirect.rb",
|
40
41
|
"test/http_client/test_request_entity.rb",
|
42
|
+
"test/http_client/test_response.rb",
|
43
|
+
"test/http_client/test_server_headers.rb",
|
41
44
|
"test/http_test_server.rb",
|
42
45
|
"vendor/commons-codec-1.4.jar",
|
43
46
|
"vendor/commons-logging-1.1.1.jar",
|
data/lib/http_client.rb
CHANGED
data/lib/http_client/client.rb
CHANGED
@@ -5,53 +5,34 @@ module HTTP
|
|
5
5
|
SocketTimeoutException = java.net.SocketTimeoutException
|
6
6
|
|
7
7
|
class Client
|
8
|
-
DEFAULT_TIMEOUT = 30_000
|
9
|
-
include HTTP::Parameters
|
10
|
-
|
11
8
|
def initialize(options = {})
|
12
|
-
|
13
|
-
|
14
|
-
if options[:disable_response_handler]
|
15
|
-
@response_handler = nil
|
16
|
-
elsif options[:response_handler]
|
17
|
-
@response_handler = options[:response_handler]
|
18
|
-
else
|
19
|
-
@response_handler = BasicResponseHandler.new
|
20
|
-
end
|
21
|
-
|
22
|
-
@encoding = options[:encoding] || "UTF-8"
|
23
|
-
|
24
|
-
# Set options from the rest of the options-hash
|
25
|
-
options.each do |parameter_name, parameter_value|
|
26
|
-
setter_name = "#{parameter_name}="
|
27
|
-
self.send(setter_name, parameter_value) if self.respond_to?(setter_name)
|
28
|
-
end
|
29
|
-
|
30
|
-
@client = DefaultHttpClient.new(params)
|
9
|
+
@client = HTTP::ClientConfiguration.new(options).build_http_client
|
31
10
|
end
|
32
11
|
|
33
|
-
# Request Methods
|
34
|
-
|
35
12
|
def get(params, options = {})
|
36
|
-
|
13
|
+
read_response(Get.new(params, options))
|
37
14
|
end
|
38
15
|
|
39
16
|
def post(params, options = {})
|
40
|
-
|
17
|
+
read_response(Post.new(params, options))
|
41
18
|
end
|
42
19
|
|
43
20
|
def delete(path)
|
44
|
-
|
21
|
+
read_response(Delete.new(path))
|
45
22
|
end
|
46
23
|
|
47
24
|
def put(path)
|
48
|
-
|
25
|
+
read_response(Put.new(path))
|
26
|
+
end
|
27
|
+
|
28
|
+
def read_response(request)
|
29
|
+
execute(request).body
|
49
30
|
end
|
50
31
|
|
51
32
|
def execute(request)
|
52
|
-
request.make_native_request(@client
|
53
|
-
rescue SocketTimeoutException
|
54
|
-
raise Timeout::Error,
|
33
|
+
request.make_native_request(@client)
|
34
|
+
rescue SocketTimeoutException => e
|
35
|
+
raise Timeout::Error, e.message
|
55
36
|
end
|
56
37
|
|
57
38
|
def shutdown
|
@@ -0,0 +1,136 @@
|
|
1
|
+
module HTTP
|
2
|
+
BasicHttpParams = org.apache.http.params.BasicHttpParams
|
3
|
+
HttpHost = org.apache.http.HttpHost
|
4
|
+
CoreProtocolPNames = org.apache.http.params.CoreProtocolPNames
|
5
|
+
CoreConnectionPNames = org.apache.http.params.CoreConnectionPNames
|
6
|
+
ConnRoutePNames = org.apache.http.conn.params.ConnRoutePNames
|
7
|
+
CookieSpecPNames = org.apache.http.cookie.params.CookieSpecPNames
|
8
|
+
AuthPNames = org.apache.http.auth.params.AuthPNames
|
9
|
+
ClientPNames = org.apache.http.client.params.ClientPNames
|
10
|
+
CookiePolicy = org.apache.http.client.params.CookiePolicy
|
11
|
+
|
12
|
+
CLIENT_CONFIGURATION_PARAMETERS = {
|
13
|
+
:protocol_version => HTTP::CoreProtocolPNames::PROTOCOL_VERSION,
|
14
|
+
:strict_transfer_encoding => HTTP::CoreProtocolPNames::STRICT_TRANSFER_ENCODING,
|
15
|
+
:http_element_charset => HTTP::CoreProtocolPNames::HTTP_ELEMENT_CHARSET,
|
16
|
+
:use_expect_continue => HTTP::CoreProtocolPNames::USE_EXPECT_CONTINUE,
|
17
|
+
:wait_for_continue => HTTP::CoreProtocolPNames::WAIT_FOR_CONTINUE,
|
18
|
+
:user_agent => HTTP::CoreProtocolPNames::USER_AGENT,
|
19
|
+
:tcp_nodelay => HTTP::CoreConnectionPNames.TCP_NODELAY,
|
20
|
+
:so_timeout => HTTP::CoreConnectionPNames.SO_TIMEOUT,
|
21
|
+
:so_linger => HTTP::CoreConnectionPNames.SO_LINGER,
|
22
|
+
:so_reuseaddr => HTTP::CoreConnectionPNames.SO_REUSEADDR,
|
23
|
+
:socket_buffer_size => HTTP::CoreConnectionPNames.SOCKET_BUFFER_SIZE,
|
24
|
+
:connection_timeout => HTTP::CoreConnectionPNames.CONNECTION_TIMEOUT,
|
25
|
+
:max_line_length => HTTP::CoreConnectionPNames.MAX_LINE_LENGTH,
|
26
|
+
:max_header_count => HTTP::CoreConnectionPNames.MAX_HEADER_COUNT,
|
27
|
+
:stale_connection_check => HTTP::CoreConnectionPNames.STALE_CONNECTION_CHECK,
|
28
|
+
# :forced_route => HTTP::ConnRoutePNames::FORCED_ROUTE, # not implemented
|
29
|
+
:local_address => HTTP::ConnRoutePNames::LOCAL_ADDRESS,
|
30
|
+
:default_proxy => HTTP::ConnRoutePNames::DEFAULT_PROXY,
|
31
|
+
:date_patterns => HTTP::CookieSpecPNames::DATE_PATTERNS,
|
32
|
+
:single_cookie_header => HTTP::CookieSpecPNames::SINGLE_COOKIE_HEADER,
|
33
|
+
:credential_charset => HTTP::AuthPNames::CREDENTIAL_CHARSET,
|
34
|
+
:cookie_policy => HTTP::ClientPNames::COOKIE_POLICY,
|
35
|
+
:handle_authentication => HTTP::ClientPNames::HANDLE_AUTHENTICATION,
|
36
|
+
:handle_redirects => HTTP::ClientPNames::HANDLE_REDIRECTS,
|
37
|
+
:max_redirects => HTTP::ClientPNames::MAX_REDIRECTS,
|
38
|
+
:allow_circular_redirects => HTTP::ClientPNames::ALLOW_CIRCULAR_REDIRECTS,
|
39
|
+
:virtual_host => HTTP::ClientPNames::VIRTUAL_HOST,
|
40
|
+
:default_host => HTTP::ClientPNames::DEFAULT_HOST
|
41
|
+
# :default_headers => HTTP::ClientPNames::DEFAULT_HEADERS, # not implemented
|
42
|
+
# :connection_manager_factory_class_name => HTTP::ClientPNames::CONNECTION_MANAGER_FACTORY_CLASS_NAME # not implemented
|
43
|
+
}
|
44
|
+
|
45
|
+
class ClientConfiguration
|
46
|
+
DEFAULT_TIMEOUT = 30_000
|
47
|
+
|
48
|
+
CLIENT_CONFIGURATION_PARAMETERS.each do |method_name, param_class|
|
49
|
+
define_method(method_name) do
|
50
|
+
params.get_parameter param_class
|
51
|
+
end
|
52
|
+
|
53
|
+
define_method("#{method_name}=") do |arg|
|
54
|
+
arg.add_to_params(params, CLIENT_CONFIGURATION_PARAMETERS[method_name])
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def initialize(options = {})
|
59
|
+
self.so_timeout = DEFAULT_TIMEOUT
|
60
|
+
|
61
|
+
options.each do |parameter_name, parameter_value|
|
62
|
+
setter_name = "#{parameter_name}="
|
63
|
+
self.send(setter_name, parameter_value) if self.respond_to?(setter_name)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def build_http_client
|
68
|
+
DefaultHttpClient.new(params)
|
69
|
+
end
|
70
|
+
|
71
|
+
def protocol_version=(version_string)
|
72
|
+
protocol, major_version, minor_version = version_string.split(/[\.|\s|\/]/)
|
73
|
+
params.set_parameter HTTP::CoreProtocolPNames::PROTOCOL_VERSION, org.apache.http.ProtocolVersion.new(protocol, major_version.to_i, minor_version.to_i)
|
74
|
+
end
|
75
|
+
|
76
|
+
def local_address=(local_addr_str)
|
77
|
+
params.set_parameter HTTP::ConnRoutePNames::LOCAL_ADDRESS, java.net.InetAddress.getByName(local_addr_str)
|
78
|
+
end
|
79
|
+
|
80
|
+
def default_proxy=(host)
|
81
|
+
set_host_parameter(HTTP::ConnRoutePNames::DEFAULT_PROXY, host)
|
82
|
+
end
|
83
|
+
|
84
|
+
def virtual_host=(host)
|
85
|
+
set_host_parameter(HTTP::ClientPNames::VIRTUAL_HOST, host)
|
86
|
+
end
|
87
|
+
|
88
|
+
def default_host=(host)
|
89
|
+
set_host_parameter(HTTP::ClientPNames::DEFAULT_HOST, host)
|
90
|
+
end
|
91
|
+
|
92
|
+
def timeout_in_seconds=(timeout)
|
93
|
+
self.so_timeout = timeout * 1000
|
94
|
+
end
|
95
|
+
|
96
|
+
private
|
97
|
+
def set_host_parameter(parameter_name, host)
|
98
|
+
uri = URI.parse host
|
99
|
+
params.set_parameter(parameter_name, HTTP::HttpHost.new(uri.host, uri.port, uri.scheme))
|
100
|
+
end
|
101
|
+
|
102
|
+
def params
|
103
|
+
@params ||= default_params
|
104
|
+
end
|
105
|
+
|
106
|
+
def default_params
|
107
|
+
params = BasicHttpParams.new
|
108
|
+
DefaultHttpClient.set_default_http_params(params)
|
109
|
+
params
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
class Integer
|
115
|
+
def add_to_params(params, param_name)
|
116
|
+
params.set_int_parameter(param_name, self)
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
class TrueClass
|
121
|
+
def add_to_params(params, param_name)
|
122
|
+
params.set_boolean_parameter(param_name, self)
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
class FalseClass
|
127
|
+
def add_to_params(params, param_name)
|
128
|
+
params.set_boolean_parameter(param_name, self)
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
class Object
|
133
|
+
def add_to_params(params, param_name)
|
134
|
+
params.set_parameter(param_name, self)
|
135
|
+
end
|
136
|
+
end
|
data/lib/http_client/methods.rb
CHANGED
@@ -2,86 +2,81 @@ require 'cgi'
|
|
2
2
|
|
3
3
|
module HTTP
|
4
4
|
class Request
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
if handler
|
39
|
-
client.execute(request, handler)
|
40
|
-
else
|
41
|
-
client.execute(request)
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
def parse_uri
|
46
|
-
uri = URI.parse(@uri)
|
47
|
-
[uri.scheme, uri.host, uri.port, uri.path, uri.query]
|
48
|
-
rescue URI::InvalidURIError
|
49
|
-
[nil, nil, nil, @uri, nil]
|
50
|
-
end
|
51
|
-
|
52
|
-
private
|
53
|
-
define_method(:create_native_request) do |encoding|
|
54
|
-
scheme, host, port, path, query = parse_uri
|
55
|
-
query_params = CGI.parse(query || "").merge(@params)
|
56
|
-
|
57
|
-
params = query_params.collect { |key, value| BasicNameValuePair.new(key.to_s, value.to_s) }
|
58
|
-
request = native_request_factory.call(scheme, host, port, path, params, encoding)
|
59
|
-
|
60
|
-
@headers.each { |name, value| request.add_header(name.to_s, value.to_s) }
|
61
|
-
|
62
|
-
request
|
63
|
-
end
|
5
|
+
attr_accessor :body, :encoding
|
6
|
+
|
7
|
+
def initialize(uri, params = {})
|
8
|
+
@scheme, @host, @port, @path, query = parse_uri(uri)
|
9
|
+
combined_params = CGI.parse(query || "").merge(params)
|
10
|
+
@query_params = combined_params.collect { |key, value| BasicNameValuePair.new(key.to_s, value.to_s) }
|
11
|
+
|
12
|
+
@encoding = "UTF-8"
|
13
|
+
@headers = {}
|
14
|
+
end
|
15
|
+
|
16
|
+
def add_headers(headers)
|
17
|
+
@headers.merge!(headers)
|
18
|
+
end
|
19
|
+
|
20
|
+
def content_type=(type)
|
21
|
+
add_headers({'content-type' => type})
|
22
|
+
end
|
23
|
+
|
24
|
+
def basic_auth(username, password)
|
25
|
+
@username = username
|
26
|
+
@password = password
|
27
|
+
end
|
28
|
+
|
29
|
+
def make_native_request(client)
|
30
|
+
request = create_native_request
|
31
|
+
|
32
|
+
request.entity = StringEntity.new(@body) unless @body.nil?
|
33
|
+
@headers.each { |name, value| request.add_header(name.to_s, value.to_s) }
|
34
|
+
|
35
|
+
unless @username.nil?
|
36
|
+
client.credentials_provider.set_credentials(AuthScope::ANY, UsernamePasswordCredentials.new(@username, @password))
|
64
37
|
end
|
38
|
+
|
39
|
+
HTTP::Response.new(client.execute(request), client.cookie_store.cookies)
|
40
|
+
end
|
41
|
+
|
42
|
+
private
|
43
|
+
def create_uri(query_string = nil)
|
44
|
+
URIUtils.create_uri(@scheme, @host, @port, @path, query_string, nil)
|
45
|
+
end
|
46
|
+
|
47
|
+
def parse_uri(uri)
|
48
|
+
uri = URI.parse(uri)
|
49
|
+
[uri.scheme, uri.host, uri.port, uri.path, uri.query]
|
50
|
+
rescue URI::InvalidURIError
|
51
|
+
[nil, nil, nil, uri, nil]
|
65
52
|
end
|
66
53
|
end
|
67
54
|
|
68
|
-
Post
|
69
|
-
|
70
|
-
|
71
|
-
|
55
|
+
class Post < Request
|
56
|
+
def create_native_request
|
57
|
+
post = HttpPost.new(create_uri)
|
58
|
+
post.entity = UrlEncodedFormEntity.new(@query_params, encoding)
|
59
|
+
post
|
60
|
+
end
|
72
61
|
end
|
73
62
|
|
74
|
-
Get
|
75
|
-
|
76
|
-
|
63
|
+
class Get < Request
|
64
|
+
def create_native_request
|
65
|
+
query_string = URLEncodedUtils.format(@query_params, encoding)
|
66
|
+
HttpGet.new(create_uri(query_string))
|
67
|
+
end
|
77
68
|
end
|
78
69
|
|
79
|
-
Delete
|
80
|
-
|
70
|
+
class Delete < Request
|
71
|
+
def create_native_request
|
72
|
+
HttpDelete.new(create_uri)
|
73
|
+
end
|
81
74
|
end
|
82
75
|
|
83
|
-
Put
|
84
|
-
|
76
|
+
class Put < Request
|
77
|
+
def create_native_request
|
78
|
+
HttpPut.new(create_uri)
|
79
|
+
end
|
85
80
|
end
|
86
81
|
|
87
82
|
private
|
@@ -0,0 +1,93 @@
|
|
1
|
+
module HTTP
|
2
|
+
class Response
|
3
|
+
attr_reader :headers, :cookies
|
4
|
+
|
5
|
+
def initialize(native_response, cookies)
|
6
|
+
@native_response = native_response
|
7
|
+
@headers = Headers.new(native_response)
|
8
|
+
@cookies = Cookies.new(cookies)
|
9
|
+
end
|
10
|
+
|
11
|
+
def body
|
12
|
+
@body ||= EntityUtils.to_string(@native_response.entity)
|
13
|
+
end
|
14
|
+
|
15
|
+
def status_code
|
16
|
+
@native_response.status_line.status_code
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
module Status
|
21
|
+
ACCEPTED = 202
|
22
|
+
BAD_GATEWAY = 502
|
23
|
+
BAD_REQUEST = 400
|
24
|
+
CONFLICT = 409
|
25
|
+
CONTINUE = 100
|
26
|
+
CREATED = 201
|
27
|
+
EXPECTATION_FAILED = 417
|
28
|
+
FAILED_DEPENDENCY = 424
|
29
|
+
FORBIDDEN = 403
|
30
|
+
GATEWAY_TIMEOUT = 504
|
31
|
+
GONE = 410
|
32
|
+
HTTP_VERSION_NOT_SUPPORTED = 505
|
33
|
+
INSUFFICIENT_SPACE_ON_RESOURCE = 419
|
34
|
+
INSUFFICIENT_STORAGE = 507
|
35
|
+
INTERNAL_SERVER_ERROR = 500
|
36
|
+
LENGTH_REQUIRED = 411
|
37
|
+
LOCKED = 423
|
38
|
+
METHOD_FAILURE = 420
|
39
|
+
METHOD_NOT_ALLOWED = 405
|
40
|
+
MOVED_PERMANENTLY = 301
|
41
|
+
MOVED_TEMPORARILY = 302
|
42
|
+
MULTI_STATUS = 207
|
43
|
+
MULTIPLE_CHOICES = 300
|
44
|
+
NO_CONTENT = 204
|
45
|
+
NON_AUTHORITATIVE_INFORMATION = 203
|
46
|
+
NOT_ACCEPTABLE = 406
|
47
|
+
NOT_FOUND = 404
|
48
|
+
NOT_IMPLEMENTED = 501
|
49
|
+
NOT_MODIFIED = 304
|
50
|
+
OK = 200
|
51
|
+
PARTIAL_CONTENT = 206
|
52
|
+
PAYMENT_REQUIRED = 402
|
53
|
+
PRECONDITION_FAILED = 412
|
54
|
+
PROCESSING = 102
|
55
|
+
PROXY_AUTHENTICATION_REQUIRED = 407
|
56
|
+
REQUEST_TIMEOUT = 408
|
57
|
+
REQUEST_TOO_LONG = 413
|
58
|
+
REQUEST_URI_TOO_LONG = 414
|
59
|
+
REQUESTED_RANGE_NOT_SATISFIABLE = 416
|
60
|
+
RESET_CONTENT = 205
|
61
|
+
SEE_OTHER = 303
|
62
|
+
SERVICE_UNAVAILABLE = 503
|
63
|
+
SWITCHING_PROTOCOLS = 101
|
64
|
+
TEMPORARY_REDIRECT = 307
|
65
|
+
UNAUTHORIZED = 401
|
66
|
+
UNPROCESSABLE_ENTITY = 422
|
67
|
+
UNSUPPORTED_MEDIA_TYPE = 415
|
68
|
+
USE_PROXY = 305
|
69
|
+
end
|
70
|
+
|
71
|
+
private
|
72
|
+
EntityUtils = org.apache.http.util.EntityUtils
|
73
|
+
|
74
|
+
class Headers
|
75
|
+
def initialize(native_response)
|
76
|
+
@native_response = native_response
|
77
|
+
end
|
78
|
+
|
79
|
+
def [](header_name)
|
80
|
+
@native_response.get_first_header(header_name).value
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
class Cookies
|
85
|
+
def initialize(cookies)
|
86
|
+
@cookies = cookies
|
87
|
+
end
|
88
|
+
|
89
|
+
def [](cookie_name)
|
90
|
+
@cookies.find {|cookie| cookie.name == cookie_name }.value
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
@@ -0,0 +1,142 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TestClientConfiguration < Test::Unit::TestCase
|
4
|
+
def test_timeout
|
5
|
+
client = HTTP::Client.new(:default_host => "http://localhost:8080", :timeout_in_seconds => 2)
|
6
|
+
|
7
|
+
assert_raises(Timeout::Error) do
|
8
|
+
client.get("/slow", :sleep => "30")
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_protocol_version
|
13
|
+
config = HTTP::ClientConfiguration.new(:protocol_version => "HTTP 1.0")
|
14
|
+
assert_equal(config.protocol_version.to_s, "HTTP/1.0")
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_strict_transfer_encodeing
|
18
|
+
config = HTTP::ClientConfiguration.new(:strict_transfer_encoding => true)
|
19
|
+
assert_equal(config.strict_transfer_encoding, true)
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_http_element_charset
|
23
|
+
config = HTTP::ClientConfiguration.new(:http_element_charset => "ASCII")
|
24
|
+
assert_equal(config.http_element_charset, "ASCII")
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_use_expect_continue
|
28
|
+
config = HTTP::ClientConfiguration.new(:use_expect_continue => true)
|
29
|
+
assert_equal(config.use_expect_continue, true)
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_wait_for_continue
|
33
|
+
config = HTTP::ClientConfiguration.new(:wait_for_continue => true)
|
34
|
+
assert_equal(config.wait_for_continue, true)
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_user_agent
|
38
|
+
user_agent_name = "Mozilla/5.0 (Macintosh; U; PPC Mac OS X 10.5; en-US; rv:1.9.0.3) Gecko/2008092414 Firefox/3.0.3"
|
39
|
+
config = HTTP::ClientConfiguration.new(:user_agent => user_agent_name)
|
40
|
+
assert_equal(config.user_agent, user_agent_name)
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_tcp_nodelay
|
44
|
+
config = HTTP::ClientConfiguration.new(:tcp_nodelay => true)
|
45
|
+
assert_equal(config.tcp_nodelay, true)
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_so_linger
|
49
|
+
config = HTTP::ClientConfiguration.new(:so_linger => 3000)
|
50
|
+
assert_equal(config.so_linger, 3000)
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_so_reuseaddr
|
54
|
+
config = HTTP::ClientConfiguration.new(:so_reuseaddr => true)
|
55
|
+
assert_equal(config.so_reuseaddr, true)
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_socket_buffer_size
|
59
|
+
config = HTTP::ClientConfiguration.new(:socket_buffer_size => 10000)
|
60
|
+
assert_equal(config.socket_buffer_size, 10000)
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_connection_timeout
|
64
|
+
config = HTTP::ClientConfiguration.new(:connection_timeout => 2)
|
65
|
+
assert_equal(config.connection_timeout, 2)
|
66
|
+
end
|
67
|
+
|
68
|
+
def test_max_line_length
|
69
|
+
config = HTTP::ClientConfiguration.new(:max_line_length => 2)
|
70
|
+
assert_equal(config.max_line_length, 2)
|
71
|
+
end
|
72
|
+
|
73
|
+
def test_max_header_count
|
74
|
+
config = HTTP::ClientConfiguration.new(:max_header_count => 10)
|
75
|
+
assert_equal(config.max_header_count, 10)
|
76
|
+
end
|
77
|
+
|
78
|
+
def test_stale_connection_check
|
79
|
+
config = HTTP::ClientConfiguration.new(:stale_connection_check => true)
|
80
|
+
assert_equal(config.stale_connection_check, true)
|
81
|
+
end
|
82
|
+
|
83
|
+
def test_local_address
|
84
|
+
config = HTTP::ClientConfiguration.new(:local_address => "127.0.0.1")
|
85
|
+
assert_equal(config.local_address.get_host_address, "127.0.0.1")
|
86
|
+
end
|
87
|
+
|
88
|
+
def test_default_proxy
|
89
|
+
config = HTTP::ClientConfiguration.new(:default_proxy => "http://127.0.0.1:8080")
|
90
|
+
assert_equal(config.default_proxy.to_s, "http://127.0.0.1:8080")
|
91
|
+
end
|
92
|
+
|
93
|
+
def test_date_patterns
|
94
|
+
config = HTTP::ClientConfiguration.new(:date_patterns => ["MDy"])
|
95
|
+
assert(config.date_patterns.include? 'MDy')
|
96
|
+
end
|
97
|
+
|
98
|
+
def test_single_cookie_header
|
99
|
+
config = HTTP::ClientConfiguration.new(:single_cookie_header => true)
|
100
|
+
assert_equal(config.single_cookie_header, true)
|
101
|
+
end
|
102
|
+
|
103
|
+
def test_credential_charset
|
104
|
+
config = HTTP::ClientConfiguration.new(:credential_charset => "ASCII")
|
105
|
+
assert_equal(config.credential_charset, "ASCII")
|
106
|
+
end
|
107
|
+
|
108
|
+
def test_cookie_policy
|
109
|
+
config = HTTP::ClientConfiguration.new(:cookie_policy => "RFC2109")
|
110
|
+
assert_equal(config.cookie_policy, "RFC2109")
|
111
|
+
end
|
112
|
+
|
113
|
+
def test_handle_authentication
|
114
|
+
config = HTTP::ClientConfiguration.new(:handle_authentication => true)
|
115
|
+
assert_equal(config.handle_authentication, true)
|
116
|
+
end
|
117
|
+
|
118
|
+
def test_handle_redirects
|
119
|
+
config = HTTP::ClientConfiguration.new(:handle_redirects => true)
|
120
|
+
assert_equal(config.handle_redirects, true)
|
121
|
+
end
|
122
|
+
|
123
|
+
def test_max_redirects
|
124
|
+
config = HTTP::ClientConfiguration.new(:max_redirects => 1)
|
125
|
+
assert_equal(config.max_redirects, 1)
|
126
|
+
end
|
127
|
+
|
128
|
+
def test_allow_circular_redirects
|
129
|
+
config = HTTP::ClientConfiguration.new(:allow_circular_redirects => true)
|
130
|
+
assert_equal(config.allow_circular_redirects, true)
|
131
|
+
end
|
132
|
+
|
133
|
+
def test_virtual_host
|
134
|
+
config = HTTP::ClientConfiguration.new(:virtual_host => "http://127.0.0.1:80")
|
135
|
+
assert_equal(config.virtual_host.to_s, "http://127.0.0.1:80")
|
136
|
+
end
|
137
|
+
|
138
|
+
def test_default_host
|
139
|
+
config = HTTP::ClientConfiguration.new(:default_host => "http://localhost:8080")
|
140
|
+
assert_equal(config.default_host.to_s, "http://localhost:8080")
|
141
|
+
end
|
142
|
+
end
|
@@ -1,12 +1,12 @@
|
|
1
1
|
require 'helper'
|
2
2
|
|
3
|
-
class
|
3
|
+
class ClientHeadersTest < Test::Unit::TestCase
|
4
4
|
def test_get_headers
|
5
5
|
get = HTTP::Get.new("/echo_header")
|
6
6
|
get.add_headers(:test_header => "get testing")
|
7
7
|
|
8
8
|
response = @client.execute(get)
|
9
|
-
assert_equal("get testing", response)
|
9
|
+
assert_equal("get testing", response.body)
|
10
10
|
end
|
11
11
|
|
12
12
|
def test_post_headers
|
@@ -14,7 +14,7 @@ class TestClientHeaders < Test::Unit::TestCase
|
|
14
14
|
get.add_headers(:test_header => "post testing")
|
15
15
|
|
16
16
|
response = @client.execute(get)
|
17
|
-
assert_equal("post testing", response)
|
17
|
+
assert_equal("post testing", response.body)
|
18
18
|
end
|
19
19
|
|
20
20
|
def test_delete_headers
|
@@ -22,7 +22,7 @@ class TestClientHeaders < Test::Unit::TestCase
|
|
22
22
|
get.add_headers(:test_header => "post testing")
|
23
23
|
|
24
24
|
response = @client.execute(get)
|
25
|
-
assert_equal("post testing", response)
|
25
|
+
assert_equal("post testing", response.body)
|
26
26
|
end
|
27
27
|
|
28
28
|
def test_put_headers
|
@@ -30,7 +30,7 @@ class TestClientHeaders < Test::Unit::TestCase
|
|
30
30
|
get.add_headers(:test_header => "post testing")
|
31
31
|
|
32
32
|
response = @client.execute(get)
|
33
|
-
assert_equal("post testing", response)
|
33
|
+
assert_equal("post testing", response.body)
|
34
34
|
end
|
35
35
|
|
36
36
|
def test_multiple_calls_to_add_headers_should_prefer_last_set_of_headers
|
@@ -39,7 +39,7 @@ class TestClientHeaders < Test::Unit::TestCase
|
|
39
39
|
get.add_headers(:test_header => "should prefer this one")
|
40
40
|
|
41
41
|
response = @client.execute(get)
|
42
|
-
assert_equal("should prefer this one", response)
|
42
|
+
assert_equal("should prefer this one", response.body)
|
43
43
|
end
|
44
44
|
|
45
45
|
def test_should_be_able_to_add_content_type
|
@@ -47,7 +47,7 @@ class TestClientHeaders < Test::Unit::TestCase
|
|
47
47
|
get.content_type = 'text/xml'
|
48
48
|
|
49
49
|
response = @client.execute(get)
|
50
|
-
assert_equal('text/xml', response)
|
50
|
+
assert_equal('text/xml', response.body)
|
51
51
|
end
|
52
52
|
|
53
53
|
def setup
|
@@ -8,6 +8,13 @@ class CookieTest < Test::Unit::TestCase
|
|
8
8
|
assert_equal("nom nom nom", result)
|
9
9
|
end
|
10
10
|
|
11
|
+
def test_cookie_can_be_retrieved_from_response
|
12
|
+
get = HTTP::Get.new("/set_cookie", :cookie => "yum")
|
13
|
+
response = @client.execute(get)
|
14
|
+
|
15
|
+
assert_equal("yum", response.cookies["test_cookie"])
|
16
|
+
end
|
17
|
+
|
11
18
|
def setup
|
12
19
|
@client = HTTP::Client.new(:default_host => "http://localhost:8080", :cookie_policy => HTTP::CookiePolicy::BROWSER_COMPATIBILITY)
|
13
20
|
end
|
@@ -5,16 +5,16 @@ class RequestEntityTest < Test::Unit::TestCase
|
|
5
5
|
post = HTTP::Post.new("/body")
|
6
6
|
post.body = "Here we are"
|
7
7
|
|
8
|
-
|
9
|
-
assert_equal("Here we are",
|
8
|
+
response = @client.execute(post)
|
9
|
+
assert_equal("Here we are", response.body)
|
10
10
|
end
|
11
11
|
|
12
12
|
def test_put_request_body
|
13
13
|
post = HTTP::Put.new("/body")
|
14
14
|
post.body = "We are there"
|
15
15
|
|
16
|
-
|
17
|
-
assert_equal("We are there",
|
16
|
+
response = @client.execute(post)
|
17
|
+
assert_equal("We are there", response.body)
|
18
18
|
end
|
19
19
|
|
20
20
|
def setup
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class ResponseTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def test_response_body
|
6
|
+
get = HTTP::Get.new("/echo", :content => "baz")
|
7
|
+
response = @client.execute(get)
|
8
|
+
assert_equal("baz", response.body)
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_response_code
|
12
|
+
get = HTTP::Get.new("/echo", :content => "baz")
|
13
|
+
response = @client.execute(get)
|
14
|
+
assert_equal(HTTP::Status::OK, response.status_code)
|
15
|
+
end
|
16
|
+
|
17
|
+
def setup
|
18
|
+
@client = HTTP::Client.new(:default_host => "http://localhost:8080")
|
19
|
+
end
|
20
|
+
|
21
|
+
def teardown
|
22
|
+
@client.shutdown
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class ServerHeadersTest < Test::Unit::TestCase
|
4
|
+
def test_response_contains_server_headers
|
5
|
+
get = HTTP::Get.new("/set_header")
|
6
|
+
response = @client.execute(get)
|
7
|
+
|
8
|
+
assert_equal("FooBar", response.headers["Test-Header"])
|
9
|
+
end
|
10
|
+
|
11
|
+
def setup
|
12
|
+
@client = HTTP::Client.new(:default_host => "http://localhost:8080")
|
13
|
+
end
|
14
|
+
|
15
|
+
def teardown
|
16
|
+
@client.shutdown
|
17
|
+
end
|
18
|
+
end
|
data/test/http_test_server.rb
CHANGED
@@ -11,9 +11,16 @@ module HTTP
|
|
11
11
|
SERVER.mount('/redirect', RedirectServlet)
|
12
12
|
SERVER.mount('/set_cookie', SetCookieServlet)
|
13
13
|
SERVER.mount('/echo_cookie', EchoCookieServlet)
|
14
|
+
SERVER.mount('/set_header', SetHeaderServlet)
|
14
15
|
Thread.new { SERVER.start }
|
15
16
|
end
|
16
17
|
|
18
|
+
class SetHeaderServlet < WEBrick::HTTPServlet::AbstractServlet
|
19
|
+
def do_GET(request, response)
|
20
|
+
response["Test-Header"] = "FooBar"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
17
24
|
class SetCookieServlet < WEBrick::HTTPServlet::AbstractServlet
|
18
25
|
def do_GET(request, response)
|
19
26
|
response.cookies << WEBrick::Cookie.new("test_cookie", request.query['cookie'])
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: jruby-httpclient
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.
|
5
|
+
version: 0.4.0
|
6
6
|
platform: java
|
7
7
|
authors:
|
8
8
|
- Adam Esterline
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-
|
13
|
+
date: 2011-07-01 00:00:00 -06:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
@@ -66,16 +66,19 @@ files:
|
|
66
66
|
- jruby-httpclient.gemspec
|
67
67
|
- lib/http_client.rb
|
68
68
|
- lib/http_client/client.rb
|
69
|
+
- lib/http_client/client_configuration.rb
|
69
70
|
- lib/http_client/methods.rb
|
70
|
-
- lib/http_client/
|
71
|
+
- lib/http_client/response.rb
|
71
72
|
- test/helper.rb
|
72
73
|
- test/http_client/test_basic_auth.rb
|
73
74
|
- test/http_client/test_basic_client_operations.rb
|
75
|
+
- test/http_client/test_client_configuration.rb
|
74
76
|
- test/http_client/test_client_headers.rb
|
75
|
-
- test/http_client/test_client_parameters.rb
|
76
77
|
- test/http_client/test_cookies.rb
|
77
78
|
- test/http_client/test_redirect.rb
|
78
79
|
- test/http_client/test_request_entity.rb
|
80
|
+
- test/http_client/test_response.rb
|
81
|
+
- test/http_client/test_server_headers.rb
|
79
82
|
- test/http_test_server.rb
|
80
83
|
- vendor/commons-codec-1.4.jar
|
81
84
|
- vendor/commons-logging-1.1.1.jar
|
@@ -1,121 +0,0 @@
|
|
1
|
-
module HTTP
|
2
|
-
BasicHttpParams = org.apache.http.params.BasicHttpParams
|
3
|
-
HttpHost = org.apache.http.HttpHost
|
4
|
-
CoreProtocolPNames = org.apache.http.params.CoreProtocolPNames
|
5
|
-
CoreConnectionPNames = org.apache.http.params.CoreConnectionPNames
|
6
|
-
ConnRoutePNames = org.apache.http.conn.params.ConnRoutePNames
|
7
|
-
CookieSpecPNames = org.apache.http.cookie.params.CookieSpecPNames
|
8
|
-
AuthPNames = org.apache.http.auth.params.AuthPNames
|
9
|
-
ClientPNames = org.apache.http.client.params.ClientPNames
|
10
|
-
CookiePolicy = org.apache.http.client.params.CookiePolicy
|
11
|
-
|
12
|
-
module Parameters
|
13
|
-
CLIENT_PARAMETERS = {
|
14
|
-
:protocol_version => HTTP::CoreProtocolPNames::PROTOCOL_VERSION,
|
15
|
-
:strict_transfer_encoding => HTTP::CoreProtocolPNames::STRICT_TRANSFER_ENCODING,
|
16
|
-
:http_element_charset => HTTP::CoreProtocolPNames::HTTP_ELEMENT_CHARSET,
|
17
|
-
:use_expect_continue => HTTP::CoreProtocolPNames::USE_EXPECT_CONTINUE,
|
18
|
-
:wait_for_continue => HTTP::CoreProtocolPNames::WAIT_FOR_CONTINUE,
|
19
|
-
:user_agent => HTTP::CoreProtocolPNames::USER_AGENT,
|
20
|
-
:tcp_nodelay => HTTP::CoreConnectionPNames.TCP_NODELAY,
|
21
|
-
:so_timeout => HTTP::CoreConnectionPNames.SO_TIMEOUT,
|
22
|
-
:so_linger => HTTP::CoreConnectionPNames.SO_LINGER,
|
23
|
-
:so_reuseaddr => HTTP::CoreConnectionPNames.SO_REUSEADDR,
|
24
|
-
:socket_buffer_size => HTTP::CoreConnectionPNames.SOCKET_BUFFER_SIZE,
|
25
|
-
:connection_timeout => HTTP::CoreConnectionPNames.CONNECTION_TIMEOUT,
|
26
|
-
:max_line_length => HTTP::CoreConnectionPNames.MAX_LINE_LENGTH,
|
27
|
-
:max_header_count => HTTP::CoreConnectionPNames.MAX_HEADER_COUNT,
|
28
|
-
:stale_connection_check => HTTP::CoreConnectionPNames.STALE_CONNECTION_CHECK,
|
29
|
-
# :forced_route => HTTP::ConnRoutePNames::FORCED_ROUTE, # not implemented
|
30
|
-
:local_address => HTTP::ConnRoutePNames::LOCAL_ADDRESS,
|
31
|
-
:default_proxy => HTTP::ConnRoutePNames::DEFAULT_PROXY,
|
32
|
-
:date_patterns => HTTP::CookieSpecPNames::DATE_PATTERNS,
|
33
|
-
:single_cookie_header => HTTP::CookieSpecPNames::SINGLE_COOKIE_HEADER,
|
34
|
-
:credential_charset => HTTP::AuthPNames::CREDENTIAL_CHARSET,
|
35
|
-
:cookie_policy => HTTP::ClientPNames::COOKIE_POLICY,
|
36
|
-
:handle_authentication => HTTP::ClientPNames::HANDLE_AUTHENTICATION,
|
37
|
-
:handle_redirects => HTTP::ClientPNames::HANDLE_REDIRECTS,
|
38
|
-
:max_redirects => HTTP::ClientPNames::MAX_REDIRECTS,
|
39
|
-
:allow_circular_redirects => HTTP::ClientPNames::ALLOW_CIRCULAR_REDIRECTS,
|
40
|
-
:virtual_host => HTTP::ClientPNames::VIRTUAL_HOST,
|
41
|
-
:default_host => HTTP::ClientPNames::DEFAULT_HOST
|
42
|
-
# :default_headers => HTTP::ClientPNames::DEFAULT_HEADERS, # not implemented
|
43
|
-
# :connection_manager_factory_class_name => HTTP::ClientPNames::CONNECTION_MANAGER_FACTORY_CLASS_NAME # not implemented
|
44
|
-
}
|
45
|
-
|
46
|
-
CLIENT_PARAMETERS.each do |method_name, param_class|
|
47
|
-
define_method(method_name) do
|
48
|
-
params.get_parameter param_class
|
49
|
-
end
|
50
|
-
|
51
|
-
define_method("#{method_name}=") do |arg|
|
52
|
-
arg.add_to_params(params, CLIENT_PARAMETERS[method_name])
|
53
|
-
end
|
54
|
-
end
|
55
|
-
|
56
|
-
def protocol_version=(version_string)
|
57
|
-
protocol, major_version, minor_version = version_string.split(/[\.|\s|\/]/)
|
58
|
-
params.set_parameter HTTP::CoreProtocolPNames::PROTOCOL_VERSION, org.apache.http.ProtocolVersion.new(protocol, major_version.to_i, minor_version.to_i)
|
59
|
-
end
|
60
|
-
|
61
|
-
def local_address=(local_addr_str)
|
62
|
-
params.set_parameter HTTP::ConnRoutePNames::LOCAL_ADDRESS, java.net.InetAddress.getByName(local_addr_str)
|
63
|
-
end
|
64
|
-
|
65
|
-
def default_proxy=(host)
|
66
|
-
set_host_parameter(HTTP::ConnRoutePNames::DEFAULT_PROXY, host)
|
67
|
-
end
|
68
|
-
|
69
|
-
def virtual_host=(host)
|
70
|
-
set_host_parameter(HTTP::ClientPNames::VIRTUAL_HOST, host)
|
71
|
-
end
|
72
|
-
|
73
|
-
def default_host=(host)
|
74
|
-
set_host_parameter(HTTP::ClientPNames::DEFAULT_HOST, host)
|
75
|
-
end
|
76
|
-
|
77
|
-
def timeout_in_seconds=(timeout)
|
78
|
-
self.so_timeout = timeout * 1000
|
79
|
-
end
|
80
|
-
|
81
|
-
private
|
82
|
-
def set_host_parameter(parameter_name, host)
|
83
|
-
uri = URI.parse host
|
84
|
-
params.set_parameter(parameter_name, HTTP::HttpHost.new(uri.host, uri.port, uri.scheme))
|
85
|
-
end
|
86
|
-
|
87
|
-
def params
|
88
|
-
@params ||= default_params
|
89
|
-
end
|
90
|
-
|
91
|
-
def default_params
|
92
|
-
params = BasicHttpParams.new
|
93
|
-
DefaultHttpClient.set_default_http_params(params)
|
94
|
-
params
|
95
|
-
end
|
96
|
-
end
|
97
|
-
end
|
98
|
-
|
99
|
-
class Integer
|
100
|
-
def add_to_params(params, param_name)
|
101
|
-
params.set_int_parameter(param_name, self)
|
102
|
-
end
|
103
|
-
end
|
104
|
-
|
105
|
-
class TrueClass
|
106
|
-
def add_to_params(params, param_name)
|
107
|
-
params.set_boolean_parameter(param_name, self)
|
108
|
-
end
|
109
|
-
end
|
110
|
-
|
111
|
-
class FalseClass
|
112
|
-
def add_to_params(params, param_name)
|
113
|
-
params.set_boolean_parameter(param_name, self)
|
114
|
-
end
|
115
|
-
end
|
116
|
-
|
117
|
-
class Object
|
118
|
-
def add_to_params(params, param_name)
|
119
|
-
params.set_parameter(param_name, self)
|
120
|
-
end
|
121
|
-
end
|
@@ -1,201 +0,0 @@
|
|
1
|
-
require 'helper'
|
2
|
-
|
3
|
-
class TestClientParameters < Test::Unit::TestCase
|
4
|
-
def test_timeout
|
5
|
-
client = HTTP::Client.new(:default_host => "http://localhost:8080", :timeout_in_seconds => 2)
|
6
|
-
|
7
|
-
assert_raises(Timeout::Error) do
|
8
|
-
client.get("/slow", :sleep => "30")
|
9
|
-
end
|
10
|
-
end
|
11
|
-
|
12
|
-
def test_protocol_version
|
13
|
-
client = HTTP::Client.new(:default_host => "http://localhost:8080", :protocol_version => "HTTP 1.0")
|
14
|
-
result = client.get("/echo", :content => "hello")
|
15
|
-
# We did set the parameter, right?
|
16
|
-
assert_equal(client.protocol_version.to_s, "HTTP/1.0")
|
17
|
-
# Make sure the client is still working (badly set params would prevent this)
|
18
|
-
assert_equal("hello", result)
|
19
|
-
end
|
20
|
-
|
21
|
-
def test_strict_transfer_encodeing
|
22
|
-
client = HTTP::Client.new(:default_host => "http://localhost:8080", :strict_transfer_encoding => true)
|
23
|
-
result = client.get("/echo", :content => "hello")
|
24
|
-
assert_equal(client.strict_transfer_encoding, true)
|
25
|
-
assert_equal("hello", result)
|
26
|
-
end
|
27
|
-
|
28
|
-
def test_http_element_charset
|
29
|
-
client = HTTP::Client.new(:default_host => "http://localhost:8080", :http_element_charset => "ASCII")
|
30
|
-
result = client.get("/echo", :content => "hello")
|
31
|
-
assert_equal(client.http_element_charset, "ASCII")
|
32
|
-
assert_equal("hello", result)
|
33
|
-
end
|
34
|
-
|
35
|
-
def test_use_expect_continue
|
36
|
-
client = HTTP::Client.new(:default_host => "http://localhost:8080", :use_expect_continue => true)
|
37
|
-
result = client.get("/echo", :content => "hello")
|
38
|
-
assert_equal(client.use_expect_continue, true)
|
39
|
-
assert_equal("hello", result)
|
40
|
-
end
|
41
|
-
|
42
|
-
def test_wait_for_continue
|
43
|
-
client = HTTP::Client.new(:default_host => "http://localhost:8080", :wait_for_continue => true)
|
44
|
-
result = client.get("/echo", :content => "hello")
|
45
|
-
assert_equal(client.wait_for_continue, true)
|
46
|
-
assert_equal("hello", result)
|
47
|
-
end
|
48
|
-
|
49
|
-
def test_user_agent
|
50
|
-
client = HTTP::Client.new(:default_host => "http://localhost:8080", :user_agent => "Mozilla/5.0 (Macintosh; U; PPC Mac OS X 10.5; en-US; rv:1.9.0.3) Gecko/2008092414 Firefox/3.0.3")
|
51
|
-
result = client.get("/echo", :content => "hello")
|
52
|
-
assert_equal(client.user_agent, "Mozilla/5.0 (Macintosh; U; PPC Mac OS X 10.5; en-US; rv:1.9.0.3) Gecko/2008092414 Firefox/3.0.3")
|
53
|
-
assert_equal("hello", result)
|
54
|
-
end
|
55
|
-
|
56
|
-
def test_tcp_nodelay
|
57
|
-
client = HTTP::Client.new(:default_host => "http://localhost:8080", :tcp_nodelay => true)
|
58
|
-
result = client.get("/echo", :content => "hello")
|
59
|
-
assert_equal(client.tcp_nodelay, true)
|
60
|
-
assert_equal("hello", result)
|
61
|
-
end
|
62
|
-
|
63
|
-
def test_so_linger
|
64
|
-
client = HTTP::Client.new(:default_host => "http://localhost:8080", :so_linger => 3000)
|
65
|
-
result = client.get("/echo", :content => "hello")
|
66
|
-
assert_equal(client.so_linger, 3000)
|
67
|
-
assert_equal("hello", result)
|
68
|
-
end
|
69
|
-
|
70
|
-
def test_so_reuseaddr
|
71
|
-
client = HTTP::Client.new(:default_host => "http://localhost:8080", :so_reuseaddr => true)
|
72
|
-
result = client.get("/echo", :content => "hello")
|
73
|
-
assert_equal(client.so_reuseaddr, true)
|
74
|
-
assert_equal("hello", result)
|
75
|
-
end
|
76
|
-
|
77
|
-
def test_socket_buffer_size
|
78
|
-
client = HTTP::Client.new(:default_host => "http://localhost:8080", :socket_buffer_size => 10000)
|
79
|
-
result = client.get("/echo", :content => "hello")
|
80
|
-
assert_equal(client.socket_buffer_size, 10000)
|
81
|
-
assert_equal("hello", result)
|
82
|
-
end
|
83
|
-
|
84
|
-
def test_connection_timeout
|
85
|
-
client = HTTP::Client.new(:default_host => "http://localhost:8080", :connection_timeout => 2)
|
86
|
-
result = client.get("/echo", :content => "hello")
|
87
|
-
assert_equal(client.connection_timeout, 2)
|
88
|
-
assert_equal("hello", result)
|
89
|
-
end
|
90
|
-
|
91
|
-
def test_max_line_length
|
92
|
-
client = HTTP::Client.new(:default_host => "http://localhost:8080", :max_line_length => 2)
|
93
|
-
result = client.get("/echo", :content => "hello")
|
94
|
-
assert_equal(client.max_line_length, 2)
|
95
|
-
assert_equal("hello", result)
|
96
|
-
end
|
97
|
-
|
98
|
-
def test_max_header_count
|
99
|
-
client = HTTP::Client.new(:default_host => "http://localhost:8080", :max_header_count => 10)
|
100
|
-
result = client.get("/echo", :content => "hello")
|
101
|
-
assert_equal(client.max_header_count, 10)
|
102
|
-
assert_equal("hello", result)
|
103
|
-
end
|
104
|
-
|
105
|
-
def test_stale_connection_check
|
106
|
-
client = HTTP::Client.new(:default_host => "http://localhost:8080", :stale_connection_check => true)
|
107
|
-
result = client.get("/echo", :content => "hello")
|
108
|
-
assert_equal(client.stale_connection_check, true)
|
109
|
-
assert_equal("hello", result)
|
110
|
-
end
|
111
|
-
|
112
|
-
def test_local_address
|
113
|
-
client = HTTP::Client.new(:default_host => "http://localhost:8080", :local_address => "127.0.0.1")
|
114
|
-
result = client.get("/echo", :content => "hello")
|
115
|
-
assert_equal(client.local_address.get_host_address, "127.0.0.1")
|
116
|
-
assert_equal("hello", result)
|
117
|
-
end
|
118
|
-
|
119
|
-
def test_default_proxy
|
120
|
-
client = HTTP::Client.new(:default_host => "http://localhost:8080", :default_proxy => "http://127.0.0.1:8080")
|
121
|
-
result = client.get("/echo", :content => "hello")
|
122
|
-
assert_equal(client.default_proxy.to_s, "http://127.0.0.1:8080")
|
123
|
-
assert_equal("hello", result)
|
124
|
-
end
|
125
|
-
|
126
|
-
def test_date_patterns
|
127
|
-
client = HTTP::Client.new(:default_host => "http://localhost:8080", :date_patterns => ["MDy"])
|
128
|
-
result = client.get("/echo", :content => "hello")
|
129
|
-
assert(client.date_patterns.include? 'MDy')
|
130
|
-
assert_equal("hello", result)
|
131
|
-
end
|
132
|
-
|
133
|
-
def test_single_cookie_header
|
134
|
-
client = HTTP::Client.new(:default_host => "http://localhost:8080", :single_cookie_header => true)
|
135
|
-
result = client.get("/echo", :content => "hello")
|
136
|
-
assert_equal(client.single_cookie_header, true)
|
137
|
-
assert_equal("hello", result)
|
138
|
-
end
|
139
|
-
|
140
|
-
def test_credential_charset
|
141
|
-
client = HTTP::Client.new(:default_host => "http://localhost:8080", :credential_charset => "ASCII")
|
142
|
-
result = client.get("/echo", :content => "hello")
|
143
|
-
assert_equal(client.credential_charset, "ASCII")
|
144
|
-
assert_equal("hello", result)
|
145
|
-
end
|
146
|
-
|
147
|
-
def test_cookie_policy
|
148
|
-
client = HTTP::Client.new(:default_host => "http://localhost:8080", :cookie_policy => "RFC2109")
|
149
|
-
result = client.get("/echo", :content => "hello")
|
150
|
-
assert_equal(client.cookie_policy, "RFC2109")
|
151
|
-
assert_equal("hello", result)
|
152
|
-
end
|
153
|
-
|
154
|
-
def test_handle_authentication
|
155
|
-
client = HTTP::Client.new(:default_host => "http://localhost:8080", :handle_authentication => true)
|
156
|
-
result = client.get("/echo", :content => "hello")
|
157
|
-
assert_equal(client.handle_authentication, true)
|
158
|
-
assert_equal("hello", result)
|
159
|
-
end
|
160
|
-
|
161
|
-
def test_handle_redirects
|
162
|
-
client = HTTP::Client.new(:default_host => "http://localhost:8080", :handle_redirects => true)
|
163
|
-
result = client.get("/echo", :content => "hello")
|
164
|
-
assert_equal(client.handle_redirects, true)
|
165
|
-
assert_equal("hello", result)
|
166
|
-
end
|
167
|
-
|
168
|
-
def test_max_redirects
|
169
|
-
client = HTTP::Client.new(:default_host => "http://localhost:8080", :max_redirects => 1)
|
170
|
-
result = client.get("/echo", :content => "hello")
|
171
|
-
assert_equal(client.max_redirects, 1)
|
172
|
-
assert_equal("hello", result)
|
173
|
-
end
|
174
|
-
|
175
|
-
def test_allow_circular_redirects
|
176
|
-
client = HTTP::Client.new(:default_host => "http://localhost:8080", :allow_circular_redirects => true)
|
177
|
-
result = client.get("/echo", :content => "hello")
|
178
|
-
assert_equal(client.allow_circular_redirects, true)
|
179
|
-
assert_equal("hello", result)
|
180
|
-
end
|
181
|
-
|
182
|
-
def test_virtual_host
|
183
|
-
client = HTTP::Client.new(:default_host => "http://localhost:8080", :virtual_host => "http://127.0.0.1:80")
|
184
|
-
result = client.get("/echo", :content => "hello")
|
185
|
-
assert_equal(client.virtual_host.to_s, "http://127.0.0.1:80")
|
186
|
-
assert_equal("hello", result)
|
187
|
-
end
|
188
|
-
|
189
|
-
def test_default_host
|
190
|
-
client = HTTP::Client.new(:default_host => "http://localhost:8080")
|
191
|
-
result = client.get("/echo", :content => "hello")
|
192
|
-
assert_equal(client.default_host.to_s, "http://localhost:8080")
|
193
|
-
assert_equal("hello", result)
|
194
|
-
end
|
195
|
-
|
196
|
-
def test_disable_response_handler
|
197
|
-
client = HTTP::Client.new(:default_host => "http://localhost:8080", :disable_response_handler => true)
|
198
|
-
result = client.get("/echo", :content => "hello")
|
199
|
-
assert_equal(Java::OrgApacheHttpMessage::BasicHttpResponse, result.class)
|
200
|
-
end
|
201
|
-
end
|