http_sim 0.0.3 → 0.0.4

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: d21ac256b9d856db653bb2db467b2554d6d2a5bd
4
- data.tar.gz: 8b2bf8488b0b3ddf3e90c268554ac97ed727bfc0
3
+ metadata.gz: 2866f09b90fbd1283a2720b908ef50aa9e026ef2
4
+ data.tar.gz: 748166f06723673e6fa78bb7ba365838261a5dc4
5
5
  SHA512:
6
- metadata.gz: 0195b67a0723716a9f85b01f42451713a92b74b6858f663ae96f63464ce2be2c8d2dba67880e07d8cec8266af57181cb88696a17093debcec58d007c2dce4996
7
- data.tar.gz: 950d7e79ff08ab5516990e63718a4c4a6cb8b798e7c6f33a0d6901f14d94ebd5f7316b63e7c7530b1c335284dead5f2cb7a564bc34f87544829cdaeda7a94f05
6
+ metadata.gz: 0c17159bd2216ccf74fabe1efe391cfc5cb31c0257a89421ba56d26653c537006fc593cc5cd6e9a78d68d9002441327f6abe0020976fb6d586f2f5a45a9a9f2e
7
+ data.tar.gz: 80ae4f7fd6edadccf95045477e67c8d1cead1ff83acf7ec7b5e37beba55e47616936fa76d2d979c670b569580b80839d3ace78028183550ceace2a6833902c0b
data/CHANGELOG.txt CHANGED
@@ -1,3 +1,4 @@
1
1
  Version 0.0.1 http_sim is released
2
2
  Version 0.0.2 Allow changing port
3
3
  Version 0.0.3 Requests to endpoints are recorded and listed at endpoint's /request endpoint
4
+ Version 0.0.4 Added run_daemon!, stop_daemon!, and wait_for_start methods
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.3'
7
+ spec.version = '0.0.4'
8
8
  spec.authors = ['Jean de Klerk']
9
9
  spec.email = ['jadekler@gmail.com']
10
10
  spec.summary = 'Simulate your external HTTP integrations.'
@@ -18,6 +18,7 @@ Gem::Specification.new do |spec|
18
18
  spec.require_paths = ['lib']
19
19
 
20
20
  spec.add_runtime_dependency 'sinatra', '~> 1.4.0'
21
+ spec.add_runtime_dependency 'httparty', '~> 0.13.7'
21
22
  spec.add_development_dependency 'bundler', '~> 1.7'
22
23
  spec.add_development_dependency 'rake', '~> 10.0'
23
24
  spec.add_development_dependency 'rspec', '~> 3.0'
data/lib/http_sim.rb CHANGED
@@ -1,5 +1,7 @@
1
1
  require 'sinatra/base'
2
2
 
3
+ require 'httparty'
4
+
3
5
  class Endpoint
4
6
  attr_reader :method, :path, :default_response, :response
5
7
  attr_accessor :requests
@@ -22,6 +24,8 @@ class Endpoint
22
24
  end
23
25
 
24
26
  module HttpSimulator
27
+ @@pid = false
28
+
25
29
  def self.read_file(path)
26
30
  lines = []
27
31
  File.open(path, 'r') do |f|
@@ -40,6 +44,8 @@ module HttpSimulator
40
44
  @@endpoints = []
41
45
 
42
46
  def self.run!(port: 4567)
47
+ check_if_port_in_use(port)
48
+
43
49
  Sinatra::Base.get '/' do
44
50
  ERB.new(@@erb_files[:index]).result binding
45
51
  end
@@ -51,6 +57,62 @@ module HttpSimulator
51
57
  }.run!
52
58
  end
53
59
 
60
+ def self.run_daemon!(port: 4567, max_wait_seconds: 5)
61
+ check_if_port_in_use(port)
62
+
63
+ Sinatra::Base.get '/' do
64
+ ERB.new(@@erb_files[:index]).result binding
65
+ end
66
+
67
+ @@pid = Process.fork do
68
+ Class.new(Sinatra::Base) {
69
+ set :port, port
70
+
71
+ include HttpSimulator
72
+ }.run!
73
+ end
74
+
75
+ wait_for_start(port, max_wait_seconds)
76
+
77
+ at_exit do
78
+ Process.kill 'SIGKILL', @@pid
79
+ end
80
+
81
+ @@pid
82
+ end
83
+
84
+ def self.stop_daemon!
85
+ Process.kill('SIGKILL', @@pid) if @@pid
86
+ end
87
+
88
+ def self.wait_for_start(port, max_wait_seconds)
89
+ wait_count = 0
90
+ while wait_count < max_wait_seconds * 4
91
+ begin
92
+ HTTParty.get("http://localhost:#{port}/")
93
+ return
94
+ rescue Errno::ECONNREFUSED
95
+ wait_count += 1
96
+ sleep 0.25
97
+ end
98
+ end
99
+
100
+ raise "Simulators failed to start - timed out after #{max_wait_seconds} seconds!"
101
+ end
102
+
103
+ def self.check_if_port_in_use(port)
104
+ begin
105
+ HTTParty.get("http://localhost:#{port}/")
106
+ raise "Port #{port} already in use"
107
+ rescue Errno::ECONNREFUSED
108
+ # ignored
109
+ end
110
+ end
111
+
112
+ def self.reset_endpoints
113
+ @@endpoints = []
114
+ end
115
+
54
116
  def self.register_endpoint(method, path, default_response)
