fakeweb 1.1.2 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,38 +1,18 @@
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'
1
+ require File.join(File.dirname(__FILE__), "test_helper")
23
2
 
24
3
  class TestFakeWebOpenURI < Test::Unit::TestCase
25
4
 
26
5
  def setup
27
6
  FakeWeb.clean_registry
28
- FakeWeb.register_uri('http://mock/test_example.txt', :file => File.dirname(__FILE__) + '/../fixtures/test_example.txt')
29
7
  end
30
8
 
31
9
  def test_content_for_registered_uri
10
+ FakeWeb.register_uri('http://mock/test_example.txt', :file => File.dirname(__FILE__) + '/fixtures/test_example.txt')
32
11
  assert_equal 'test example content', FakeWeb.response_for('http://mock/test_example.txt').body
33
12
  end
34
13
 
35
14
  def test_mock_open
15
+ FakeWeb.register_uri('http://mock/test_example.txt', :file => File.dirname(__FILE__) + '/fixtures/test_example.txt')
36
16
  assert_equal 'test example content', open('http://mock/test_example.txt').read
37
17
  end
38
18
 
@@ -42,6 +22,7 @@ class TestFakeWebOpenURI < Test::Unit::TestCase
42
22
  end
43
23
 
44
24
  def test_real_open
25
+ setup_expectations_for_real_apple_hot_news_request
45
26
  resp = open('http://images.apple.com/main/rss/hotnews/hotnews.rss')
46
27
  assert_equal "200", resp.status.first
47
28
  body = resp.read
@@ -73,6 +54,7 @@ class TestFakeWebOpenURI < Test::Unit::TestCase
73
54
  end
74
55
 
75
56
  def test_mock_open_with_block
57
+ FakeWeb.register_uri('http://mock/test_example.txt', :file => File.dirname(__FILE__) + '/fixtures/test_example.txt')
76
58
  open('http://mock/test_example.txt') do |f|
77
59
  assert 'test example content', f.readlines
78
60
  end
@@ -0,0 +1,52 @@
1
+ $:.unshift "#{File.dirname(__FILE__)}/../lib"
2
+
3
+ require 'test/unit'
4
+ require 'open-uri'
5
+ require 'fake_web'
6
+ require 'rubygems'
7
+ require 'mocha'
8
+
9
+ module FakeWebTestHelper
10
+
11
+ # Sets several expectations (using Mocha) that a real HTTP request makes it
12
+ # past FakeWeb to the socket layer. You can use this when you need to check
13
+ # that a request isn't handled by FakeWeb.
14
+ def setup_expectations_for_real_request(options = {})
15
+ # Socket handling
16
+ if options[:port] == 443
17
+ socket = mock("SSLSocket")
18
+ OpenSSL::SSL::SSLSocket.expects(:===).with(socket).returns(true).at_least_once
19
+ OpenSSL::SSL::SSLSocket.expects(:new).with(socket, instance_of(OpenSSL::SSL::SSLContext)).returns(socket).at_least_once
20
+ socket.stubs(:sync_close=).returns(true)
21
+ socket.expects(:connect).with().at_least_once
22
+ else
23
+ socket = mock("TCPSocket")
24
+ Socket.expects(:===).with(socket).returns(true)
25
+ end
26
+
27
+ TCPSocket.expects(:open).with(options[:host], options[:port]).returns(socket).at_least_once
28
+ socket.stubs(:closed?).returns(false)
29
+ socket.stubs(:close).returns(true)
30
+
31
+ # Request/response handling
32
+ request_parts = ["#{options[:method]} #{options[:path]} HTTP/1.1", "Host: #{options[:host]}"]
33
+ socket.expects(:write).with(all_of(includes(request_parts[0]), includes(request_parts[1]))).returns(100)
34
+
35
+ # TODO: handle long response bodies that use more than one #sysread call
36
+ socket.expects(:sysread).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)
37
+ end
38
+
39
+
40
+ # A helper that calls #setup_expectations_for_real_request for you, using
41
+ # defaults for our commonly used test request to images.apple.com.
42
+ def setup_expectations_for_real_apple_hot_news_request(options = {})
43
+ defaults = { :host => "images.apple.com", :port => 80, :method => "GET",
44
+ :path => "/main/rss/hotnews/hotnews.rss",
45
+ :response_code => 200, :response_message => "OK",
46
+ :response_body => "<title>Apple Hot News</title>" }
47
+ setup_expectations_for_real_request(defaults.merge(options))
48
+ end
49
+
50
+ end
51
+
52
+ Test::Unit::TestCase.send(:include, FakeWebTestHelper)
@@ -0,0 +1,37 @@
1
+ require File.join(File.dirname(__FILE__), "test_helper")
2
+
3
+ class TestFakeWebQueryString < Test::Unit::TestCase
4
+
5
+ def setup
6
+ FakeWeb.clean_registry
7
+ end
8
+
9
+ def test_register_uri_with_query_params
10
+ FakeWeb.register_uri('http://example.com/?a=1&b=1', :string => 'foo')
11
+ assert FakeWeb.registered_uri?('http://example.com/?a=1&b=1')
12
+ end
13
+
14
+ def test_register_uri_with_query_params_and_check_in_different_order
15
+ FakeWeb.register_uri('http://example.com/?a=1&b=1', :string => 'foo')
16
+ assert FakeWeb.registered_uri?('http://example.com/?b=1&a=1')
17
+ end
18
+
19
+ def test_registered_uri_gets_recognized_with_empty_query_params
20
+ FakeWeb.register_uri('http://example.com/', :string => 'foo')
21
+ assert FakeWeb.registered_uri?('http://example.com/?')
22
+ end
23
+
24
+ def test_register_uri_with_empty_query_params_and_check_with_none
25
+ FakeWeb.register_uri('http://example.com/?', :string => 'foo')
26
+ assert FakeWeb.registered_uri?('http://example.com/')
27
+ end
28
+
29
+ def test_registry_sort_query_params
30
+ assert_equal "a=1&b=2", FakeWeb::Registry.instance.send(:sort_query_params, "b=2&a=1")
31
+ end
32
+
33
+ def test_registry_sort_query_params_sorts_by_value_if_keys_collide
34
+ assert_equal "a=1&a=2&b=2", FakeWeb::Registry.instance.send(:sort_query_params, "a=2&b=2&a=1")
35
+ end
36
+
37
+ end
metadata CHANGED
@@ -1,65 +1,77 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fakeweb
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.2
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Blaine Cook
8
+ - Chris Kampmeier
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
12
 
