webhook_recorder 0.1.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b28c08371915ba53f2690fd7265983c59845cbb8
4
+ data.tar.gz: d16f369b1cdbb9ee00e2aa6c258754ac90f90754
5
+ SHA512:
6
+ metadata.gz: 37db9108e7916a5b22267517a89e349c1c7727790cd99c8d65e9cb9ce3a50bb3e0b4f847023983dd26f9235fcc14f3a2071f66895f0e47f54165739b24e62276
7
+ data.tar.gz: 69d7024537cbccc2dfb778c71e77addeb3b2807d8c47543032184a473022c20efad9fa90b77d6958785d91163d9e0d1a1f97ac388889c2caecac0c93f3af75f4
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.3
5
+ before_install: gem install bundler -v 1.14.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in webhook_recorder.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Senthil
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,68 @@
1
+ # WebhookRecorder
2
+
3
+ When you build a feature that makes calls to webhooks of subscribers, this library helps to test that behaviour.
4
+
5
+ This runs a simple WEBrick server, makes it accessible via [ngrok](https://ngrok.com) and records the results. You can assert on the recorded requests to ensure the code you built made calls to registered webhooks.
6
+
7
+ [![CircleCI](https://circleci.com/gh/siliconsenthil/webhook_recorder/tree/master.svg?style=svg)](https://circleci.com/gh/siliconsenthil/webhook_recorder/tree/master)
8
+
9
+ Read the intro blog post [here](http://siliconsenthil.in/blog/2017/04/02/so-you-built-webhooks-and-wanna-test/).
10
+
11
+ ## Dependency
12
+
13
+ This uses [ngrok](https://ngrok.com) to publish URL that's accessible via internet.
14
+
15
+ ## Installation
16
+
17
+ Add this line to your application's Gemfile:
18
+
19
+ ```ruby
20
+ gem 'webhook_recorder'
21
+ ```
22
+
23
+ And then execute:
24
+
25
+ $ bundle
26
+
27
+ Or install it yourself as:
28
+
29
+ $ gem install webhook_recorder
30
+
31
+ ## Usage
32
+
33
+ * If you have ngrok account, set env. var `NGROK_AUTH_TOKEN` before running
34
+
35
+ ```ruby
36
+ it 'should respond as defined as response_config' do
37
+ response_config = { '/hello' => { code: 200, body: 'Expected result' } }
38
+ WebhookRecorder::Server.open(@port, response_config) do |server|
39
+ #These URLs are accessible from internet
40
+ p server.http_url
41
+ p server.https_url
42
+
43
+ # Register the webhook with any of the above URLs
44
+ # Make call to the code that invokes webhooks
45
+ # For e.g. if it made call to /hello with query params as q=1 and JSON body as {some: 1, other: 2}, you can assert like below.
46
+
47
+ req1 = server.recorded_reqs.first
48
+ expect(req1[:request_path]).to eq('/hello')
49
+ expect(req1[:query_string]).to include('q=1')
50
+ expect(JSON.parse(req1[:request_body]).symbolize_keys).to eq({some: 1, other: 2})
51
+ end
52
+ end
53
+ ```
54
+
55
+ ## Development
56
+
57
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
58
+
59
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
60
+
61
+ ## Contributing
62
+
63
+ Bug reports and pull requests are welcome on GitHub at https://github.com/siliconsenthil]/webhook_recorder.
64
+
65
+
66
+ ## License
67
+
68
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "webhook_recorder"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/circle.yml ADDED
@@ -0,0 +1,9 @@
1
+ checkout:
2
+ pre:
3
+ - wget https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-amd64.zip
4
+ - unzip ngrok-stable-linux-amd64.zip
5
+ - mv ngrok $HOME/bin
6
+
7
+ dependencies:
8
+ pre:
9
+ - gem install bundler -v 1.14.6
@@ -0,0 +1,6 @@
1
+ require "webhook_recorder/version"
2
+ require "webhook_recorder/server"
3
+
4
+ module WebhookRecorder
5
+ # Your code goes here...
6
+ end
@@ -0,0 +1,67 @@
1
+ require 'webhook_recorder/version'
2
+ require 'rack'
3
+ require 'webrick'
4
+ require 'ngrok/tunnel'
5
+ require 'active_support/core_ext/hash'
6
+
7
+ module WebhookRecorder
8
+ class Server
9
+ attr_accessor :recorded_reqs, :http_url, :https_url, :response_config, :port, :log_verbose
10
+
11
+ def initialize(port, response_config, log_verbose = false)
12
+ self.port = port
13
+ self.response_config = response_config
14
+ self.recorded_reqs = []
15
+ self.log_verbose = log_verbose
16
+ @started = false
17
+ end
18
+
19
+ def self.open(port, response_config, log_verbose=false)
20
+ server = new(port, response_config, log_verbose)
21
+ server.start
22
+ server.wait
23
+ Ngrok::Tunnel.start(port: port, authtoken: ENV['NGROK_AUTH_TOKEN'])
24
+ server.http_url = Ngrok::Tunnel.ngrok_url
25
+ server.https_url = Ngrok::Tunnel.ngrok_url_https
26
+ yield server
27
+ ensure
28
+ server.recorded_reqs.clear
29
+ server.stop
30
+ Ngrok::Tunnel.stop
31
+ end
32
+
33
+ def start
34
+ Thread.new do
35
+ options = {
36
+ Port: @port,
37
+ Logger: WEBrick::Log.new(self.log_verbose ? STDOUT : "/dev/null"),
38
+ AccessLog: [],
39
+ DoNotReverseLookup: true,
40
+ StartCallback: -> { @started = true }
41
+ }
42
+ Rack::Handler::WEBrick.run(self, options) { |s| @server = s }
43
+ end
44
+ end
45
+
46
+ def wait
47
+ Timeout.timeout(10) { sleep 0.1 until @started }
48
+ end
49
+
50
+ def call(env)
51
+ path = env['PATH_INFO']
52
+ request = Rack::Request.new(env)
53
+ recorded_reqs << env.merge(request_body: request.body.string).deep_transform_keys(&:downcase).with_indifferent_access
54
+ if response_config[path]
55
+ res = response_config[path]
56
+ [res[:code], res[:headers] || {}, [res[:body] || "Missing body in response_config"]]
57
+ else
58
+ warn "WebhookRecorder::Server: Missing response_config for path #{path}"
59
+ [404, {}, ["WebhookRecorder::Server: Missing response_config for path #{path}"]]
60
+ end
61
+ end
62
+
63
+ def stop
64
+ @server.shutdown if @server
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,3 @@
1
+ module WebhookRecorder
2
+ VERSION = "0.1.1"
3
+ end
@@ -0,0 +1,33 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'webhook_recorder/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'webhook_recorder'
8
+ spec.version = WebhookRecorder::VERSION
9
+ spec.authors = ['Senthil V S']
10
+ spec.email = ['vss123@gmail.com']
11
+
12
+ spec.summary = 'Simple HTTP server helps to test behaviour that calls webhooks'
13
+ spec.description = 'It runs a HTTP server, exposes in internet via ngrok, and records the requests'
14
+ spec.homepage = 'https://github.com/siliconsenthil/webhook_recorder'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ # spec.bindir = 'exe'
21
+ # spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ['lib']
23
+
24
+ spec.add_runtime_dependency 'activesupport', '~>5.0.2'
25
+ spec.add_runtime_dependency 'webrick', '~>1.3.1'
26
+ spec.add_runtime_dependency 'ngrok-tunnel', '~>2.1.0'
27
+ spec.add_runtime_dependency 'rack', '~>2.0.1'
28
+
29
+ spec.add_development_dependency 'bundler', '~> 1.14'
30
+ spec.add_development_dependency 'rake', '~> 10.0'
31
+ spec.add_development_dependency 'rspec', '~> 3.0'
32
+ spec.add_development_dependency 'rest-client', '~> 2.0.1'
33
+ end
metadata ADDED
@@ -0,0 +1,171 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: webhook_recorder
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Senthil V S
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-04-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 5.0.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 5.0.2
27
+ - !ruby/object:Gem::Dependency
28
+ name: webrick
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 1.3.1
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 1.3.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: ngrok-tunnel
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 2.1.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 2.1.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: rack
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 2.0.1
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 2.0.1
69
+ - !ruby/object:Gem::Dependency
70
+ name: bundler
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.14'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.14'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '10.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '10.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '3.0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '3.0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rest-client
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: 2.0.1
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: 2.0.1
125
+ description: It runs a HTTP server, exposes in internet via ngrok, and records the
126
+ requests
127
+ email:
128
+ - vss123@gmail.com
129
+ executables: []
130
+ extensions: []
131
+ extra_rdoc_files: []
132
+ files:
133
+ - ".gitignore"
134
+ - ".rspec"
135
+ - ".travis.yml"
136
+ - Gemfile
137
+ - LICENSE.txt
138
+ - README.md
139
+ - Rakefile
140
+ - bin/console
141
+ - bin/setup
142
+ - circle.yml
143
+ - lib/webhook_recorder.rb
144
+ - lib/webhook_recorder/server.rb
145
+ - lib/webhook_recorder/version.rb
146
+ - webhook_recorder.gemspec
147
+ homepage: https://github.com/siliconsenthil/webhook_recorder
148
+ licenses:
149
+ - MIT
150
+ metadata: {}
151
+ post_install_message:
152
+ rdoc_options: []
153
+ require_paths:
154
+ - lib
155
+ required_ruby_version: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ required_rubygems_version: !ruby/object:Gem::Requirement
161
+ requirements:
162
+ - - ">="
163
+ - !ruby/object:Gem::Version
164
+ version: '0'
165
+ requirements: []
166
+ rubyforge_project:
167
+ rubygems_version: 2.6.10
168
+ signing_key:
169
+ specification_version: 4
170
+ summary: Simple HTTP server helps to test behaviour that calls webhooks
171
+ test_files: []