responsalizr 1.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,28 @@
1
+ Copyright (c) 2010, Nathaniel Ritmeyer
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without
5
+ modification, are permitted provided that the following conditions are met:
6
+
7
+ 1. Redistributions of source code must retain the above copyright notice,
8
+ this list of conditions and the following disclaimer.
9
+
10
+ 2. Redistributions in binary form must reproduce the above copyright
11
+ notice, this list of conditions and the following disclaimer in the
12
+ documentation and/or other materials provided with the distribution.
13
+
14
+ 3. Neither the name Nathaniel Ritmeyer nor the names of contributors to
15
+ this software may be used to endorse or promote products derived from this
16
+ software without specific prior written permission.
17
+
18
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
19
+ IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20
+ THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21
+ PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
22
+ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23
+ EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24
+ PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25
+ OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26
+ WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27
+ OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28
+ ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
data/README ADDED
@@ -0,0 +1,3 @@
1
+ == responsalizr
2
+
3
+ You should document your project here.
data/Rakefile ADDED
@@ -0,0 +1,45 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'rake/clean'
4
+ require 'rake/gempackagetask'
5
+ require 'rake/rdoctask'
6
+ require 'rake/testtask'
7
+ require 'spec/rake/spectask'
8
+
9
+ spec = Gem::Specification.new do |s|
10
+ s.name = 'responsalizr'
11
+ s.version = '1.0'
12
+ s.has_rdoc = false
13
+ s.extra_rdoc_files = ['README', 'LICENSE']
14
+ s.summary = 'Test HTTP responses in ruby'
15
+ s.description = s.summary
16
+ s.author = 'Nat Ritmeyer'
17
+ s.email = 'nat@natontesting.com'
18
+ s.files = %w(LICENSE README Rakefile) + Dir.glob("{bin,lib,spec}/**/*")
19
+ s.require_path = "lib"
20
+ s.bindir = "bin"
21
+ end
22
+
23
+ Rake::GemPackageTask.new(spec) do |p|
24
+ p.gem_spec = spec
25
+ p.need_tar = true
26
+ p.need_zip = true
27
+ end
28
+
29
+ #Rake::RDocTask.new do |rdoc|
30
+ # files =['README', 'LICENSE', 'lib/**/*.rb']
31
+ # rdoc.rdoc_files.add(files)
32
+ # rdoc.main = "README" # page to start on
33
+ # rdoc.title = "responsalizr Docs"
34
+ # rdoc.rdoc_dir = 'doc/rdoc' # rdoc output folder
35
+ # rdoc.options << '--line-numbers'
36
+ #end
37
+
38
+ Rake::TestTask.new do |t|
39
+ t.test_files = FileList['test/**/*.rb']
40
+ end
41
+
42
+ Spec::Rake::SpecTask.new do |t|
43
+ t.spec_files = FileList['spec/**/*.rb']
44
+ t.libs << Dir["lib"]
45
+ end
@@ -0,0 +1,38 @@
1
+ #Copyright (c) 2010, Nathaniel Ritmeyer. All rights reserved.
2
+
3
+ require 'rubygems'
4
+ require 'net/http'
5
+ require 'active_support/core_ext/string/inflections'
6
+
7
+ module Responsalizr
8
+ class Response
9
+ def self.from(url_string = nil, proxy = {:proxy_host => nil, :port => nil})
10
+ raise ArgumentError if url_string.nil?
11
+ Net::HTTP.Proxy(proxy[:proxy_host], proxy[:port]).get_response(URI.parse(url_string))
12
+ end
13
+ end
14
+
15
+ Net::HTTPResponse.send :define_method, :code? do |expected_code|
16
+ unless Net::HTTPResponse.const_get("CODE_TO_OBJ").keys.collect{|key| key.to_i}.include?(expected_code)
17
+ raise ArgumentError.new "#{expected_code} not a valid response code"
18
+ end
19
+ self.code.to_i == expected_code
20
+ end
21
+
22
+ all_response_classes = []
23
+
24
+ ObjectSpace.each_object(Class) do |some_class|
25
+ next unless some_class.ancestors.include?(Net::HTTPResponse) and (some_class != Net::HTTPResponse)
26
+ all_response_classes << some_class
27
+ end
28
+
29
+ all_response_classes.each do |response_class|
30
+ underscored_class_name = response_class.name.demodulize.underscore
31
+ question_method_name = underscored_class_name.gsub(/^http_?/, "") + "?"
32
+ Net::HTTPResponse.send :define_method, question_method_name.to_sym do
33
+ kind_of?(response_class)
34
+ end
35
+ end
36
+
37
+ all_response_classes = nil
38
+ end
@@ -0,0 +1,59 @@
1
+ #Copyright (c) 2010, Nathaniel Ritmeyer. All rights reserved.
2
+
3
+ #while writing responsalizr, I had a bare bones rails app that
4
+ #I used to have the tests run against. This being a very small
5
+ #project, I didn't want to spend ages mocking out a web server
6
+ #or writing a sinatra app - the following commands are enough
7
+ #to get you to where you need to be:
8
+ #
9
+ # rails test
10
+ # cd test
11
+ # script/server
12
+ #
13
+ #Having done that, the tests will run.
14
+ #
15
+ #I'm going to replace the following with something that doesn't
16
+ #break the "don't let your tests touch the wire" antipattern.
17
+
18
+ require 'rubygems'
19
+ $: << "./lib"
20
+ require 'responsalizr'
21
+
22
+ include Responsalizr
23
+
24
+ describe Response do
25
+ it "should not allow a nil url string" do
26
+ lambda { Response.from() }.should raise_error(ArgumentError)
27
+ end
28
+
29
+ it "should respond with a Net::HTTPResponse" do
30
+ Response.from("http://localhost:3000").should be_a_kind_of(Net::HTTPResponse)
31
+ end
32
+
33
+ it "should allow the use of a proxy" do
34
+ #beware, this uses a real proxy out on the interwebs somewhere
35
+ Response.from("http://localhost:3000", {:proxy_host => "63.223.106.54", :port => 80}).should be_a_code(200)
36
+ end
37
+
38
+ it "should allow pretty testing api" do
39
+ Response.from("http://localhost:3000").should be_ok
40
+ Response.from("http://localhost:3000/dfshjidf").should be_not_found
41
+ end
42
+
43
+ it "should allow using testing success by response code" do
44
+ Response.from("http://localhost:3000").should be_a_code(200)
45
+ Response.from("http://localhost:3000/dfshjidf").should be_a_code(404)
46
+ end
47
+
48
+ it "should allow using testing failure by response code" do
49
+ Response.from("http://localhost:3000").should_not be_a_code(404)
50
+ end
51
+
52
+ it "should allow using testing failure by response code without brackets" do
53
+ Response.from("http://localhost:3000").should_not be_a_code 400
54
+ end
55
+
56
+ it "should now allow bad codes" do
57
+ lambda { Response.from("http://localhost:3000").should be_a_code(999) }.should raise_error(ArgumentError)
58
+ end
59
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: responsalizr
3
+ version: !ruby/object:Gem::Version
4
+ hash: 15
5
+ prerelease: false
6
+ segments:
7
+ - 1
8
+ - 0
9
+ version: "1.0"
10
+ platform: ruby
11
+ authors:
12
+ - Nat Ritmeyer
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-09-06 00:00:00 +01:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: Test HTTP responses in ruby
22
+ email: nat@natontesting.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files:
28
+ - README
29
+ - LICENSE
30
+ files:
31
+ - LICENSE
32
+ - README
33
+ - Rakefile
34
+ - lib/responsalizr.rb
35
+ - spec/responsalizr_tests.rb
36
+ has_rdoc: true
37
+ homepage:
38
+ licenses: []
39
+
40
+ post_install_message:
41
+ rdoc_options: []
42
+
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ none: false
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ hash: 3
51
+ segments:
52
+ - 0
53
+ version: "0"
54
+ required_rubygems_version: !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
+ requirements: []
64
+
65
+ rubyforge_project:
66
+ rubygems_version: 1.3.7
67
+ signing_key:
68
+ specification_version: 3
69
+ summary: Test HTTP responses in ruby
70
+ test_files: []
71
+