http_sim 0.0.6 → 0.0.7

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4e8b845cf4bcfebc30186771e7caa4eee7dd4bd2
4
- data.tar.gz: 37130ffeb405e894f02fad5654e9f196dd8d7c24
3
+ metadata.gz: 4099a803c584f2bfcc99221146df54fd94d08006
4
+ data.tar.gz: faa01c38b740a304663ce17c4e83dac7329ee8ab
5
5
  SHA512:
6
- metadata.gz: c3672147e1325a44908b8af46fbea3d8a7bf5d0d48c3bcbafc509593e405f96c8643023a66b0f84b4f3487521bea486dea0d22c4f4eb6ac0f5374b2bcde1eef7
7
- data.tar.gz: e21d82cf6defa514007a1e740824406fae2b5b5705b853b08fa2938b8a9284fb853984d709f313458c18bb8c23aae2076f7354a06ac3123aa8ff6721cc4439fd
6
+ metadata.gz: 2ad9529b8139e6de649cd35c2e534d2664601e3afced7caccf1cfb9316c34566b5b4fcb6915c33d9d42c0f7a2ca076e8b0ddacc84fd23d462b353fd4dce28676
7
+ data.tar.gz: 6f7d08bf223ad0e8c17c840bc2e3fad767fb61ddfdfe1fcaec4d265ec0f9b056fa8b691a87aa1bfd206d38620daa900da0d2b4dc0dfab22ae28aa2d590f1464e
data/CHANGELOG.txt CHANGED
@@ -4,3 +4,4 @@ Version 0.0.3 Requests to endpoints are recorded and listed at endpoint's /reque
4
4
  Version 0.0.4 Added run_daemon!, stop_daemon!, and wait_for_start methods
5
5
  Version 0.0.5 run_daemon! now writes to a (configurable) log file
6
6
  Version 0.0.6 PUT and DELETE endpoints now generated for /<endpoint>/response
7
+ Version 0.0.7 GET, PUT, DELETE /response endpoints now working
data/http_sim.gemspec CHANGED
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = 'http_sim'
7
- spec.version = '0.0.6'
7
+ spec.version = '0.0.7'
8
8
  spec.authors = ['Jean de Klerk']
9
9
  spec.email = ['jadekler@gmail.com']
10
10
  spec.summary = 'Simulate your external HTTP integrations.'
data/lib/http_sim.rb CHANGED
@@ -75,8 +75,9 @@ module HttpSimulator
75
75
  @@pid
76
76
  end
77
77
 
78
- def self.stop_daemon!
78
+ def self.stop_daemon!(port: 4567, max_wait_seconds: 5)
79
79
  Process.kill('SIGKILL', @@pid) if @@pid
80
+ wait_for_stop(port, max_wait_seconds)
80
81
  end
81
82
 
82
83
  def self.wait_for_start(port, max_wait_seconds)
@@ -94,6 +95,21 @@ module HttpSimulator
94
95
  raise "Simulators failed to start - timed out after #{max_wait_seconds} seconds!"
95
96
  end
96
97
 
98
+ def self.wait_for_stop(port, max_wait_seconds)
99
+ wait_count = 0
100
+ while wait_count < max_wait_seconds * 4
101
+ begin
102
+ HTTParty.get("http://localhost:#{port}/")
103
+ wait_count += 1
104
+ sleep 0.25
105
+ rescue Errno::ECONNREFUSED
106
+ return
107
+ end
108
+ end
109
+
110
+ raise "Simulators failed to stop - timed out after #{max_wait_seconds} seconds!"
111
+ end
112
+
97
113
  def self.check_if_port_in_use(port)
98
114
  begin
99
115
  HTTParty.get("http://localhost:#{port}/")
@@ -143,7 +159,11 @@ module HttpSimulator
143
159
  end
144
160
 
145
161
  Sinatra::Base.get "#{endpoint.path}/response" do
146
- ERB.new(@@erb_files[:response]).result binding
162
+ if env.key?('CONTENT_TYPE') && env['CONTENT_TYPE'] && env['CONTENT_TYPE'].include?('json')
163
+ endpoint.response
164
+ else
165
+ ERB.new(@@erb_files[:response]).result binding
166
+ end
147
167
  end
148
168
 
149
169
  Sinatra::Base.put "#{endpoint.path}/response" do
@@ -155,8 +175,11 @@ module HttpSimulator
155
175
  end
156
176
 
157
177
  Sinatra::Base.get "#{endpoint.path}/requests" do
158
- @endpoint = endpoint
159
- ERB.new(@@erb_files[:request]).result binding
178
+ if env.key?('CONTENT_TYPE') && env['CONTENT_TYPE'] && env['CONTENT_TYPE'].include?('json')
179
+ endpoint.requests.to_json
180
+ else
181
+ ERB.new(@@erb_files[:request]).result binding
182
+ end
160
183
  end
