Initial implementation of HTTP.

git-svn-id: https://google-api-ruby-client.googlecode.com/svn/trunk@22 c1d61fac-ed7f-fcc1-18f7-ff78120a04ef
This commit is contained in:
bobaman@google.com committed 2010-08-19 23:21:45 +00:00
1 parent a9523bca32
commit bc844db311
6 files changed
+395 -81

No files matched your search

@@ -0,0 +1,104 @@
# Copyright 2010 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
require 'spec_helper'
require 'net/http'
require 'net/https'
require 'google/api_client/transport/http_transport'
class AlwaysFail
def initialize(*args)
raise IOError, "This would never work."
end
end
Google::APIClient::HTTPTransport::METHOD_MAPPING[:fail] = AlwaysFail
describe Google::APIClient::HTTPTransport, 'in the default configuration' do
before do
@http = Google::APIClient::HTTPTransport.new
end
it 'should send a GET request' do
request = @http.build_request(:get, "http://www.google.com/")
response = @http.send_request(request)
status, headers, body = response
status.should >= 100
body.size.should > 0
headers.size.should > 0
end
it 'should send a GET request using SSL' do
request = @http.build_request(:get, "https://www.google.com/")
response = @http.send_request(request)
status, headers, body = response
status.should >= 100
body.size.should > 0
headers.size.should > 0
end
it 'should send a POST request' do
request = @http.build_request(
:post, "http://www.google.com/", :body => "A Body."
)
response = @http.send_request(request)
status, headers, body = response
status.should >= 100
body.size.should > 0
headers.size.should > 0
end
it 'should send a PUT request' do
request = @http.build_request(
:put, "http://www.google.com/", :body => "A Body."
)
response = @http.send_request(request)
status, headers, body = response
status.should >= 100
body.size.should > 0
headers.size.should > 0
end
it 'should send a DELETE request' do
request = @http.build_request(:delete, "http://www.google.com/")
response = @http.send_request(request)
status, headers, body = response
status.should >= 100
body.size.should > 0
headers.size.should > 0
end
it 'should fail to send a FAIL request' do
(lambda do
request = @http.build_request(:fail, "http://www.google.com/")
response = @http.send_request(request)
end).should raise_error(IOError)
end
it 'should fail to send a BOGUS request' do
(lambda do
response = @http.send_request(
["BOGUS", "http://www.google.com/", {}, [""]]
)
end).should raise_error(ArgumentError)
end
it 'should fail to connect to a non-addressable URI' do
(lambda do
request = @http.build_request(:get, "bogus://www.google.com/")
response = @http.send_request(request)
end).should raise_error(ArgumentError)
end
end
@@ -14,50 +14,89 @@
require 'spec_helper'
require 'net/http'
require 'net/https'
require 'google/api_client/transport/http_transport'
require 'google/api_client/parser/json_parser'
describe Google::APIClient::HTTPTransport, 'with default configuration' do
def assemble_body_string(body)
body_string = ""
body.each do |chunk|
body_string += chunk
end
return body_string
end
describe Google::APIClient::HTTPTransport, 'in the default configuration' do
before do
@transport = Google::APIClient::HTTPTransport.new
@http = Google::APIClient::HTTPTransport.new
end
it 'should use the default json parser' do
@transport.parser.should be_instance_of Google::APIClient::JSONParser
it 'should build a valid GET request' do
method, uri, headers, body =
@http.build_request(:get, "http://www.example.com/")
body_string = assemble_body_string(body)
method.should == "GET"
uri.should === "http://www.example.com/"
headers.keys.should_not include("Content-Length")
body_string.should == ""
end
it 'should build a valid POST request' do
method, uri, headers, body = @http.build_request(
:post, "http://www.example.com/", :body => "A body."
)
body_string = assemble_body_string(body)
method.should == "POST"
uri.should === "http://www.example.com/"
headers["Content-Length"].should == "7"
body_string.should == "A body."
end
it 'should build a valid PUT request' do
method, uri, headers, body = @http.build_request(
:put, "http://www.example.com/", :body => "A body."
)
body_string = assemble_body_string(body)
method.should == "PUT"
uri.should === "http://www.example.com/"
headers["Content-Length"].should == "7"
body_string.should == "A body."
end
it 'should build a valid DELETE request' do
method, uri, headers, body =
@http.build_request(:delete, "http://www.example.com/")
body_string = assemble_body_string(body)
method.should == "DELETE"
uri.should === "http://www.example.com/"
headers.keys.should_not include("Content-Length")
body_string.should == ""
end
it 'should not build a BOGUS request' do
(lambda do
@http.build_request(:bogus, "http://www.example.com/")
end).should raise_error(ArgumentError)
end
end
describe Google::APIClient::HTTPTransport, 'with custom pluggable parser' do
describe Google::APIClient::HTTPTransport,
'with a certificate store and connection pool' do
before do
class FakeJsonParser
end
@transport = Google::APIClient::HTTPTransport.new(:json_parser => FakeJsonParser.new)
end
it 'should use the custom parser' do
@transport.parser.should be_instance_of FakeJsonParser
end
end
describe Google::APIClient::HTTPTransport, 'with new parser type' do
before do
class FakeNewParser
end
@transport = Google::APIClient::HTTPTransport.new(
:parser => :new_parser,
:new_parser => FakeNewParser.new
@http = Google::APIClient::HTTPTransport.new(
:cert_store => OpenSSL::X509::Store.new,
:connection_pool => {
"http://www.example.com" => Net::HTTP.new("www.example.com", 80)
}
)
end
it 'should use new parser type' do
@transport.parser.should be_instance_of FakeNewParser
it 'should have the correct certificate store' do
# TODO(bobaman) Write a real test
@http.cert_store.should_not == nil
end
end
describe Google::APIClient::HTTPTransport, 'with illegal parser config' do
it 'should raise ArgumentError' do
lambda { Google::APIClient::HTTPTransport.new(:parser => :fakeclass) }.should raise_exception(ArgumentError)
it 'should have the correct connection pool' do
@http.connection_pool.keys.should include("http://www.example.com")
end
end
+49
View File
@@ -0,0 +1,49 @@
# Copyright 2010 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
require 'spec_helper'
require 'google/api_client'
require 'google/api_client/version'
require 'google/api_client/parser/json_parser'
require 'google/api_client/auth/oauth_1'
require 'google/api_client/transport/http_transport'
describe Google::APIClient, 'with default configuration' do
before do
@client = Google::APIClient.new
end
it 'should make its version number available' do
::Google::APIClient::VERSION::STRING.should be_instance_of(String)
end
it 'should use the default JSON parser' do
@client.parser.should be_instance_of(Google::APIClient::JSONParser)
end
end
describe Google::APIClient, 'with custom pluggable parser' do
before do
class FakeJsonParser
end
@client = Google::APIClient.new(:parser => FakeJsonParser.new)
end
it 'should use the custom parser' do
@client.parser.should be_instance_of(FakeJsonParser)
end
end