webvalve 0.9.0

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.
Files changed (64) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +21 -0
  3. data/README.md +195 -0
  4. data/Rakefile +22 -0
  5. data/lib/generators/webvalve/fake_service_generator.rb +70 -0
  6. data/lib/generators/webvalve/install_generator.rb +33 -0
  7. data/lib/tasks/webvalve_tasks.rake +0 -0
  8. data/lib/webvalve.rb +40 -0
  9. data/lib/webvalve/engine.rb +26 -0
  10. data/lib/webvalve/fake_service.rb +22 -0
  11. data/lib/webvalve/fake_service_config.rb +53 -0
  12. data/lib/webvalve/fake_service_wrapper.rb +18 -0
  13. data/lib/webvalve/instrumentation.rb +4 -0
  14. data/lib/webvalve/instrumentation/log_subscriber.rb +16 -0
  15. data/lib/webvalve/instrumentation/middleware.rb +24 -0
  16. data/lib/webvalve/manager.rb +76 -0
  17. data/lib/webvalve/rspec.rb +8 -0
  18. data/lib/webvalve/version.rb +3 -0
  19. data/spec/dummy/README.rdoc +28 -0
  20. data/spec/dummy/Rakefile +6 -0
  21. data/spec/dummy/app/assets/javascripts/application.js +13 -0
  22. data/spec/dummy/app/assets/stylesheets/application.css +15 -0
  23. data/spec/dummy/app/controllers/application_controller.rb +5 -0
  24. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  25. data/spec/dummy/app/views/layouts/application.html.erb +13 -0
  26. data/spec/dummy/bin/bundle +3 -0
  27. data/spec/dummy/bin/rails +4 -0
  28. data/spec/dummy/bin/rake +4 -0
  29. data/spec/dummy/bin/setup +29 -0
  30. data/spec/dummy/config.ru +4 -0
  31. data/spec/dummy/config/application.rb +31 -0
  32. data/spec/dummy/config/boot.rb +5 -0
  33. data/spec/dummy/config/database.yml +25 -0
  34. data/spec/dummy/config/environment.rb +5 -0
  35. data/spec/dummy/config/environments/development.rb +41 -0
  36. data/spec/dummy/config/environments/production.rb +79 -0
  37. data/spec/dummy/config/environments/test.rb +42 -0
  38. data/spec/dummy/config/initializers/assets.rb +11 -0
  39. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  40. data/spec/dummy/config/initializers/cookies_serializer.rb +3 -0
  41. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  42. data/spec/dummy/config/initializers/inflections.rb +16 -0
  43. data/spec/dummy/config/initializers/mime_types.rb +4 -0
  44. data/spec/dummy/config/initializers/session_store.rb +3 -0
  45. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  46. data/spec/dummy/config/locales/en.yml +23 -0
  47. data/spec/dummy/config/routes.rb +3 -0
  48. data/spec/dummy/config/secrets.yml +22 -0
  49. data/spec/dummy/db/test.sqlite3 +0 -0
  50. data/spec/dummy/log/development.log +0 -0
  51. data/spec/dummy/log/test.log +3938 -0
  52. data/spec/dummy/public/404.html +67 -0
  53. data/spec/dummy/public/422.html +67 -0
  54. data/spec/dummy/public/500.html +66 -0
  55. data/spec/dummy/public/favicon.ico +0 -0
  56. data/spec/examples.txt +30 -0
  57. data/spec/rails_helper.rb +14 -0
  58. data/spec/spec_helper.rb +22 -0
  59. data/spec/support/helpers.rb +24 -0
  60. data/spec/webvalve/fake_service_config_spec.rb +133 -0
  61. data/spec/webvalve/fake_service_spec.rb +47 -0
  62. data/spec/webvalve/manager_spec.rb +132 -0
  63. data/spec/webvalve_spec.rb +19 -0
  64. metadata +268 -0