161
184
 
162
185
  Sinatra::Base.delete "#{endpoint.path}/requests" do
data/lib/request.html.erb CHANGED
@@ -9,11 +9,11 @@
9
9
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
10
10
  </head>
11
11
  <body>
12
- <h2>Requests for: <%= @endpoint.method %> <%= @endpoint.path %></h2>
12
+ <h2>Requests for: <%= endpoint.method %> <%= endpoint.path %></h2>
13
13
 
14
14
  <table>
15
15
  <tbody>
16
- <% @endpoint.requests.each_with_index do |request, index| %>
16
+ <% endpoint.requests.each_with_index do |request, index| %>
17
17
  <tr>
18
18
  <td><%= index %>:</td>
19
19
  <td>
@@ -11,6 +11,6 @@
11
11
  <body>
12
12
  <h2>Response for: <%= endpoint.method %> <%= endpoint.path %></h2>
13
13
 
14
- <div>Response stuff goes here</div>
14
+ <div><%= endpoint.response %></div>
15
15
  </body>
16
16
  </html>
@@ -3,21 +3,22 @@ require 'spec_helper'
3
3
  describe '/<endpoint>' do
4
4
  before :each do
5
5
  HttpSimulator.reset_endpoints
6
+
7
+ HttpSimulator.register_endpoint 'GET', '/foo', 'something'
8
+ HttpSimulator.register_endpoint 'POST', '/bar', 'something else'
9
+
10
+ HttpSimulator.run_daemon!(port: test_port)
6
11
  end
7
12
 
8
13
  after :each do
9
- HttpSimulator.stop_daemon!
14
+ HttpSimulator.stop_daemon!(port: test_port)
10
15
  end
11
16
 
12
17
  it 'sets up an endpoint for each registered endpoint' do
13
- HttpSimulator.register_endpoint 'GET', '/foo', ''
14
- HttpSimulator.register_endpoint 'POST', '/bar', ''
15
- HttpSimulator.run_daemon!(port: 6565)
16
-
17
- resp = HTTParty.get('http://localhost:6565/foo')
18
+ resp = get '/foo'
18
19
  expect(resp.code).to eq 200
19
20
 
20
- resp = HTTParty.post('http://localhost:6565/bar', :body => '')
21
+ resp = post '/bar', 'some inconsequential json'
21
22
  expect(resp.code).to eq 200
22
23
  end
23
24
  end
@@ -3,34 +3,31 @@ require 'spec_helper'
3
3
  describe 'requests' do
4
4
  before :each do
5
5
  HttpSimulator.reset_endpoints
6
+
7
+ HttpSimulator.register_endpoint 'POST', '/hello', 'jan'
8
+ HttpSimulator.register_endpoint 'POST', '/world', 'van riebeck'
9
+
10
+ HttpSimulator.run_daemon!(port: test_port)
6
11
  end
7
12
 
8
13
  after :each do
9
- HttpSimulator.stop_daemon!
14
+ HttpSimulator.stop_daemon!(port: test_port)
10
15
  end
11
16
 
12
17
  describe 'getting requests' do
13
18
  it 'sets up GET /<endpoint>/requests for each registered endpoint' do
14
- HttpSimulator.register_endpoint 'POST', '/hi', ''
15
- HttpSimulator.register_endpoint 'POST', '/bye', ''
16
- HttpSimulator.run_daemon!(port: 6565)
17
-
18
- resp = HTTParty.get('http://localhost:6565/hi/requests')
19
+ resp = get '/hello/requests'
19
20
  expect(resp.code).to eq 200
20
21
 
21
- resp = HTTParty.get('http://localhost:6565/bye/requests')
22
+ resp = get '/world/requests'
22
23
  expect(resp.code).to eq 200
23
24
  end
24
25
 
25
26
  it 'GET /<endpoint>/requests returns recorded requests' do
26
- HttpSimulator.register_endpoint 'POST', '/bye', 'byeeeee'
27
- HttpSimulator.run_daemon!(port: 6565)
28
-
29
- HTTParty.post('http://localhost:6565/bye', :body => 'hello world')
30
-
31
- resp = HTTParty.get('http://localhost:6565/bye/requests')
27
+ post '/world', 'hello world'
28
+ resp = json_get '/world/requests'
32
29
  expect(resp.code).to eq 200
33
- expect(resp.body).to include 'hello world'
30
+ expect(resp.body).to eq '["hello world"]'
34
31
  end
35
32
 
36
33
  xit '.resets returns recorded requests' # TODO
@@ -4,52 +4,77 @@ describe 'responses' do
4
4
  before :each do
5
5
  HttpSimulator.reset_endpoints
6
6
 
