kerryb-fakettp 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/bin/fakettp ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'fakettp/commands/fakettp_command'
4
+
5
+ exit Fakettp::Commands::FakettpCommand.new(ARGV).run
@@ -0,0 +1,7 @@
1
+ $:.unshift(File.dirname(__FILE__) + '/../../lib')
2
+ require 'cucumber/rake/task'
3
+
4
+ task :default => :features
5
+ Cucumber::Rake::Task.new do |t|
6
+ t.cucumber_opts = "--format pretty"
7
+ end
@@ -0,0 +1,52 @@
1
+ module Fakettp
2
+ module Commands
3
+ class FakettpCommand
4
+ def initialize args
5
+ @args = args
6
+ end
7
+
8
+ def run
9
+ command = get_command
10
+ return usage unless command
11
+ case command
12
+ when 'install' then
13
+ return install
14
+ end
15
+ return 0
16
+ end
17
+
18
+ private
19
+
20
+ def get_command
21
+ @args[0]
22
+ end
23
+
24
+ def install
25
+ dir = get_dir
26
+ return usage unless dir
27
+ if File.exist? dir
28
+ $stderr.puts "File or directory #{dir} already exists."
29
+ return 1
30
+ end
31
+ FileUtils.mkdir_p dir + '/tmp/expectations', :mode => 0777
32
+ FileUtils.mkdir_p dir + '/public'
33
+ FileUtils.cp File.dirname(__FILE__) + '/../config.ru', dir
34
+ FileUtils.cp File.dirname(__FILE__) + '/../../../README.html', dir
35
+ return 0
36
+ end
37
+
38
+ def get_dir
39
+ @args[1]
40
+ end
41
+
42
+ def usage
43
+ $stderr.puts <<-EOF
44
+ Usage:
45
+
46
+ [TODO]
47
+ EOF
48
+ return 1
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,6 @@
1
+ require 'rubygems'
2
+
3
+ FAKETTP_BASE = File.expand_path(File.dirname(__FILE__))
4
+ require 'fakettp'
5
+
6
+ run Sinatra.application
@@ -0,0 +1,76 @@
1
+ require 'spec'
2
+ include Spec::Matchers
3
+
4
+ TMP_DIR = File.join FAKETTP_BASE, 'tmp'
5
+ ERROR_FILE = File.join TMP_DIR, 'errors'
6
+ EXPECTATION_DIR = File.join TMP_DIR, 'expectations'
7
+
8
+ def expect label
9
+ begin
10
+ yield
11
+ rescue Exception => e
12
+ File.open(ERROR_FILE, 'a') do |f|
13
+ f.puts "\nError in #{label}: #{e.message}\n\nRequest: #{request.inspect}"
14
+ end
15
+ throw :halt, [500, "Simulator received mismatched request\n"]
16
+ end
17
+ end
18
+
19
+ # Note: client *must* send a Content-Type header of 'text/plain'. Probably.
20
+ def set_expectation
21
+ content_type 'text/plain'
22
+ expectation = request.body.read
23
+ File.open(next_expectation_file, 'w') do |f|
24
+ f.write expectation
25
+ end
26
+ "Expect OK\n"
27
+ end
28
+
29
+ def run_expectation
30
+ content_type 'text/plain'
31
+ files = Dir.glob(EXPECTATION_DIR + '/*.rb').sort
32
+ if files.empty?
33
+ File.open(ERROR_FILE, 'a') do |f|
34
+ f.puts "\nReceived unexpected request: #{request.inspect}"
35
+ end
36
+ throw :halt, [500, "Simulator received unexpected request\n"]
37
+ end
38
+ expectation = File.read files.first
39
+ FileUtils.rm files.first
40
+ eval(expectation)
41
+ end
42
+
43
+ def reset_expectations
44
+ content_type 'text/plain'
45
+ FileUtils.rm_f(Dir.glob(EXPECTATION_DIR + '/*'))
46
+ File.open(ERROR_FILE, 'w') {|f|}
47
+ "Reset OK\n"
48
+ end
49
+
50
+ def verify_expectations
51
+ content_type 'text/plain'
52
+ check_for_non_received_requests
53
+ errors = File.read ERROR_FILE
54
+ throw :halt, [400, errors] unless errors.empty?
55
+ "Verify OK\n"
56
+ end
57
+
58
+ private
59
+
60
+ def next_expectation_file
61
+ files = Dir.glob(EXPECTATION_DIR + '/*.rb').sort
62
+ if files.empty?
63
+ "#{EXPECTATION_DIR}/1.rb"
64
+ else
65
+ "#{EXPECTATION_DIR}/#{File.basename(files.last, '.rb').to_i + 1}.rb"
66
+ end
67
+ end
68
+
69
+ def check_for_non_received_requests
70
+ Dir.glob(EXPECTATION_DIR + '/*.rb').each do |f|
71
+ File.open(ERROR_FILE, 'a') do |f|
72
+ f.puts "\nExpected request not received."
73
+ # TODO: Grab label from expectation (redefine expect?)
74
+ end
75
+ end
76
+ end
data/lib/fakettp.rb ADDED
@@ -0,0 +1,30 @@
1
+ $:.unshift(File.dirname(__FILE__))
2
+
3
+ require 'sinatra'
4
+ require 'fakettp/helper'
5
+ require 'fakettp/commands/fakettp_command'
6
+
7
+ Sinatra::Default.set :run, false
8
+ Sinatra::Default.set :environment, ENV['RACK_ENV']
9
+
10
+ error do
11
+ request.env['sinatra.error'].inspect
12
+ end
13
+
14
+ post '/expect' do
15
+ set_expectation
16
+ end
17
+
18
+ post '/reset' do
19
+ reset_expectations
20
+ end
21
+
22
+ get '/verify' do
23
+ verify_expectations
24
+ end
25
+
26
+ [:get, :post, :put, :delete, :head].each do |method|
27
+ send method, '/**' do
28
+ run_expectation
29
+ end
30
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kerryb-fakettp
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Kerry Buckley
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-02-13 00:00:00 -08:00
13
+ default_executable: fakettp
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: sinatra
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 0.3.0
23
+ version:
24
+ - !ruby/object:Gem::Dependency
25
+ name: rspec
26
+ version_requirement:
27
+ version_requirements: !ruby/object:Gem::Requirement
28
+ requirements:
29
+ - - ">="
30
+ - !ruby/object:Gem::Version
31
+ version: 1.1.12
32
+ version:
33
+ - !ruby/object:Gem::Dependency
34
+ name: cucumber
35
+ version_requirement:
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 0.1.16
41
+ version:
42
+ - !ruby/object:Gem::Dependency
43
+ name: RedCloth
44
+ version_requirement:
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: 4.1.1
50
+ version:
51
+ description:
52
+ email: kerryjbuckley@gmail.com
53
+ executables:
54
+ - fakettp
55
+ extensions: []
56
+
57
+ extra_rdoc_files: []
58
+
59
+ files:
60
+ - lib/fakettp
61
+ - lib/fakettp/commands
62
+ - lib/fakettp/commands/fakettp_command.rb
63
+ - lib/fakettp/config.ru
64
+ - lib/fakettp/helper.rb
65
+ - lib/fakettp/Rakefile.rb
66
+ - lib/fakettp.rb
67
+ - bin/fakettp
68
+ - README.html
69
+ has_rdoc: true
70
+ homepage: http://github.com/kerryb/fakettp/
71
+ post_install_message:
72
+ rdoc_options: []
73
+
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: "0"
81
+ version:
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: "0"
87
+ version:
88
+ requirements: []
89
+
90
+ rubyforge_project: fakettp
91
+ rubygems_version: 1.2.0
92
+ signing_key:
93
+ specification_version: 2
94
+ summary: HTTP server mocking tool
95
+ test_files: []
96
+