jruby-httpclient 0.1.0-java → 0.2.0-java

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -3,6 +3,5 @@ source "http://rubygems.org"
3
3
  group :development do
4
4
  gem "bundler", "~> 1.0.0"
5
5
  gem "jeweler", "~> 1.6.2"
6
- gem "rcov", ">= 0"
7
6
  gem "rdoc"
8
7
  end
data/Gemfile.lock CHANGED
@@ -7,7 +7,6 @@ GEM
7
7
  git (>= 1.2.5)
8
8
  rake
9
9
  rake (0.9.2)
10
- rcov (0.9.9-java)
11
10
  rdoc (3.6.1)
12
11
 
13
12
  PLATFORMS
@@ -16,5 +15,4 @@ PLATFORMS
16
15
  DEPENDENCIES
17
16
  bundler (~> 1.0.0)
18
17
  jeweler (~> 1.6.2)
19
- rcov
20
18
  rdoc
data/README.rdoc CHANGED
@@ -1,6 +1,14 @@
1
1
  = jruby-httpclient
2
2
 
3
- Description goes here.
3
+ jruby-httpclient is a thin wrapper around Apache's HttpClient (version 4.1). I found that Net::HTTP was
4
+ not threadsafe in JRuby. This project is an attempt to make a threadsafe HTTP client for JRuby.
5
+
6
+ == Usage
7
+
8
+ client = HTTP::Client.new(:host => "localhost", :port => 8080)
9
+ client.get("/src", :param => "value")
10
+
11
+ client.post("/create", :param => "value")
4
12
 
5
13
  == Contributing to jruby-httpclient
6
14
 
data/Rakefile CHANGED
@@ -33,14 +33,6 @@ Rake::TestTask.new(:test) do |test|
33
33
  test.verbose = true
34
34
  end
35
35
 
36
- require 'rcov/rcovtask'
37
- Rcov::RcovTask.new do |test|
38
- test.libs << 'test'
39
- test.pattern = 'test/**/test_*.rb'
40
- test.verbose = true
41
- test.rcov_opts << '--exclude "gems/*"'
42
- end
43
-
44
36
  task :default => :test
45
37
 
