net-httpstub 0.0.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.
- data/ChangeLog +4 -0
- data/README +67 -0
- data/Rakefile +134 -0
- data/lib/net/httpstub.rb +82 -0
- data/test/net-httpstub_test.rb +176 -0
- data/test/test_helper.rb +3 -0
- metadata +72 -0
data/ChangeLog
ADDED
data/README
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
|
2
|
+
= net-httpstub - a stub of Net::HTTP.
|
3
|
+
|
4
|
+
by momotaro <info@momo-lab.net>
|
5
|
+
|
6
|
+
== Description
|
7
|
+
|
8
|
+
net-httpstub is a stub of Net::HTTP.
|
9
|
+
|
10
|
+
This changes the operation of the Net::HTTP,
|
11
|
+
and returns the response originally specified.
|
12
|
+
|
13
|
+
== Installation
|
14
|
+
|
15
|
+
=== Archive Installation
|
16
|
+
|
17
|
+
rake install
|
18
|
+
|
19
|
+
=== Gem Installation
|
20
|
+
|
21
|
+
gem install nethttpstub
|
22
|
+
|
23
|
+
|
24
|
+
== Features/Problems
|
25
|
+
|
26
|
+
This depends on internal processing of Net::HTTP.
|
27
|
+
Therefore, it is likely not to move excluding
|
28
|
+
the following environment.
|
29
|
+
|
30
|
+
* ruby 1.8.7 (2008-08-11 patchlevel 72) [i386-mswin32]
|
31
|
+
|
32
|
+
== Synopsis
|
33
|
+
|
34
|
+
class SampleTest < Test::Unit:TestCase
|
35
|
+
def test_get_request
|
36
|
+
Net::HTTP.push "response body"
|
37
|
+
Net::HTTP.start('www.example.com') do |http|
|
38
|
+
res = http.get('/')
|
39
|
+
assert_instance_of Net::HTTPOK, res
|
40
|
+
assert_equal "200", res.code
|
41
|
+
assert_equal "response body", res.body
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
The HTTP response header can be specified.
|
47
|
+
|
48
|
+
class SampleTest < Test::Unit:TestCase
|
49
|
+
def test_get_request
|
50
|
+
Net::HTTP.push <<-EOF
|
51
|
+
HTTP/1.1 404 Not Found
|
52
|
+
|
53
|
+
EOF
|
54
|
+
Net::HTTP.start('www.example.com') do |http|
|
55
|
+
res = http.get('/')
|
56
|
+
assert_instance_of Net::HTTPNotFound, res
|
57
|
+
assert_equal "404", res.code
|
58
|
+
assert_equal "", res.body
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
== Copyright
|
64
|
+
|
65
|
+
Author:: momotaro <info@momo-lab.net>
|
66
|
+
Copyright:: Copyright (c) 2000-2009 momotaro
|
67
|
+
License:: Ruby's
|
data/Rakefile
ADDED
@@ -0,0 +1,134 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'rake/clean'
|
4
|
+
require 'rake/testtask'
|
5
|
+
require 'rake/packagetask'
|
6
|
+
require 'rake/gempackagetask'
|
7
|
+
require 'rake/rdoctask'
|
8
|
+
require 'rake/contrib/rubyforgepublisher'
|
9
|
+
require 'rake/contrib/sshpublisher'
|
10
|
+
require 'rubyforge'
|
11
|
+
require 'fileutils'
|
12
|
+
include FileUtils
|
13
|
+
|
14
|
+
NAME = "net-httpstub"
|
15
|
+
AUTHOR = "momotaro"
|
16
|
+
EMAIL = "info@momo-lab.net"
|
17
|
+
DESCRIPTION = "net-httpstub is a stub of Net::HTTP."
|
18
|
+
RUBYFORGE_PROJECT = "net-httpstub"
|
19
|
+
HOMEPATH = "http://#{RUBYFORGE_PROJECT}.rubyforge.org"
|
20
|
+
BIN_FILES = %w( )
|
21
|
+
VERS = "0.0.1"
|
22
|
+
|
23
|
+
sudo = 'sudo ' unless /mswin|mingw/ =~ RUBY_PLATFORM
|
24
|
+
|
25
|
+
REV = File.read(".svn/entries")[/committed-rev="(d+)"/, 1] rescue nil
|
26
|
+
CLEAN.include ['**/.*.sw?', '*.gem', '.config']
|
27
|
+
RDOC_OPTS = [
|
28
|
+
'--title', "#{NAME} documentation",
|
29
|
+
"--charset", "utf-8",
|
30
|
+
"--opname", "index.html",
|
31
|
+
"--line-numbers",
|
32
|
+
"--main", "README",
|
33
|
+
"--inline-source",
|
34
|
+
]
|
35
|
+
|
36
|
+
task :default => [:test]
|
37
|
+
task :package => [:clean]
|
38
|
+
|
39
|
+
Rake::TestTask.new("test") do |t|
|
40
|
+
t.libs << "test"
|
41
|
+
t.pattern = "test/**/*_test.rb"
|
42
|
+
t.verbose = true
|
43
|
+
end
|
44
|
+
|
45
|
+
spec = Gem::Specification.new do |s|
|
46
|
+
s.name = NAME
|
47
|
+
s.version = VERS
|
48
|
+
s.platform = Gem::Platform::RUBY
|
49
|
+
s.has_rdoc = true
|
50
|
+
s.extra_rdoc_files = ["README", "ChangeLog"]
|
51
|
+
s.rdoc_options += RDOC_OPTS + ['--exclude', '^(examples|extras)/']
|
52
|
+
s.summary = DESCRIPTION
|
53
|
+
s.description = DESCRIPTION
|
54
|
+
s.author = AUTHOR
|
55
|
+
s.email = EMAIL
|
56
|
+
s.homepage = HOMEPATH
|
57
|
+
s.executables = BIN_FILES
|
58
|
+
s.rubyforge_project = RUBYFORGE_PROJECT
|
59
|
+
s.bindir = "bin"
|
60
|
+
s.require_path = "lib"
|
61
|
+
s.test_files = Dir["test/test_*.rb"]
|
62
|
+
|
63
|
+
#s.add_dependency('activesupport', '>=1.3.1')
|
64
|
+
#s.required_ruby_version = '>= 1.8.2'
|
65
|
+
|
66
|
+
s.files = %w(README ChangeLog Rakefile) +
|
67
|
+
Dir.glob("{bin,doc,test,lib,templates,generator,extras,website,script}/**/*") +
|
68
|
+
Dir.glob("ext/**/*.{h,c,rb}") +
|
69
|
+
Dir.glob("examples/**/*.rb") +
|
70
|
+
Dir.glob("tools/*.rb")
|
71
|
+
|
72
|
+
s.extensions = FileList["ext/**/extconf.rb"].to_a
|
73
|
+
end
|
74
|
+
|
75
|
+
Rake::GemPackageTask.new(spec) do |p|
|
76
|
+
p.need_tar = true
|
77
|
+
p.gem_spec = spec
|
78
|
+
end
|
79
|
+
|
80
|
+
task :install do
|
81
|
+
name = "#{NAME}-#{VERS}.gem"
|
82
|
+
sh %{rake package}
|
83
|
+
sh %{#{sudo}gem install pkg/#{name}}
|
84
|
+
end
|
85
|
+
|
86
|
+
task :uninstall => [:clean] do
|
87
|
+
sh %{#{sudo}gem uninstall #{NAME}}
|
88
|
+
end
|
89
|
+
|
90
|
+
|
91
|
+
Rake::RDocTask.new do |rdoc|
|
92
|
+
rdoc.rdoc_dir = 'html'
|
93
|
+
rdoc.options += RDOC_OPTS
|
94
|
+
rdoc.template = "resh"
|
95
|
+
#rdoc.template = "#{ENV['template']}.rb" if ENV['template']
|
96
|
+
if ENV['DOC_FILES']
|
97
|
+
rdoc.rdoc_files.include(ENV['DOC_FILES'].split(/,\s*/))
|
98
|
+
else
|
99
|
+
rdoc.rdoc_files.include('README', 'ChangeLog')
|
100
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
101
|
+
rdoc.rdoc_files.include('ext/**/*.c')
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
desc "Publish to RubyForge"
|
106
|
+
task :rubyforge => [:rdoc, :package] do
|
107
|
+
require 'rubyforge'
|
108
|
+
Rake::RubyForgePublisher.new(RUBYFORGE_PROJECT, 'momotaro').upload
|
109
|
+
end
|
110
|
+
|
111
|
+
desc 'Package and upload the release to rubyforge.'
|
112
|
+
task :release => [:clean, :package] do |t|
|
113
|
+
v = ENV["VERSION"] or abort "Must supply VERSION=x.y.z"
|
114
|
+
abort "Versions don't match #{v} vs #{VERS}" unless v == VERS
|
115
|
+
pkg = "pkg/#{NAME}-#{VERS}"
|
116
|
+
|
117
|
+
rf = RubyForge.new
|
118
|
+
puts "Logging in"
|
119
|
+
rf.configure
|
120
|
+
rf.login
|
121
|
+
|
122
|
+
c = rf.userconfig
|
123
|
+
# c["release_notes"] = description if description
|
124
|
+
# c["release_changes"] = changes if changes
|
125
|
+
c["preformatted"] = true
|
126
|
+
|
127
|
+
files = [
|
128
|
+
"#{pkg}.tgz",
|
129
|
+
"#{pkg}.gem"
|
130
|
+
].compact
|
131
|
+
|
132
|
+
puts "Releasing #{NAME} v.#{VERS}"
|
133
|
+
rf.add_release RUBYFORGE_PROJECT, NAME, VERS, *files
|
134
|
+
end
|
data/lib/net/httpstub.rb
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'stringio'
|
3
|
+
|
4
|
+
module Net
|
5
|
+
class HTTP
|
6
|
+
@@responses = []
|
7
|
+
|
8
|
+
class << HTTP
|
9
|
+
# push response list and stub mode.
|
10
|
+
def push(res)
|
11
|
+
@@responses.push add_header(res)
|
12
|
+
self.stub = true
|
13
|
+
end
|
14
|
+
|
15
|
+
# return stub mode.
|
16
|
+
def stub
|
17
|
+
@stub || false
|
18
|
+
end
|
19
|
+
|
20
|
+
alias :stub? :stub
|
21
|
+
|
22
|
+
# set stub mode.
|
23
|
+
def stub=(value)
|
24
|
+
@stub = value
|
25
|
+
if @stub
|
26
|
+
module_eval do
|
27
|
+
alias :request :request_stub
|
28
|
+
alias :connect :connect_stub
|
29
|
+
public :request
|
30
|
+
end
|
31
|
+
else
|
32
|
+
module_eval do
|
33
|
+
alias :request :request_orig
|
34
|
+
alias :connect :connect_orig
|
35
|
+
public :request
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
def add_header(res) #:nodoc:
|
43
|
+
head, body = res.split(/(\r|\n|\r\n)\1/, 2)
|
44
|
+
if body.nil?
|
45
|
+
res = [
|
46
|
+
"HTTP/1.1 200 OK",
|
47
|
+
"Content-Length: #{res.size}",
|
48
|
+
"",
|
49
|
+
res
|
50
|
+
].join("\n")
|
51
|
+
end
|
52
|
+
res
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
private
|
57
|
+
|
58
|
+
alias :request_orig :request #:nodoc:
|
59
|
+
alias :connect_orig :connect #:nodoc:
|
60
|
+
private :request_orig, :connect_orig
|
61
|
+
|
62
|
+
def request_stub(req, body = nil, &block) #:nodoc:
|
63
|
+
res = @@responses.shift
|
64
|
+
if res.nil?
|
65
|
+
res = "HTTP/1.1 404 Not Found\n\n"
|
66
|
+
else
|
67
|
+
Net::HTTP.stub = false if @@responses.empty?
|
68
|
+
end
|
69
|
+
io = BufferedIO.new(StringIO.new(res))
|
70
|
+
res = HTTPResponse.read_new(io)
|
71
|
+
res.reading_body(io, req.response_body_permitted?) do
|
72
|
+
yield res if block_given?
|
73
|
+
end
|
74
|
+
res
|
75
|
+
end
|
76
|
+
|
77
|
+
def connect_stub #:nodoc:
|
78
|
+
@socket = BufferedIO.new(StringIO.new)
|
79
|
+
on_connect
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
@@ -0,0 +1,176 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper.rb'
|
2
|
+
|
3
|
+
require 'test/unit'
|
4
|
+
require 'open-uri'
|
5
|
+
|
6
|
+
class SampleTest < Test::Unit::TestCase
|
7
|
+
def test_get_request
|
8
|
+
Net::HTTP.push "response body"
|
9
|
+
Net::HTTP.start('www.example.com') do |http|
|
10
|
+
res = http.get('/')
|
11
|
+
assert_instance_of Net::HTTPOK, res
|
12
|
+
assert_equal "200", res.code
|
13
|
+
assert_equal "response body", res.body
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_get_request
|
18
|
+
Net::HTTP.push <<-EOF
|
19
|
+
HTTP/1.1 404 Not Found
|
20
|
+
|
21
|
+
EOF
|
22
|
+
Net::HTTP.start('www.example.com') do |http|
|
23
|
+
res = http.get('/')
|
24
|
+
assert_instance_of Net::HTTPNotFound, res
|
25
|
+
assert_equal "404", res.code
|
26
|
+
assert_equal "", res.body
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
class HttpstubTest < Test::Unit::TestCase
|
33
|
+
def test_stub
|
34
|
+
assert_equal false, Net::HTTP.stub?
|
35
|
+
assert_equal false, Net::HTTP.stub
|
36
|
+
Net::HTTP.push "response body"
|
37
|
+
assert_equal true, Net::HTTP.stub?
|
38
|
+
assert_equal true, Net::HTTP.stub
|
39
|
+
assert_equal "response body", open('http://www.example.com/').read
|
40
|
+
assert_equal false, Net::HTTP.stub?
|
41
|
+
assert_equal false, Net::HTTP.stub
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_stub=
|
45
|
+
assert_equal false, Net::HTTP.stub?
|
46
|
+
Net::HTTP.stub = true
|
47
|
+
assert_equal true, Net::HTTP.stub?
|
48
|
+
Net::HTTP.stub = false
|
49
|
+
assert_equal false, Net::HTTP.stub?
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_stub_noset
|
53
|
+
Net::HTTP.stub = true
|
54
|
+
Net::HTTP.start('www.example.com') do |http|
|
55
|
+
res = http.get('/')
|
56
|
+
assert_instance_of Net::HTTPNotFound, res
|
57
|
+
end
|
58
|
+
assert_equal true, Net::HTTP.stub
|
59
|
+
end
|
60
|
+
|
61
|
+
def test_http_get_request
|
62
|
+
Net::HTTP.push "response body"
|
63
|
+
Net::HTTP.start('www.example.com') do |http|
|
64
|
+
res = http.get('/')
|
65
|
+
assert_equal "200", res.code
|
66
|
+
assert_equal "response body", res.body
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_http_get_request_has_body
|
71
|
+
Net::HTTP.push "response body"
|
72
|
+
Net::HTTP.start('www.example.com') do |http|
|
73
|
+
http.request(Net::HTTP::Get.new('/')) do |res|
|
74
|
+
assert_equal res.code, "200"
|
75
|
+
res.read_body do |str|
|
76
|
+
assert_equal "response body", str
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def test_http_get_request_twice
|
83
|
+
Net::HTTP.push "response body"
|
84
|
+
Net::HTTP.push "response body2"
|
85
|
+
Net::HTTP.start('www.example.com') do |http|
|
86
|
+
res = http.get('/')
|
87
|
+
assert_equal "response body", res.body
|
88
|
+
res = http.get('/')
|
89
|
+
assert_equal "response body2", res.body
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
def test_http_get_request_with_header
|
94
|
+
Net::HTTP.push <<-EOF
|
95
|
+
HTTP/1.1 200 OKOK
|
96
|
+
Content-Type: text/plain
|
97
|
+
Content-Length: 14
|
98
|
+
|
99
|
+
response body
|
100
|
+
EOF
|
101
|
+
Net::HTTP.start('www.example.com') do |http|
|
102
|
+
res = http.get('/')
|
103
|
+
assert_equal "OKOK", res.message
|
104
|
+
assert_equal "response body\n", res.body
|
105
|
+
assert_equal "text/plain", res['Content-Type']
|
106
|
+
assert_equal "14", res['Content-Length']
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
def test_http_get_request_with_header_404
|
111
|
+
Net::HTTP.push <<-EOF
|
112
|
+
HTTP/1.1 404 Not Found
|
113
|
+
|
114
|
+
response body
|
115
|
+
EOF
|
116
|
+
Net::HTTP.start('www.example.com') do |http|
|
117
|
+
res = http.get('/')
|
118
|
+
assert_instance_of Net::HTTPNotFound, res
|
119
|
+
assert_equal "404", res.code
|
120
|
+
assert_equal "Not Found", res.message
|
121
|
+
assert_equal "response body\n", res.body
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
def test_proxy
|
126
|
+
assert_equal false, Net::HTTP.stub
|
127
|
+
Net::HTTP.push "response body"
|
128
|
+
assert_equal true, Net::HTTP.stub
|
129
|
+
Net::HTTP.Proxy('proxy.example.com', 8080).start('www.example.com') do |http|
|
130
|
+
res = http.get('/')
|
131
|
+
assert_equal "response body", res.body
|
132
|
+
end
|
133
|
+
assert_equal false, Net::HTTP.stub
|
134
|
+
end
|
135
|
+
|
136
|
+
def test_proxy2
|
137
|
+
assert_equal false, Net::HTTP.stub
|
138
|
+
Net::HTTP.push "response body"
|
139
|
+
assert_equal true, Net::HTTP.stub
|
140
|
+
Net::HTTP.start('www.example.com', 80, 'proxy.example.com', 8080) do |http|
|
141
|
+
res = http.get('/')
|
142
|
+
assert_equal "response body", res.body
|
143
|
+
end
|
144
|
+
assert_equal false, Net::HTTP.stub
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
class OpenURITest < Test::Unit::TestCase
|
149
|
+
def test_open
|
150
|
+
Net::HTTP.push "response body"
|
151
|
+
open("http://www.example.com/") do |f|
|
152
|
+
assert_equal "response body", f.read
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
def test_uri_read
|
157
|
+
Net::HTTP.push "response body"
|
158
|
+
assert_equal "response body", URI.parse("http://www.example.com").read
|
159
|
+
end
|
160
|
+
|
161
|
+
def test_uri_open
|
162
|
+
Net::HTTP.push "response body"
|
163
|
+
assert_equal "response body", URI.parse("http://www.example.com").open.read
|
164
|
+
end
|
165
|
+
|
166
|
+
def test_redirect
|
167
|
+
Net::HTTP.push <<-EOF
|
168
|
+
HTTP/1.1 302 Found
|
169
|
+
Location: http://www.example.com/index.html
|
170
|
+
|
171
|
+
response moved.
|
172
|
+
EOF
|
173
|
+
Net::HTTP.push "response body"
|
174
|
+
assert_equal "response body", open("http://www.example.com/").read
|
175
|
+
end
|
176
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: net-httpstub
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- momotaro
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-04-25 00:00:00 +09:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: net-httpstub is a stub of Net::HTTP.
|
17
|
+
email: info@momo-lab.net
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README
|
24
|
+
- ChangeLog
|
25
|
+
files:
|
26
|
+
- README
|
27
|
+
- ChangeLog
|
28
|
+
- Rakefile
|
29
|
+
- test/net-httpstub_test.rb
|
30
|
+
- test/test_helper.rb
|
31
|
+
- lib/net/httpstub.rb
|
32
|
+
has_rdoc: true
|
33
|
+
homepage: http://net-httpstub.rubyforge.org
|
34
|
+
licenses: []
|
35
|
+
|
36
|
+
post_install_message:
|
37
|
+
rdoc_options:
|
38
|
+
- --title
|
39
|
+
- net-httpstub documentation
|
40
|
+
- --charset
|
41
|
+
- utf-8
|
42
|
+
- --opname
|
43
|
+
- index.html
|
44
|
+
- --line-numbers
|
45
|
+
- --main
|
46
|
+
- README
|
47
|
+
- --inline-source
|
48
|
+
- --exclude
|
49
|
+
- ^(examples|extras)/
|
50
|
+
require_paths:
|
51
|
+
- lib
|
52
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: "0"
|
57
|
+
version:
|
58
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: "0"
|
63
|
+
version:
|
64
|
+
requirements: []
|
65
|
+
|
66
|
+
rubyforge_project: net-httpstub
|
67
|
+
rubygems_version: 1.3.2
|
68
|
+
signing_key:
|
69
|
+
specification_version: 3
|
70
|
+
summary: net-httpstub is a stub of Net::HTTP.
|
71
|
+
test_files:
|
72
|
+
- test/test_helper.rb
|