12
- date: 2009-03-11 00:00:00 -07:00
13
+ date: 2009-03-07 00:00:00 -08:00
13
14
  default_executable:
14
15
  dependencies:
15
16
  - !ruby/object:Gem::Dependency
16
- name: rake
17
- type: :runtime
17
+ name: mocha
18
+ type: :development
18
19
  version_requirement:
19
20
  version_requirements: !ruby/object:Gem::Requirement
20
21
  requirements:
21
22
  - - ">="
22
23
  - !ruby/object:Gem::Version
23
- version: "0"
24
- version:
25
- - !ruby/object:Gem::Dependency
26
- name: rcov
27
- type: :runtime
28
- version_requirement:
29
- version_requirements: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - ">="
32
- - !ruby/object:Gem::Version
33
- version: "0"
24
+ version: 0.9.5
34
25
  version:
35
26
  description:
36
- email: romeda@gmail.com
27
+ email: chris@kampers.net
37
28
  executables: []
38
29
 
39
30
  extensions: []
40
31
 
41
32
  extra_rdoc_files:
42
- - README
43
- - COPYING
33
+ - CHANGELOG
34
+ - LICENSE.txt
35
+ - README.rdoc
44
36
  files:
45
- - test/fixtures
46
- - test/fixtures/test_example.txt
47
- - test/fixtures/test_request
48
- - test/unit
49
- - test/unit/test_examples.rb
50
- - test/unit/test_fake_web.rb
51
- - test/unit/test_fake_web_open_uri.rb
52
- - lib/fake_net_http.rb
53
- - lib/fake_web.rb
54
37
  - CHANGELOG
55
- - COPYING
38
+ - LICENSE.txt
39
+ - README.rdoc
56
40
  - Rakefile
57
- - README
41
+ - lib
42
+ - lib/fake_web
43
+ - lib/fake_web.rb
44
+ - lib/fake_web/ext
45
+ - lib/fake_web/ext/net_http.rb
46
+ - lib/fake_web/registry.rb
47
+ - lib/fake_web/responder.rb
48
+ - lib/fake_web/response.rb
49
+ - lib/fake_web/stub_socket.rb
50
+ - lib/fakeweb.rb
51
+ - test
52
+ - test/fixtures
53
+ - test/fixtures/google_response_from_curl
54
+ - test/fixtures/google_response_with_transfer_encoding
55
+ - test/fixtures/google_response_without_transfer_encoding
56
+ - test/fixtures/test_example.txt
57
+ - test/fixtures/test_txt_file
58
+ - test/test_allow_net_connect.rb
59
+ - test/test_fake_web.rb
60
+ - test/test_fake_web_open_uri.rb
61
+ - test/test_helper.rb
62
+ - test/test_query_string.rb
58
63
  has_rdoc: true
