harper 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/.rvmrc CHANGED
@@ -1,2 +1,2 @@
1
1
  rvm_install_on_use_flag=1
2
- rvm --create use ruby-1.8.7-p330@harper
2
+ rvm --create use ruby-1.9.3-p0@harper
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- harper (0.0.1)
4
+ harper (0.0.3)
5
5
  httparty
6
6
  json (>= 1.4.6)
7
7
  sinatra (>= 1.0.0)
@@ -10,45 +10,50 @@ GEM
10
10
  remote: http://rubygems.org/
11
11
  specs:
12
12
  builder (3.0.0)
13
- crack (0.1.8)
14
- cucumber (0.10.3)
13
+ cucumber (1.1.4)
15
14
  builder (>= 2.1.2)
16
15
  diff-lcs (>= 1.1.2)
17
- gherkin (>= 2.3.8)
16
+ gherkin (~> 2.7.1)
18
17
  json (>= 1.4.6)
19
- term-ansicolor (>= 1.0.5)
20
- cucumber-sinatra (0.3.1)
18
+ term-ansicolor (>= 1.0.6)
19
+ cucumber-sinatra (0.4.0)
21
20
  templater (>= 1.0.0)
22
- diff-lcs (1.1.2)
21
+ diff-lcs (1.1.3)
23
22
  extlib (0.9.15)
24
- gherkin (2.3.9)
23
+ gherkin (2.7.6)
25
24
  json (>= 1.4.6)
26
- highline (1.6.2)
27
- httparty (0.7.7)
28
- crack (= 0.1.8)
29
- json (1.4.6)
30
- rack (1.3.0)
31
- rack-test (0.6.0)
25
+ highline (1.6.11)
26
+ httparty (0.8.1)
27
+ multi_json
28
+ multi_xml
29
+ json (1.6.5)
30
+ multi_json (1.0.4)
31
+ multi_xml (0.4.1)
32
+ rack (1.4.1)
33
+ rack-protection (1.2.0)
34
+ rack
35
+ rack-test (0.6.1)
32
36
  rack (>= 1.0)
33
- rspec (2.6.0)
34
- rspec-core (~> 2.6.0)
35
- rspec-expectations (~> 2.6.0)
36
- rspec-mocks (~> 2.6.0)
37
- rspec-core (2.6.3)
38
- rspec-expectations (2.6.0)
37
+ rspec (2.8.0)
38
+ rspec-core (~> 2.8.0)
39
+ rspec-expectations (~> 2.8.0)
40
+ rspec-mocks (~> 2.8.0)
41
+ rspec-core (2.8.0)
42
+ rspec-expectations (2.8.0)
39
43
  diff-lcs (~> 1.1.2)
40
- rspec-mocks (2.6.0)
44
+ rspec-mocks (2.8.0)
41
45
  sham_rack (1.3.3)
42
46
  rack
43
- sinatra (1.2.6)
44
- rack (~> 1.1)
45
- tilt (>= 1.2.2, < 2.0)
47
+ sinatra (1.3.2)
48
+ rack (~> 1.3, >= 1.3.6)
49
+ rack-protection (~> 1.2)
50
+ tilt (~> 1.3, >= 1.3.3)
46
51
  templater (1.0.0)
47
52
  diff-lcs (>= 1.1.2)
48
53
  extlib (>= 0.9.5)
49
54
  highline (>= 1.4.0)
50
- term-ansicolor (1.0.5)
51
- tilt (1.3.1)
55
+ term-ansicolor (1.0.7)
56
+ tilt (1.3.3)
52
57
 
53
58
  PLATFORMS
54
59
  ruby
data/bin/harper CHANGED
@@ -4,4 +4,8 @@ require "harper"
4
4
 
5
5
  port = ARGV.shift || 4567
6
6
 
