fakeweb 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +6 -0
- data/COPYING +281 -0
- data/README +55 -0
- data/Rakefile +68 -0
- data/lib/fake_net_http.rb +65 -0
- data/lib/fake_web.rb +273 -0
- data/test/fixtures/test_example.txt +1 -0
- data/test/fixtures/test_request +21 -0
- data/test/unit/test_examples.rb +40 -0
- data/test/unit/test_fake_web.rb +214 -0
- data/test/unit/test_fake_web_open_uri.rb +79 -0
- metadata +66 -0
data/CHANGELOG
ADDED
data/COPYING
ADDED
@@ -0,0 +1,281 @@
|
|
1
|
+
GNU GENERAL PUBLIC LICENSE
|
2
|
+
Version 2, June 1991
|
3
|
+
|
4
|
+
Copyright (C) 1989, 1991 Free Software Foundation, Inc.
|
5
|
+
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
6
|
+
Everyone is permitted to copy and distribute verbatim copies
|
7
|
+
of this license document, but changing it is not allowed.
|
8
|
+
|
9
|
+
Preamble
|
10
|
+
|
11
|
+
The licenses for most software are designed to take away your
|
12
|
+
freedom to share and change it. By contrast, the GNU General Public
|
13
|
+
License is intended to guarantee your freedom to share and change free
|
14
|
+
software--to make sure the software is free for all its users. This
|
15
|
+
General Public License applies to most of the Free Software
|
16
|
+
Foundation's software and to any other program whose authors commit to
|
17
|
+
using it. (Some other Free Software Foundation software is covered by
|
18
|
+
the GNU Lesser General Public License instead.) You can apply it to
|
19
|
+
your programs, too.
|
20
|
+
|
21
|
+
When we speak of free software, we are referring to freedom, not
|
22
|
+
price. Our General Public Licenses are designed to make sure that you
|
23
|
+
have the freedom to distribute copies of free software (and charge for
|
24
|
+
this service if you wish), that you receive source code or can get it
|
25
|
+
if you want it, that you can change the software or use pieces of it
|
26
|
+
in new free programs; and that you know you can do these things.
|
27
|
+
|
28
|
+
To protect your rights, we need to make restrictions that forbid
|
29
|
+
anyone to deny you these rights or to ask you to surrender the rights.
|
30
|
+
These restrictions translate to certain responsibilities for you if you
|
31
|
+
distribute copies of the software, or if you modify it.
|
32
|
+
|
33
|
+
For example, if you distribute copies of such a program, whether
|
34
|
+
gratis or for a fee, you must give the recipients all the rights that
|
35
|
+
you have. You must make sure that they, too, receive or can get the
|
36
|
+
source code. And you must show them these terms so they know their
|
37
|
+
rights.
|
38
|
+
|
39
|
+
We protect your rights with two steps: (1) copyright the software, and
|
40
|
+
(2) offer you this license which gives you legal permission to copy,
|
41
|
+
distribute and/or modify the software.
|
42
|
+
|
43
|
+
Also, for each author's protection and ours, we want to make certain
|
44
|
+
that everyone understands that there is no warranty for this free
|
45
|
+
software. If the software is modified by someone else and passed on, we
|
46
|
+
want its recipients to know that what they have is not the original, so
|
47
|
+
that any problems introduced by others will not reflect on the original
|
48
|
+
authors' reputations.
|
49
|
+
|
50
|
+
Finally, any free program is threatened constantly by software
|
51
|
+
patents. We wish to avoid the danger that redistributors of a free
|
52
|
+
program will individually obtain patent licenses, in effect making the
|
53
|
+
program proprietary. To prevent this, we have made it clear that any
|
54
|
+
patent must be licensed for everyone's free use or not licensed at all.
|
55
|
+
|
56
|
+
The precise terms and conditions for copying, distribution and
|
57
|
+
modification follow.
|
58
|
+
|
59
|
+
GNU GENERAL PUBLIC LICENSE
|
60
|
+
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
|
61
|
+
|
62
|
+
0. This License applies to any program or other work which contains
|
63
|
+
a notice placed by the copyright holder saying it may be distributed
|
64
|
+
under the terms of this General Public License. The "Program", below,
|
65
|
+
refers to any such program or work, and a "work based on the Program"
|
66
|
+
means either the Program or any derivative work under copyright law:
|
67
|
+
that is to say, a work containing the Program or a portion of it,
|
68
|
+
either verbatim or with modifications and/or translated into another
|
69
|
+
language. (Hereinafter, translation is included without limitation in
|
70
|
+
the term "modification".) Each licensee is addressed as "you".
|
71
|
+
|
72
|
+
Activities other than copying, distribution and modification are not
|
73
|
+
covered by this License; they are outside its scope. The act of
|
74
|
+
running the Program is not restricted, and the output from the Program
|
75
|
+
is covered only if its contents constitute a work based on the
|
76
|
+
Program (independent of having been made by running the Program).
|
77
|
+
Whether that is true depends on what the Program does.
|
78
|
+
|
79
|
+
1. You may copy and distribute verbatim copies of the Program's
|
80
|
+
source code as you receive it, in any medium, provided that you
|
81
|
+
conspicuously and appropriately publish on each copy an appropriate
|
82
|
+
copyright notice and disclaimer of warranty; keep intact all the
|
83
|
+
notices that refer to this License and to the absence of any warranty;
|
84
|
+
and give any other recipients of the Program a copy of this License
|
85
|
+
along with the Program.
|
86
|
+
|
87
|
+
You may charge a fee for the physical act of transferring a copy, and
|
88
|
+
you may at your option offer warranty protection in exchange for a fee.
|
89
|
+
|
90
|
+
2. You may modify your copy or copies of the Program or any portion
|
91
|
+
of it, thus forming a work based on the Program, and copy and
|
92
|
+
distribute such modifications or work under the terms of Section 1
|
93
|
+
above, provided that you also meet all of these conditions:
|
94
|
+
|
95
|
+
a) You must cause the modified files to carry prominent notices
|
96
|
+
stating that you changed the files and the date of any change.
|
97
|
+
|
98
|
+
b) You must cause any work that you distribute or publish, that in
|
99
|
+
whole or in part contains or is derived from the Program or any
|
100
|
+
part thereof, to be licensed as a whole at no charge to all third
|
101
|
+
parties under the terms of this License.
|
102
|
+
|
103
|
+
c) If the modified program normally reads commands interactively
|
104
|
+
when run, you must cause it, when started running for such
|
105
|
+
interactive use in the most ordinary way, to print or display an
|
106
|
+
announcement including an appropriate copyright notice and a
|
107
|
+
notice that there is no warranty (or else, saying that you provide
|
108
|
+
a warranty) and that users may redistribute the program under
|
109
|
+
these conditions, and telling the user how to view a copy of this
|
110
|
+
License. (Exception: if the Program itself is interactive but
|
111
|
+
does not normally print such an announcement, your work based on
|
112
|
+
the Program is not required to print an announcement.)
|
113
|
+
|
114
|
+
These requirements apply to the modified work as a whole. If
|
115
|
+
identifiable sections of that work are not derived from the Program,
|
116
|
+
and can be reasonably considered independent and separate works in
|
117
|
+
themselves, then this License, and its terms, do not apply to those
|
118
|
+
sections when you distribute them as separate works. But when you
|
119
|
+
distribute the same sections as part of a whole which is a work based
|
120
|
+
on the Program, the distribution of the whole must be on the terms of
|
121
|
+
this License, whose permissions for other licensees extend to the
|
122
|
+
entire whole, and thus to each and every part regardless of who wrote it.
|
123
|
+
|
124
|
+
Thus, it is not the intent of this section to claim rights or contest
|
125
|
+
your rights to work written entirely by you; rather, the intent is to
|
126
|
+
exercise the right to control the distribution of derivative or
|
127
|
+
collective works based on the Program.
|
128
|
+
|
129
|
+
In addition, mere aggregation of another work not based on the Program
|
130
|
+
with the Program (or with a work based on the Program) on a volume of
|
131
|
+
a storage or distribution medium does not bring the other work under
|
132
|
+
the scope of this License.
|
133
|
+
|
134
|
+
3. You may copy and distribute the Program (or a work based on it,
|
135
|
+
under Section 2) in object code or executable form under the terms of
|
136
|
+
Sections 1 and 2 above provided that you also do one of the following:
|
137
|
+
|
138
|
+
a) Accompany it with the complete corresponding machine-readable
|
139
|
+
source code, which must be distributed under the terms of Sections
|
140
|
+
1 and 2 above on a medium customarily used for software interchange; or,
|
141
|
+
|
142
|
+
b) Accompany it with a written offer, valid for at least three
|
143
|
+
years, to give any third party, for a charge no more than your
|
144
|
+
cost of physically performing source distribution, a complete
|
145
|
+
machine-readable copy of the corresponding source code, to be
|
146
|
+
distributed under the terms of Sections 1 and 2 above on a medium
|
147
|
+
customarily used for software interchange; or,
|
148
|
+
|
149
|
+
c) Accompany it with the information you received as to the offer
|
150
|
+
to distribute corresponding source code. (This alternative is
|
151
|
+
allowed only for noncommercial distribution and only if you
|
152
|
+
received the program in object code or executable form with such
|
153
|
+
an offer, in accord with Subsection b above.)
|
154
|
+
|
155
|
+
The source code for a work means the preferred form of the work for
|
156
|
+
making modifications to it. For an executable work, complete source
|
157
|
+
code means all the source code for all modules it contains, plus any
|
158
|
+
associated interface definition files, plus the scripts used to
|
159
|
+
control compilation and installation of the executable. However, as a
|
160
|
+
special exception, the source code distributed need not include
|
161
|
+
anything that is normally distributed (in either source or binary
|
162
|
+
form) with the major components (compiler, kernel, and so on) of the
|
163
|
+
operating system on which the executable runs, unless that component
|
164
|
+
itself accompanies the executable.
|
165
|
+
|
166
|
+
If distribution of executable or object code is made by offering
|
167
|
+
access to copy from a designated place, then offering equivalent
|
168
|
+
access to copy the source code from the same place counts as
|
169
|
+
distribution of the source code, even though third parties are not
|
170
|
+
compelled to copy the source along with the object code.
|
171
|
+
|
172
|
+
4. You may not copy, modify, sublicense, or distribute the Program
|
173
|
+
except as expressly provided under this License. Any attempt
|
174
|
+
otherwise to copy, modify, sublicense or distribute the Program is
|
175
|
+
void, and will automatically terminate your rights under this License.
|
176
|
+
However, parties who have received copies, or rights, from you under
|
177
|
+
this License will not have their licenses terminated so long as such
|
178
|
+
parties remain in full compliance.
|
179
|
+
|
180
|
+
5. You are not required to accept this License, since you have not
|
181
|
+
signed it. However, nothing else grants you permission to modify or
|
182
|
+
distribute the Program or its derivative works. These actions are
|
183
|
+
prohibited by law if you do not accept this License. Therefore, by
|
184
|
+
modifying or distributing the Program (or any work based on the
|
185
|
+
Program), you indicate your acceptance of this License to do so, and
|
186
|
+
all its terms and conditions for copying, distributing or modifying
|
187
|
+
the Program or works based on it.
|
188
|
+
|
189
|
+
6. Each time you redistribute the Program (or any work based on the
|
190
|
+
Program), the recipient automatically receives a license from the
|
191
|
+
original licensor to copy, distribute or modify the Program subject to
|
192
|
+
these terms and conditions. You may not impose any further
|
193
|
+
restrictions on the recipients' exercise of the rights granted herein.
|
194
|
+
You are not responsible for enforcing compliance by third parties to
|
195
|
+
this License.
|
196
|
+
|
197
|
+
7. If, as a consequence of a court judgment or allegation of patent
|
198
|
+
infringement or for any other reason (not limited to patent issues),
|
199
|
+
conditions are imposed on you (whether by court order, agreement or
|
200
|
+
otherwise) that contradict the conditions of this License, they do not
|
201
|
+
excuse you from the conditions of this License. If you cannot
|
202
|
+
distribute so as to satisfy simultaneously your obligations under this
|
203
|
+
License and any other pertinent obligations, then as a consequence you
|
204
|
+
may not distribute the Program at all. For example, if a patent
|
205
|
+
license would not permit royalty-free redistribution of the Program by
|
206
|
+
all those who receive copies directly or indirectly through you, then
|
207
|
+
the only way you could satisfy both it and this License would be to
|
208
|
+
refrain entirely from distribution of the Program.
|
209
|
+
|
210
|
+
If any portion of this section is held invalid or unenforceable under
|
211
|
+
any particular circumstance, the balance of the section is intended to
|
212
|
+
apply and the section as a whole is intended to apply in other
|
213
|
+
circumstances.
|
214
|
+
|
215
|
+
It is not the purpose of this section to induce you to infringe any
|
216
|
+
patents or other property right claims or to contest validity of any
|
217
|
+
such claims; this section has the sole purpose of protecting the
|
218
|
+
integrity of the free software distribution system, which is
|
219
|
+
implemented by public license practices. Many people have made
|
220
|
+
generous contributions to the wide range of software distributed
|
221
|
+
through that system in reliance on consistent application of that
|
222
|
+
system; it is up to the author/donor to decide if he or she is willing
|
223
|
+
to distribute software through any other system and a licensee cannot
|
224
|
+
impose that choice.
|
225
|
+
|
226
|
+
This section is intended to make thoroughly clear what is believed to
|
227
|
+
be a consequence of the rest of this License.
|
228
|
+
|
229
|
+
8. If the distribution and/or use of the Program is restricted in
|
230
|
+
certain countries either by patents or by copyrighted interfaces, the
|
231
|
+
original copyright holder who places the Program under this License
|
232
|
+
may add an explicit geographical distribution limitation excluding
|
233
|
+
those countries, so that distribution is permitted only in or among
|
234
|
+
countries not thus excluded. In such case, this License incorporates
|
235
|
+
the limitation as if written in the body of this License.
|
236
|
+
|
237
|
+
9. The Free Software Foundation may publish revised and/or new versions
|
238
|
+
of the General Public License from time to time. Such new versions will
|
239
|
+
be similar in spirit to the present version, but may differ in detail to
|
240
|
+
address new problems or concerns.
|
241
|
+
|
242
|
+
Each version is given a distinguishing version number. If the Program
|
243
|
+
specifies a version number of this License which applies to it and "any
|
244
|
+
later version", you have the option of following the terms and conditions
|
245
|
+
either of that version or of any later version published by the Free
|
246
|
+
Software Foundation. If the Program does not specify a version number of
|
247
|
+
this License, you may choose any version ever published by the Free Software
|
248
|
+
Foundation.
|
249
|
+
|
250
|
+
10. If you wish to incorporate parts of the Program into other free
|
251
|
+
programs whose distribution conditions are different, write to the author
|
252
|
+
to ask for permission. For software which is copyrighted by the Free
|
253
|
+
Software Foundation, write to the Free Software Foundation; we sometimes
|
254
|
+
make exceptions for this. Our decision will be guided by the two goals
|
255
|
+
of preserving the free status of all derivatives of our free software and
|
256
|
+
of promoting the sharing and reuse of software generally.
|
257
|
+
|
258
|
+
NO WARRANTY
|
259
|
+
|
260
|
+
11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
|
261
|
+
FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
|
262
|
+
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
|
263
|
+
PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
|
264
|
+
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
265
|
+
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
|
266
|
+
TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
|
267
|
+
PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
|
268
|
+
REPAIR OR CORRECTION.
|
269
|
+
|
270
|
+
12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
|
271
|
+
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
|
272
|
+
REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
|
273
|
+
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
|
274
|
+
OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
|
275
|
+
TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
|
276
|
+
YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
|
277
|
+
PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
|
278
|
+
POSSIBILITY OF SUCH DAMAGES.
|
279
|
+
|
280
|
+
END OF TERMS AND CONDITIONS
|
281
|
+
|
data/README
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
= Name
|
2
|
+
|
3
|
+
FakeWeb - Helper for Faking Web Requests
|
4
|
+
|
5
|
+
= Synopsis
|
6
|
+
|
7
|
+
:include:test/unit/test_examples.rb
|
8
|
+
|
9
|
+
= Description
|
10
|
+
|
11
|
+
FakeWeb is a helper for faking web requests. This makes testing easier,
|
12
|
+
because you can decouple your test environment from live services without
|
13
|
+
modifying code. It allows for a range of request behaviour, from simple
|
14
|
+
stubbing of HTTP responses to re-playing complete recorded responses.
|
15
|
+
|
16
|
+
In addition to the conceptual advantage of having idempotent request behaviour,
|
17
|
+
FakeWeb makes tests run faster than if they were made to remote (or even local)
|
18
|
+
web servers. It also makes it possible to run tests without a network
|
19
|
+
connection or in situations where the server is behind a firewall or has
|
20
|
+
host based access controls.
|
21
|
+
|
22
|
+
FakeWeb is tested with
|
23
|
+
Net::HTTP[http://www.ruby-doc.org/stdlib/libdoc/net/http/rdoc/index.html] and
|
24
|
+
OpenURI[http://www.ruby-doc.org/stdlib/libdoc/open-uri/rdoc/]. It should work
|
25
|
+
with any web client library that uses Net::HTTP for its underlying requests
|
26
|
+
(e.g., Flickr.rb[http://redgreenblu.com/flickr/],
|
27
|
+
Ruby/Amazon[http://www.caliban.org/ruby/ruby-amazon.shtml],
|
28
|
+
soap4r[http://dev.ctor.org/soap4r/], etc.)
|
29
|
+
|
30
|
+
= Known Issues
|
31
|
+
|
32
|
+
* The port-specific nature of requests is ignored; requests to <tt>test.com</tt> and
|
33
|
+
<tt>test.com:8080</tt> are treated identically.
|
34
|
+
* Request bodies are ignored, including GET, PUT, and POST parameters. If you
|
35
|
+
need different responses for different request bodies, you need to request
|
36
|
+
different URLs, and register different responses for each.
|
37
|
+
|
38
|
+
= Copyright
|
39
|
+
|
40
|
+
FakeWeb - Ruby Helper for Faking Web Requests
|
41
|
+
Copyright 2006 Blaine Cook <romeda@gmail.com>.
|
42
|
+
|
43
|
+
FakeWeb is free software; you can redistribute it and/or modify
|
44
|
+
it under the terms of the GNU General Public License as published by
|
45
|
+
the Free Software Foundation; either version 2 of the License, or
|
46
|
+
(at your option) any later version.
|
47
|
+
|
48
|
+
FakeWeb is distributed in the hope that it will be useful,
|
49
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
50
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
51
|
+
GNU General Public License for more details.
|
52
|
+
|
53
|
+
You should have received a copy of the GNU General Public License
|
54
|
+
along with FakeWeb; if not, write to the Free Software
|
55
|
+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
data/Rakefile
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
# FakeWeb - Ruby Helper for Faking Web Requests
|
2
|
+
# Copyright 2006 Blaine Cook <romeda@gmail.com>.
|
3
|
+
#
|
4
|
+
# FakeWeb is free software; you can redistribute it and/or modify
|
5
|
+
# it under the terms of the GNU General Public License as published by
|
6
|
+
# the Free Software Foundation; either version 2 of the License, or
|
7
|
+
# (at your option) any later version.
|
8
|
+
#
|
9
|
+
# FakeWeb is distributed in the hope that it will be useful,
|
10
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
12
|
+
# GNU General Public License for more details.
|
13
|
+
#
|
14
|
+
# You should have received a copy of the GNU General Public License
|
15
|
+
# along with FakeWeb; if not, write to the Free Software
|
16
|
+
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
17
|
+
|
18
|
+
require 'rubygems'
|
19
|
+
Gem::manage_gems
|
20
|
+
require 'rake/gempackagetask'
|
21
|
+
require 'rake/testtask'
|
22
|
+
require 'rake/rdoctask'
|
23
|
+
require 'rcov/rcovtask'
|
24
|
+
|
25
|
+
spec = Gem::Specification.new do |s|
|
26
|
+
s.name = "fakeweb"
|
27
|
+
s.version = "1.0.0"
|
28
|
+
s.author = "Blaine Cook"
|
29
|
+
s.email = "romeda@gmail.com"
|
30
|
+
s.homepage = "http://fakeweb.rubyforge.org/"
|
31
|
+
s.platform = Gem::Platform::RUBY
|
32
|
+
s.summary = "A test helper that makes it simple to test HTTP interaction"
|
33
|
+
s.files = FileList["{test,lib}/**/*", '[A-Z]*'].exclude("rdoc", ".svn").to_a
|
34
|
+
s.require_path = "lib"
|
35
|
+
s.autorequire = "fake_web"
|
36
|
+
s.test_files = Dir.glob("test/test_*.rb")
|
37
|
+
s.has_rdoc = true
|
38
|
+
s.extra_rdoc_files = ["README", "COPYING"]
|
39
|
+
s.rubyforge_project = "fakeweb"
|
40
|
+
end
|
41
|
+
|
42
|
+
Rake::GemPackageTask.new(spec) do |pkg|
|
43
|
+
pkg.gem_spec = spec
|
44
|
+
pkg.need_tar = true
|
45
|
+
pkg.need_zip = true
|
46
|
+
end
|
47
|
+
|
48
|
+
desc "Default Task"
|
49
|
+
task :default => [:tests]
|
50
|
+
|
51
|
+
desc "Run All Tests"
|
52
|
+
Rake::TestTask.new :tests do |test|
|
53
|
+
test.test_files = ["test/**/*.rb"]
|
54
|
+
test.verbose = true
|
55
|
+
end
|
56
|
+
|
57
|
+
desc "Generate Documentation"
|
58
|
+
Rake::RDocTask.new do |rdoc|
|
59
|
+
rdoc.main = "README"
|
60
|
+
rdoc.rdoc_dir = "doc"
|
61
|
+
rdoc.rdoc_files.include("README", "COPYING", "lib/*.rb")
|
62
|
+
rdoc.title = "FakeWeb"
|
63
|
+
end
|
64
|
+
|
65
|
+
Rcov::RcovTask.new do |t|
|
66
|
+
t.test_files = FileList['test/**/test*.rb']
|
67
|
+
t.rcov_opts << "--sort coverage"
|
68
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
# FakeWeb - Ruby Helper for Faking Web Requests
|
2
|
+
# Copyright 2006 Blaine Cook <romeda@gmail.com>.
|
3
|
+
#
|
4
|
+
# FakeWeb is free software; you can redistribute it and/or modify
|
5
|
+
# it under the terms of the GNU General Public License as published by
|
6
|
+
# the Free Software Foundation; either version 2 of the License, or
|
7
|
+
# (at your option) any later version.
|
8
|
+
#
|
9
|
+
# FakeWeb is distributed in the hope that it will be useful,
|
10
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
12
|
+
# GNU General Public License for more details.
|
13
|
+
#
|
14
|
+
# You should have received a copy of the GNU General Public License
|
15
|
+
# along with FakeWeb; if not, write to the Free Software
|
16
|
+
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
17
|
+
|
18
|
+
require 'net/http'
|
19
|
+
require 'net/https'
|
20
|
+
require 'stringio'
|
21
|
+
|
22
|
+
module Net #:nodoc:
|
23
|
+
|
24
|
+
class LocalMessageIO < Net::InternetMessageIO #:nodoc:
|
25
|
+
def initialize( io, debug_output = nil )
|
26
|
+
@debug_output = debug_output
|
27
|
+
@socket = case io
|
28
|
+
when Socket, IO: io
|
29
|
+
when String
|
30
|
+
File.exists?(io) ? File.open(io, "r") : StringIO.new(io)
|
31
|
+
end
|
32
|
+
raise "Unable to create local socket" unless @socket
|
33
|
+
connect
|
34
|
+
end
|
35
|
+
|
36
|
+
def connect(*args)
|
37
|
+
@rbuf = ''
|
38
|
+
end
|
39
|
+
|
40
|
+
def rbuf_fill
|
41
|
+
@rbuf << @socket.sysread(1024)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
class HTTP #:nodoc:
|
46
|
+
|
47
|
+
def HTTP.socket_type #:nodoc:
|
48
|
+
FakeWeb::SocketDelegator
|
49
|
+
end
|
50
|
+
|
51
|
+
alias :original_net_http_request :request
|
52
|
+
alias :original_net_http_do_start :do_start
|
53
|
+
|
54
|
+
def request(req, body = nil, &block)
|
55
|
+
prot = use_ssl ? "https" : "http"
|
56
|
+
uri = "#{prot}://#{self.address}#{req.path}"
|
57
|
+
if FakeWeb.registered_uri?(uri)
|
58
|
+
@socket = Net::HTTP.socket_type.new
|
59
|
+
return FakeWeb.response_for(uri, &block)
|
60
|
+
else
|
61
|
+
return original_net_http_request(req, body, &block)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
data/lib/fake_web.rb
ADDED
@@ -0,0 +1,273 @@
|
|
1
|
+
# FakeWeb - Ruby Helper for Faking Web Requests
|
2
|
+
# Copyright 2006 Blaine Cook <romeda@gmail.com>.
|
3
|
+
#
|
4
|
+
# FakeWeb is free software; you can redistribute it and/or modify
|
5
|
+
# it under the terms of the GNU General Public License as published by
|
6
|
+
# the Free Software Foundation; either version 2 of the License, or
|
7
|
+
# (at your option) any later version.
|
8
|
+
#
|
9
|
+
# FakeWeb is distributed in the hope that it will be useful,
|
10
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
12
|
+
# GNU General Public License for more details.
|
13
|
+
#
|
14
|
+
# You should have received a copy of the GNU General Public License
|
15
|
+
# along with FakeWeb; if not, write to the Free Software
|
16
|
+
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
17
|
+
|
18
|
+
require 'fake_net_http'
|
19
|
+
require 'singleton'
|
20
|
+
|
21
|
+
module OpenURI #:nodoc: all
|
22
|
+
class HTTPError < StandardError; end;
|
23
|
+
end
|
24
|
+
|
25
|
+
module FakeWeb
|
26
|
+
|
27
|
+
# Resets the FakeWeb Registry. This will force all subsequent web requests to
|
28
|
+
# behave as real requests.
|
29
|
+
def FakeWeb.clean_registry; FakeWeb::Registry.instance.clean_registry; end
|
30
|
+
|
31
|
+
# Register +uri+ to be handled according to +options+. +uri+ can be a
|
32
|
+
# +String+ or an +URI+ object. +options+ must be a +Hash+ that must contain
|
33
|
+
# any one of the following keys:
|
34
|
+
#
|
35
|
+
# <tt>:string</tt>::
|
36
|
+
# Takes a +String+ argument that is returned as the body of the response.
|
37
|
+
# FakeWeb.register_uri('http://example.com/', :string => "Hello World!")
|
38
|
+
# <tt>:file</tt>::
|
39
|
+
# Takes a valid filesystem path to a file that is slurped and returned as
|
40
|
+
# the body of the response.
|
41
|
+
# FakeWeb.register_uri('http://example.com/', :file => "/tmp/my_response_body.txt")
|
42
|
+
# <tt>:response</tt>::
|
43
|
+
# Either an <tt>Net::HTTPResponse</tt>, an +IO+ or a +String+.
|
44
|
+
#
|
45
|
+
# The easier way by far is to pass the <tt>:response</tt> option to
|
46
|
+
# +register_uri+ as a +String+ or an (open for reads) +IO+ object which
|
47
|
+
# will be used as the complete HTTP response, including headers and body.
|
48
|
+
# If the string points to a readable file, this file will be used as the
|
49
|
+
# content for the request.
|
50
|
+
#
|
51
|
+
# To obtain a complete response document, you can use the +curl+ command,
|
52
|
+
# like so:
|
53
|
+
#
|
54
|
+
# curl -i http://www.example.com/ > response_for_www.example.com
|
55
|
+
#
|
56
|
+
# which can then be used in your test environment like so:
|
57
|
+
#
|
58
|
+
# FakeWeb.register_uri('http://www.example.com/', :response => 'response_for_www.example.com')
|
59
|
+
#
|
60
|
+
# See the <tt>Net::HTTPResponse</tt>
|
61
|
+
# documentation[http://ruby-doc.org/stdlib/libdoc/net/http/rdoc/classes/Net/HTTPResponse.html]
|
62
|
+
# for more information on creating custom response objects.
|
63
|
+
#
|
64
|
+
# Two optional arguments are also accepted:
|
65
|
+
#
|
66
|
+
# <tt>:status</tt>::
|
67
|
+
# Passing <tt>:status</tt> as a two-value array will set the response code
|
68
|
+
# and message. The defaults are <tt>200</tt> and <tt>OK</tt>, respectively.
|
69
|
+
# Example:
|
70
|
+
# FakeWeb.register_uri('http://www.example.com/', :response => "Go away!", :status => [ 404, "Not Found" ])
|
71
|
+
# <tt>:exception</tt>::
|
72
|
+
# The argument passed via <tt>:exception</tt> will be raised when the
|
73
|
+
# specified URL is requested. Any +Exception+ class is valid. Example:
|
74
|
+
# FakeWeb.register_uri('http://www.example.com/', :exception => Net::HTTPError)
|
75
|
+
#
|
76
|
+
def FakeWeb.register_uri(uri, options); FakeWeb::Registry.instance.register_uri(uri, options); end
|
77
|
+
|
78
|
+
# Returns the faked Net::HTTPResponse object associated with +uri+.
|
79
|
+
def FakeWeb.response_for(uri, &block) #:nodoc: :yields: response
|
80
|
+
FakeWeb::Registry.instance.response_for(uri, &block)
|
81
|
+
end
|
82
|
+
|
83
|
+
# Checks for presence of +uri+ in the +FakeWeb+ registry.
|
84
|
+
def FakeWeb.registered_uri?(uri); FakeWeb::Registry.instance.registered_uri?(uri); end
|
85
|
+
|
86
|
+
def FakeWeb.registered_host?(*args) #:nodoc:
|
87
|
+
FakeWeb::Registry.instance.registered_host?(*args)
|
88
|
+
end
|
89
|
+
|
90
|
+
|
91
|
+
class Registry #:nodoc:
|
92
|
+
include Singleton
|
93
|
+
|
94
|
+
attr_accessor :uri_map
|
95
|
+
|
96
|
+
def initialize
|
97
|
+
clean_registry
|
98
|
+
end
|
99
|
+
|
100
|
+
def clean_registry
|
101
|
+
self.uri_map = {}
|
102
|
+
end
|
103
|
+
|
104
|
+
def register_uri(uri, options)
|
105
|
+
uri_map[normalize_uri(uri)] = FakeWeb::Responder.new(uri, options)
|
106
|
+
end
|
107
|
+
|
108
|
+
def registered_uri?(uri)
|
109
|
+
uri_map.has_key?(normalize_uri(uri))
|
110
|
+
end
|
111
|
+
|
112
|
+
def registered_host?(host)
|
113
|
+
uri_map.each_key do |uri|
|
114
|
+
return true if uri.host == host
|
115
|
+
end
|
116
|
+
false
|
117
|
+
end
|
118
|
+
|
119
|
+
def registered_uri(uri)
|
120
|
+
uri = normalize_uri(uri)
|
121
|
+
return uri_map[uri] if registered_uri?(uri)
|
122
|
+
end
|
123
|
+
|
124
|
+
def response_for(uri, &block)
|
125
|
+
return registered_uri(uri).response(&block)
|
126
|
+
end
|
127
|
+
|
128
|
+
private
|
129
|
+
|
130
|
+
def normalize_uri(uri)
|
131
|
+
case uri
|
132
|
+
when URI: uri
|
133
|
+
else
|
134
|
+
uri = 'http://' + uri unless uri.match('^https?://')
|
135
|
+
URI.parse(uri)
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
module Response #:nodoc:
|
141
|
+
def read_body(*args, &block)
|
142
|
+
yield @body if block_given?
|
143
|
+
@body
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
class Responder #:nodoc:
|
148
|
+
|
149
|
+
attr_accessor :uri, :options
|
150
|
+
|
151
|
+
def initialize(uri, options)
|
152
|
+
self.uri = uri
|
153
|
+
self.options = options
|
154
|
+
end
|
155
|
+
|
156
|
+
def response(&block)
|
157
|
+
if has_baked_response?
|
158
|
+
response = baked_response
|
159
|
+
else
|
160
|
+
code, msg = meta_information
|
161
|
+
response = Net::HTTPResponse.send(:response_class, code.to_s).new(uri, code.to_s, msg)
|
162
|
+
response.instance_variable_set(:@body, content)
|
163
|
+
end
|
164
|
+
response.instance_variable_set(:@read, true)
|
165
|
+
response.extend FakeWeb::Response
|
166
|
+
|
167
|
+
optionally_raise(response)
|
168
|
+
|
169
|
+
yield response if block_given?
|
170
|
+
|
171
|
+
response
|
172
|
+
end
|
173
|
+
|
174
|
+
private
|
175
|
+
|
176
|
+
def content
|
177
|
+
[ :file, :string ].each do |map_option|
|
178
|
+
next unless options.has_key?(map_option)
|
179
|
+
return self.send("#{map_option}_response", options[map_option])
|
180
|
+
end
|
181
|
+
|
182
|
+
return ''
|
183
|
+
end
|
184
|
+
|
185
|
+
def file_response(path)
|
186
|
+
IO.readlines(path).join("\n")
|
187
|
+
end
|
188
|
+
|
189
|
+
def string_response(string)
|
190
|
+
string
|
191
|
+
end
|
192
|
+
|
193
|
+
def baked_response
|
194
|
+
resp = case options[:response]
|
195
|
+
when Net::HTTPResponse: options[:response]
|
196
|
+
when String
|
197
|
+
socket = Net::LocalMessageIO.new(options[:response])
|
198
|
+
r = Net::HTTPResponse.read_new(socket)
|
199
|
+
r.instance_eval { @header['transfer-encoding'] = nil }
|
200
|
+
r.reading_body(socket, true) {}
|
201
|
+
r
|
202
|
+
else raise StandardError, "Handler unimplemented for response #{options[:response]}"
|
203
|
+
end
|
204
|
+
end
|
205
|
+
|
206
|
+
def has_baked_response?
|
207
|
+
options.has_key?(:response)
|
208
|
+
end
|
209
|
+
|
210
|
+
def optionally_raise(response)
|
211
|
+
return unless options.has_key?(:exception)
|
212
|
+
ex_alloc = options[:exception].allocate
|
213
|
+
ex_instance = case ex_alloc
|
214
|
+
when Net::HTTPError, OpenURI::HTTPError: options[:exception].new('Exception from FakeWeb', response)
|
215
|
+
else options[:exception].new
|
216
|
+
end
|
217
|
+
raise ex_instance
|
218
|
+
end
|
219
|
+
|
220
|
+
def meta_information
|
221
|
+
if options.has_key?(:status); options[:status]
|
222
|
+
else; [ 200, 'OK' ]
|
223
|
+
end
|
224
|
+
end
|
225
|
+
end
|
226
|
+
|
227
|
+
class SocketDelegator #:nodoc:
|
228
|
+
|
229
|
+
def self.delegate_to
|
230
|
+
Net::InternetMessageIO
|
231
|
+
end
|
232
|
+
|
233
|
+
def self.open(*args)
|
234
|
+
if FakeWeb.registered_host?(args[0])
|
235
|
+
new().open(*args)
|
236
|
+
else
|
237
|
+
delegate_to.open(*args)
|
238
|
+
end
|
239
|
+
end
|
240
|
+
|
241
|
+
def initialize(delegate=nil)
|
242
|
+
@delegate = nil
|
243
|
+
end
|
244
|
+
|
245
|
+
def method_missing(method, *args, &block)
|
246
|
+
return @delegate.send(method, *args, &block) if @delegate
|
247
|
+
return self.send("my_#{method}", *args, &block)
|
248
|
+
end
|
249
|
+
|
250
|
+
def my_open(*args)
|
251
|
+
@open_args = args
|
252
|
+
@closed = false
|
253
|
+
return self
|
254
|
+
end
|
255
|
+
|
256
|
+
def my_closed?
|
257
|
+
@closed ||= true
|
258
|
+
end
|
259
|
+
|
260
|
+
def my_reopen(*args)
|
261
|
+
@reopen_args = args
|
262
|
+
@close = false
|
263
|
+
end
|
264
|
+
|
265
|
+
def my_write(*args)
|
266
|
+
@delegate = self.class.delegate_to.open(*@open_args)
|
267
|
+
return @delegate.write(*args)
|
268
|
+
end
|
269
|
+
|
270
|
+
def my_readuntil(*args)
|
271
|
+
end
|
272
|
+
end
|
273
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
test example content
|
@@ -0,0 +1,21 @@
|
|
1
|
+
HTTP/1.1 200 OK
|
2
|
+
Cache-Control: private
|
3
|
+
Content-Type: text/html
|
4
|
+
Set-Cookie: PREF=ID=c1e3135f3180fabf:TM=1148188144:LM=1148188144:S=x9CJYC71XSKRA32Q; expires=Sun, 17-Jan-2038 19:14:07 GMT; path=/; domain=.google.com
|
5
|
+
Server: GWS/2.1
|
6
|
+
Transfer-Encoding: chunked
|
7
|
+
Date: Sun, 21 May 2006 05:09:04 GMT
|
8
|
+
|
9
|
+
<html><head><meta http-equiv="content-type" content="text/html; charset=ISO-8859-1"><title>Google</title><style><!--
|
10
|
+
body,td,a,p,.h{font-family:arial,sans-serif;}
|
11
|
+
.h{font-size: 20px;}
|
12
|
+
.q{color:#0000cc;}
|
13
|
+
-->
|
14
|
+
</style>
|
15
|
+
<script>
|
16
|
+
<!--
|
17
|
+
function sf(){document.f.q.focus();}
|
18
|
+
// -->
|
19
|
+
</script>
|
20
|
+
</head><body bgcolor=#ffffff text=#000000 link=#0000cc vlink=#551a8b alink=#ff0000 onLoad=sf() topmargin=3 marginheight=3><center><table border=0 cellspacing=0 cellpadding=0 width=100%><tr><td align=right nowrap><font size=-1><a href="/url?sa=p&pref=ig&pval=2&q=http://www.google.com/ig%3Fhl%3Den">Personalized Home</a> | <a href="https://www.google.com/accounts/Login?continue=http://www.google.com/&hl=en">Sign in</a></font></td></tr><tr height=4><td><img alt="" width=1 height=1></td></tr></table><img src="/intl/en/images/logo.gif" width=276 height=110 alt="Google"><br><br>
|
21
|
+
<form action=/search name=f><table border=0 cellspacing=0 cellpadding=4><tr><td nowrap><font size=-1><b>Web</b> <a id=1a class=q href="/imghp?hl=en&tab=wi&ie=UTF-8">Images</a> <a id=2a class=q href="http://groups.google.com/grphp?hl=en&tab=wg&ie=UTF-8">Groups</a> <a id=4a class=q href="http://news.google.com/nwshp?hl=en&tab=wn&ie=UTF-8">News</a> <a id=5a class=q href="http://froogle.google.com/frghp?hl=en&tab=wf&ie=UTF-8">Froogle</a> <a id=7a class=q href="/maphp?hl=en&tab=wl&ie=UTF-8">Maps</a> <b><a href="/intl/en/options/" class=q>more »</a></b></font></td></tr></table><table cellspacing=0 cellpadding=0><tr><td width=25%> </td><td align=center><input type=hidden name=hl value=en><input type=hidden name=ie value="ISO-8859-1"><input maxlength=2048 size=55 name=q value="" title="Google Search"><br><input type=submit value="Google Search" name=btnG><input type=submit value="I'm Feeling Lucky" name=btnI></td><td valign=top nowrap width=25%><font size=-2> <a href=/advanced_search?hl=en>Advanced Search</a><br> <a href=/preferences?hl=en>Preferences</a><br> <a href=/language_tools?hl=en>Language Tools</a></font></td></tr></table></form><br><br><font size=-1><a href="/intl/en/ads/">Advertising Programs</a> - <a href=/services/>Business Solutions</a> - <a href=/intl/en/about.html>About Google</a></font><p><font size=-2>©2006 Google</font></p></center></body></html>
|
@@ -0,0 +1,40 @@
|
|
1
|
+
$:.unshift "#{File.dirname(__FILE__)}/../../lib"
|
2
|
+
|
3
|
+
require 'test/unit'
|
4
|
+
require 'fake_web'
|
5
|
+
require 'open-uri'
|
6
|
+
|
7
|
+
class FakeWebExampleTest < Test::Unit::TestCase
|
8
|
+
def test_request
|
9
|
+
FakeWeb.register_uri('http://example.com/test_me', :string => "Hello World!")
|
10
|
+
content = Net::HTTP.get(URI.parse('http://example.com/test_me'))
|
11
|
+
assert_equal "Hello World!", content
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_request_with_response
|
15
|
+
FakeWeb.register_uri('http://www.google.com/', :response => `curl -is http://www.google.com/`)
|
16
|
+
Net::HTTP.start('www.google.com') do |req|
|
17
|
+
response = req.get('/')
|
18
|
+
assert_equal "200", response.code
|
19
|
+
assert_equal "OK", response.message
|
20
|
+
assert response.body.include?('<title>Google')
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_request_with_custom_status
|
25
|
+
FakeWeb.register_uri('http://example.com/', :string => "Nothing to be found 'round here",
|
26
|
+
:status => ['404', 'Not Found'])
|
27
|
+
Net::HTTP.start('example.com') do |req|
|
28
|
+
response = req.get('/')
|
29
|
+
assert_equal "404", response.code
|
30
|
+
assert_equal "Not Found", response.message
|
31
|
+
assert_equal "Nothing to be found 'round here", response.body
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_open_uri
|
36
|
+
FakeWeb.register_uri('http://example.com/', :string => "Hello, World!")
|
37
|
+
content = open('http://example.com/').string
|
38
|
+
assert_equal "Hello, World!", content
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,214 @@
|
|
1
|
+
# FakeWeb - Ruby Helper for Faking Web Requests
|
2
|
+
# Copyright 2006 Blaine Cook <romeda@gmail.com>.
|
3
|
+
#
|
4
|
+
# FakeWeb is free software; you can redistribute it and/or modify
|
5
|
+
# it under the terms of the GNU General Public License as published by
|
6
|
+
# the Free Software Foundation; either version 2 of the License, or
|
7
|
+
# (at your option) any later version.
|
8
|
+
#
|
9
|
+
# FakeWeb is distributed in the hope that it will be useful,
|
10
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
12
|
+
# GNU General Public License for more details.
|
13
|
+
#
|
14
|
+
# You should have received a copy of the GNU General Public License
|
15
|
+
# along with FakeWeb; if not, write to the Free Software
|
16
|
+
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
17
|
+
|
18
|
+
$:.unshift "#{File.dirname(__FILE__)}/../../lib"
|
19
|
+
|
20
|
+
require 'test/unit'
|
21
|
+
require 'fake_web'
|
22
|
+
|
23
|
+
class TestFakeWeb < Test::Unit::TestCase
|
24
|
+
|
25
|
+
def setup
|
26
|
+
FakeWeb.clean_registry
|
27
|
+
FakeWeb.register_uri('http://mock/test_example.txt', :file => File.dirname(__FILE__) + '/../fixtures/test_example.txt')
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_register_uri
|
31
|
+
assert FakeWeb.registered_uri?('http://mock/test_example.txt')
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_register_url_without_domain_name
|
35
|
+
assert_raises URI::InvalidURIError do
|
36
|
+
FakeWeb.register_uri('test_example2.txt', File.dirname(__FILE__) + '/../fixtures/test_example.txt')
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_content_for_registered_uri
|
41
|
+
assert_equal 'test example content', FakeWeb.response_for('http://mock/test_example.txt').body
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_mock_request_with_block
|
45
|
+
Net::HTTP.start('mock') do |http|
|
46
|
+
response = http.get('/test_example.txt')
|
47
|
+
assert 'test example content', response.body
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def test_mock_post
|
52
|
+
response = nil
|
53
|
+
Net::HTTP.start('mock') do |query|
|
54
|
+
response = query.post('/test_example.txt', '')
|
55
|
+
end
|
56
|
+
assert_equal 'test example content', response.body
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_mock_post_with_string_as_registered_uri
|
60
|
+
response = nil
|
61
|
+
FakeWeb.register_uri('http://mock/test_string.txt', :string => 'foo')
|
62
|
+
Net::HTTP.start('mock') do |query|
|
63
|
+
response = query.post('/test_string.txt', '')
|
64
|
+
end
|
65
|
+
assert_equal 'foo', response.body
|
66
|
+
end
|
67
|
+
|
68
|
+
def test_mock_get_with_request_as_registered_uri
|
69
|
+
fake_response = Net::HTTPOK.new('1.1', '200', 'OK')
|
70
|
+
FakeWeb.register_uri('http://mock/test_response', :response => fake_response)
|
71
|
+
response = nil
|
72
|
+
Net::HTTP.start('mock') do |query|
|
73
|
+
response = query.get('/test_response')
|
74
|
+
end
|
75
|
+
|
76
|
+
assert_equal fake_response, response
|
77
|
+
end
|
78
|
+
|
79
|
+
def test_mock_get_with_request_from_file_as_registered_uri
|
80
|
+
FakeWeb.register_uri('http://www.google.com/', :response => File.dirname(__FILE__) + '/../fixtures/test_request')
|
81
|
+
response = nil
|
82
|
+
Net::HTTP.start('www.google.com') do |query|
|
83
|
+
response = query.get('/')
|
84
|
+
end
|
85
|
+
assert_equal '200', response.code
|
86
|
+
assert response.body.include?('<title>Google</title>')
|
87
|
+
end
|
88
|
+
|
89
|
+
def test_mock_post_with_request_from_file_as_registered_uri
|
90
|
+
FakeWeb.register_uri('http://www.google.com/', :response => File.dirname(__FILE__) + '/../fixtures/test_request')
|
91
|
+
response = nil
|
92
|
+
Net::HTTP.start('www.google.com') do |query|
|
93
|
+
response = query.post('/', '')
|
94
|
+
end
|
95
|
+
assert_equal "200", response.code
|
96
|
+
assert response.body.include?('<title>Google</title>')
|
97
|
+
end
|
98
|
+
|
99
|
+
def test_proxy_request
|
100
|
+
FakeWeb.register_uri('http://www.example.com/', :string => "hello world")
|
101
|
+
FakeWeb.register_uri('http://your.proxy.host/', :string => "lala")
|
102
|
+
proxy_addr = 'your.proxy.host'
|
103
|
+
proxy_port = 8080
|
104
|
+
|
105
|
+
Net::HTTP::Proxy(proxy_addr, proxy_port).start('www.example.com') do |http|
|
106
|
+
response = http.get('/')
|
107
|
+
assert_equal "hello world", response.body
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
def test_https_request
|
112
|
+
FakeWeb.register_uri('https://www.example.com/', :string => "Hello World")
|
113
|
+
http = Net::HTTP.new('www.example.com', 443)
|
114
|
+
http.use_ssl = true
|
115
|
+
response = http.get('/')
|
116
|
+
assert_equal "Hello World", response.body
|
117
|
+
end
|
118
|
+
|
119
|
+
def test_register_unimplemented_response
|
120
|
+
FakeWeb.register_uri('http://mock/unimplemented', :response => 1)
|
121
|
+
assert_raises StandardError do
|
122
|
+
Net::HTTP.start('mock') { |q| q.get('/unimplemented') }
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
def test_real_request
|
127
|
+
resp = nil
|
128
|
+
Net::HTTP.start('images.apple.com') do |query|
|
129
|
+
resp = query.get('/main/rss/hotnews/hotnews.rss')
|
130
|
+
end
|
131
|
+
assert resp.body.include?('Apple')
|
132
|
+
assert resp.body.include?('News')
|
133
|
+
end
|
134
|
+
|
135
|
+
def test_real_request_on_same_domain_as_mock
|
136
|
+
FakeWeb.register_uri('http://images.apple.com/test_string.txt', :string => 'foo')
|
137
|
+
resp = nil
|
138
|
+
Net::HTTP.start('images.apple.com') do |query|
|
139
|
+
resp = query.get('/main/rss/hotnews/hotnews.rss')
|
140
|
+
end
|
141
|
+
assert resp.body.include?('Apple')
|
142
|
+
assert resp.body.include?('News')
|
143
|
+
end
|
144
|
+
|
145
|
+
def test_mock_request_on_real_domain
|
146
|
+
FakeWeb.register_uri('http://images.apple.com/test_string.txt', :string => 'foo')
|
147
|
+
resp = nil
|
148
|
+
Net::HTTP.start('images.apple.com') do |query|
|
149
|
+
resp = query.get('/test_string.txt')
|
150
|
+
end
|
151
|
+
assert_equal 'foo', resp.body
|
152
|
+
end
|
153
|
+
|
154
|
+
def test_mock_post_that_raises_exception
|
155
|
+
FakeWeb.register_uri('http://mock/raising_exception.txt', :exception => StandardError)
|
156
|
+
assert_raises(StandardError) do
|
157
|
+
Net::HTTP.start('mock') do |query|
|
158
|
+
query.post('/raising_exception.txt', 'some data')
|
159
|
+
end
|
160
|
+
end
|
161
|
+
end
|
162
|
+
|
163
|
+
def test_mock_post_that_raises_an_http_error
|
164
|
+
FakeWeb.register_uri('http://mock/raising_exception.txt', :exception => Net::HTTPError)
|
165
|
+
assert_raises(Net::HTTPError) do
|
166
|
+
Net::HTTP.start('mock') do |query|
|
167
|
+
query.post('/raising_exception.txt', '')
|
168
|
+
end
|
169
|
+
end
|
170
|
+
end
|
171
|
+
|
172
|
+
def test_mock_instance_syntax
|
173
|
+
response = nil
|
174
|
+
uri = URI.parse('http://mock/test_example.txt')
|
175
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
176
|
+
response = http.start do
|
177
|
+
http.get(uri.path)
|
178
|
+
end
|
179
|
+
|
180
|
+
assert_equal 'test example content', response.body
|
181
|
+
end
|
182
|
+
|
183
|
+
def test_mock_via_nil_proxy
|
184
|
+
response = nil
|
185
|
+
proxy_address = nil
|
186
|
+
proxy_port = nil
|
187
|
+
|
188
|
+
uri = URI.parse('http://mock/test_example.txt')
|
189
|
+
http = Net::HTTP::Proxy(proxy_address, proxy_port).new(
|
190
|
+
uri.host, (uri.port or 80))
|
191
|
+
response = http.start do
|
192
|
+
http.get(uri.path)
|
193
|
+
end
|
194
|
+
assert_equal 'test example content', response.body
|
195
|
+
|
196
|
+
end
|
197
|
+
|
198
|
+
def test_reponse_type
|
199
|
+
Net::HTTP.start('mock') do |http|
|
200
|
+
response = http.get('/test_example.txt', '')
|
201
|
+
assert_kind_of(Net::HTTPSuccess, response)
|
202
|
+
end
|
203
|
+
end
|
204
|
+
|
205
|
+
def test_mock_request_that_raises_an_http_error_with_a_specific_status
|
206
|
+
FakeWeb.register_uri('http://mock/raising_exception.txt', :exception => Net::HTTPError, :status => ['404', 'Not Found'])
|
207
|
+
exception = assert_raises(Net::HTTPError) do
|
208
|
+
Net::HTTP.start('mock') { |http| response = http.get('/raising_exception.txt') }
|
209
|
+
end
|
210
|
+
assert_equal '404', exception.response.code
|
211
|
+
assert_equal 'Not Found', exception.response.msg
|
212
|
+
end
|
213
|
+
|
214
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
# FakeWeb - Ruby Helper for Faking Web Requests
|
2
|
+
# Copyright 2006 Blaine Cook <romeda@gmail.com>.
|
3
|
+
#
|
4
|
+
# FakeWeb is free software; you can redistribute it and/or modify
|
5
|
+
# it under the terms of the GNU General Public License as published by
|
6
|
+
# the Free Software Foundation; either version 2 of the License, or
|
7
|
+
# (at your option) any later version.
|
8
|
+
#
|
9
|
+
# FakeWeb is distributed in the hope that it will be useful,
|
10
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
12
|
+
# GNU General Public License for more details.
|
13
|
+
#
|
14
|
+
# You should have received a copy of the GNU General Public License
|
15
|
+
# along with FakeWeb; if not, write to the Free Software
|
16
|
+
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
17
|
+
|
18
|
+
$:.unshift "#{File.dirname(__FILE__)}/../../lib"
|
19
|
+
|
20
|
+
require 'test/unit'
|
21
|
+
require 'open-uri'
|
22
|
+
require 'fake_web'
|
23
|
+
|
24
|
+
class TestFakeWebOpenURI < Test::Unit::TestCase
|
25
|
+
|
26
|
+
def setup
|
27
|
+
FakeWeb.clean_registry
|
28
|
+
FakeWeb.register_uri('http://mock/test_example.txt', :file => File.dirname(__FILE__) + '/../fixtures/test_example.txt')
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_content_for_registered_uri
|
32
|
+
assert_equal 'test example content', FakeWeb.response_for('http://mock/test_example.txt').body
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_mock_open
|
36
|
+
assert_equal 'test example content', open('http://mock/test_example.txt').string
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_mock_open_with_string_as_registered_uri
|
40
|
+
FakeWeb.register_uri('http://mock/test_string.txt', :string => 'foo')
|
41
|
+
assert_equal 'foo', open('http://mock/test_string.txt').string
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_real_open
|
45
|
+
resp = open('http://images.apple.com/main/rss/hotnews/hotnews.rss')
|
46
|
+
assert_equal "200", resp.status.first
|
47
|
+
assert resp.string.include?('Apple')
|
48
|
+
assert resp.string.include?('News')
|
49
|
+
end
|
50
|
+
|
51
|
+
def test_mock_open_that_raises_exception
|
52
|
+
FakeWeb.register_uri('http://mock/raising_exception.txt', :exception => StandardError)
|
53
|
+
assert_raises(StandardError) do
|
54
|
+
open('http://mock/raising_exception.txt')
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_mock_open_that_raises_an_http_error
|
59
|
+
FakeWeb.register_uri('http://mock/raising_exception.txt', :exception => OpenURI::HTTPError)
|
60
|
+
assert_raises(OpenURI::HTTPError) do
|
61
|
+
open('http://mock/raising_exception.txt')
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def test_mock_open_that_raises_an_http_error_with_a_specific_status
|
66
|
+
FakeWeb.register_uri('http://mock/raising_exception.txt', :exception => OpenURI::HTTPError, :status => ['123', 'jodel'])
|
67
|
+
exception = assert_raises(OpenURI::HTTPError) do
|
68
|
+
open('http://mock/raising_exception.txt')
|
69
|
+
end
|
70
|
+
assert_equal '123', exception.io.code
|
71
|
+
assert_equal 'jodel', exception.io.message
|
72
|
+
end
|
73
|
+
|
74
|
+
def test_mock_open_with_block
|
75
|
+
open('http://mock/test_example.txt') do |f|
|
76
|
+
assert 'test example content', f.readlines
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
metadata
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fakeweb
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Blaine Cook
|
8
|
+
autorequire: fake_web
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-03-11 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description:
|
17
|
+
email: romeda@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README
|
24
|
+
- COPYING
|
25
|
+
files:
|
26
|
+
- test/fixtures
|
27
|
+
- test/fixtures/test_example.txt
|
28
|
+
- test/fixtures/test_request
|
29
|
+
- test/unit
|
30
|
+
- test/unit/test_examples.rb
|
31
|
+
- test/unit/test_fake_web.rb
|
32
|
+
- test/unit/test_fake_web_open_uri.rb
|
33
|
+
- lib/fake_net_http.rb
|
34
|
+
- lib/fake_web.rb
|
35
|
+
- CHANGELOG
|
36
|
+
- COPYING
|
37
|
+
- Rakefile
|
38
|
+
- README
|
39
|
+
has_rdoc: true
|
40
|
+
homepage: http://fakeweb.rubyforge.org/
|
41
|
+
post_install_message:
|
42
|
+
rdoc_options: []
|
43
|
+
|
44
|
+
require_paths:
|
45
|
+
- lib
|
46
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: "0"
|
51
|
+
version:
|
52
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: "0"
|
57
|
+
version:
|
58
|
+
requirements: []
|
59
|
+
|
60
|
+
rubyforge_project: fakeweb
|
61
|
+
rubygems_version: 1.3.1
|
62
|
+
signing_key:
|
63
|
+
specification_version: 2
|
64
|
+
summary: A test helper that makes it simple to test HTTP interaction
|
65
|
+
test_files: []
|
66
|
+
|