59
- homepage: http://fakeweb.rubyforge.org/
64
+ homepage: http://github.com/chrisk/fakeweb
60
65
  post_install_message:
61
- rdoc_options: []
62
-
66
+ rdoc_options:
67
+ - --main
68
+ - README.rdoc
69
+ - --title
70
+ - FakeWeb API Documentation
71
+ - --charset
72
+ - utf-8
73
+ - --line-numbers
74
+ - --inline-source
63
75
  require_paths:
64
76
  - lib
65
77
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -80,6 +92,16 @@ rubyforge_project: fakeweb
80
92
  rubygems_version: 1.3.1
81
93
  signing_key:
82
94
  specification_version: 2
83
- summary: A test helper that makes it simple to test HTTP interaction
84
- test_files: []
85
-
95
+ summary: A tool for faking responses to HTTP requests
96
+ test_files:
97
+ - test/fixtures
98
+ - test/fixtures/google_response_from_curl
99
+ - test/fixtures/google_response_with_transfer_encoding
100
+ - test/fixtures/google_response_without_transfer_encoding
101
+ - test/fixtures/test_example.txt
102
+ - test/fixtures/test_txt_file
103
+ - test/test_allow_net_connect.rb
104
+ - test/test_fake_web.rb
105
+ - test/test_fake_web_open_uri.rb
106
+ - test/test_helper.rb
107
+ - test/test_query_string.rb
data/README DELETED
@@ -1,55 +0,0 @@
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
@@ -1,69 +0,0 @@
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 BufferedIO #:nodoc:
25
- def initialize( io, debug_output = nil )
26
- @debug_output = debug_output
27
- @io = 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 @io
33
- connect
34
- end
35
-
36
- def connect(*args)
37
- @rbuf = ''
38
- end
39
-
40
- def rbuf_fill
41
- @rbuf << @io.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_connect :connect
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
- original_net_http_connect
62
- return original_net_http_request(req, body, &block)
63
- end
64
- end
65
-
66
- def connect
67
- end
68
- end
69
- end
@@ -1,21 +0,0 @@
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>&nbsp;|&nbsp;<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>&nbsp;&nbsp;&nbsp;&nbsp;<a id=1a class=q href="/imghp?hl=en&tab=wi&ie=UTF-8">Images</a>&nbsp;&nbsp;&nbsp;&nbsp;<a id=2a class=q href="http://groups.google.com/grphp?hl=en&tab=wg&ie=UTF-8">Groups</a>&nbsp;&nbsp;&nbsp;&nbsp;<a id=4a class=q href="http://news.google.com/nwshp?hl=en&tab=wn&ie=UTF-8">News</a>&nbsp;&nbsp;&nbsp;&nbsp;<a id=5a class=q href="http://froogle.google.com/frghp?hl=en&tab=wf&ie=UTF-8">Froogle</a>&nbsp;&nbsp;&nbsp;&nbsp;<a id=7a class=q href="/maphp?hl=en&tab=wl&ie=UTF-8">Maps</a>&nbsp;&nbsp;&nbsp;&nbsp;<b><a href="/intl/en/options/" class=q>more&nbsp;&raquo;</a></b></font></td></tr></table><table cellspacing=0 cellpadding=0><tr><td width=25%>&nbsp;</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>&nbsp;&nbsp;<a href=/advanced_search?hl=en>Advanced Search</a><br>&nbsp;&nbsp;<a href=/preferences?hl=en>Preferences</a><br>&nbsp;&nbsp;<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&nbsp;Programs</a> - <a href=/services/>Business Solutions</a> - <a href=/intl/en/about.html>About Google</a></font><p><font size=-2>&copy;2006 Google</font></p></center></body></html>
@@ -1,45 +0,0 @@
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
- if response.code == 200
19
- assert_equal "OK", response.message
20
- assert response.body.include?('<title>Google')
21
- elsif response.code == 302
22
- # Google redirects foreign sites to ccTLDs.
23
- assert_equal "Found", response.message
24
- assert response.body.include?('The document has moved')
25
- end
26
- end
27
- end
28
-
29
- def test_request_with_custom_status
30
- FakeWeb.register_uri('http://example.com/', :string => "Nothing to be found 'round here",
31
- :status => ['404', 'Not Found'])
32
- Net::HTTP.start('example.com') do |req|
33
- response = req.get('/')
34
- assert_equal "404", response.code
35
- assert_equal "Not Found", response.message
36
- assert_equal "Nothing to be found 'round here", response.body
37
- end
38
- end
39
-
40
- def test_open_uri
41
- FakeWeb.register_uri('http://example.com/', :string => "Hello, World!")
42
- content = open('http://example.com/').string
43
- assert_equal "Hello, World!", content
44
- end
45
- end