Bacon_Rack 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+
6
+ coverage
7
+ rdoc
8
+ .yardoc
@@ -0,0 +1,34 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ $:.push File.expand_path("../lib", __FILE__)
4
+ require "Bacon_Rack/version"
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "Bacon_Rack"
8
+ s.version = Bacon_Rack_Version
9
+ s.authors = ["da99"]
10
+ s.email = ["i-hate-spam-45671204@mailinator.com"]
11
+ s.homepage = "https://github.com/da99/Bacon_Rack"
12
+ s.summary = %q{Helper methods for Bacon specs.}
13
+ s.description = %q{
14
+
15
+ Provides helpers for your Bacon specs:
16
+ :renders, :redirects_to, :renders_assets.
17
+
18
+ Read more at the homepage.
19
+
20
+ }
21
+
22
+ s.files = `git ls-files`.split("\n")
23
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
24
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
25
+ s.require_paths = ["lib"]
26
+
27
+ s.add_development_dependency 'bacon'
28
+ s.add_development_dependency 'rake'
29
+ s.add_development_dependency 'Bacon_Colored'
30
+ s.add_development_dependency 'pry'
31
+
32
+ # Specify any dependencies here; for example:
33
+ # s.add_runtime_dependency 'rest-client'
34
+ end
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ source "http://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in Bob.gemspec
6
+ gemspec
data/README.md ADDED
@@ -0,0 +1,57 @@
1
+
2
+ Bacon\_Rack
3
+ ================
4
+
5
+ A Ruby gem providing helper methods for your Bacon specs.
6
+
7
+ Installation
8
+ ------------
9
+
10
+ gem install Bacon_Rack
11
+
12
+ Usage
13
+ ------
14
+
15
+ require 'Bacon'
16
+ require 'rack/test'
17
+ require "Bacon_Rack"
18
+
19
+
20
+ ...
21
+
22
+ it 'renders a message' do
23
+ get "/missing-page"
24
+ renders 404, "my message"
25
+ end
26
+
27
+ it 'redirects to other page' do
28
+ get "/redirect-page"
29
+ redirects_to "/page"
30
+ redirects_to 304, "/page"
31
+ end
32
+
33
+ it 'renders js/css/gif/jpg assets' do
34
+ get '/my-bueatiful page'
35
+
36
+ renders_assets
37
+ # response of asset link must be within 200..310
38
+ # response HTTP code.
39
+ end
40
+
41
+ The source code is one page long if you have more questions:
42
+ [Source Code](https://github.com/da99/Bacon_Rack/master/lib/Bacon_Rack/module.rb).
43
+
44
+ Run Tests
45
+ ---------
46
+
47
+ git clone git@github.com:da99/Bacon_Rack.git
48
+ cd Bacon_Rack
49
+ bundle update
50
+ bundle exec bacon spec/lib/main.rb
51
+
52
+ "I hate writing."
53
+ -----------------------------
54
+
55
+ If you know of existing software that makes the above redundant,
56
+ please tell me. The last thing I want to do is maintain code.
57
+
data/Rakefile ADDED
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/gem_tasks"
@@ -0,0 +1,54 @@
1
+ module Bacon_Rack
2
+
3
+ def redirects_to status, path = nil
4
+ if status.is_a?(Integer)
5
+ # do nothing
6
+ else
7
+ path, status = status, path
8
+ end
9
+
10
+ status ||= [ 301, 302, 303, 307 ]
11
+ status = [ status ].compact.flatten
12
+
13
+ status.should.include last_response.status
14
+
15
+ last_response['Location'].sub(%r!http://(www.)?example\.com!, '')
16
+ .should == path
17
+ end
18
+
19
+ def renders status, body = nil
20
+ case status
21
+ when Regexp, String
22
+ body, status = status, body
23
+ else
24
+ # do nothing
25
+ end
26
+
27
+ status ||= 200
28
+ l = last_response
29
+ l.status.should == status
30
+
31
+ case body
32
+ when Regexp
33
+ l.body.should.match body
34
+ else
35
+ l.body.should == body
36
+ end
37
+
38
+ [ nil, l.body.bytesize.to_s ]
39
+ .should.include l['Content-Length']
40
+ end
41
+
42
+ def renders_assets
43
+ files = last_response.body \
44
+ .scan( %r!"(/[^"]+.(js|css|png|gif|ico|jpg|jpeg)[^"]*)"!i ) \
45
+ .map(&:first)
46
+
47
+ files.each { |f|
48
+ get f
49
+ (200..310).should.include last_response.status
50
+ }
51
+ end
52
+
53
+ end # === class Bacon_Rack
54
+
@@ -0,0 +1 @@
1
+ Bacon_Rack_Version = "0.1.0"
data/lib/Bacon_Rack.rb ADDED
@@ -0,0 +1,8 @@
1
+ require 'Bacon_Rack/version'
2
+ require 'Bacon_Rack/module'
3
+
4
+ class Bacon::Context
5
+
6
+ include Bacon_Rack
7
+
8
+ end # === class Bacon::Context
@@ -0,0 +1,121 @@
1
+
2
+ describe ":redirects_to" do
3
+
4
+ it "fails if status is one of: 301, 302, 303, 307" do
5
+ %w{ 200 304 305 306 }.each { |s|
6
+ should.raise Bacon::Error do
7
+ last_response.status = Integer(s)
8
+ last_response['Location'] = '/'
9
+
10
+ redirects_to "/"
11
+ end.message.should.==("[301, 302, 303, 307].include?(#{s}) failed")
12
+ }
13
+ end
14
+
15
+ it "passes if status is in: 301, 302, 303, 307" do
16
+ %w{ 301 302 303 307 }.each { |s|
17
+ last_response.status = Integer(s)
18
+ last_response['Location'] = '/pass'
19
+
20
+ redirects_to '/pass'
21
+ }
22
+ end
23
+
24
+ %w{ example.com www.example.com }.each { |str|
25
+ it "ignores http://#{str} host in location header" do
26
+ last_response.status = 301
27
+ last_response['Location'] = "http://#{str}/new"
28
+
29
+ redirects_to '/new'
30
+ end
31
+ }
32
+
33
+ it "accepts both a status and path" do
34
+ last_response.status = 303
35
+ last_response['Location'] = "/news"
36
+
37
+ redirects_to 303, "/news"
38
+ end
39
+
40
+ end # === Bacon_Rack
41
+
42
+
43
+ describe ":renders" do
44
+
45
+ it "passes if status is 200" do
46
+ response 200, "Body 2"
47
+
48
+ renders "Body 2"
49
+ end
50
+
51
+ it 'accepts a single string without a status' do
52
+ response 200, "Single"
53
+ renders "Single"
54
+ end
55
+
56
+ it 'accepts a regular expression in place of a String' do
57
+ response 200, "Regular"
58
+ renders %r!Regular!
59
+ end
60
+
61
+ it "fails if status is not 200" do
62
+ response 304, "Body 1"
63
+
64
+ should.raise(Bacon::Error) {
65
+ renders "Body 1"
66
+ }.message.should == '304.==(200) failed'
67
+ end
68
+
69
+ it "fails if Content-Length does not match bytesize of body" do
70
+ response 200, "Body 3"
71
+ last_response['Content-Length'] = 2
72
+
73
+ should.raise(Bacon::Error) {
74
+ renders "Body 3"
75
+ }.message.should == "[nil, \"6\"].include?(2) failed"
76
+ end
77
+
78
+ it 'fails if regular expression does not match body' do
79
+ response 200, "No match."
80
+ should.raise( Bacon::Error ) {
81
+ renders %r!No single.!
82
+ }.message
83
+ .should == "\"No match.\".=~(/No single./) failed"
84
+ end
85
+
86
+ it 'fails if String is not a full match with the body.' do
87
+ response 200, "Partial string"
88
+ should.raise( Bacon::Error ){
89
+ renders 200, "string"
90
+ }.message
91
+ .should == "\"Partial string\".==(\"string\") failed"
92
+ end
93
+
94
+ end # === :renders
95
+
96
+ describe ":renders_assets" do
97
+
98
+ it "passes if relative links in pages all return a non-400, non-500 status" do
99
+ response 200, %(
100
+ <a href="/200/0.js">test</a>
101
+ <a href="/301/1.css">test</a>
102
+ <a href="/304/2.php">test</a>
103
+ )
104
+
105
+ renders_assets
106
+ end
107
+
108
+ it "fails if any relative link is within 400-510" do
109
+ response 200, %(
110
+ <a href="/200/0.js">test</a>
111
+ <a href="/200/1.css">test</a>
112
+ <a href="/509/2.gif">test</a>
113
+ )
114
+
115
+ should.raise(Bacon::Error) {
116
+ renders_assets
117
+ }.message.should == "200..310.include?(509) failed"
118
+ end
119
+
120
+ end # === :renders_assets
121
+
data/spec/bin.rb ADDED
@@ -0,0 +1,13 @@
1
+
2
+ bins = Dir.glob("bin/*")
3
+
4
+ unless bins.empty?
5
+ describe "permissions of bin/" do
6
+ bins.each { |file|
7
+ it "should chmod 755 for: #{file}" do
8
+ `stat -c %a #{file}`.strip
9
+ .should.be == "755"
10
+ end
11
+ }
12
+ end # === permissions of bin/
13
+ end # === unless bins.empty?
data/spec/lib/main.rb ADDED
@@ -0,0 +1,77 @@
1
+
2
+ require 'rubygems'
3
+ require 'bundler'
4
+ begin
5
+ Bundler.setup(:default, :development)
6
+ rescue Bundler::BundlerError => e
7
+ $stderr.print e.message, "\n"
8
+ $stderr.print "Run `bundle install` to install missing gems\n"
9
+ exit e.status_code
10
+ end
11
+ require 'bacon'
12
+
13
+ Gem_Dir = File.expand_path( File.join(File.dirname(__FILE__) + '/../..') )
14
+ $LOAD_PATH.unshift Gem_Dir
15
+ $LOAD_PATH.unshift( Gem_Dir + "/lib" )
16
+
17
+ Bacon.summary_on_exit
18
+
19
+ require 'Bacon_Rack'
20
+ require 'Bacon_Colored'
21
+ require 'pry'
22
+
23
+
24
+
25
+ # ======== Custom code.
26
+
27
+ class Bacon::Context
28
+
29
+ def last_response
30
+ @fake_rack ||= Fake_Rack.new
31
+ end
32
+
33
+ def response stat, body
34
+ l = last_response
35
+ l.status = stat
36
+ l.body = body
37
+ l['Content-Length'] = body.bytesize.to_s
38
+ end
39
+
40
+ def get str
41
+ pieces = str.split('/').map(&:strip).reject(&:empty?)
42
+ stat, body = pieces
43
+ last_response.status = Integer(stat)
44
+ end
45
+
46
+ end # === class
47
+
48
+ class Fake_Rack < Hash
49
+
50
+ attr_accessor :status, :body
51
+
52
+ def last_response
53
+ self
54
+ end
55
+
56
+ def ok?
57
+ status == 200
58
+ end
59
+
60
+ end # === Fake_Rack
61
+
62
+
63
+
64
+ # ======== Include the tests.
65
+ target_files = ARGV[1, ARGV.size - 1].select { |a| File.file?(a) }
66
+
67
+ if target_files.empty?
68
+
69
+ # include all files
70
+ Dir.glob('./spec/*.rb').each { |file|
71
+ require file.sub('.rb', '') if File.file?(file)
72
+ }
73
+
74
+ else
75
+ # Do nothing. Bacon grabs the file.
76
+
77
+ end
metadata ADDED
@@ -0,0 +1,121 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: Bacon_Rack
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - da99
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-05-09 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bacon
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: Bacon_Colored
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: pry
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ description: ! "\n\n Provides helpers for your Bacon specs:\n :renders, :redirects_to,
79
+ :renders_assets.\n\n Read more at the homepage.\n\n "
80
+ email:
81
+ - i-hate-spam-45671204@mailinator.com
82
+ executables: []
83
+ extensions: []
84
+ extra_rdoc_files: []
85
+ files:
86
+ - .gitignore
87
+ - Bacon_Rack.gemspec
88
+ - Gemfile
89
+ - README.md
90
+ - Rakefile
91
+ - lib/Bacon_Rack.rb
92
+ - lib/Bacon_Rack/module.rb
93
+ - lib/Bacon_Rack/version.rb
94
+ - spec/Bacon_Rack.rb
95
+ - spec/bin.rb
96
+ - spec/lib/main.rb
97
+ homepage: https://github.com/da99/Bacon_Rack
98
+ licenses: []
99
+ post_install_message:
100
+ rdoc_options: []
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ none: false
105
+ requirements:
106
+ - - ! '>='
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ required_rubygems_version: !ruby/object:Gem::Requirement
110
+ none: false
111
+ requirements:
112
+ - - ! '>='
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ requirements: []
116
+ rubyforge_project:
117
+ rubygems_version: 1.8.23
118
+ signing_key:
119
+ specification_version: 3
120
+ summary: Helper methods for Bacon specs.
121
+ test_files: []