46
38
  require 'rdoc/task'
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.2.0
@@ -0,0 +1,71 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{jruby-httpclient}
8
+ s.version = "0.2.0"
9
+ s.platform = Gem::Platform.new([nil, "java", nil])
10
+
11
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
+ s.authors = ["Adam Esterline"]
13
+ s.date = %q{2011-06-28}
14
+ s.description = %q{An HTTP client designed for use with JRuby in a threaded environment}
15
+ s.email = %q{adam@esterlines.com}
16
+ s.extra_rdoc_files = [
17
+ "LICENSE.txt",
18
+ "README.rdoc"
19
+ ]
20
+ s.files = [
21
+ ".document",
22
+ "Gemfile",
23
+ "Gemfile.lock",
24
+ "LICENSE.txt",
25
+ "README.rdoc",
26
+ "Rakefile",
27
+ "VERSION",
28
+ "jruby-httpclient.gemspec",
29
+ "lib/http_client.rb",
30
+ "lib/http_client/client.rb",
31
+ "lib/http_client/methods.rb",
32
+ "lib/http_client/parameters.rb",
33
+ "test/helper.rb",
34
+ "test/http_client/test_basic_auth.rb",
35
+ "test/http_client/test_basic_client_operations.rb",
36
+ "test/http_client/test_client_headers.rb",
37
+ "test/http_client/test_client_parameters.rb",
38
+ "test/http_client/test_request_entity.rb",
39
+ "test/http_test_server.rb",
40
+ "vendor/commons-codec-1.4.jar",
41
+ "vendor/commons-logging-1.1.1.jar",
42
+ "vendor/httpclient-4.1.1.jar",
43
+ "vendor/httpclient-cache-4.1.1.jar",
44
+ "vendor/httpcore-4.1.jar",
45
+ "vendor/httpmime-4.1.1.jar"
46
+ ]
47
+ s.homepage = %q{http://github.com/aesterline/jruby-httpclient}
48
+ s.licenses = ["Apache 2.0"]
49
+ s.require_paths = ["lib"]
50
+ s.rubygems_version = %q{1.5.1}
51
+ s.summary = %q{A thin wrapper around the Apache HttpClient}
52
+
53
+ if s.respond_to? :specification_version then
54
+ s.specification_version = 3
55
+
56
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
57
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
58
+ s.add_development_dependency(%q<jeweler>, ["~> 1.6.2"])
59
+ s.add_development_dependency(%q<rdoc>, [">= 0"])
60
+ else
61
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
62
+ s.add_dependency(%q<jeweler>, ["~> 1.6.2"])
63
+ s.add_dependency(%q<rdoc>, [">= 0"])
64
+ end
65
+ else
66
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
67
+ s.add_dependency(%q<jeweler>, ["~> 1.6.2"])
68
+ s.add_dependency(%q<rdoc>, [">= 0"])
69
+ end
70
+ end
71
+
data/lib/http_client.rb CHANGED
@@ -12,4 +12,6 @@ require 'httpmime-4.1.1'
12
12
  require 'httpclient-4.1.1'
13
13
  require 'httpclient-cache-4.1.1'
14
14
 
15
- require 'client'
15
+ require 'parameters'
16
+ require 'methods'
17
+ require 'client'
@@ -1,60 +1,73 @@
1
+ require 'uri'
1
2
  module HTTP
3
+ DefaultHttpClient = org.apache.http.impl.client.DefaultHttpClient
4
+ BasicResponseHandler = org.apache.http.impl.client.BasicResponseHandler
5
+ SocketTimeoutException = java.net.SocketTimeoutException
6
+
2
7
  class Client
3
- attr_reader :protocol, :host, :port, :timeout_in_seconds
4
- attr_writer :timeout_in_seconds
8
+ DEFAULT_TIMEOUT = 30_000
9
+ include HTTP::Parameters
5
10
 
6
11
  def initialize(options = {})
7
- @host = options[:host] || "localhost"
8
- @port = options[:port] || 8080
9
- @protocol = options[:protocol] || "http"
12
+ self.so_timeout = DEFAULT_TIMEOUT
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
10
21
 
11
- @timeout_in_seconds = 30
22
+ @uri_builder = URIBuilder.new(nil, nil, nil, "")
23
+ @encoding = options[:encoding] || "UTF-8"
24
+
25
+ # Set options from the rest of the options-hash
26
+ options.each do |parameter_name, parameter_value|
27
+ setter_name = "#{parameter_name}="
28
+ self.send(setter_name, parameter_value) if self.respond_to?(setter_name)
29
+ end
12
30
  end
13
31
 
14
- def get(path, options = {})
15
- params = options.collect { |key, value| BasicNameValuePair.new(key.to_s, value.to_s) }
16
- query_string = URLEncodedUtils.format(params, "UTF-8")
17
- uri = URIUtils.create_uri(@protocol, @host, @port, path, query_string, nil)
32
+ # Request Methods
18
33
 
19
- make_request(HttpGet.new(uri))
34
+ def get(params, options = {})
35
+ execute(Get.new(params, options))
20
36
  end
21
37
 
22
- def post(path, options = {})
23
- params = options.collect { |key, value| BasicNameValuePair.new(key.to_s, value.to_s) }
38
+ def post(params, options = {})
39
+ execute(Post.new(params, options))
40
+ end
24
41
 
25
- post = HttpPost.new(URIUtils.create_uri(@protocol, @host, @port, path, nil, nil))
26
- post.entity = UrlEncodedFormEntity.new(params, "UTF-8")
42
+ def delete(path)
43
+ execute(Delete.new(path))
44
+ end
27
45
 
28
- make_request(post)
46
+ def put(path)
47
+ execute(Put.new(path))
29
48
  end
30
49
 
31
- private
32
- def make_request(request)
33
- client = DefaultHttpClient.new(create_client_params)
34
- client.execute(request, BasicResponseHandler.new)
50
+ def execute(request)
51
+ client = DefaultHttpClient.new(params)
52
+
53
+ request.make_native_request(client, @uri_builder, @encoding, @response_handler)
35
54
  rescue SocketTimeoutException
36
- raise Timeout::Error, "timed out after #{timeout_in_seconds} seconds"
55
+ raise Timeout::Error, "timed out after #{so_timeout} ms"
37
56
  ensure
38
57
  client.connection_manager.shutdown
39
58
  end
40
-
41
- def create_client_params
42
- params = BasicHttpParams.new
43
- DefaultHttpClient.set_default_http_params(params)
44
- params.set_int_parameter(CoreConnectionPNames::SO_TIMEOUT, @timeout_in_seconds * 1000)
45
- end
46
59
  end
47
60
 
48
- DefaultHttpClient = org.apache.http.impl.client.DefaultHttpClient
49
- BasicResponseHandler = org.apache.http.impl.client.BasicResponseHandler
50
- HttpGet = org.apache.http.client.methods.HttpGet
51
- HttpPost = org.apache.http.client.methods.HttpPost
52
- BasicNameValuePair = org.apache.http.message.BasicNameValuePair
53
- URIUtils = org.apache.http.client.utils.URIUtils
54
- URLEncodedUtils = org.apache.http.client.utils.URLEncodedUtils
55
- UrlEncodedFormEntity = org.apache.http.client.entity.UrlEncodedFormEntity
56
- BasicHttpParams = org.apache.http.params.BasicHttpParams
57
- CoreConnectionPNames = org.apache.http.params.CoreConnectionPNames
58
- SocketTimeoutException = java.net.SocketTimeoutException
61
+ class URIBuilder
62
+ def initialize(protocol, host, port, base_path)
63
+ @protocol = protocol
64
+ @host = host
65
+ @port = port
66
+ @base_path = base_path
67
+ end
59
68
 
69
+ def create_uri(path, query_string = nil)
70
+ URIUtils.create_uri(@protocol, @host, @port, "#{@base_path}#{path}", query_string, nil)
71
+ end
72
+ end
60
73
  end
@@ -0,0 +1,89 @@
1
+ module HTTP
2
+ class Request
3
+ def self.create_type(&native_request_factory)
4
+ Class.new do
5
+ def initialize(path, params = {})
6
+ @path = path
7
+ @params = params
8
+ @headers = {}
9
+ end
10
+
11
+ def add_headers(headers)
12
+ @headers.merge!(headers)
13
+ end
14
+
15
+ def content_type=(type)
16
+ add_headers({'content-type' => type})
17
+ end
18
+
19
+ def body=(request_body)
20
+ @body = request_body
21
+ end
22
+
23
+ def basic_auth(username, password)
24
+ @username = username
25
+ @password = password
26
+ end
27
+
28
+ def make_native_request(client, uri_builder, encoding, handler=nil)
29
+ request = create_native_request(uri_builder, encoding)
30
+ request.entity = StringEntity.new(@body) unless @body.nil?
31
+
32
+ unless @username.nil?
33
+ client.credentials_provider.set_credentials(AuthScope::ANY, UsernamePasswordCredentials.new(@username, @password))
34
+ end
35
+
36
+ if handler
37
+ client.execute(request, handler)
38
+ else
39
+ client.execute(request)
40
+ end
41
+ end
42
+
43
+ private
44
+ define_method(:create_native_request) do |uri_builder, encoding|
45
+ params = @params.collect { |key, value| BasicNameValuePair.new(key.to_s, value.to_s) }
46
+ request = native_request_factory.call(uri_builder, @path, params, encoding)
47
+
48
+ @headers.each { |name, value| request.add_header(name.to_s, value.to_s) }
49
+
50
+ request
51
+ end
52
+ end
53
+ end
54
+ end
55
+
56
+ Post = Request.create_type do |uri_builder, path, params, encoding|
57
+ post = HttpPost.new(uri_builder.create_uri(path))
58
+ post.entity = UrlEncodedFormEntity.new(params, encoding)
59
+ post
60
+ end
61
+
62
+ Get = Request.create_type do |uri_builder, path, params, encoding|
63
+ query_string = URLEncodedUtils.format(params, encoding)
64
+ get = HttpGet.new(uri_builder.create_uri(path, query_string))
65
+ get
66
+ end
67
+
68
+ Delete = Request.create_type do |uri_builder, path|
69
+ HttpDelete.new(uri_builder.create_uri(path))
70
+ end
71
+
72
+ Put = Request.create_type do |uri_builder, path|
73
+ HttpPut.new(uri_builder.create_uri(path))
74
+ end
75
+
76
+ private
77
+ HttpGet = org.apache.http.client.methods.HttpGet
78
+ HttpPost = org.apache.http.client.methods.HttpPost
79
+ HttpDelete = org.apache.http.client.methods.HttpDelete
80
+ HttpPut = org.apache.http.client.methods.HttpPut
81
+ BasicNameValuePair = org.apache.http.message.BasicNameValuePair
82
+ URIUtils = org.apache.http.client.utils.URIUtils
83
+ URLEncodedUtils = org.apache.http.client.utils.URLEncodedUtils
84
+ UrlEncodedFormEntity = org.apache.http.client.entity.UrlEncodedFormEntity
85
+ BasicResponseHandler = org.apache.http.impl.client.BasicResponseHandler
86
+ AuthScope = org.apache.http.auth.AuthScope
87
+ UsernamePasswordCredentials = org.apache.http.auth.UsernamePasswordCredentials
88
+ StringEntity = org.apache.http.entity.StringEntity
89
+ end
@@ -0,0 +1,120 @@
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
+
11
+ module Parameters
12
+ CLIENT_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
+ CLIENT_PARAMETERS.each do |method_name, param_class|
46
+ define_method(method_name) do
47
+ params.get_parameter param_class
48
+ end
49
+
50
+ define_method("#{method_name}=") do |arg|
51
+ arg.add_to_params(params, CLIENT_PARAMETERS[method_name])
52
+ end
53
+ end
54
+
55
+ def protocol_version=(version_string)
56
+ protocol, major_version, minor_version = version_string.split(/[\.|\s|\/]/)
57
+ params.set_parameter HTTP::CoreProtocolPNames::PROTOCOL_VERSION, org.apache.http.ProtocolVersion.new(protocol, major_version.to_i, minor_version.to_i)
58
+ end
59
+
60
+ def local_address=(local_addr_str)
61
+ params.set_parameter HTTP::ConnRoutePNames::LOCAL_ADDRESS, java.net.InetAddress.getByName(local_addr_str)
62
+ end
63
+
64
+ def default_proxy=(host)
65
+ set_host_parameter(HTTP::ConnRoutePNames::DEFAULT_PROXY, host)
66
+ end
67
+
68
+ def virtual_host=(host)
69
+ set_host_parameter(HTTP::ClientPNames::VIRTUAL_HOST, host)
70
+ end
71
+
72
+ def default_host=(host)
73
+ set_host_parameter(HTTP::ClientPNames::DEFAULT_HOST, host)
74
+ end
75
+
76
+ def timeout_in_seconds=(timeout)
77
+ self.so_timeout = timeout * 1000
78
+ end
79
+
80
+ private
81
+ def set_host_parameter(parameter_name, host)
82
+ uri = URI.parse host
83
+ params.set_parameter(parameter_name, HTTP::HttpHost.new(uri.host, uri.port, uri.scheme))
84
+ end
85
+
86
+ def params
87
+ @params ||= default_params
88
+ end
89
+
90
+ def default_params
91
+ params = BasicHttpParams.new
92
+ DefaultHttpClient.set_default_http_params(params)
93
+ params
94
+ end
95
+ end
96
+ end
97
+
98
+ class Integer
99
+ def add_to_params(params, param_name)
100
+ params.set_int_parameter(param_name, self)
101
+ end
102
+ end
103
+
104
+ class TrueClass
105
+ def add_to_params(params, param_name)
106
+ params.set_boolean_parameter(param_name, self)
107
+ end
108
+ end
109
+
110
+ class FalseClass
111
+ def add_to_params(params, param_name)
112
+ params.set_boolean_parameter(param_name, self)
113
+ end
114
+ end
115
+
116
+ class Object
117
+ def add_to_params(params, param_name)
118
+ params.set_parameter(param_name, self)
119
+ end
120
+ end
@@ -0,0 +1,16 @@
1
+ require 'helper'
2
+
3
+ class BasicAuthTest < Test::Unit::TestCase
4
+ def test_get_can_authenticate
5
+ get = HTTP::Get.new("/protected")
6
+ get.basic_auth("user", "Password")
7
+
8
+ response = @client.execute(get)
9
+
10
+ assert_equal("Logged In", response)
11
+ end
12
+
13
+ def setup
14
+ @client = HTTP::Client.new(:default_host => "http://localhost:8080")
15
+ end
16
+ end
@@ -0,0 +1,31 @@
1
+ require 'helper'
2
+
3
+ class TestBasicClientOperations < Test::Unit::TestCase
4
+ def test_simple_get
5
+ result = @client.get("/echo", :content => "hello")
6
+
7
+ assert_equal("hello", result)
8
+ end
9
+
10
+ def test_simple_post
11
+ result = @client.post("/echo", :content => "world")
12
+
13
+ assert_equal("world", result)
14
+ end
15
+
16
+ def test_simple_delete
17
+ result = @client.delete("/echo")
18
+
19
+ assert_equal("delete", result)
20
+ end
21
+
22
+ def test_simple_put
23
+ result = @client.put("/echo")
24
+
25
+ assert_equal("put", result)
26
+ end
27
+
28
+ def setup
29
+ @client = HTTP::Client.new(:default_host => "http://localhost:8080")
30
+ end
31
+ end
@@ -0,0 +1,56 @@
1
+ require 'helper'
2
+
3
+ class TestClientHeaders < Test::Unit::TestCase
4
+ def test_get_headers
5
+ get = HTTP::Get.new("/echo_header")
6
+ get.add_headers(:test_header => "get testing")
7
+
8
+ response = @client.execute(get)
9
+ assert_equal("get testing", response)
10
+ end
11
+
12
+ def test_post_headers
13
+ get = HTTP::Post.new("/echo_header")
14
+ get.add_headers(:test_header => "post testing")
15
+
16
+ response = @client.execute(get)
17
+ assert_equal("post testing", response)
18
+ end
19
+
20
+ def test_delete_headers
21
+ get = HTTP::Delete.new("/echo_header")
22
+ get.add_headers(:test_header => "post testing")
23
+
24
+ response = @client.execute(get)
25
+ assert_equal("post testing", response)
26
+ end
27
+
28
+ def test_put_headers
29
+ get = HTTP::Put.new("/echo_header")
30
+ get.add_headers(:test_header => "post testing")
31
+
32
+ response = @client.execute(get)
33
+ assert_equal("post testing", response)
34
+ end
35
+
36
+ def test_multiple_calls_to_add_headers_should_prefer_last_set_of_headers
37
+ get = HTTP::Get.new("/echo_header")
38
+ get.add_headers(:test_header => "get testing")
39
+ get.add_headers(:test_header => "should prefer this one")
40
+
41
+ response = @client.execute(get)
42
+ assert_equal("should prefer this one", response)
43
+ end
44
+
45
+ def test_should_be_able_to_add_content_type
46
+ get = HTTP::Get.new("/echo_header", :header => 'content-type')
47
+ get.content_type = 'text/xml'
48
+
49
+ response = @client.execute(get)
50
+ assert_equal('text/xml', response)
51
+ end
52
+
53
+ def setup
54
+ @client = HTTP::Client.new(:default_host => "http://localhost:8080")
55
+ end
56
+ end
@@ -0,0 +1,201 @@
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
@@ -0,0 +1,23 @@
1
+ require 'helper'
2
+
3
+ class RequestEntityTest < Test::Unit::TestCase
4
+ def test_post_request_body
5
+ post = HTTP::Post.new("/body")
6
+ post.body = "Here we are"
7
+
8
+ result = @client.execute(post)
9
+ assert_equal("Here we are", result)
10
+ end
11
+
12
+ def test_put_request_body
13
+ post = HTTP::Put.new("/body")
14
+ post.body = "We are there"
15
+
16
+ result = @client.execute(post)
17
+ assert_equal("We are there", result)
18
+ end
19
+
20
+ def setup
21
+ @client = HTTP::Client.new(:default_host => "http://localhost:8080")
22
+ end
23
+ end
@@ -5,9 +5,62 @@ module HTTP
5
5
  def self.start_server
6
6
  SERVER.mount('/echo', EchoServlet)
7
7
  SERVER.mount('/slow', SlowServlet)
8
+ SERVER.mount('/echo_header', HeaderEchoServlet)
9
+ SERVER.mount('/protected', ProtectedServlet)
10
+ SERVER.mount('/body', BodyServlet)
8
11
  Thread.new { SERVER.start }
9
12
  end
10
13
 
14
+ class BodyServlet < WEBrick::HTTPServlet::AbstractServlet
15
+ def do_POST(request, response)
16
+ response.status = 200
17
+ response.body = request.body
18
+ end
19
+
20
+ def do_PUT(request, response)
21
+ response.status = 200
22
+ response.body = request.body
23
+ end
24
+ end
25
+
26
+ class ProtectedServlet < WEBrick::HTTPServlet::AbstractServlet
27
+ def do_GET(request, response)
28
+ WEBrick::HTTPAuth.basic_auth(request, response, "Mine") do |user, pass|
29
+ user == "user" && pass == "Password"
30
+ end
31
+
32
+ response.status = 200
33
+ response.body = "Logged In"
34
+ end
35
+ end
36
+
37
+ class HeaderEchoServlet < WEBrick::HTTPServlet::AbstractServlet
38
+ def do_GET(request, response)
39
+ echo_header(request, response)
40
+ end
41
+
42
+ def do_POST(request, response)
43
+ echo_header(request, response)
44
+ end
45
+
46
+ def do_DELETE(request, response)
47
+ echo_header(request, response)
48
+ end
49
+
50
+ def do_PUT(request, response)
51
+ echo_header(request, response)
52
+ end
53
+
54
+ private
55
+ def echo_header(request, response)
56
+ header_to_echo = request.query['header'] || 'test_header'
57
+
58
+ response.status = 200
59
+ response['Content-Type'] = 'text/plain'
60
+ response.body = request.header[header_to_echo].first
61
+ end
62
+ end
63
+
11
64
  class EchoServlet < WEBrick::HTTPServlet::AbstractServlet
12
65
 
13
66
  def do_GET(request, response)
@@ -18,11 +71,19 @@ module HTTP
18
71
  echo(request, response)
19
72
  end
20
73
 
74
+ def do_DELETE(request, response)
75
+ echo(request, response, "delete")
76
+ end
77
+
78
+ def do_PUT(request, response)
79
+ echo(request, response, "put")
80
+ end
81
+
21
82
  private
22
- def echo(request, response)
83
+ def echo(request, response, canned_response = nil)
23
84
  response.status = 200
24
85
  response['Content-Type'] = 'text/plain'
25
- response.body = request.query['content']
86
+ response.body = canned_response || request.query['content']
26
87
  end
27
88
  end
28
89
 
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: jruby-httpclient
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.0
5
+ version: 0.2.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-06-08 00:00:00 -06:00
13
+ date: 2011-06-28 00:00:00 -06:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -36,7 +36,7 @@ dependencies:
36
36
  prerelease: false
37
37
  type: :development
38
38
  - !ruby/object:Gem::Dependency
39
- name: rcov
39
+ name: rdoc
40
40
  version_requirements: &id003 !ruby/object:Gem::Requirement
41
41
  none: false
42
42
  requirements:
@@ -46,17 +46,6 @@ dependencies:
46
46
  requirement: *id003
47
47
  prerelease: false
48
48
  type: :development
49
- - !ruby/object:Gem::Dependency
50
- name: rdoc
51
- version_requirements: &id004 !ruby/object:Gem::Requirement
52
- none: false
53
- requirements:
54
- - - ">="
55
- - !ruby/object:Gem::Version
56
- version: "0"
57
- requirement: *id004
58
- prerelease: false
59
- type: :development
60
49
  description: An HTTP client designed for use with JRuby in a threaded environment
61
50
  email: adam@esterlines.com
62
51
  executables: []
@@ -74,10 +63,17 @@ files:
74
63
  - README.rdoc
75
64
  - Rakefile
76
65
  - VERSION
66
+ - jruby-httpclient.gemspec
77
67
  - lib/http_client.rb
78
68
  - lib/http_client/client.rb
69
+ - lib/http_client/methods.rb
70
+ - lib/http_client/parameters.rb
79
71
  - test/helper.rb
80
- - test/http_client/test_client.rb
72
+ - test/http_client/test_basic_auth.rb
73
+ - test/http_client/test_basic_client_operations.rb
74
+ - test/http_client/test_client_headers.rb
75
+ - test/http_client/test_client_parameters.rb
76
+ - test/http_client/test_request_entity.rb
81
77
  - test/http_test_server.rb
82
78
  - vendor/commons-codec-1.4.jar
83
79
  - vendor/commons-logging-1.1.1.jar
@@ -1,28 +0,0 @@
1
- require 'helper'
2
-
3
- class TestClient < Test::Unit::TestCase
4
- def test_simple_get
5
- result = @client.get("/echo", :content => "hello")
6
-
7
- assert_equal("hello", result)
8
- end
9
-
10
- def test_simple_post
11
- result = @client.post("/echo", :content => "world")
12
-
13
- assert_equal("world", result)
14
- end
15
-
16
- def test_timeout
17
- client = HTTP::Client.new(:host => "localhost", :port => 8080)
18
- @client.timeout_in_seconds = 2
19
-
20
- assert_raises(Timeout::Error) do
21
- @client.get("/slow", :sleep => "30")
22
- end
23
- end
24
-
25
- def setup
26
- @client = HTTP::Client.new(:host => "localhost", :port => 8080)
27
- end
28
- end