7
- Rack::Server.start :app => Harper, :Port => port
7
+ # Harper needs access to the server hosting itself so that it can shut
8
+ # it down on demand
9
+ server = Rack::Server.new :app => Harper::App, :Port => port, :server => 'webrick'
10
+ Harper::App.server(server.server)
11
+ server.start
@@ -14,7 +14,7 @@ When /^the application issues an? "([A-Z]+)" request for "([^"]*)"$/ do |method,
14
14
  end
15
15
 
16
16
  When /^the application issues an? "([A-Z]+)" request to the mock$/ do |method|
17
- When %{the application issues a "#{method}" request for "/service"}
17
+ step %{the application issues a "#{method}" request for "/service"}
18
18
  end
19
19
 
20
20
  Then /^the response code should be "(\d+)"$/ do |expected|
@@ -21,5 +21,5 @@ World do
21
21
  end
22
22
 
23
23
  ShamRack.at("example.com").rackup do
24
- run Harper.new
24
+ run Harper::App.new
25
25
  end
data/harper.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
  $:.push File.expand_path("../lib", __FILE__)
3
- require "harper"
3
+ require "harper/version"
4
4
 
5
5
  Gem::Specification.new do |s|
6
6
  s.name = "harper"
data/lib/harper/app.rb ADDED
@@ -0,0 +1,97 @@
1
+ require 'base64'
2
+ require 'sinatra/base'
3
+ require 'logger'
4
+ require 'json'
5
+
6
+ module Harper
7
+ class App < Sinatra::Base
8
+
9
+ Version = "0.0.2"
10
+
11
+ @@mocks = {}
12
+ @@server = nil
13
+
14
+ def self.server(server)
15
+ @@server = server
16
+ end
17
+
18
+ enable :logging
19
+
20
+ configure do
21
+ LOGGER = Logger.new("sinatra.log")
22
+ end
23
+
24
+ helpers do
25
+ def logger
26
+ LOGGER
27
+ end
28
+
29
+ def mock_id(url)
30
+ [url].pack('m').tr("+/=", "-_.").gsub("\n", '')
31
+ end
32
+ end
33
+
34
+ post '/h/mocks' do
35
+ mock = JSON(request.body.read)
36
+
37
+ mock['url'] = mock['url'][1..-1] if mock['url'] =~ /^\//
38
+
39
+ mock['id'] = mock_id(mock['url'])
40
+ mock['method'].upcase!
41
+ mock['delay'] = mock['delay'].to_f / 1000.0
42
+ @@mocks[mock['id']] = mock
43
+
44
+ logger.info("Created mock for endpoint: '#{mock['url']}'")
45
+
46
+ headers['location'] = "/h/mocks/#{mock['id']}"
47
+ status "201"
48
+ end
49
+
50
+ delete '/h/mocks' do
51
+ @@mocks = {}
52
+ end
53
+
54
+ get '/h/mocks/:mock_id' do |mock_name|
55
+ content_type :json
56
+ status "200"
57
+ @@mocks[mock_name].to_json
58
+ end
59
+
60
+ delete '/h/mocks/:mock_id' do |mock_name|
61
+ @@mocks[mock_name] = nil
62
+
63
+ status "200"
64
+ end
65
+
66
+ put '/h/control' do
67
+ cmd = JSON(request.body.read)
68
+
69
+ case cmd["command"]
70
+ when "quit"
71
+ @@server.shutdown
72
+ end
73
+ end
74
+
75
+ [:get, :post, :put, :delete].each do |method|
76
+ self.send(method, '*') do
77
+ mock_id = mock_id(request.path[1..-1])
78
+
79
+ logger.debug("#{request.request_method} request for a mock: '#{request.path}'")
80
+
81
+ mock = @@mocks[mock_id]
82
+ if mock && request.request_method == mock['method']
83
+ content_type mock['content-type']
84
+ status mock['status'] || "200"
85
+ sleep mock['delay']
86
+
87
+ logger.info("Serving mocked body for endpoint: '#{mock['url']}'")
88
+
89
+ mock['body']
90
+ else
91
+ status "503"
92
+ end
93
+ end
94
+ end
95
+
96
+ end
97
+ end
@@ -0,0 +1,3 @@
1
+ module Harper
2
+ Version = "0.0.3"
3
+ end
data/lib/harper.rb CHANGED
@@ -1,81 +1 @@
1
- require 'base64'
2
- require 'sinatra/base'
3
- require 'logger'
4
- require 'json'
5
-
6
- class Harper < Sinatra::Base
7
-
8
- Version = "0.0.2"
9
-
10
- @@mocks = {}
11
-
12
- enable :logging
13
-
14
- configure do
15
- LOGGER = Logger.new("sinatra.log")
16
- end
17
-
18
- helpers do
19
- def logger
20
- LOGGER
21
- end
22
-
23
- def mock_id(url)
24
- [url].pack('m').tr("+/=", "-_.").gsub("\n", '')
25
- end
26
- end
27
-
28
- post '/h/mocks' do
29
- mock = JSON(request.body.read)
30
-
31
- mock['url'] = mock['url'][1..-1] if mock['url'] =~ /^\//
32
-
33
- mock['id'] = mock_id(mock['url'])
34
- mock['method'].upcase!
35
- mock['delay'] = mock['delay'].to_f / 1000.0
36
- @@mocks[mock['id']] = mock
37
-
38
- logger.info("Created mock for endpoint: '#{mock['url']}'")
39
-
40
- headers['location'] = "/h/mocks/#{mock['id']}"
41
- status "201"
42
- end
43
-
44
- delete '/h/mocks' do
45
- @@mocks = {}
46
- end
47
-
48
- get '/h/mocks/:mock_id' do |mock_name|
49
- content_type :json
50
- status "200"
51
- @@mocks[mock_name].to_json
52
- end
53
-
54
- delete '/h/mocks/:mock_id' do |mock_name|
55
- @@mocks[mock_name] = nil
56
-
57
- status "200"
58
- end
59
-
60
- [:get, :post, :put, :delete].each do |method|
61
- self.send(method, '*') do
62
- mock_id = mock_id(request.path[1..-1])
63
-
64
- logger.debug("#{request.request_method} request for a mock: '#{request.path}'")
65
-
66
- mock = @@mocks[mock_id]
67
- if mock && request.request_method == mock['method']
68
- content_type mock['content-type']
69
- status mock['status'] || "200"
70
- sleep mock['delay']
71
-
72
- logger.info("Serving mocked body for endpoint: '#{mock['url']}'")
73
-
74
- mock['body']
75
- else
76
- status "503"
77
- end
78
- end
79
- end
80
-
81
- end
1
+ require 'harper/app'
data/spec/harper_spec.rb CHANGED
@@ -1,12 +1,14 @@
1
+ $: << File.dirname(__FILE__) + "/.."
2
+
1
3
  require 'spec/spec_helper'
