fakeweb 1.2.8 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.autotest +5 -0
- data/CHANGELOG +16 -0
- data/LICENSE.txt +19 -281
- data/README.rdoc +21 -26
- data/Rakefile +6 -4
- data/fakeweb.gemspec +8 -5
- data/lib/fake_web.rb +52 -16
- data/lib/fake_web/ext/net_http.rb +4 -13
- data/lib/fake_web/registry.rb +20 -7
- data/lib/fake_web/responder.rb +8 -2
- data/lib/fake_web/utility.rb +22 -0
- data/test/test_allow_net_connect.rb +85 -2
- data/test/test_fake_web.rb +38 -1
- data/test/test_helper.rb +4 -1
- data/test/test_last_request.rb +29 -0
- data/test/test_response_headers.rb +7 -1
- data/test/test_utility.rb +7 -0
- metadata +30 -9
data/.autotest
ADDED
data/CHANGELOG
CHANGED
@@ -1,3 +1,19 @@
|
|
1
|
+
fakeweb (1.3.0)
|
2
|
+
|
3
|
+
* improve response header registration so you can pass an array to set a header
|
4
|
+
more than once [Myron Marston]
|
5
|
+
|
6
|
+
* fix an exception when the response's :body option was set to nil [Chris Zingel]
|
7
|
+
|
8
|
+
* fix that stubbed requests weren't mutating the Net::HTTP request object to set
|
9
|
+
the body and content-length, like real requests do [Chris Kampmeier]
|
10
|
+
|
11
|
+
* add FakeWeb.last_request [Chris Kampmeier]
|
12
|
+
|
13
|
+
* assigning a String or Regexp to FakeWeb.allow_net_connect= sets a whitelist
|
14
|
+
for outbound requests [Dan Dofter, Tim Carey-Smith, Ben Woosley]
|
15
|
+
|
16
|
+
|
1
17
|
fakeweb (1.2.8)
|
2
18
|
|
3
19
|
* support Pathname objects where a filename is expected [Chris Kampmeier]
|
data/LICENSE.txt
CHANGED
@@ -1,281 +1,19 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
of
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
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
|
-
|
1
|
+
Copyright 2006-2010 Blaine Cook, Chris Kampmeier, and other contributors
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
5
|
+
in the Software without restriction, including without limitation the rights
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
8
|
+
furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
11
|
+
all copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
THE SOFTWARE.
|
data/README.rdoc
CHANGED
@@ -131,9 +131,18 @@ behavior, which is to pass those requests through to Net::HTTP as usual).
|
|
131
131
|
Net::HTTP.get(URI.parse("http://example.com/"))
|
132
132
|
=> FakeWeb is bypassed and the response from a real request is returned
|
133
133
|
|
134
|
-
|
135
|
-
|
136
|
-
|
134
|
+
It's recommended that you set <tt>FakeWeb.allow_net_connect = false</tt> in the
|
135
|
+
setup for your tests.
|
136
|
+
|
137
|
+
==== Allowing requests to a specific server
|
138
|
+
|
139
|
+
If you want to prevent your tests from hitting the internet while allowing
|
140
|
+
access to a specific server for integration testing, you can assign a URI or
|
141
|
+
+Regexp+ to be used as a whitelist for outbound requests:
|
142
|
+
|
143
|
+
FakeWeb.allow_net_connect = %r[^https?://localhost]
|
144
|
+
Net::HTTP.get(URI.parse("http://localhost/path")) # => allowed
|
145
|
+
Net::HTTP.get(URI.parse("http://example.com/")) # => raises FakeWeb::NetConnectNotAllowedError
|
137
146
|
|
138
147
|
=== Specifying HTTP response headers
|
139
148
|
|
@@ -145,6 +154,15 @@ simply add the header as an option to +register_uri+:
|
|
145
154
|
|
146
155
|
This sets the "Content-Type" header in the response.
|
147
156
|
|
157
|
+
=== Checking the last request
|
158
|
+
|
159
|
+
It's often useful to retrieve the last request made by your code, so you can
|
160
|
+
write tests for its content. FakeWeb keeps track of the last request, whether it
|
161
|
+
was stubbed or not:
|
162
|
+
|
163
|
+
Net::HTTP.get(URI.parse("http://example.com"))
|
164
|
+
FakeWeb.last_request # => Net::HTTP::Get request object
|
165
|
+
|
148
166
|
== More info
|
149
167
|
|
150
168
|
FakeWeb lets you decouple your test environment from live services without
|
@@ -169,26 +187,3 @@ like OpenURI, as well as a ton of libraries for popular web services.
|
|
169
187
|
change to add support for request bodies in 1.3.0. Your input would be really
|
170
188
|
helpful: see http://groups.google.com/group/fakeweb-users/browse_thread/thread/44d190a6b12e4273
|
171
189
|
for a discussion of some different options. Thanks!
|
172
|
-
|
173
|
-
|
174
|
-
== Copyright
|
175
|
-
|
176
|
-
Copyright 2006-2007 Blaine Cook
|
177
|
-
|
178
|
-
Copyright 2008-2009 various contributors
|
179
|
-
|
180
|
-
FakeWeb is free software; you can redistribute it and/or modify it under the
|
181
|
-
terms of the GNU General Public License as published by the Free Software
|
182
|
-
Foundation; either version 2 of the License, or (at your option) any later
|
183
|
-
version.
|
184
|
-
|
185
|
-
FakeWeb is distributed in the hope that it will be useful, but WITHOUT ANY
|
186
|
-
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
187
|
-
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
188
|
-
details.
|
189
|
-
|
190
|
-
You should have received a copy of the GNU General Public License along
|
191
|
-
with FakeWeb; if not, write to the Free Software Foundation, Inc., 51
|
192
|
-
Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
193
|
-
|
194
|
-
See <tt>LICENSE.txt</tt> for the full terms.
|
data/Rakefile
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
require 'rake'
|
3
3
|
|
4
|
-
version = '1.
|
4
|
+
version = '1.3.0'
|
5
5
|
|
6
6
|
begin
|
7
7
|
require 'jeweler'
|
@@ -52,14 +52,16 @@ end
|
|
52
52
|
|
53
53
|
|
54
54
|
begin
|
55
|
+
require 'sdoc'
|
55
56
|
require 'rdoc/task'
|
56
57
|
Rake::RDocTask.new do |rdoc|
|
57
58
|
rdoc.main = "README.rdoc"
|
58
59
|
rdoc.rdoc_files.include("README.rdoc", "CHANGELOG", "LICENSE.txt", "lib/*.rb")
|
59
60
|
rdoc.title = "FakeWeb #{version} API Documentation"
|
60
|
-
rdoc.
|
61
|
+
rdoc.rdoc_dir = "doc"
|
62
|
+
rdoc.template = "direct"
|
63
|
+
rdoc.options << "--line-numbers" << "--show-hash" << "--charset=utf-8"
|
61
64
|
end
|
62
65
|
rescue LoadError
|
63
|
-
puts "
|
64
|
-
puts "You can try upgrading with `gem install rdoc`.\n\n"
|
66
|
+
puts "SDoc (or a dependency) not available. Install it with: gem install sdoc"
|
65
67
|
end
|
data/fakeweb.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{fakeweb}
|
8
|
-
s.version = "1.
|
8
|
+
s.version = "1.3.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Chris Kampmeier", "Blaine Cook"]
|
12
|
-
s.date = %q{
|
12
|
+
s.date = %q{2010-08-22}
|
13
13
|
s.description = %q{FakeWeb is a helper for faking web requests in Ruby. It works at a global level, without modifying code or writing extensive stubs.}
|
14
14
|
s.email = ["chris@kampers.net", "romeda@gmail.com"]
|
15
15
|
s.extra_rdoc_files = [
|
@@ -17,7 +17,8 @@ Gem::Specification.new do |s|
|
|
17
17
|
"README.rdoc"
|
18
18
|
]
|
19
19
|
s.files = [
|
20
|
-
".
|
20
|
+
".autotest",
|
21
|
+
".gitignore",
|
21
22
|
"CHANGELOG",
|
22
23
|
"LICENSE.txt",
|
23
24
|
"README.rdoc",
|
@@ -42,6 +43,7 @@ Gem::Specification.new do |s|
|
|
42
43
|
"test/test_fake_web.rb",
|
43
44
|
"test/test_fake_web_open_uri.rb",
|
44
45
|
"test/test_helper.rb",
|
46
|
+
"test/test_last_request.rb",
|
45
47
|
"test/test_missing_open_uri.rb",
|
46
48
|
"test/test_missing_pathname.rb",
|
47
49
|
"test/test_other_net_http_libraries.rb",
|
@@ -77,7 +79,7 @@ Gem::Specification.new do |s|
|
|
77
79
|
s.rdoc_options = ["--charset=UTF-8"]
|
78
80
|
s.require_paths = ["lib"]
|
79
81
|
s.rubyforge_project = %q{fakeweb}
|
80
|
-
s.rubygems_version = %q{1.3.
|
82
|
+
s.rubygems_version = %q{1.3.7}
|
81
83
|
s.summary = %q{A tool for faking responses to HTTP requests}
|
82
84
|
s.test_files = [
|
83
85
|
"test/test_allow_net_connect.rb",
|
@@ -86,6 +88,7 @@ Gem::Specification.new do |s|
|
|
86
88
|
"test/test_fake_web.rb",
|
87
89
|
"test/test_fake_web_open_uri.rb",
|
88
90
|
"test/test_helper.rb",
|
91
|
+
"test/test_last_request.rb",
|
89
92
|
"test/test_missing_open_uri.rb",
|
90
93
|
"test/test_missing_pathname.rb",
|
91
94
|
"test/test_other_net_http_libraries.rb",
|
@@ -111,7 +114,7 @@ Gem::Specification.new do |s|
|
|
111
114
|
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
112
115
|
s.specification_version = 3
|
113
116
|
|
114
|
-
if Gem::Version.new(Gem::
|
117
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
115
118
|
s.add_development_dependency(%q<mocha>, [">= 0.9.5"])
|
116
119
|
else
|
117
120
|
s.add_dependency(%q<mocha>, [">= 0.9.5"])
|
data/lib/fake_web.rb
CHANGED
@@ -13,7 +13,7 @@ FakeWeb::Utility.puts_warning_for_net_http_around_advice_libs_if_needed
|
|
13
13
|
module FakeWeb
|
14
14
|
|
15
15
|
# Returns the version string for the copy of FakeWeb you have loaded.
|
16
|
-
VERSION = '1.
|
16
|
+
VERSION = '1.3.0'
|
17
17
|
|
18
18
|
# Resets the FakeWeb Registry. This will force all subsequent web requests to
|
19
19
|
# behave as real requests.
|
@@ -33,8 +33,20 @@ module FakeWeb
|
|
33
33
|
#
|
34
34
|
# When <tt>FakeWeb.allow_net_connect = true</tt> (the default), requests to
|
35
35
|
# URIs not stubbed with FakeWeb are passed through to Net::HTTP.
|
36
|
+
#
|
37
|
+
# If you assign a +String+, +URI+, or +Regexp+ object, unstubbed requests
|
38
|
+
# will be allowed if they match that value. This is useful when you want to
|
39
|
+
# allow access to a local server for integration testing, while still
|
40
|
+
# preventing your tests from using the internet.
|
36
41
|
def self.allow_net_connect=(allowed)
|
37
|
-
|
42
|
+
case allowed
|
43
|
+
when String, URI, Regexp
|
44
|
+
@allow_all_connections = false
|
45
|
+
Registry.instance.register_passthrough_uri(allowed)
|
46
|
+
else
|
47
|
+
@allow_all_connections = allowed
|
48
|
+
Registry.instance.remove_passthrough_uri
|
49
|
+
end
|
38
50
|
end
|
39
51
|
|
40
52
|
# Enable pass-through to Net::HTTP by default.
|
@@ -43,8 +55,17 @@ module FakeWeb
|
|
43
55
|
# Returns +true+ if requests to URIs not registered with FakeWeb are passed
|
44
56
|
# through to Net::HTTP for normal processing (the default). Returns +false+
|
45
57
|
# if an exception is raised for these requests.
|
46
|
-
|
47
|
-
|
58
|
+
#
|
59
|
+
# If you've assigned a +String+, +URI+, or +Regexp+ to
|
60
|
+
# <tt>FakeWeb.allow_net_connect=</tt>, you must supply a URI to check
|
61
|
+
# against that filter. Otherwise, an ArgumentError will be raised.
|
62
|
+
def self.allow_net_connect?(uri = nil)
|
63
|
+
if Registry.instance.passthrough_uri_map.any?
|
64
|
+
raise ArgumentError, "You must supply a URI to test" if uri.nil?
|
65
|
+
Registry.instance.passthrough_uri_matches?(uri)
|
66
|
+
else
|
67
|
+
@allow_all_connections
|
68
|
+
end
|
48
69
|
end
|
49
70
|
|
50
71
|
# This exception is raised if you set <tt>FakeWeb.allow_net_connect =
|
@@ -74,7 +95,7 @@ module FakeWeb
|
|
74
95
|
# as the body of the response instead. (This used to be two options,
|
75
96
|
# <tt>:string</tt> and <tt>:file</tt>, respectively. These are now deprecated.)
|
76
97
|
# <tt>:response</tt>::
|
77
|
-
# Either
|
98
|
+
# Either a <tt>Net::HTTPResponse</tt>, an +IO+, or a +String+ which is used
|
78
99
|
# as the full response for the request.
|
79
100
|
#
|
80
101
|
# The easier way by far is to pass the <tt>:response</tt> option to
|
@@ -85,41 +106,47 @@ module FakeWeb
|
|
85
106
|
#
|
86
107
|
# To obtain a complete response document, you can use the +curl+ command,
|
87
108
|
# like so:
|
88
|
-
#
|
89
|
-
# curl -i http://
|
109
|
+
#
|
110
|
+
# curl -i http://example.com > response_from_example.com
|
90
111
|
#
|
91
112
|
# which can then be used in your test environment like so:
|
92
113
|
#
|
93
|
-
# FakeWeb.register_uri(:get,
|
114
|
+
# FakeWeb.register_uri(:get, "http://example.com", :response => "response_from_example.com")
|
94
115
|
#
|
95
116
|
# See the <tt>Net::HTTPResponse</tt>
|
96
117
|
# documentation[http://ruby-doc.org/stdlib/libdoc/net/http/rdoc/classes/Net/HTTPResponse.html]
|
97
118
|
# for more information on creating custom response objects.
|
98
119
|
#
|
99
120
|
# +options+ may also be an +Array+ containing a list of the above-described
|
100
|
-
# +Hash+. In this case, FakeWeb will rotate through each
|
101
|
-
#
|
121
|
+
# +Hash+. In this case, FakeWeb will rotate through each response. You can
|
122
|
+
# optionally repeat a response more than once before rotating:
|
102
123
|
#
|
103
124
|
# <tt>:times</tt>::
|
104
|
-
# The number of times this response will be used
|
105
|
-
#
|
106
|
-
#
|
125
|
+
# The number of times this response will be used before moving on to the
|
126
|
+
# next one. The last response will be repeated indefinitely, regardless of
|
127
|
+
# its <tt>:times</tt> parameter.
|
128
|
+
#
|
107
129
|
# Two optional arguments are also accepted:
|
108
130
|
#
|
109
131
|
# <tt>:status</tt>::
|
110
132
|
# Passing <tt>:status</tt> as a two-value array will set the response code
|
111
133
|
# and message. The defaults are <tt>200</tt> and <tt>OK</tt>, respectively.
|
112
134
|
# Example:
|
113
|
-
# FakeWeb.register_uri("http://
|
135
|
+
# FakeWeb.register_uri(:get, "http://example.com", :body => "Go away!", :status => [404, "Not Found"])
|
114
136
|
# <tt>:exception</tt>::
|
115
137
|
# The argument passed via <tt>:exception</tt> will be raised when the
|
116
138
|
# specified URL is requested. Any +Exception+ class is valid. Example:
|
117
|
-
# FakeWeb.register_uri(
|
139
|
+
# FakeWeb.register_uri(:get, "http://example.com", :exception => Net::HTTPError)
|
118
140
|
#
|
119
141
|
# If you're using the <tt>:body</tt> response type, you can pass additional
|
120
142
|
# options to specify the HTTP headers to be used in the response. Example:
|
121
143
|
#
|
122
144
|
# FakeWeb.register_uri(:get, "http://example.com/index.txt", :body => "Hello", :content_type => "text/plain")
|
145
|
+
#
|
146
|
+
# You can also pass an array of header values to include a header in the
|
147
|
+
# response more than once:
|
148
|
+
#
|
149
|
+
# FakeWeb.register_uri(:get, "http://example.com", :set_cookie => ["name=value", "example=1"])
|
123
150
|
def self.register_uri(*args)
|
124
151
|
case args.length
|
125
152
|
when 3
|
@@ -152,7 +179,7 @@ module FakeWeb
|
|
152
179
|
# FakeWeb.registered_uri?(method, uri)
|
153
180
|
#
|
154
181
|
# Returns true if a +method+ request for +uri+ is registered with FakeWeb.
|
155
|
-
# Specify a method of <tt>:any</tt> to check
|
182
|
+
# Specify a method of <tt>:any</tt> to check against all HTTP methods.
|
156
183
|
def self.registered_uri?(*args)
|
157
184
|
case args.length
|
158
185
|
when 2
|
@@ -165,6 +192,15 @@ module FakeWeb
|
|
165
192
|
end
|
166
193
|
end
|
167
194
|
|
195
|
+
# Returns the request object from the last request made via Net::HTTP.
|
196
|
+
def self.last_request
|
197
|
+
@last_request
|
198
|
+
end
|
199
|
+
|
200
|
+
def self.last_request=(request) #:nodoc:
|
201
|
+
@last_request = request
|
202
|
+
end
|
203
|
+
|
168
204
|
private
|
169
205
|
|
170
206
|
def self.print_missing_http_method_deprecation_warning(*args)
|
@@ -36,25 +36,16 @@ module Net #:nodoc: all
|
|
36
36
|
end
|
37
37
|
|
38
38
|
def request_with_fakeweb(request, body = nil, &block)
|
39
|
-
|
39
|
+
FakeWeb.last_request = request
|
40
40
|
|
41
|
-
|
42
|
-
path = URI.parse(request.path).request_uri if request.path =~ /^http/
|
43
|
-
|
44
|
-
if request["authorization"] =~ /^Basic /
|
45
|
-
userinfo = FakeWeb::Utility.decode_userinfo_from_header(request["authorization"])
|
46
|
-
userinfo = FakeWeb::Utility.encode_unsafe_chars_in_userinfo(userinfo) + "@"
|
47
|
-
else
|
48
|
-
userinfo = ""
|
49
|
-
end
|
50
|
-
|
51
|
-
uri = "#{protocol}://#{userinfo}#{self.address}:#{self.port}#{path}"
|
41
|
+
uri = FakeWeb::Utility.request_uri_as_string(self, request)
|
52
42
|
method = request.method.downcase.to_sym
|
53
43
|
|
54
44
|
if FakeWeb.registered_uri?(method, uri)
|
55
45
|
@socket = Net::HTTP.socket_type.new
|
46
|
+
FakeWeb::Utility.produce_side_effects_of_net_http_request(request, body)
|
56
47
|
FakeWeb.response_for(method, uri, &block)
|
57
|
-
elsif FakeWeb.allow_net_connect?
|
48
|
+
elsif FakeWeb.allow_net_connect?(uri)
|
58
49
|
connect_without_fakeweb
|
59
50
|
request_without_fakeweb(request, body, &block)
|
60
51
|
else
|
data/lib/fake_web/registry.rb
CHANGED
@@ -2,7 +2,7 @@ module FakeWeb
|
|
2
2
|
class Registry #:nodoc:
|
3
3
|
include Singleton
|
4
4
|
|
5
|
-
attr_accessor :uri_map
|
5
|
+
attr_accessor :uri_map, :passthrough_uri_map
|
6
6
|
|
7
7
|
def initialize
|
8
8
|
clean_registry
|
@@ -38,23 +38,36 @@ module FakeWeb
|
|
38
38
|
next_responder.response(&block)
|
39
39
|
end
|
40
40
|
|
41
|
+
def register_passthrough_uri(uri)
|
42
|
+
self.passthrough_uri_map = {normalize_uri(uri) => {:any => true}}
|
43
|
+
end
|
44
|
+
|
45
|
+
def remove_passthrough_uri
|
46
|
+
self.passthrough_uri_map = {}
|
47
|
+
end
|
48
|
+
|
49
|
+
def passthrough_uri_matches?(uri)
|
50
|
+
uri = normalize_uri(uri)
|
51
|
+
uri_map_matches(passthrough_uri_map, :any, uri, URI) ||
|
52
|
+
uri_map_matches(passthrough_uri_map, :any, uri, Regexp)
|
53
|
+
end
|
41
54
|
|
42
55
|
private
|
43
56
|
|
44
57
|
def responders_for(method, uri)
|
45
58
|
uri = normalize_uri(uri)
|
46
59
|
|
47
|
-
uri_map_matches(method, uri, URI) ||
|
48
|
-
uri_map_matches(:any, uri, URI) ||
|
49
|
-
uri_map_matches(method, uri, Regexp) ||
|
50
|
-
uri_map_matches(:any, uri, Regexp) ||
|
60
|
+
uri_map_matches(uri_map, method, uri, URI) ||
|
61
|
+
uri_map_matches(uri_map, :any, uri, URI) ||
|
62
|
+
uri_map_matches(uri_map, method, uri, Regexp) ||
|
63
|
+
uri_map_matches(uri_map, :any, uri, Regexp) ||
|
51
64
|
[]
|
52
65
|
end
|
53
66
|
|
54
|
-
def uri_map_matches(method, uri, type_to_check = URI)
|
67
|
+
def uri_map_matches(map, method, uri, type_to_check = URI)
|
55
68
|
uris_to_check = variations_of_uri_as_strings(uri)
|
56
69
|
|
57
|
-
matches =
|
70
|
+
matches = map.select { |registered_uri, method_hash|
|
58
71
|
registered_uri.is_a?(type_to_check) && method_hash.has_key?(method)
|
59
72
|
}.select { |registered_uri, method_hash|
|
60
73
|
if type_to_check == URI
|
data/lib/fake_web/responder.rb
CHANGED
@@ -23,7 +23,13 @@ module FakeWeb
|
|
23
23
|
code, msg = meta_information
|
24
24
|
response = Net::HTTPResponse.send(:response_class, code.to_s).new("1.0", code.to_s, msg)
|
25
25
|
response.instance_variable_set(:@body, body)
|
26
|
-
headers_extracted_from_options.each
|
26
|
+
headers_extracted_from_options.each do |name, value|
|
27
|
+
if value.respond_to?(:each)
|
28
|
+
value.each { |v| response.add_field(name, v) }
|
29
|
+
else
|
30
|
+
response[name] = value
|
31
|
+
end
|
32
|
+
end
|
27
33
|
end
|
28
34
|
|
29
35
|
response.instance_variable_set(:@read, true)
|
@@ -45,7 +51,7 @@ module FakeWeb
|
|
45
51
|
end
|
46
52
|
|
47
53
|
def body
|
48
|
-
return ''
|
54
|
+
return '' if options[:body].nil?
|
49
55
|
|
50
56
|
options[:body] = options[:body].to_s if defined?(Pathname) && options[:body].is_a?(Pathname)
|
51
57
|
|
data/lib/fake_web/utility.rb
CHANGED
@@ -18,6 +18,23 @@ module FakeWeb
|
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
21
|
+
# Returns a string with a normalized version of a Net::HTTP request's URI.
|
22
|
+
def self.request_uri_as_string(net_http, request)
|
23
|
+
protocol = net_http.use_ssl? ? "https" : "http"
|
24
|
+
|
25
|
+
path = request.path
|
26
|
+
path = URI.parse(request.path).request_uri if request.path =~ /^http/
|
27
|
+
|
28
|
+
if request["authorization"] =~ /^Basic /
|
29
|
+
userinfo = FakeWeb::Utility.decode_userinfo_from_header(request["authorization"])
|
30
|
+
userinfo = FakeWeb::Utility.encode_unsafe_chars_in_userinfo(userinfo) + "@"
|
31
|
+
else
|
32
|
+
userinfo = ""
|
33
|
+
end
|
34
|
+
|
35
|
+
uri = "#{protocol}://#{userinfo}#{net_http.address}:#{net_http.port}#{path}"
|
36
|
+
end
|
37
|
+
|
21
38
|
# Wrapper for URI escaping that switches between URI::Parser#escape and
|
22
39
|
# URI.escape for 1.9-compatibility
|
23
40
|
def self.uri_escape(*args)
|
@@ -28,6 +45,11 @@ module FakeWeb
|
|
28
45
|
end
|
29
46
|
end
|
30
47
|
|
48
|
+
def self.produce_side_effects_of_net_http_request(request, body)
|
49
|
+
request.set_body_internal(body)
|
50
|
+
request.content_length = request.body.length unless request.body.nil?
|
51
|
+
end
|
52
|
+
|
31
53
|
def self.puts_warning_for_net_http_around_advice_libs_if_needed
|
32
54
|
libs = {"Samuel" => defined?(Samuel)}
|
33
55
|
warnings = libs.select { |_, loaded| loaded }.map do |name, _|
|
@@ -1,7 +1,6 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
3
|
class TestFakeWebAllowNetConnect < Test::Unit::TestCase
|
4
|
-
|
5
4
|
def test_unregistered_requests_are_passed_through_when_allow_net_connect_is_true
|
6
5
|
FakeWeb.allow_net_connect = true
|
7
6
|
setup_expectations_for_real_apple_hot_news_request
|
@@ -10,11 +9,72 @@ class TestFakeWebAllowNetConnect < Test::Unit::TestCase
|
|
10
9
|
|
11
10
|
def test_raises_for_unregistered_requests_when_allow_net_connect_is_false
|
12
11
|
FakeWeb.allow_net_connect = false
|
13
|
-
|
12
|
+
assert_raise FakeWeb::NetConnectNotAllowedError do
|
14
13
|
Net::HTTP.get(URI.parse("http://example.com/"))
|
15
14
|
end
|
16
15
|
end
|
17
16
|
|
17
|
+
def test_unregistered_requests_are_passed_through_when_allow_net_connect_is_the_same_string
|
18
|
+
FakeWeb.allow_net_connect = "http://images.apple.com/main/rss/hotnews/hotnews.rss"
|
19
|
+
setup_expectations_for_real_apple_hot_news_request
|
20
|
+
Net::HTTP.get(URI.parse("http://images.apple.com/main/rss/hotnews/hotnews.rss"))
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_unregistered_requests_are_passed_through_when_allow_net_connect_is_the_same_string_with_default_port
|
24
|
+
FakeWeb.allow_net_connect = "http://images.apple.com:80/main/rss/hotnews/hotnews.rss"
|
25
|
+
setup_expectations_for_real_apple_hot_news_request
|
26
|
+
Net::HTTP.get(URI.parse("http://images.apple.com/main/rss/hotnews/hotnews.rss"))
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_unregistered_requests_are_passed_through_when_allow_net_connect_is_the_same_uri
|
30
|
+
FakeWeb.allow_net_connect = URI.parse("http://images.apple.com/main/rss/hotnews/hotnews.rss")
|
31
|
+
setup_expectations_for_real_apple_hot_news_request
|
32
|
+
Net::HTTP.get(URI.parse("http://images.apple.com/main/rss/hotnews/hotnews.rss"))
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_unregistered_requests_are_passed_through_when_allow_net_connect_is_a_matching_regexp
|
36
|
+
FakeWeb.allow_net_connect = %r[^http://images\.apple\.com]
|
37
|
+
setup_expectations_for_real_apple_hot_news_request
|
38
|
+
Net::HTTP.get(URI.parse("http://images.apple.com/main/rss/hotnews/hotnews.rss"))
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_raises_for_unregistered_requests_when_allow_net_connect_is_a_different_string
|
42
|
+
FakeWeb.allow_net_connect = "http://example.com"
|
43
|
+
assert_raise FakeWeb::NetConnectNotAllowedError do
|
44
|
+
Net::HTTP.get(URI.parse("http://example.com/path"))
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_raises_for_unregistered_requests_when_allow_net_connect_is_a_different_uri
|
49
|
+
FakeWeb.allow_net_connect = URI.parse("http://example.com")
|
50
|
+
assert_raise FakeWeb::NetConnectNotAllowedError do
|
51
|
+
Net::HTTP.get(URI.parse("http://example.com/path"))
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_raises_for_unregistered_requests_when_allow_net_connect_is_a_non_matching_regexp
|
56
|
+
FakeWeb.allow_net_connect = %r[example\.net]
|
57
|
+
assert_raise FakeWeb::NetConnectNotAllowedError do
|
58
|
+
Net::HTTP.get(URI.parse("http://example.com"))
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def test_changing_allow_net_connect_from_string_to_false_corretly_removes_whitelist
|
63
|
+
FakeWeb.allow_net_connect = "http://example.com"
|
64
|
+
FakeWeb.allow_net_connect = false
|
65
|
+
assert_raise FakeWeb::NetConnectNotAllowedError do
|
66
|
+
Net::HTTP.get(URI.parse("http://example.com"))
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_changing_allow_net_connect_from_true_to_string_corretly_limits_connections
|
71
|
+
FakeWeb.allow_net_connect = true
|
72
|
+
FakeWeb.allow_net_connect = "http://example.com"
|
73
|
+
assert_raise FakeWeb::NetConnectNotAllowedError do
|
74
|
+
Net::HTTP.get(URI.parse("http://example.net"))
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
18
78
|
def test_exception_message_includes_unregistered_request_method_and_uri_but_no_default_port
|
19
79
|
FakeWeb.allow_net_connect = false
|
20
80
|
exception = assert_raise FakeWeb::NetConnectNotAllowedError do
|
@@ -70,6 +130,29 @@ class TestFakeWebAllowNetConnect < Test::Unit::TestCase
|
|
70
130
|
assert !FakeWeb.allow_net_connect?
|
71
131
|
end
|
72
132
|
|
133
|
+
def test_question_mark_method_raises_with_no_argument_when_allow_net_connect_is_a_whitelist
|
134
|
+
FakeWeb.allow_net_connect = "http://example.com"
|
135
|
+
exception = assert_raise ArgumentError do
|
136
|
+
FakeWeb.allow_net_connect?
|
137
|
+
end
|
138
|
+
assert_equal "You must supply a URI to test", exception.message
|
139
|
+
end
|
140
|
+
|
141
|
+
def test_question_mark_method_returns_true_when_argument_is_same_uri_as_allow_net_connect_string
|
142
|
+
FakeWeb.allow_net_connect = "http://example.com"
|
143
|
+
assert FakeWeb.allow_net_connect?("http://example.com/")
|
144
|
+
end
|
145
|
+
|
146
|
+
def test_question_mark_method_returns_true_when_argument_matches_allow_net_connect_regexp
|
147
|
+
FakeWeb.allow_net_connect = %r[^https?://example.com/]
|
148
|
+
assert FakeWeb.allow_net_connect?("http://example.com/path")
|
149
|
+
assert FakeWeb.allow_net_connect?("https://example.com:443/")
|
150
|
+
end
|
151
|
+
|
152
|
+
def test_question_mark_method_returns_false_when_argument_does_not_match_allow_net_connect_regexp
|
153
|
+
FakeWeb.allow_net_connect = %r[^http://example.com/]
|
154
|
+
assert !FakeWeb.allow_net_connect?("http://example.com:8080")
|
155
|
+
end
|
73
156
|
end
|
74
157
|
|
75
158
|
|
data/test/test_fake_web.rb
CHANGED
@@ -276,6 +276,37 @@ class TestFakeWeb < Test::Unit::TestCase
|
|
276
276
|
assert_equal 'foo', response.body
|
277
277
|
end
|
278
278
|
|
279
|
+
def test_mock_post_with_body_sets_the_request_body
|
280
|
+
FakeWeb.register_uri(:post, "http://example.com/posts", :status => [201, "Created"])
|
281
|
+
http = Net::HTTP.new("example.com")
|
282
|
+
request = Net::HTTP::Post.new("/posts")
|
283
|
+
http.request(request, "title=Test")
|
284
|
+
assert_equal "title=Test", request.body
|
285
|
+
assert_equal 10, request.content_length
|
286
|
+
end
|
287
|
+
|
288
|
+
def test_mock_post_with_body_using_other_syntax_sets_the_request_body
|
289
|
+
FakeWeb.register_uri(:post, "http://example.com/posts", :status => [201, "Created"])
|
290
|
+
http = Net::HTTP.new("example.com")
|
291
|
+
request = Net::HTTP::Post.new("/posts")
|
292
|
+
request.body = "title=Test"
|
293
|
+
http.request(request)
|
294
|
+
assert_equal "title=Test", request.body
|
295
|
+
assert_equal 10, request.content_length
|
296
|
+
end
|
297
|
+
|
298
|
+
def test_real_post_with_body_sets_the_request_body
|
299
|
+
FakeWeb.allow_net_connect = true
|
300
|
+
setup_expectations_for_real_apple_hot_news_request :method => "POST",
|
301
|
+
:path => "/posts", :request_body => "title=Test"
|
302
|
+
http = Net::HTTP.new("images.apple.com")
|
303
|
+
request = Net::HTTP::Post.new("/posts")
|
304
|
+
request["Content-Type"] = "application/x-www-form-urlencoded"
|
305
|
+
http.request(request, "title=Test")
|
306
|
+
assert_equal "title=Test", request.body
|
307
|
+
assert_equal 10, request.content_length
|
308
|
+
end
|
309
|
+
|
279
310
|
def test_mock_get_with_request_as_registered_uri
|
280
311
|
fake_response = Net::HTTPOK.new('1.1', '200', 'OK')
|
281
312
|
FakeWeb.register_uri(:get, 'http://mock/test_response', :response => fake_response)
|
@@ -323,6 +354,12 @@ class TestFakeWeb < Test::Unit::TestCase
|
|
323
354
|
end
|
324
355
|
end
|
325
356
|
|
357
|
+
def test_specifying_nil_for_body
|
358
|
+
FakeWeb.register_uri(:head, "http://example.com", :body => nil)
|
359
|
+
response = Net::HTTP.start("example.com") { |query| query.head("/") }
|
360
|
+
assert_equal "", response.body
|
361
|
+
end
|
362
|
+
|
326
363
|
def test_real_http_request
|
327
364
|
FakeWeb.allow_net_connect = true
|
328
365
|
setup_expectations_for_real_apple_hot_news_request
|
@@ -547,7 +584,7 @@ class TestFakeWeb < Test::Unit::TestCase
|
|
547
584
|
end
|
548
585
|
|
549
586
|
def test_version
|
550
|
-
assert_equal "1.
|
587
|
+
assert_equal "1.3.0", FakeWeb::VERSION
|
551
588
|
end
|
552
589
|
|
553
590
|
end
|
data/test/test_helper.rb
CHANGED
@@ -56,7 +56,7 @@ module FakeWebTestHelper
|
|
56
56
|
socket.expects(:connect).with().at_least_once
|
57
57
|
else
|
58
58
|
socket = mock("TCPSocket")
|
59
|
-
Socket.expects(:===).with(socket).returns(true)
|
59
|
+
Socket.expects(:===).with(socket).at_least_once.returns(true)
|
60
60
|
end
|
61
61
|
|
62
62
|
TCPSocket.expects(:open).with(options[:host], options[:port]).returns(socket).at_least_once
|
@@ -66,6 +66,9 @@ module FakeWebTestHelper
|
|
66
66
|
# Request/response handling
|
67
67
|
request_parts = ["#{options[:method]} #{options[:path]} HTTP/1.1", "Host: #{options[:host]}"]
|
68
68
|
socket.expects(:write).with(all_of(includes(request_parts[0]), includes(request_parts[1]))).returns(100)
|
69
|
+
if !options[:request_body].nil?
|
70
|
+
socket.expects(:write).with(options[:request_body]).returns(100)
|
71
|
+
end
|
69
72
|
|
70
73
|
read_method = RUBY_VERSION >= "1.9.2" ? :read_nonblock : :sysread
|
71
74
|
socket.expects(read_method).at_least_once.returns("HTTP/1.1 #{options[:response_code]} #{options[:response_message]}\nContent-Length: #{options[:response_body].length}\n\n#{options[:response_body]}").then.raises(EOFError)
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class TestLastRequest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def test_last_request_returns_correct_net_http_request_class
|
6
|
+
FakeWeb.register_uri(:get, "http://example.com", :status => [200, "OK"])
|
7
|
+
Net::HTTP.start("example.com") { |http| http.get("/") }
|
8
|
+
assert_instance_of Net::HTTP::Get, FakeWeb.last_request
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_last_request_has_correct_method_path_and_body_for_get
|
12
|
+
FakeWeb.register_uri(:get, "http://example.com", :status => [200, "OK"])
|
13
|
+
Net::HTTP.start("example.com") { |http| http.get("/") }
|
14
|
+
assert_equal "GET", FakeWeb.last_request.method
|
15
|
+
assert_equal "/", FakeWeb.last_request.path
|
16
|
+
assert_nil FakeWeb.last_request.body
|
17
|
+
assert_nil FakeWeb.last_request.content_length
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_last_request_has_correct_method_path_and_body_for_post
|
21
|
+
FakeWeb.register_uri(:post, "http://example.com/posts", :status => [201, "Created"])
|
22
|
+
Net::HTTP.start("example.com") { |http| http.post("/posts", "title=Test") }
|
23
|
+
assert_equal "POST", FakeWeb.last_request.method
|
24
|
+
assert_equal "/posts", FakeWeb.last_request.path
|
25
|
+
assert_equal "title=Test", FakeWeb.last_request.body
|
26
|
+
assert_equal 10, FakeWeb.last_request.content_length
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
@@ -1,7 +1,6 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
3
|
class TestResponseHeaders < Test::Unit::TestCase
|
4
|
-
|
5
4
|
def test_content_type_when_registering_with_string_and_content_type_header_as_symbol_option
|
6
5
|
FakeWeb.register_uri(:get, "http://example.com/users.json", :body => '[{"username": "chrisk"}]', :content_type => "application/json")
|
7
6
|
response = Net::HTTP.start("example.com") { |query| query.get("/users.json") }
|
@@ -30,6 +29,13 @@ class TestResponseHeaders < Test::Unit::TestCase
|
|
30
29
|
assert_equal "user_id=1; example=yes", response['Set-Cookie']
|
31
30
|
end
|
32
31
|
|
32
|
+
def test_multiple_set_cookie_headers
|
33
|
+
FakeWeb.register_uri(:get, "http://example.com", :set_cookie => ["user_id=1", "example=yes"])
|
34
|
+
response = Net::HTTP.start("example.com") { |query| query.get("/") }
|
35
|
+
assert_equal ["user_id=1", "example=yes"], response.get_fields('Set-Cookie')
|
36
|
+
assert_equal "user_id=1, example=yes", response['Set-Cookie']
|
37
|
+
end
|
38
|
+
|
33
39
|
def test_registering_with_baked_response_ignores_header_options
|
34
40
|
fake_response = Net::HTTPOK.new('1.1', '200', 'OK')
|
35
41
|
fake_response["Server"] = "Apache/1.3.27 (Unix)"
|
data/test/test_utility.rb
CHANGED
@@ -67,6 +67,13 @@ class TestUtility < Test::Unit::TestCase
|
|
67
67
|
assert_equal uri, FakeWeb::Utility.strip_default_port_from_uri(uri)
|
68
68
|
end
|
69
69
|
|
70
|
+
def test_request_uri_as_string
|
71
|
+
http = Net::HTTP.new("www.example.com", 80)
|
72
|
+
request = Net::HTTP::Get.new("/index.html")
|
73
|
+
expected = "http://www.example.com:80/index.html"
|
74
|
+
assert_equal expected, FakeWeb::Utility.request_uri_as_string(http, request)
|
75
|
+
end
|
76
|
+
|
70
77
|
def test_uri_escape_delegates_to_uri_parser_when_available
|
71
78
|
parsing_object = URI.const_defined?(:Parser) ? URI::Parser.any_instance : URI
|
72
79
|
parsing_object.expects(:escape).with("string", /unsafe/).returns("escaped")
|
metadata
CHANGED
@@ -1,7 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fakeweb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
hash: 27
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 1
|
8
|
+
- 3
|
9
|
+
- 0
|
10
|
+
version: 1.3.0
|
5
11
|
platform: ruby
|
6
12
|
authors:
|
7
13
|
- Chris Kampmeier
|
@@ -10,19 +16,25 @@ autorequire:
|
|
10
16
|
bindir: bin
|
11
17
|
cert_chain: []
|
12
18
|
|
13
|
-
date:
|
19
|
+
date: 2010-08-22 00:00:00 -07:00
|
14
20
|
default_executable:
|
15
21
|
dependencies:
|
16
22
|
- !ruby/object:Gem::Dependency
|
17
23
|
name: mocha
|
18
|
-
|
19
|
-
|
20
|
-
|
24
|
+
prerelease: false
|
25
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
26
|
+
none: false
|
21
27
|
requirements:
|
22
28
|
- - ">="
|
23
29
|
- !ruby/object:Gem::Version
|
30
|
+
hash: 49
|
31
|
+
segments:
|
32
|
+
- 0
|
33
|
+
- 9
|
34
|
+
- 5
|
24
35
|
version: 0.9.5
|
25
|
-
|
36
|
+
type: :development
|
37
|
+
version_requirements: *id001
|
26
38
|
description: FakeWeb is a helper for faking web requests in Ruby. It works at a global level, without modifying code or writing extensive stubs.
|
27
39
|
email:
|
28
40
|
- chris@kampers.net
|
@@ -35,6 +47,7 @@ extra_rdoc_files:
|
|
35
47
|
- LICENSE.txt
|
36
48
|
- README.rdoc
|
37
49
|
files:
|
50
|
+
- .autotest
|
38
51
|
- .gitignore
|
39
52
|
- CHANGELOG
|
40
53
|
- LICENSE.txt
|
@@ -60,6 +73,7 @@ files:
|
|
60
73
|
- test/test_fake_web.rb
|
61
74
|
- test/test_fake_web_open_uri.rb
|
62
75
|
- test/test_helper.rb
|
76
|
+
- test/test_last_request.rb
|
63
77
|
- test/test_missing_open_uri.rb
|
64
78
|
- test/test_missing_pathname.rb
|
65
79
|
- test/test_other_net_http_libraries.rb
|
@@ -100,21 +114,27 @@ rdoc_options:
|
|
100
114
|
require_paths:
|
101
115
|
- lib
|
102
116
|
required_ruby_version: !ruby/object:Gem::Requirement
|
117
|
+
none: false
|
103
118
|
requirements:
|
104
119
|
- - ">="
|
105
120
|
- !ruby/object:Gem::Version
|
121
|
+
hash: 3
|
122
|
+
segments:
|
123
|
+
- 0
|
106
124
|
version: "0"
|
107
|
-
version:
|
108
125
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
126
|
+
none: false
|
109
127
|
requirements:
|
110
128
|
- - ">="
|
111
129
|
- !ruby/object:Gem::Version
|
130
|
+
hash: 3
|
131
|
+
segments:
|
132
|
+
- 0
|
112
133
|
version: "0"
|
113
|
-
version:
|
114
134
|
requirements: []
|
115
135
|
|
116
136
|
rubyforge_project: fakeweb
|
117
|
-
rubygems_version: 1.3.
|
137
|
+
rubygems_version: 1.3.7
|
118
138
|
signing_key:
|
119
139
|
specification_version: 3
|
120
140
|
summary: A tool for faking responses to HTTP requests
|
@@ -125,6 +145,7 @@ test_files:
|
|
125
145
|
- test/test_fake_web.rb
|
126
146
|
- test/test_fake_web_open_uri.rb
|
127
147
|
- test/test_helper.rb
|
148
|
+
- test/test_last_request.rb
|
128
149
|
- test/test_missing_open_uri.rb
|
129
150
|
- test/test_missing_pathname.rb
|
130
151
|
- test/test_other_net_http_libraries.rb
|