55
117
  raise '/ is a reserved path' if path == '/'
56
118
 
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+
3
+ describe '/<endpoint>' do
4
+ before :each do
5
+ HttpSimulator.reset_endpoints
6
+ end
7
+
8
+ after :each do
9
+ HttpSimulator.stop_daemon!
10
+ end
11
+
12
+ 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
+ expect(resp.code).to eq 200
19
+
20
+ resp = HTTParty.post('http://localhost:6565/bar', :body => '')
21
+ expect(resp.code).to eq 200
22
+ end
23
+ end
@@ -1,7 +1,34 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe 'request' do
4
- it 'works' do
5
- expect(true).to eq true
3
+ describe '/<endpoint>/requests' do
4
+ before :each do
5
+ HttpSimulator.reset_endpoints
6
+ end
7
+
8
+ after :each do
9
+ HttpSimulator.stop_daemon!
10
+ end
11
+
12
+ it 'sets up a /request endpoint for each registered endpoint' do
13
+ HttpSimulator.register_endpoint 'POST', '/hi', ''
14
+ HttpSimulator.register_endpoint 'POST', '/bye', ''
15
+ HttpSimulator.run_daemon!(port: 6565)
16
+
17
+ resp = HTTParty.get('http://localhost:6565/hi/requests')
18
+ expect(resp.code).to eq 200
19
+
20
+ resp = HTTParty.get('http://localhost:6565/bye/requests')
21
+ expect(resp.code).to eq 200
22
+ end
23
+
24
+ it 'records requests at /requests endpoint' do
25
+ HttpSimulator.register_endpoint 'POST', '/bye', 'byeeeee'
26
+ HttpSimulator.run_daemon!(port: 6565)
27
+
28
+ HTTParty.post('http://localhost:6565/bye', :body => 'hello world')
29
+
30
+ resp = HTTParty.get('http://localhost:6565/bye/requests')
31
+ expect(resp.code).to eq 200
32
+ expect(resp.body).to include 'hello world'
6
33
  end
7
34
  end
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+
3
+ describe '/<endpoint>/response' do
4
+ before :each do
5
+ HttpSimulator.reset_endpoints
6
+ end
7
+
8
+ after :each do
9
+ HttpSimulator.stop_daemon!
10
+ end
11
+
12
+ it 'sets up a /response endpoint for each registered endpoint' do
13
+ HttpSimulator.register_endpoint 'POST', '/hi', ''
14
+ HttpSimulator.register_endpoint 'POST', '/bye', ''
15
+ HttpSimulator.run_daemon!(port: 6565)
16
+
17
+ resp = HTTParty.get('http://localhost:6565/hi/response')
18
+ expect(resp.code).to eq 200
19
+
20
+ resp = HTTParty.get('http://localhost:6565/bye/response')
21
+ expect(resp.code).to eq 200
22
+ end
23
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,3 +1,7 @@
1
+ require 'httparty'
2
+
3
+ require_relative '../lib/http_sim'
4
+
1
5
  RSpec.configure do |c|
2
6
  c.order = :random
3
7
  c.default_formatter = 'doc'
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.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jean de Klerk
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: 1.4.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: httparty
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.13.7
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.13.7
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: bundler
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -89,7 +103,9 @@ files:
89
103
  - lib/index.html.erb
90
104
  - lib/request.html.erb
91
105
  - lib/response.html.erb
106
+ - spec/features/register_endpoint_spec.rb
92
107
  - spec/features/request_spec.rb
108
+ - spec/features/response_spec.rb
93
109
  - spec/spec_helper.rb
94
110
  homepage: http://github.com/jadekler/http_sim
95
111
  licenses:
@@ -116,5 +132,7 @@ signing_key:
116
132
  specification_version: 4
117
133
  summary: Simulate your external HTTP integrations.
118
134
  test_files:
135
+ - spec/features/register_endpoint_spec.rb
119
136
  - spec/features/request_spec.rb
137
+ - spec/features/response_spec.rb
120
138
  - spec/spec_helper.rb