2
4
  require 'harper'
3
5
 
4
- describe Harper do
6
+ describe Harper::App do
5
7
  include Rack::Test::Methods
6
8
 
7
9
  supported_verbs = ["GET", "POST", "PUT", "DELETE"]
8
10
 
9
- let(:app) { Harper.new }
11
+ let(:app) { Harper::App.new }
10
12
 
11
13
  let(:method) { "GET" }
12
14
  let(:url) { "/service" }
@@ -148,4 +150,14 @@ describe Harper do
148
150
  end
149
151
  end
150
152
 
153
+ context "controlling harper" do
154
+
155
+ it "should exit, abruptly, on demand" do
156
+ host = mock('hosting server')
157
+ host.should_receive(:shutdown)
158
+ Harper::App.server(host)
159
+ put '/h/control', {:command => "quit"}.to_json
160
+ end
161
+ end
162
+
151
163
  end
metadata CHANGED
@@ -1,150 +1,112 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: harper
3
- version: !ruby/object:Gem::Version
4
- hash: 27
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
5
  prerelease:
6
- segments:
7
- - 0
8
- - 0
9
- - 2
10
- version: 0.0.2
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - Giles Alexander
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2011-06-15 00:00:00 -07:00
19
- default_executable:
20
- dependencies:
21
- - !ruby/object:Gem::Dependency
12
+ date: 2012-01-29 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
22
15
  name: sinatra
