netnoop 0.1.0 → 0.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.
data/Rakefile CHANGED
@@ -12,6 +12,7 @@ begin
12
12
  gem.authors = ["Michael Klett"]
13
13
  gem.add_dependency "fakeweb"
14
14
  gem.add_development_dependency "rspec", ">= 1.2.9"
15
+ gem.add_development_dependency "jeweler"
15
16
  end
16
17
  Jeweler::GemcutterTasks.new
17
18
  rescue LoadError
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.2.0
@@ -8,6 +8,7 @@ end
8
8
  require 'uri'
9
9
  require 'netnoop/hooks'
10
10
  require 'netnoop/request'
11
+ require 'netnoop/response'
11
12
  require 'netnoop/bucket'
12
13
 
13
14
  module NetNoop
@@ -11,9 +11,8 @@ module NetNoop
11
11
  empty_bucket
12
12
  end
13
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]
14
+ def add_request(req)
15
+ self.request_map[req.uri] = self.request_map[req.uri] + [req]
17
16
  self.requests << req
18
17
  end
19
18
 
@@ -1,10 +1,17 @@
1
1
  module Net
2
2
  class HTTP
3
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)
4
+ response = request_without_netnoop(request, body, &block)
5
+
6
+ begin
7
+ uri, method = netnoop_parse_request_details(request)
8
+ NetNoop::Bucket.instance.add_request(
9
+ NetNoop::Request.new(uri, method, body, response)
10
+ )
11
+ rescue
12
+ end
13
+
14
+ response
8
15
  end
9
16
  alias_method :request_without_netnoop, :request
10
17
  alias_method :request, :request_with_netnoop
@@ -3,11 +3,13 @@ module NetNoop
3
3
  attr_reader :uri
4
4
  attr_reader :method
5
5
  attr_reader :body
6
+ attr_reader :response
6
7
 
7
- def initialize(uri, method, body)
8
+ def initialize(uri, method, body, http_response = nil)
8
9
  @uri = uri
9
10
  @method = method
10
11
  @body = body
12
+ @response = Response.new(http_response)
11
13
  end
12
14
  end
13
15
  end