@@ -0,0 +1,67 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>The page you were looking for doesn't exist (404)</title>
5
+ <meta name="viewport" content="width=device-width,initial-scale=1">
6
+ <style>
7
+ body {
8
+ background-color: #EFEFEF;
9
+ color: #2E2F30;
10
+ text-align: center;
11
+ font-family: arial, sans-serif;
12
+ margin: 0;
13
+ }
14
+
15
+ div.dialog {
16
+ width: 95%;
17
+ max-width: 33em;
18
+ margin: 4em auto 0;
19
+ }
20
+
21
+ div.dialog > div {
22
+ border: 1px solid #CCC;
23
+ border-right-color: #999;
24
+ border-left-color: #999;
25
+ border-bottom-color: #BBB;
26
+ border-top: #B00100 solid 4px;
27
+ border-top-left-radius: 9px;
28
+ border-top-right-radius: 9px;
29
+ background-color: white;
30
+ padding: 7px 12% 0;
31
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
32
+ }
33
+
34
+ h1 {
35
+ font-size: 100%;
36
+ color: #730E15;
37
+ line-height: 1.5em;
38
+ }
39
+
40
+ div.dialog > p {
41
+ margin: 0 0 1em;
42
+ padding: 1em;
43
+ background-color: #F7F7F7;
44
+ border: 1px solid #CCC;
45
+ border-right-color: #999;
46
+ border-left-color: #999;
47
+ border-bottom-color: #999;
48
+ border-bottom-left-radius: 4px;
49
+ border-bottom-right-radius: 4px;
50
+ border-top-color: #DADADA;
51
+ color: #666;
52
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
53
+ }
54
+ </style>
55
+ </head>
56
+
57
+ <body>
58
+ <!-- This file lives in public/404.html -->
59
+ <div class="dialog">
60
+ <div>
61
+ <h1>The page you were looking for doesn't exist.</h1>
62
+ <p>You may have mistyped the address or the page may have moved.</p>
63
+ </div>
64
+ <p>If you are the application owner check the logs for more information.</p>
65
+ </div>
66
+ </body>
67
+ </html>
@@ -0,0 +1,67 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>The change you wanted was rejected (422)</title>
5
+ <meta name="viewport" content="width=device-width,initial-scale=1">
6
+ <style>
7
+ body {
8
+ background-color: #EFEFEF;
9
+ color: #2E2F30;
10
+ text-align: center;
11
+ font-family: arial, sans-serif;
12
+ margin: 0;
13
+ }
14
+
15
+ div.dialog {
16
+ width: 95%;
17
+ max-width: 33em;
18
+ margin: 4em auto 0;
19
+ }
20
+
21
+ div.dialog > div {
22
+ border: 1px solid #CCC;
23
+ border-right-color: #999;
24
+ border-left-color: #999;
25
+ border-bottom-color: #BBB;
26
+ border-top: #B00100 solid 4px;
27
+ border-top-left-radius: 9px;
28
+ border-top-right-radius: 9px;
29
+ background-color: white;
30
+ padding: 7px 12% 0;
31
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
32
+ }
33
+
34
+ h1 {
35
+ font-size: 100%;
36
+ color: #730E15;
37
+ line-height: 1.5em;
38
+ }
39
+
40
+ div.dialog > p {
41
+ margin: 0 0 1em;
42
+ padding: 1em;
43
+ background-color: #F7F7F7;
44
+ border: 1px solid #CCC;
45
+ border-right-color: #999;
46
+ border-left-color: #999;
47
+ border-bottom-color: #999;
48
+ border-bottom-left-radius: 4px;
49
+ border-bottom-right-radius: 4px;
50
+ border-top-color: #DADADA;
51
+ color: #666;
52
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
53
+ }
54
+ </style>
55
+ </head>
56
+
57
+ <body>
58
+ <!-- This file lives in public/422.html -->
59
+ <div class="dialog">
60
+ <div>
61
+ <h1>The change you wanted was rejected.</h1>
62
+ <p>Maybe you tried to change something you didn't have access to.</p>
63
+ </div>
64
+ <p>If you are the application owner check the logs for more information.</p>
65
+ </div>
66
+ </body>
67
+ </html>
@@ -0,0 +1,66 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>We're sorry, but something went wrong (500)</title>
5
+ <meta name="viewport" content="width=device-width,initial-scale=1">
6
+ <style>
7
+ body {
8
+ background-color: #EFEFEF;
9
+ color: #2E2F30;
10
+ text-align: center;
11
+ font-family: arial, sans-serif;
12
+ margin: 0;
13
+ }
14
+
15
+ div.dialog {
16
+ width: 95%;
17
+ max-width: 33em;
18
+ margin: 4em auto 0;
19
+ }
20
+
21
+ div.dialog > div {
22
+ border: 1px solid #CCC;
23
+ border-right-color: #999;
24
+ border-left-color: #999;
25
+ border-bottom-color: #BBB;
26
+ border-top: #B00100 solid 4px;
27
+ border-top-left-radius: 9px;
28
+ border-top-right-radius: 9px;
29
+ background-color: white;
30
+ padding: 7px 12% 0;
31
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
32
+ }
33
+
34
+ h1 {
35
+ font-size: 100%;
36
+ color: #730E15;
37
+ line-height: 1.5em;
38
+ }
39
+
40
+ div.dialog > p {
41
+ margin: 0 0 1em;
42
+ padding: 1em;
43
+ background-color: #F7F7F7;
44
+ border: 1px solid #CCC;
45
+ border-right-color: #999;
46
+ border-left-color: #999;
47
+ border-bottom-color: #999;
48
+ border-bottom-left-radius: 4px;
49
+ border-bottom-right-radius: 4px;
50
+ border-top-color: #DADADA;
51
+ color: #666;
52
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
53
+ }
54
+ </style>
55
+ </head>
56
+
57
+ <body>
58
+ <!-- This file lives in public/500.html -->
59
+ <div class="dialog">
60
+ <div>
61
+ <h1>We're sorry, but something went wrong.</h1>
62
+ </div>
63
+ <p>If you are the application owner check the logs for more information.</p>
64
+ </div>
65
+ </body>
66
+ </html>
File without changes
@@ -0,0 +1,30 @@
1
+ example_id | status | run_time |
2
+ ---------------------------------------------------- | ------ | --------------- |
3
+ ./spec/webvalve/fake_service_config_spec.rb[1:1:1:1] | passed | 0.00176 seconds |
4
+ ./spec/webvalve/fake_service_config_spec.rb[1:1:1:2] | passed | 0.00061 seconds |
5
+ ./spec/webvalve/fake_service_config_spec.rb[1:1:2:1] | passed | 0.0005 seconds |
6
+ ./spec/webvalve/fake_service_config_spec.rb[1:1:2:2] | passed | 0.00061 seconds |
7
+ ./spec/webvalve/fake_service_config_spec.rb[1:1:2:3] | passed | 0.00093 seconds |
8
+ ./spec/webvalve/fake_service_config_spec.rb[1:1:3:1] | passed | 0.00054 seconds |
9
+ ./spec/webvalve/fake_service_config_spec.rb[1:1:3:2] | passed | 0.0024 seconds |
10
+ ./spec/webvalve/fake_service_config_spec.rb[1:2:1] | passed | 0.001 seconds |
11
+ ./spec/webvalve/fake_service_config_spec.rb[1:2:2] | passed | 0.00067 seconds |
12
+ ./spec/webvalve/fake_service_config_spec.rb[1:2:3] | passed | 0.00061 seconds |
13
+ ./spec/webvalve/fake_service_spec.rb[1:1] | passed | 0.00162 seconds |
14
+ ./spec/webvalve/fake_service_spec.rb[1:2:1] | passed | 0.01159 seconds |
15
+ ./spec/webvalve/fake_service_spec.rb[1:2:2] | passed | 0.00863 seconds |
16
+ ./spec/webvalve/manager_spec.rb[1:1] | passed | 0.00048 seconds |
17
+ ./spec/webvalve/manager_spec.rb[1:2:1] | passed | 0.0009 seconds |
18
+ ./spec/webvalve/manager_spec.rb[1:3:1] | passed | 0.00063 seconds |
19
+ ./spec/webvalve/manager_spec.rb[1:4:1] | passed | 0.00057 seconds |
20
+ ./spec/webvalve/manager_spec.rb[1:5:1] | passed | 0.00155 seconds |
21
+ ./spec/webvalve/manager_spec.rb[1:5:2] | passed | 0.00286 seconds |
22
+ ./spec/webvalve/manager_spec.rb[1:5:3] | passed | 0.01329 seconds |
23
+ ./spec/webvalve/manager_spec.rb[1:5:4:1] | passed | 0.00184 seconds |
24
+ ./spec/webvalve/manager_spec.rb[1:5:5:1] | passed | 0.00182 seconds |
25
+ ./spec/webvalve/manager_spec.rb[1:5:5:2] | passed | 0.00243 seconds |
26
+ ./spec/webvalve/manager_spec.rb[1:5:5:3] | passed | 0.00334 seconds |
27
+ ./spec/webvalve_spec.rb[1:1] | passed | 0.00046 seconds |
28
+ ./spec/webvalve_spec.rb[1:2] | passed | 0.00048 seconds |
29
+ ./spec/webvalve_spec.rb[1:3] | passed | 0.00398 seconds |
30
+ ./spec/webvalve_spec.rb[1:4] | passed | 0.00047 seconds |
@@ -0,0 +1,14 @@
1
+ ENV['RAILS_ENV'] ||= 'test'
2
+ require File.expand_path('../dummy/config/environment', __FILE__)
3
+ abort("The Rails environment is running in production mode!") if Rails.env.production?
4
+ require 'spec_helper'
5
+ require 'rspec/rails'
6
+ require 'pry'
7
+ Dir[Rails.root.join("#{WebValve::Engine.root}/spec/support/**/*.rb")].each { |f| require f }
8
+
9
+ RSpec.configure do |config|
10
+ config.use_transactional_fixtures = true
11
+ config.infer_spec_type_from_file_location!
12
+ config.filter_rails_from_backtrace!
13
+ config.include Helpers
14
+ end
@@ -0,0 +1,22 @@
1
+ RSpec.configure do |config|
2
+ config.expect_with :rspec do |expectations|
3
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
4
+ end
5
+
6
+ config.mock_with :rspec do |mocks|
7
+ mocks.verify_partial_doubles = true
8
+ end
9
+
10
+ config.filter_run :focus
11
+ config.run_all_when_everything_filtered = true
12
+ config.example_status_persistence_file_path = "spec/examples.txt"
13
+
14
+ if config.files_to_run.one?
15
+ config.default_formatter = 'doc'
16
+ end
17
+
18
+ config.order = :random
19
+ Kernel.srand config.seed
20
+
21
+ # config.profile_examples = 10
22
+ end
@@ -0,0 +1,24 @@
1
+ module Helpers
2
+ def with_env(opts = {})
3
+ old = {}
4
+ opts.each do |k, v|
5
+ k = k.to_s
6
+ v = v.to_s unless v.nil?
7
+ old[k] = ENV[k]
8
+ ENV[k] = v
9
+ end
10
+ yield
11
+ ensure
12
+ old.each do |k, v|
13
+ ENV[k] = v
14
+ end
15
+ end
16
+
17
+ def with_rails_env(env)
18
+ initial_env = Rails.env
19
+ Rails.env = env
20
+ yield
21
+ ensure
22
+ Rails.env = initial_env
23
+ end
24
+ end
@@ -0,0 +1,133 @@
1
+ require 'rails_helper'
2
+
3
+ RSpec.describe WebValve::FakeServiceConfig do
4
+ let(:fake_service) do
5
+ Class.new(described_class) do
6
+ def self.service_name
7
+ 'dummy'
8
+ end
9
+
10
+ def self.name
11
+ 'FakeDummy'
12
+ end
13
+ end
14
+ end
15
+
16
+ before do
17
+ stub_const('FakeDummy', fake_service)
18
+ end
19
+
20
+ subject { described_class.new service: fake_service }
21
+
22
+ describe '.should_intercept?' do
23
+ context 'in test env' do
24
+ around do |ex|
25
+ with_rails_env 'test' do
26
+ ex.run
27
+ end
28
+ end
29
+
30
+ it 'returns true when DUMMY_ENABLED is unset' do
31
+ expect(subject.should_intercept?).to eq true
32
+ end
33
+
34
+ it 'returns true regardless of DUMMY_ENABLED value' do
35
+ with_env 'DUMMY_ENABLED' => '1' do
36
+ expect(subject.should_intercept?).to eq true
37
+ end
38
+
39
+ with_env 'DUMMY_ENABLED' => '0' do
40
+ expect(subject.should_intercept?).to eq true
41
+ end
42
+ end
43
+ end
44
+
45
+ context 'in development env' do
46
+ around do |ex|
47
+ with_rails_env 'development' do
48
+ ex.run
49
+ end
50
+ end
51
+
52
+ it 'returns true when DUMMY_ENABLED is unset' do
53
+ expect(subject.should_intercept?).to eq true
54
+ end
55
+
56
+ it 'returns false when DUMMY_ENABLED is truthy' do
57
+ with_env 'DUMMY_ENABLED' => '1' do
58
+ expect(subject.should_intercept?).to eq false
59
+ end
60
+
61
+ with_env 'DUMMY_ENABLED' => 't' do
62
+ expect(subject.should_intercept?).to eq false
63
+ end
64
+
65
+ with_env 'DUMMY_ENABLED' => 'true' do
66
+ expect(subject.should_intercept?).to eq false
67
+ end
68
+ end
69
+
70
+ it 'returns true when DUMMY_ENABLED is not truthy' do
71
+ with_env 'DUMMY_ENABLED' => '0' do
72
+ expect(subject.should_intercept?).to eq true
73
+ end
74
+
75
+ with_env 'DUMMY_ENABLED' => 'f' do
76
+ expect(subject.should_intercept?).to eq true
77
+ end
78
+
79
+ with_env 'DUMMY_ENABLED' => 'false' do
80
+ expect(subject.should_intercept?).to eq true
81
+ end
82
+
83
+ with_env 'DUMMY_ENABLED' => 'not true or false' do
84
+ expect(subject.should_intercept?).to eq true
85
+ end
86
+ end
87
+ end
88
+
89
+ context 'in production enviroment' do
90
+ around do |ex|
91
+ with_rails_env 'production' do
92
+ ex.run
93
+ end
94
+ end
95
+
96
+ it 'returns false' do
97
+ expect(subject.should_intercept?).to eq false
98
+ end
99
+
100
+ it 'returns false regardless of DUMMY_ENABLED value' do
101
+ with_env 'DUMMY_ENABLED' => '1' do
102
+ expect(subject.should_intercept?).to eq false
103
+ end
104
+
105
+ with_env 'DUMMY_ENABLED' => '0' do
106
+ expect(subject.should_intercept?).to eq false
107
+ end
108
+ end
109
+ end
110
+ end
111
+
112
+ describe '.service_url' do
113
+ it 'raises if the url is not present' do
114
+ expect { subject.service_url }.to raise_error <<~MESSAGE
115
+ There is no URL defined for FakeDummy.
116
+ Configure one by setting the ENV variable "DUMMY_API_URL"
117
+ or by using WebValve.register FakeDummy, url: "http://something.dev"
118
+ MESSAGE
119
+ end
120
+
121
+ it 'discovers url via ENV based on fake service name' do
122
+ with_env 'DUMMY_API_URL' => 'http://thingy.dev' do
123
+ expect(subject.service_url).to eq 'http://thingy.dev'
124
+ end
125
+ end
126
+
127
+ it 'removes embedded basic auth credentials' do
128
+ with_env 'DUMMY_API_URL' => 'http://foo:bar@thingy.dev' do
129
+ expect(subject.service_url).to eq 'http://thingy.dev'
130
+ end
131
+ end
132
+ end
133
+ end
@@ -0,0 +1,47 @@
1
+ require 'rails_helper'
2
+
3
+ RSpec.describe WebValve::FakeService do
4
+ subject do
5
+ Class.new(described_class) do
6
+ def self.name
7
+ 'FakeDummy'
8
+ end
9
+
10
+ get '/widgets' do
11
+ json({ result: 'it works!' })
12
+ end
13
+ end
14
+ end
15
+
16
+ before do
17
+ stub_const('FakeDummy', subject)
18
+ end
19
+
20
+ it 'is a Sinatra::Base' do
21
+ expect(subject).to be < Sinatra::Base
22
+ end
23
+
24
+ describe 'integrated behavior' do
25
+ after do
26
+ WebValve.reset
27
+ end
28
+
29
+ it 'raise a useful error when an unmapped route is requested' do
30
+ with_env 'DUMMY_API_URL' => 'http://dummy.dev' do
31
+ WebValve.register subject
32
+ WebValve.setup
33
+
34
+ expect { Net::HTTP.get(URI('http://dummy.dev/foos')) }.to raise_error(RuntimeError, /route not defined for GET/)
35
+ end
36
+ end
37
+
38
+ it 'returns the result from the fake when a mapped route is requested' do
39
+ with_env 'DUMMY_API_URL' => 'http://dummy.dev' do
40
+ WebValve.register subject
41
+ WebValve.setup
42
+
43
+ expect(Net::HTTP.get(URI('http://dummy.dev/widgets'))).to eq({ result: 'it works!' }.to_json)
44
+ end
45
+ end
46
+ end
47
+ end