23
- prerelease: false
24
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: &70267968221820 !ruby/object:Gem::Requirement
25
17
  none: false
26
- requirements:
27
- - - ">="
28
- - !ruby/object:Gem::Version
29
- hash: 23
30
- segments:
31
- - 1
32
- - 0
33
- - 0
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
34
21
  version: 1.0.0
35
22
  type: :runtime
36
- version_requirements: *id001
37
- - !ruby/object:Gem::Dependency
38
- name: httparty
39
23
  prerelease: false
40
- requirement: &id002 !ruby/object:Gem::Requirement
24
+ version_requirements: *70267968221820
25
+ - !ruby/object:Gem::Dependency
26
+ name: httparty
27
+ requirement: &70267968220540 !ruby/object:Gem::Requirement
41
28
  none: false
42
- requirements:
43
- - - ">="
44
- - !ruby/object:Gem::Version
45
- hash: 3
46
- segments:
47
- - 0
48
- version: "0"
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
49
33
  type: :runtime
50
- version_requirements: *id002
51
- - !ruby/object:Gem::Dependency
52
- name: json
53
34
  prerelease: false
54
- requirement: &id003 !ruby/object:Gem::Requirement
35
+ version_requirements: *70267968220540
36
+ - !ruby/object:Gem::Dependency
37
+ name: json
38
+ requirement: &70267968218680 !ruby/object:Gem::Requirement
55
39
  none: false
56
- requirements:
57
- - - ">="
58
- - !ruby/object:Gem::Version
59
- hash: 11
60
- segments:
61
- - 1
62
- - 4
63
- - 6
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
64
43
  version: 1.4.6
65
44
  type: :runtime
66
- version_requirements: *id003
67
- - !ruby/object:Gem::Dependency
68
- name: rack-test
69
45
  prerelease: false
70
- requirement: &id004 !ruby/object:Gem::Requirement
46
+ version_requirements: *70267968218680
47
+ - !ruby/object:Gem::Dependency
48
+ name: rack-test
49
+ requirement: &70267968217900 !ruby/object:Gem::Requirement
71
50
  none: false
72
- requirements:
73
- - - ">="
74
- - !ruby/object:Gem::Version
75
- hash: 3
76
- segments:
77
- - 0
78
- version: "0"
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
79
55
  type: :development
80
- version_requirements: *id004
81
- - !ruby/object:Gem::Dependency
82
- name: sham_rack
83
56
  prerelease: false
84
- requirement: &id005 !ruby/object:Gem::Requirement
57
+ version_requirements: *70267968217900
58
+ - !ruby/object:Gem::Dependency
59
+ name: sham_rack
60
+ requirement: &70267968217200 !ruby/object:Gem::Requirement
85
61
  none: false
86
- requirements:
87
- - - ">="
88
- - !ruby/object:Gem::Version
89
- hash: 3
90
- segments:
91
- - 0
92
- version: "0"
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
93
66
  type: :development
94
- version_requirements: *id005
95
- - !ruby/object:Gem::Dependency
96
- name: rspec
97
67
  prerelease: false
98
- requirement: &id006 !ruby/object:Gem::Requirement
68
+ version_requirements: *70267968217200
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: &70267968216220 !ruby/object:Gem::Requirement
99
72
  none: false
100
- requirements:
101
- - - ">="
102
- - !ruby/object:Gem::Version
103
- hash: 3
104
- segments:
105
- - 0
106
- version: "0"
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
107
77
  type: :development