@@ -0,0 +1,20 @@
1
+ module NetNoop
2
+ class Response
3
+ attr_reader :status
4
+ attr_reader :body
5
+ attr_reader :headers
6
+
7
+ def initialize(http_response)
8
+ @status = http_response.code
9
+ @body = http_response.body
10
+ @headers = http_response.header.to_hash
11
+ end
12
+
13
+ def content_type
14
+ if ct = headers['content-type']
15
+ return ct.first if ct.is_a?(Array)
16
+ return ct
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,69 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{netnoop}
8
+ s.version = "0.2.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Michael Klett"]
12
+ s.date = %q{2010-08-11}
13
+ s.description = %q{Collects and stores your outgoing HTTP requests in NetNoop.requests for later inspection, usually in your test assertions or matchers.
14
+
15
+ Can be used in conjunction with FakeWeb to disable outbound HTTP requests while also making the contents of those requests visible.}
16
+ s.email = %q{michael@webadvocate.com}
17
+ s.extra_rdoc_files = [
18
+ "LICENSE",
19
+ "README.md"
20
+ ]
21
+ s.files = [
22
+ ".document",
23
+ ".gitignore",
24
+ "LICENSE",
25
+ "README.md",
26
+ "Rakefile",
27
+ "VERSION",
28
+ "examples/basic.rb",
29
+ "lib/netnoop.rb",
30
+ "lib/netnoop/bucket.rb",
31
+ "lib/netnoop/hooks.rb",
32
+ "lib/netnoop/request.rb",
33
+ "lib/netnoop/response.rb",
34
+ "netnoop.gemspec",
35
+ "spec/netnoop_spec.rb",
36
+ "spec/spec.opts",
37
+ "spec/spec_helper.rb"
38
+ ]
39
+ s.homepage = %q{http://github.com/moklett/netnoop}
40
+ s.rdoc_options = ["--charset=UTF-8"]
41
+ s.require_paths = ["lib"]
42
+ s.rubygems_version = %q{1.3.7}
43
+ s.summary = %q{Collects and stores your outgoing HTTP requests for later inspection}
44
+ s.test_files = [
45
+ "spec/netnoop_spec.rb",
46
+ "spec/spec_helper.rb",
47
+ "examples/basic.rb"
48
+ ]
49
+
50
+ if s.respond_to? :specification_version then
51
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
52
+ s.specification_version = 3
53
+
54
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
55
+ s.add_runtime_dependency(%q<fakeweb>, [">= 0"])
56
+ s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
57
+ s.add_development_dependency(%q<jeweler>, [">= 0"])
58
+ else
59
+ s.add_dependency(%q<fakeweb>, [">= 0"])
60
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
61
+ s.add_dependency(%q<jeweler>, [">= 0"])
62
+ end
63
+ else
64
+ s.add_dependency(%q<fakeweb>, [">= 0"])
65
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
66
+ s.add_dependency(%q<jeweler>, [">= 0"])
67
+ end
68
+ end
69
+
@@ -6,20 +6,69 @@ describe "NetNoop" do
6
6
  end
7
7
 
8
8
  context "upon a GET request" do
9
- it "stores the request in the +requests+ array" do
9
+ attr_reader :the_stored_request
10
+
11
+ before(:each) do
10
12
  perform_net_http_get(example_uri)
13
+ @the_stored_request = NetNoop.requests.last
14
+ end
15
+
16
+ it "stores 1 request in the +NetNoop.requests+ array" do
17
+ NetNoop.requests.size.should == 1
18
+ end
19
+
20
+ it "is stores 1 request in the +NetNoop.request_map+ hash at the correct key" do
21
+ NetNoop.request_map[example_uri].size.should == 1
22
+ end
23
+
24
+ describe "the stored request" do
25
+ it "holds the URI" do
26
+ the_stored_request.uri.should == example_uri
27
+ end
11
28
 
12
- req = NetNoop.requests.first
13
- req.uri.should == example_uri
14
- req.method.should == :get
29
+ it "holds the method" do
30
+ the_stored_request.method.should == :get
31
+ end
32
+
33
+ it "has a nil body" do
34
+ the_stored_request.body.should be_nil
35
+ end
15
36
  end
16
-
17
- it "stores the request in the +request_map+ hash" do
18
- perform_net_http_get(example_uri)
37
+
38
+ describe "the stored response" do
39
+ attr_reader :uri, :response_body, :response_status, :response_content_type, :the_stored_response
19
40
 
20
- req = NetNoop.request_map[example_uri].first
21
- req.uri.should == example_uri
22
- req.method.should == :get
41
+ before(:each) do
42
+ @uri = "http://www.example.com/storeresponse"
43
+ response_hash = {
44
+ :body => @response_body = "I am the response",
45
+ :status => @response_status = '200',
46
+ }.merge({:content_type => @response_content_type = "text/plain"})
47
+
48
+ FakeWeb.register_uri(:any, uri, response_hash)
49
+ perform_net_http_get(uri)
50
+ @the_stored_response = NetNoop.requests.last.response
51
+ end
52
+
53
+ it "is present" do
54
+ the_stored_response.should_not be_nil
55
+ end
56
+
57
+ it "holds the response status" do
58
+ the_stored_response.status.should == response_status
59
+ end
60
+
61
+ it "holds the response body" do
62
+ the_stored_response.body.should == response_body
63
+ end
64
+
65
+ it "holds the response headers" do
66
+ the_stored_response.headers.should be_a(Hash)
67
+ end
68
+
69
+ it "parses the content-type" do
70
+ the_stored_response.content_type.should == response_content_type
71
+ end
23
72
  end
24
73
  end
25
74
 
@@ -31,6 +80,11 @@ describe "NetNoop" do
31
80
  end
32
81
  end
33
82
 
83
+ it "does not interfere with the original request" do
84
+ lambda {
85
+ perform_net_http_get("http://www.example.com/foobar.html")
86
+ }.should raise_error(FakeWeb::NetConnectNotAllowedError)
87
+ end
34
88
 
35
89
  def perform_net_http_get(uri)
36
90
  url = URI.parse(uri)
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: netnoop
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 23
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 1
8
+ - 2
9
9
  - 0
10
- version: 0.1.0
10
+ version: 0.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Michael Klett
@@ -48,6 +48,20 @@ dependencies:
48
48
  version: 1.2.9
49
49
  type: :development
50
50
  version_requirements: *id002
51
+ - !ruby/object:Gem::Dependency
52
+ name: jeweler
53
+ prerelease: false
54
+ requirement: &id003 !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ hash: 3
60
+ segments:
61
+ - 0
62
+ version: "0"
63
+ type: :development
64
+ version_requirements: *id003
51
65
  description: |-
52
66
  Collects and stores your outgoing HTTP requests in NetNoop.requests for later inspection, usually in your test assertions or matchers.
53
67
 
@@ -72,6 +86,8 @@ files:
72
86
  - lib/netnoop/bucket.rb
73
87
  - lib/netnoop/hooks.rb
74
88
  - lib/netnoop/request.rb
89
+ - lib/netnoop/response.rb
90
+ - netnoop.gemspec
75
91
  - spec/netnoop_spec.rb
76
92
  - spec/spec.opts
77
93
  - spec/spec_helper.rb