netnoop 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,3 @@
1
+ README.md
2
+ lib/**/*.rb
3
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Michael Klett
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # NetNoop
2
+
3
+ Collects and stores your outgoing HTTP requests in NetNoop.requests for later inspection, usually in your test assertions or matchers.
4
+
5
+ Can be used in conjunction with FakeWeb to disable outbound HTTP requests while also making the contents of those requests visible.
6
+
7
+
8
+ url = URI.parse("http://www.example.com/")
9
+ req = Net::HTTP::Post.new(url.path)
10
+ res = Net::HTTP.start(url.host, url.port) {|http|
11
+ http.request(req, "Inspect this.")
12
+ }
13
+
14
+ NetNoop.requests
15
+ # => [#<NetNoop::Request:0x10052ba60 @method=:post, @uri="http://www.example.com/", @body="Inspect this.">]
16
+
17
+ NetNoop.requests.first
18
+ # => #<NetNoop::Request:0x10052ba60 @method=:post, @uri="http://www.example.com/", @body="Inspect this.">
19
+
20
+ NetNoop.request_map
21
+ # => {"http://www.example.com/"=>[#<NetNoop::Request:0x10052ba60 @method=:post, @uri="http://www.example.com/", @body="Inspect this.">]}
22
+
23
+ NetNoop.request_map["http://www.example.com/"].first
24
+ # => #<NetNoop::Request:0x10052ba60 @method=:post, @uri="http://www.example.com/", @body="Inspect this.">
25
+
26
+
27
+ Currently requires [FakeWeb](http://github.com/chrisk/fakeweb/), although this dependency can likely be later removed.
28
+
29
+ ## Note on Patches/Pull Requests
30
+
31
+ * Fork the project.
32
+ * Make your feature addition or bug fix.
33
+ * Add tests for it. This is important so I don't break it in a
34
+ future version unintentionally.
35
+ * Commit, do not mess with rakefile, version, or history.
36
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
37
+ * Send me a pull request. Bonus points for topic branches.
38
+
39
+ ## Copyright
40
+
41
+ Copyright (c) 2010 Michael Klett. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,45 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "netnoop"
8
+ gem.summary = %Q{Collects and stores your outgoing HTTP requests for later inspection}
9
+ gem.description = %Q{Collects and stores your outgoing HTTP requests in NetNoop.requests for later inspection, usually in your test assertions or matchers.\n\nCan be used in conjunction with FakeWeb to disable outbound HTTP requests while also making the contents of those requests visible.}
10
+ gem.email = "michael@webadvocate.com"
11
+ gem.homepage = "http://github.com/moklett/netnoop"
12
+ gem.authors = ["Michael Klett"]
13
+ gem.add_dependency "fakeweb"
14
+ gem.add_development_dependency "rspec", ">= 1.2.9"
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
19
+ end
20
+
21
+ require 'spec/rake/spectask'
22
+ Spec::Rake::SpecTask.new(:spec) do |spec|
23
+ spec.libs << 'lib' << 'spec'
24
+ spec.spec_files = FileList['spec/**/*_spec.rb']
25
+ end
26
+
27
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
28
+ spec.libs << 'lib' << 'spec'
29
+ spec.pattern = 'spec/**/*_spec.rb'
30
+ spec.rcov = true
31
+ end
32
+
33
+ task :spec => :check_dependencies
34
+
35
+ task :default => :spec
36
+
37
+ require 'rake/rdoctask'
38
+ Rake::RDocTask.new do |rdoc|
39
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
40
+
41
+ rdoc.rdoc_dir = 'rdoc'
42
+ rdoc.title = "netnoop #{version}"
43
+ rdoc.rdoc_files.include('README*')
44
+ rdoc.rdoc_files.include('lib/**/*.rb')
45
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/examples/basic.rb ADDED
@@ -0,0 +1,16 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ require 'netnoop'
3
+
4
+ url = URI.parse("http://www.example.com/")
5
+ req = Net::HTTP::Post.new(url.path)
6
+ res = Net::HTTP.start(url.host, url.port) {|http|
7
+ http.request(req, "Inspect this.")
8
+ }
9
+
10
+ p NetNoop.requests
11
+
12
+ p NetNoop.requests.first
13
+
14
+ p NetNoop.request_map
15
+
16
+ p NetNoop.request_map["http://www.example.com/"].first
@@ -0,0 +1,26 @@
1
+ require 'singleton'
2
+
3
+ module NetNoop
4
+ class Bucket
5
+ include Singleton
6
+
7
+ attr_accessor :request_map
8
+ attr_accessor :requests
9
+
10
+ def initialize
11
+ empty_bucket
12
+ end
13
+
14
+ def add_request(uri, method, body = nil)
15
+ req = NetNoop::Request.new(uri, method, body)
16
+ self.request_map[uri] = self.request_map[uri] + [req]
17
+ self.requests << req
18
+ end
19
+
20
+ def empty_bucket
21
+ self.request_map = Hash.new([])
22
+ self.requests = []
23
+ end
24
+ end
25
+ end
26
+
@@ -0,0 +1,36 @@
1
+ module Net
2
+ class HTTP
3
+ def request_with_netnoop(request, body = nil, &block)
4
+ uri, method = netnoop_parse_request_details(request)
5
+
6
+ NetNoop::Bucket.instance.add_request(uri, method, body)
7
+ request_without_netnoop(request, body, &block)
8
+ end
9
+ alias_method :request_without_netnoop, :request
10
+ alias_method :request, :request_with_netnoop
11
+
12
+ private
13
+
14
+ # Borrowed from FakeWeb code
15
+ def netnoop_parse_request_details(request)
16
+ protocol = use_ssl? ? "https" : "http"
17
+
18
+ path = request.path
19
+ path = URI.parse(request.path).request_uri if request.path =~ /^http/
20
+
21
+ if request["authorization"] =~ /^Basic /
22
+ userinfo = FakeWeb::Utility.decode_userinfo_from_header(request["authorization"])
23
+ userinfo = FakeWeb::Utility.encode_unsafe_chars_in_userinfo(userinfo) + "@"
24
+ else
25
+ userinfo = ""
26
+ end
27
+
28
+ # TODO: optionally store the port
29
+ # uri = "#{protocol}://#{userinfo}#{self.address}:#{self.port}#{path}"
30
+ uri = "#{protocol}://#{userinfo}#{self.address}#{path}"
31
+ method = request.method.downcase.to_sym
32
+
33
+ return uri, method
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,13 @@
1
+ module NetNoop
2
+ class Request
3
+ attr_reader :uri
4
+ attr_reader :method
5
+ attr_reader :body
6
+
7
+ def initialize(uri, method, body)
8
+ @uri = uri
9
+ @method = method
10
+ @body = body
11
+ end
12
+ end
13
+ end
data/lib/netnoop.rb ADDED
@@ -0,0 +1,26 @@
1
+ begin
2
+ require 'fakeweb'
3
+ rescue LoadError
4
+ require 'rubygems'
5
+ require 'fakeweb'
6
+ end
7
+
8
+ require 'uri'
9
+ require 'netnoop/hooks'
10
+ require 'netnoop/request'
11
+ require 'netnoop/bucket'
12
+
13
+ module NetNoop
14
+ def self.requests
15
+ Bucket.instance.requests
16
+ end
17
+
18
+ def self.request_map
19
+ Bucket.instance.request_map
20
+ end
21
+
22
+ def self.empty_bucket
23
+ Bucket.instance.empty_bucket
24
+ end
25
+ end
26
+
@@ -0,0 +1,50 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "NetNoop" do
4
+ before(:each) do
5
+ NetNoop.empty_bucket
6
+ end
7
+
8
+ context "upon a GET request" do
9
+ it "stores the request in the +requests+ array" do
10
+ perform_net_http_get(example_uri)
11
+
12
+ req = NetNoop.requests.first
13
+ req.uri.should == example_uri
14
+ req.method.should == :get
15
+ end
16
+
17
+ it "stores the request in the +request_map+ hash" do
18
+ perform_net_http_get(example_uri)
19
+
20
+ req = NetNoop.request_map[example_uri].first
21
+ req.uri.should == example_uri
22
+ req.method.should == :get
23
+ end
24
+ end
25
+
26
+ context "upon a POST request" do
27
+ it "stores the request in the +requests+ array and the body is accessible" do
28
+ body = "I am the request body"
29
+ perform_net_http_post(example_uri, body)
30
+ NetNoop.requests.first.body.should == body
31
+ end
32
+ end
33
+
34
+
35
+ def perform_net_http_get(uri)
36
+ url = URI.parse(uri)
37
+ req = Net::HTTP::Get.new(url.path)
38
+ res = Net::HTTP.start(url.host, url.port) {|http|
39
+ http.request(req)
40
+ }
41
+ end
42
+
43
+ def perform_net_http_post(uri, body)
44
+ url = URI.parse(uri)
45
+ req = Net::HTTP::Post.new(url.path)
46
+ res = Net::HTTP.start(url.host, url.port) {|http|
47
+ http.request(req, body)
48
+ }
49
+ end
50
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,18 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+
4
+ require 'netnoop'
5
+ require 'spec'
6
+ require 'spec/autorun'
7
+
8
+ def example_uri
9
+ "http://www.example.com/"
10
+ end
11
+
12
+ # FakeWeb setup
13
+ FakeWeb.allow_net_connect = false
14
+ FakeWeb.register_uri(:any, example_uri, :body => "Hello World")
15
+
16
+ Spec::Runner.configure do |config|
17
+ end
18
+
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: netnoop
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Michael Klett
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-08-11 00:00:00 -04:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: fakeweb
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: rspec
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 13
44
+ segments:
45
+ - 1
46
+ - 2
47
+ - 9
48
+ version: 1.2.9
49
+ type: :development
50
+ version_requirements: *id002
51
+ description: |-
52
+ Collects and stores your outgoing HTTP requests in NetNoop.requests for later inspection, usually in your test assertions or matchers.
53
+
54
+ Can be used in conjunction with FakeWeb to disable outbound HTTP requests while also making the contents of those requests visible.
55
+ email: michael@webadvocate.com
56
+ executables: []
57
+
58
+ extensions: []
59
+
60
+ extra_rdoc_files:
61
+ - LICENSE
62
+ - README.md
63
+ files:
64
+ - .document
65
+ - .gitignore
66
+ - LICENSE
67
+ - README.md
68
+ - Rakefile
69
+ - VERSION
70
+ - examples/basic.rb
71
+ - lib/netnoop.rb
72
+ - lib/netnoop/bucket.rb
73
+ - lib/netnoop/hooks.rb
74
+ - lib/netnoop/request.rb
75
+ - spec/netnoop_spec.rb
76
+ - spec/spec.opts
77
+ - spec/spec_helper.rb
78
+ has_rdoc: true
79
+ homepage: http://github.com/moklett/netnoop
80
+ licenses: []
81
+
82
+ post_install_message:
83
+ rdoc_options:
84
+ - --charset=UTF-8
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ hash: 3
93
+ segments:
94
+ - 0
95
+ version: "0"
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ hash: 3
102
+ segments:
103
+ - 0
104
+ version: "0"
105
+ requirements: []
106
+
107
+ rubyforge_project:
108
+ rubygems_version: 1.3.7
109
+ signing_key:
110
+ specification_version: 3
111
+ summary: Collects and stores your outgoing HTTP requests for later inspection
112
+ test_files:
113
+ - spec/netnoop_spec.rb
114
+ - spec/spec_helper.rb
115
+ - examples/basic.rb