108
- version_requirements: *id006
109
- - !ruby/object:Gem::Dependency
110
- name: cucumber
111
78
  prerelease: false
112
- requirement: &id007 !ruby/object:Gem::Requirement
79
+ version_requirements: *70267968216220
80
+ - !ruby/object:Gem::Dependency
81
+ name: cucumber
82
+ requirement: &70267968215240 !ruby/object:Gem::Requirement
113
83
  none: false
114
- requirements:
115
- - - ">="
116
- - !ruby/object:Gem::Version
117
- hash: 31
118
- segments:
119
- - 0
120
- - 10
121
- version: "0.10"
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0.10'
122
88
  type: :development
123
- version_requirements: *id007
124
- - !ruby/object:Gem::Dependency
125
- name: cucumber-sinatra
126
89
  prerelease: false
127
- requirement: &id008 !ruby/object:Gem::Requirement
90
+ version_requirements: *70267968215240
91
+ - !ruby/object:Gem::Dependency
92
+ name: cucumber-sinatra
93
+ requirement: &70267968214820 !ruby/object:Gem::Requirement
128
94
  none: false
129
- requirements:
130
- - - ">="
131
- - !ruby/object:Gem::Version
132
- hash: 3
133
- segments:
134
- - 0
135
- version: "0"
95
+ requirements:
96
+ - - ! '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
136
99
  type: :development
137
- version_requirements: *id008
100
+ prerelease: false
101
+ version_requirements: *70267968214820
138
102
  description: Dead simple mocking for HTTP services, a Sinatra app
139
- email:
103
+ email:
140
104
  - giles.alexander@gmail.com
141
- executables:
105
+ executables:
142
106
  - harper
143
107
  extensions: []
144
-
145
108
  extra_rdoc_files: []
146
-
147
- files:
109
+ files:
148
110
  - .gitignore
149
111
  - .rvmrc
150
112
  - Gemfile
@@ -160,45 +122,35 @@ files:
160
122
  - features/support/paths.rb
161
123
  - harper.gemspec
162
124
  - lib/harper.rb
125
+ - lib/harper/app.rb
126
+ - lib/harper/version.rb
163
127
  - spec/harper_spec.rb
164
128
  - spec/spec_helper.rb
165
- has_rdoc: true
166
129
  homepage: http://github.com/gga/harper
167
130
  licenses: []
168
-
169
131
  post_install_message:
170
132
  rdoc_options: []
171
-
172
- require_paths:
133
+ require_paths:
173
134
  - lib
174
- required_ruby_version: !ruby/object:Gem::Requirement
135
+ required_ruby_version: !ruby/object:Gem::Requirement
175
136
  none: false
176
- requirements:
177
- - - ">="
178
- - !ruby/object:Gem::Version
179
- hash: 3
180
- segments:
181
- - 0
182
- version: "0"
183
- required_rubygems_version: !ruby/object:Gem::Requirement
137
+ requirements:
138
+ - - ! '>='
139
+ - !ruby/object:Gem::Version
140
+ version: '0'
141
+ required_rubygems_version: !ruby/object:Gem::Requirement
184
142
  none: false
185
- requirements:
186
- - - ">="
187
- - !ruby/object:Gem::Version
188
- hash: 23
189
- segments:
190
- - 1
191
- - 3
192
- - 6
143
+ requirements:
144
+ - - ! '>='
145
+ - !ruby/object:Gem::Version
193
146
  version: 1.3.6
194
147
  requirements: []
195
-
196
148
  rubyforge_project: harper
197
- rubygems_version: 1.6.2
149
+ rubygems_version: 1.8.10
198
150
  signing_key:
199
151
  specification_version: 3
200
152
  summary: Dead simple mocking for HTTP services
201
- test_files:
153
+ test_files:
202
154
  - features/mock_http_rpc_service.feature
203
155
  - features/step_definitions/http_steps.rb
204
156
  - features/step_definitions/mock_definition_steps.rb