/
net_http_spec.rb
36 lines (31 loc) · 1.03 KB
/
net_http_spec.rb
1
2
3
4
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
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
require 'webmock_spec'
require 'ostruct'
include WebMock
def http_request(method, uri, options = {})
begin
uri = URI.parse(uri)
rescue
uri = Addressable::URI.heuristic_parse(uri)
end
response = nil
clazz = Net::HTTP.const_get("#{method.to_s.capitalize}")
req = clazz.new("#{uri.path}#{uri.query ? '?' : ''}#{uri.query}", options[:headers])
req.basic_auth uri.user, uri.password if uri.user
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true if uri.scheme == "https"
response = http.start {|http|
http.request(req, options[:body])
}
OpenStruct.new({
:body => response.body,
:headers => response,
:status => response.code })
end
describe "Webmock with Net:HTTP" do
it_should_behave_like "WebMock"
it "should work with block provided" do
stub_http_request(:get, "www.google.com").to_return(:body => "abc"*100000)
Net::HTTP.start("www.google.com") { |query| query.get("/") }.body.should == "abc"*100000
end
end