7
- HttpSimulator.register_endpoint 'POST', '/hi', ''
8
- HttpSimulator.register_endpoint 'POST', '/bye', ''
7
+ HttpSimulator.register_endpoint 'POST', '/hi', 'this is hi response'
8
+ HttpSimulator.register_endpoint 'POST', '/bye', 'this is bye response'
9
9
 
10
- HttpSimulator.run_daemon!(port: 6565)
10
+ HttpSimulator.run_daemon!(port: test_port)
11
11
  end
12
12
 
13
13
  after :each do
14
- HttpSimulator.stop_daemon!
14
+ HttpSimulator.stop_daemon!(port: test_port)
15
15
  end
16
16
 
17
17
  describe 'getting response' do
18
18
  it 'sets up GET /<endpoint>/response for each registered endpoint' do
19
- resp = HTTParty.get('http://localhost:6565/hi/response')
19
+ resp = get '/hi/response'
20
20
  expect(resp.code).to eq 200
21
21
 
22
- resp = HTTParty.get('http://localhost:6565/bye/response')
22
+ resp = get '/bye/response'
23
23
  expect(resp.code).to eq 200
24
24
  end
25
25
 
26
- xit 'GET /<endpoint>/response returns response for endpoint' # TODO
26
+ it 'GET /<endpoint>/response returns response for endpoint' do
27
+ resp = json_get '/hi/response'
28
+ expect(resp.body).to eq 'this is hi response'
29
+
30
+ resp = json_get '/bye/response'
31
+ expect(resp.body).to eq 'this is bye response'
32
+ end
33
+
27
34
  xit '.response returns response for endpoint' # TODO
28
35
  end
29
36
 
30
37
  describe 'setting response' do
31
38
  it 'sets up PUT /<endpoint>/response for each registered endpoint' do
32
- resp = HTTParty.put('http://localhost:6565/hi/response', :body => '{}')
39
+ resp = put '/hi/response', '{}'
33
40
  expect(resp.code).to eq 200
34
41
 
35
- resp = HTTParty.put('http://localhost:6565/bye/response', :body => '{}')
42
+ resp = put '/bye/response', '{}'
36
43
  expect(resp.code).to eq 200
37
44
  end
38
45
 
39
- xit 'PUT /<endpoint>/response alters response for endpoint' # TODO
46
+ it 'PUT /<endpoint>/response alters response for endpoint' do
47
+ resp = json_get '/hi/response'
48
+ expect(resp.body).to eq 'this is hi response'
49
+
50
+ put '/hi/response', 'this is some new response'
51
+ resp = json_get '/hi/response'
52
+ expect(resp.body).to eq 'this is some new response'
53
+ end
54
+
40
55
  xit '.set_response alters response for endpoint' # TODO
41
56
  end
42
57
 
43
58
  describe 'resetting response' do
44
59
  it 'sets up a DELETE /<endpoint>/response for each registered endpoint' do
45
- resp = HTTParty.delete('http://localhost:6565/hi/response')
60
+ resp = delete '/hi/response'
46
61
  expect(resp.code).to eq 200
47
62
 
48
- resp = HTTParty.put('http://localhost:6565/bye/response')
63
+ resp = delete '/bye/response'
49
64
  expect(resp.code).to eq 200
50
65
  end
51
66
 
52
- xit 'DELETE /<endpoint>/response resets response for endpoint' # TODO
67
+ it 'DELETE /<endpoint>/response resets response for endpoint' do
68
+ put '/hi/response', 'this is some new response'
69
+ resp = json_get '/hi/response'
70
+ expect(resp.body).to eq 'this is some new response'
71
+
72
+ delete '/hi/response'
73
+
74
+ resp = json_get '/hi/response'
75
+ expect(resp.body).to eq 'this is hi response'
76
+ end
77
+
53
78
  xit '.reset_response resets response for endpoint' # TODO
54
79
  end
55
80
  end
data/spec/spec_helper.rb CHANGED
@@ -6,3 +6,27 @@ RSpec.configure do |c|
6
6
  c.order = :random
7
7
  c.default_formatter = 'doc'
8
8
  end
9
+
10
+ def test_port
11
+ 6565
12
+ end
13
+
14
+ def get(path)
15
+ HTTParty.get("http://localhost:#{test_port}#{path}")
16
+ end
17
+
18
+ def json_get(path)
19
+ HTTParty.get("http://localhost:#{test_port}#{path}", :headers => {'Content-Type' => 'application/json'})
20
+ end
21
+
22
+ def post(path, body)
23
+ HTTParty.post("http://localhost:#{test_port}#{path}", :body => body)
24
+ end
25
+
26
+ def put(path, body)
27
+ HTTParty.put("http://localhost:#{test_port}#{path}", :body => body)
28
+ end
29
+
30
+ def delete(path)
31
+ HTTParty.delete("http://localhost:#{test_port}#{path}")
32
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: http_sim
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jean de Klerk