http_stub 0.13.5 → 0.14.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (77) hide show
  1. checksums.yaml +4 -4
  2. data/lib/http_stub.rb +13 -4
  3. data/lib/http_stub/configurer.rb +9 -63
  4. data/lib/http_stub/configurer/deprecated_dsl.rb +57 -0
  5. data/lib/http_stub/configurer/request/stub.rb +7 -13
  6. data/lib/http_stub/configurer/request/stub_activator.rb +8 -4
  7. data/lib/http_stub/configurer/request/stub_activator_payload_builder.rb +25 -0
  8. data/lib/http_stub/configurer/request/stub_payload_builder.rb +44 -0
  9. data/lib/http_stub/configurer/server/buffered_command_processor.rb +24 -0
  10. data/lib/http_stub/configurer/server/command.rb +18 -0
  11. data/lib/http_stub/configurer/server/command_processor.rb +38 -0
  12. data/lib/http_stub/configurer/server/dsl.rb +56 -0
  13. data/lib/http_stub/configurer/server/facade.rb +57 -0
  14. data/lib/http_stub/configurer/server/request_processor.rb +30 -0
  15. data/lib/http_stub/models/registry.rb +4 -0
  16. data/lib/http_stub/models/stub.rb +2 -1
  17. data/lib/http_stub/models/stub_registry.rb +29 -0
  18. data/lib/http_stub/models/stub_triggers.rb +23 -0
  19. data/lib/http_stub/rake/server_tasks.rb +2 -5
  20. data/lib/http_stub/server.rb +14 -4
  21. data/lib/http_stub/version.rb +1 -1
  22. data/lib/http_stub/views/_stub.haml +7 -0
  23. data/spec/lib/http_stub/configurer/deprecated_dsl_spec.rb +232 -0
  24. data/spec/lib/http_stub/configurer/request/controllable_value_spec.rb +8 -8
  25. data/spec/lib/http_stub/configurer/request/omittable_spec.rb +8 -8
  26. data/spec/lib/http_stub/configurer/request/regexpable_spec.rb +14 -14
  27. data/spec/lib/http_stub/configurer/request/stub_activator_payload_builder_spec.rb +74 -0
  28. data/spec/lib/http_stub/configurer/request/stub_activator_spec.rb +19 -26
  29. data/spec/lib/http_stub/configurer/request/stub_payload_builder_spec.rb +203 -0
  30. data/spec/lib/http_stub/configurer/request/stub_spec.rb +18 -157
  31. data/spec/lib/http_stub/configurer/server/buffered_command_processor_spec.rb +35 -0
  32. data/spec/lib/http_stub/configurer/server/command_processor_integration_spec.rb +62 -0
  33. data/spec/lib/http_stub/configurer/server/command_spec.rb +25 -0
  34. data/spec/lib/http_stub/configurer/server/dsl_spec.rb +241 -0
  35. data/spec/lib/http_stub/configurer/server/facade_spec.rb +253 -0
  36. data/spec/lib/http_stub/configurer/server/request_processor_spec.rb +99 -0
  37. data/spec/lib/http_stub/configurer_integration_spec.rb +172 -122
  38. data/spec/lib/http_stub/configurer_spec.rb +4 -4
  39. data/spec/lib/http_stub/controllers/stub_activator_controller_spec.rb +25 -25
  40. data/spec/lib/http_stub/controllers/stub_controller_spec.rb +22 -22
  41. data/spec/lib/http_stub/hash_extensions_spec.rb +24 -24
  42. data/spec/lib/http_stub/models/exact_value_matcher_spec.rb +4 -4
  43. data/spec/lib/http_stub/models/hash_with_string_value_matchers_spec.rb +29 -29
  44. data/spec/lib/http_stub/models/headers_spec.rb +10 -10
  45. data/spec/lib/http_stub/models/omitted_value_matcher_spec.rb +8 -8
  46. data/spec/lib/http_stub/models/regexp_value_matcher_spec.rb +6 -6
  47. data/spec/lib/http_stub/models/registry_spec.rb +71 -28
  48. data/spec/lib/http_stub/models/request_header_parser_spec.rb +4 -4
  49. data/spec/lib/http_stub/models/request_pipeline_spec.rb +2 -2
  50. data/spec/lib/http_stub/models/response_spec.rb +38 -38
  51. data/spec/lib/http_stub/models/string_value_matcher_spec.rb +25 -25
  52. data/spec/lib/http_stub/models/stub_activator_spec.rb +15 -15
  53. data/spec/lib/http_stub/models/stub_headers_spec.rb +18 -18
  54. data/spec/lib/http_stub/models/stub_parameters_spec.rb +16 -16
  55. data/spec/lib/http_stub/models/stub_registry_integration_spec.rb +62 -0
  56. data/spec/lib/http_stub/models/stub_registry_spec.rb +152 -0
  57. data/spec/lib/http_stub/models/stub_spec.rb +67 -43
  58. data/spec/lib/http_stub/models/stub_triggers_spec.rb +64 -0
  59. data/spec/lib/http_stub/models/stub_uri_spec.rb +9 -9
  60. data/spec/lib/http_stub/rake/server_daemon_tasks_smoke_spec.rb +7 -7
  61. data/spec/lib/http_stub/rake/server_tasks_smoke_spec.rb +5 -5
  62. data/spec/lib/http_stub/rake/server_tasks_spec.rb +17 -15
  63. data/spec/lib/http_stub/server_daemon_spec.rb +16 -16
  64. data/spec/lib/http_stub/server_integration_spec.rb +112 -65
  65. data/spec/lib/http_stub/server_spec.rb +134 -96
  66. data/spec/spec_helper.rb +1 -1
  67. data/spec/support/http_stub/configurer/request/stub_payload_builder_shared_context.rb +27 -0
  68. data/spec/support/server_integration.rb +2 -2
  69. metadata +69 -30
  70. data/lib/http_stub/configurer/command.rb +0 -26
  71. data/lib/http_stub/configurer/command_processor.rb +0 -36
  72. data/lib/http_stub/configurer/impatient_command_chain.rb +0 -13
  73. data/lib/http_stub/configurer/patient_command_chain.rb +0 -25
  74. data/spec/lib/http_stub/configurer/command_processor_integration_spec.rb +0 -63
  75. data/spec/lib/http_stub/configurer/command_spec.rb +0 -40
  76. data/spec/lib/http_stub/configurer/impatient_command_chain_spec.rb +0 -17
  77. data/spec/lib/http_stub/configurer/patient_command_chain_spec.rb +0 -73
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a55a4336aae9316c8d777ed41a0f0727a3e823f9
4
- data.tar.gz: 1626c8d91fed1870ba6c4876e281a60efe06cb66
3
+ metadata.gz: 3cec045dffff863269a30e24044093374be1218f
4
+ data.tar.gz: dc15184117f995a3718732ab94bddc6d4fbd4166
5
5
  SHA512:
6
- metadata.gz: 5a9281b96bad47e690cf8ab42b73ae0da0a52200561a307aaa0e2ea16e0c1cc1e4998ddf9779d311aeb67fb968fd9ede7b4fe4169ffef26ca8c2c84d2157047b
7
- data.tar.gz: 802e5b6711c1f8c3468210dc27e3c9a2cbd9e5f6670c20fdbd4a23a63621252059231ffe11a292f7939e8075db0c33e513536b076c8f5bb4d7b2f220178868e5
6
+ metadata.gz: 7e903d5defca3127e859d7d66831bbcd66ad5d0c8a3cbbcaf5a5b08f2a431a18bf7d97441405272643a1ea0c9a60b58988b2af3b7704b5d2f98c715017ed46a4
7
+ data.tar.gz: 6fcd2527a0bab4f1006890912b57628cea65d8032de7e71e73a252108eefeccf452fc979f5f72a8ac6e9e3284c82f787e468c0cb98bbd0f56c0455612cd2130e
data/lib/http_stub.rb CHANGED
@@ -5,6 +5,8 @@ require 'sass'
5
5
  require 'net/http'
6
6
  require 'json'
7
7
  require 'http_server_manager'
8
+ require 'active_support/core_ext/module/delegation'
9
+ require 'active_support/core_ext/hash/slice'
8
10
 
9
11
  require_relative 'http_stub/hash_extensions'
10
12
  require_relative 'http_stub/models/headers'
@@ -18,9 +20,11 @@ require_relative 'http_stub/models/request_header_parser'
18
20
  require_relative 'http_stub/models/stub_uri'
19
21
  require_relative 'http_stub/models/stub_headers'
20
22
  require_relative 'http_stub/models/stub_parameters'
23
+ require_relative 'http_stub/models/stub_triggers'
21
24
  require_relative 'http_stub/models/stub'
22
25
  require_relative 'http_stub/models/stub_activator'
23
26
  require_relative 'http_stub/models/registry'
27
+ require_relative 'http_stub/models/stub_registry'
24
28
  require_relative 'http_stub/models/request_pipeline'
25
29
  require_relative 'http_stub/controllers/stub_controller'
26
30
  require_relative 'http_stub/controllers/stub_activator_controller'
@@ -30,9 +34,14 @@ require_relative 'http_stub/configurer/request/omittable'
30
34
  require_relative 'http_stub/configurer/request/regexpable'
31
35
  require_relative 'http_stub/configurer/request/controllable_value'
32
36
  require_relative 'http_stub/configurer/request/stub'
37
+ require_relative 'http_stub/configurer/request/stub_payload_builder'
33
38
  require_relative 'http_stub/configurer/request/stub_activator'
34
- require_relative 'http_stub/configurer/command'
35
- require_relative 'http_stub/configurer/command_processor'
36
- require_relative 'http_stub/configurer/patient_command_chain'
37
- require_relative 'http_stub/configurer/impatient_command_chain'
39
+ require_relative 'http_stub/configurer/request/stub_activator_payload_builder'
40
+ require_relative 'http_stub/configurer/server/command'
41
+ require_relative 'http_stub/configurer/server/command_processor'
42
+ require_relative 'http_stub/configurer/server/buffered_command_processor'
43
+ require_relative 'http_stub/configurer/server/request_processor'
44
+ require_relative 'http_stub/configurer/server/facade'
45
+ require_relative 'http_stub/configurer/server/dsl'
46
+ require_relative 'http_stub/configurer/deprecated_dsl'
38
47
  require_relative 'http_stub/configurer'
@@ -5,6 +5,7 @@ module HttpStub
5
5
  def self.included(mod)
6
6
  mod.extend(HttpStub::Configurer::ClassMethods)
7
7
  mod.send(:include, HttpStub::Configurer::InstanceMethods)
8
+ mod.send(:include, HttpStub::Configurer::DeprecatedDSL)
8
9
  end
9
10
 
10
11
  module ClassMethods
@@ -29,84 +30,29 @@ module HttpStub
29
30
  "http://#{@host}:#{@port}"
30
31
  end
31
32
 
32
- def stub_activator(activation_uri, stub_uri, options)
33
- request = HttpStub::Configurer::Request::StubActivator.new(activation_uri, stub_uri, options)
34
- handle(request: request, description: "registering activator '#{activation_uri}'")
35
- end
36
-
37
- def stub!(uri, options)
38
- request = HttpStub::Configurer::Request::Stub.new(uri, options)
39
- handle(request: request, description: "stubbing '#{uri}'", resetable: true)
40
- end
41
-
42
- alias_method :stub_response!, :stub!
43
-
44
- def activate!(uri)
45
- handle(request: Net::HTTP::Get.new(uri), description: "activating '#{uri}'", resetable: true)
46
- end
47
-
48
- alias_method :activate_stub!, :activate!
49
-
50
- def server_has_started!
51
- @effective_command_chain = HttpStub::Configurer::ImpatientCommandChain.new()
33
+ def stub_server
34
+ @dsl ||= HttpStub::Configurer::Server::DSL.new(server_facade)
52
35
  end
53
36
 
54
37
  def initialize!
55
38
  on_initialize if self.respond_to?(:on_initialize) && !@initialized
56
- initialize_command_chain.execute()
39
+ server_facade.flush_requests
40
+ server_facade.remember_stubs
57
41
  @initialized = true
58
- server_has_started!
59
42
  end
60
43
 
61
- def reset!
62
- clear!
63
- initialize_command_chain.filter(&:resetable?).execute()
64
- end
65
-
66
- def clear_activators!
67
- handle(request: Net::HTTP::Delete.new("/stubs/activators"), description: "clearing activators")
68
- end
69
-
70
- def clear!
71
- handle(request: Net::HTTP::Delete.new("/stubs"), description: "clearing stubs")
72
- end
73
-
74
- alias_method :clear_stubs!, :clear!
75
-
76
44
  private
77
45
 
78
- def handle(command_args)
79
- effective_command_chain <<
80
- HttpStub::Configurer::Command.new({ processor: command_processor }.merge(command_args))
81
- end
82
-
83
- def effective_command_chain
84
- @effective_command_chain ||= initialize_command_chain
85
- end
86
-
87
- def initialize_command_chain
88
- @initialize_command_chain ||= HttpStub::Configurer::PatientCommandChain.new()
89
- end
90
-
91
- def command_processor
92
- @command_processor ||= HttpStub::Configurer::CommandProcessor.new(self)
46
+ def server_facade
47
+ @server_facade ||= HttpStub::Configurer::Server::Facade.new(self)
93
48
  end
94
49
 
95
50
  end
96
51
 
97
52
  module InstanceMethods
98
53
 
99
- DELEGATE_METHODS = %w{ stub_activator stub! stub_response! activate! activate_stub!
100
- server_has_started! reset! clear_activators! clear! clear_stubs! }
101
-
102
- def self.included(mod)
103
- DELEGATE_METHODS.each do |method_name|
104
- mod.class_eval <<-METHOD_DEF
105
- def #{method_name}(*args)
106
- self.class.#{method_name}(*args)
107
- end
108
- METHOD_DEF
109
- end
54
+ def stub_server
55
+ self.class.stub_server
110
56
  end
111
57
 
112
58
  end
@@ -0,0 +1,57 @@
1
+ module HttpStub
2
+ module Configurer
3
+
4
+ module DeprecatedDSL
5
+
6
+ def self.included(mod)
7
+ mod.extend(HttpStub::Configurer::DeprecatedDSL::Methods)
8
+ mod.send(:include, HttpStub::Configurer::DeprecatedDSL::Methods)
9
+ end
10
+
11
+ module Methods
12
+
13
+ def server_has_started!
14
+ stub_server.has_started!
15
+ end
16
+
17
+ def stub!(uri, options)
18
+ stub_server.add_stub! { |stub| add_stub_options_to_builder(stub, uri, options) }
19
+ end
20
+
21
+ alias_method :stub_response!, :stub!
22
+
23
+ def stub_activator(activation_uri, stub_uri, options)
24
+ stub_server.add_activator! do |activator|
25
+ activator.path(activation_uri)
26
+ add_stub_options_to_builder(activator, stub_uri, options)
27
+ end
28
+ end
29
+
30
+ def activate!(uri)
31
+ stub_server.activate!(uri)
32
+ end
33
+
34
+ def recall_stubs!
35
+ stub_server.recall_stubs!
36
+ end
37
+
38
+ def clear_stubs!
39
+ stub_server.clear_stubs!
40
+ end
41
+
42
+ def clear_activators!
43
+ stub_server.clear_activators!
44
+ end
45
+
46
+ private
47
+
48
+ def add_stub_options_to_builder(stub, uri, options)
49
+ stub.match_request(uri, options.slice(:method, :headers, :parameters))
50
+ stub.with_response(options[:response])
51
+ end
52
+
53
+ end
54
+
55
+ end
56
+ end
57
+ end
@@ -4,21 +4,15 @@ module HttpStub
4
4
 
5
5
  class Stub < Net::HTTP::Post
6
6
 
7
- def initialize(uri, args)
7
+ def initialize(payload)
8
8
  super("/stubs")
9
+ @payload = payload
9
10
  self.content_type = "application/json"
10
- self.body = {
11
- "uri" => HttpStub::Configurer::Request::ControllableValue.format(uri),
12
- "method" => args[:method],
13
- "headers" => HttpStub::Configurer::Request::ControllableValue.format(args[:headers] || {}),
14
- "parameters" => HttpStub::Configurer::Request::ControllableValue.format(args[:parameters] || {}),
15
- "response" => {
16
- "status" => args[:response][:status] || "",
17
- "headers" => args[:response][:headers] || {},
18
- "body" => args[:response][:body],
19
- "delay_in_seconds" => args[:response][:delay_in_seconds] || ""
20
- }
21
- }.to_json
11
+ self.body = payload.to_json
12
+ end
13
+
14
+ def stub_uri
15
+ @payload[:uri]
22
16
  end
23
17
 
24
18
  end
@@ -4,11 +4,15 @@ module HttpStub
4
4
 
5
5
  class StubActivator < Net::HTTP::Post
6
6
 
7
- def initialize(activation_uri, stub_uri, options)
7
+ def initialize(payload)
8
8
  super("/stubs/activators")
9
- stub_request = HttpStub::Configurer::Request::Stub.new(stub_uri, options)
10
- self.content_type = stub_request.content_type
11
- self.body = JSON.parse(stub_request.body).merge({ "activation_uri" => activation_uri }).to_json
9
+ @payload = payload
10
+ self.content_type = "application/json"
11
+ self.body = payload.to_json
12
+ end
13
+
14
+ def activation_uri
15
+ @payload[:activation_uri]
12
16
  end
13
17
 
14
18
  end
@@ -0,0 +1,25 @@
1
+ module HttpStub
2
+ module Configurer
3
+ module Request
4
+
5
+ class StubActivatorPayloadBuilder
6
+
7
+ delegate :match_request, :with_response, :and_add_stub, to: :@stub_payload_builder
8
+
9
+ def initialize
10
+ @stub_payload_builder = HttpStub::Configurer::Request::StubPayloadBuilder.new
11
+ end
12
+
13
+ def path(uri)
14
+ @activation_uri = uri
15
+ end
16
+
17
+ def build
18
+ { activation_uri: @activation_uri }.merge(@stub_payload_builder.build)
19
+ end
20
+
21
+ end
22
+
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,44 @@
1
+ module HttpStub
2
+ module Configurer
3
+ module Request
4
+
5
+ class StubPayloadBuilder
6
+
7
+ def initialize
8
+ @args = { triggers: [] }
9
+ end
10
+
11
+ def match_request(uri, args={})
12
+ @uri = uri
13
+ @args.merge!(args)
14
+ end
15
+
16
+ def with_response(args)
17
+ @args.merge!(response: args)
18
+ end
19
+
20
+ def and_add_stub(stub_builder)
21
+ @args[:triggers] << stub_builder
22
+ end
23
+
24
+ def build
25
+ {
26
+ uri: HttpStub::Configurer::Request::ControllableValue.format(@uri),
27
+ method: @args[:method],
28
+ headers: HttpStub::Configurer::Request::ControllableValue.format(@args[:headers] || {}),
29
+ parameters: HttpStub::Configurer::Request::ControllableValue.format(@args[:parameters] || {}),
30
+ response: {
31
+ status: @args[:response][:status] || "",
32
+ headers: @args[:response][:headers] || {},
33
+ body: @args[:response][:body],
34
+ delay_in_seconds: @args[:response][:delay_in_seconds] || ""
35
+ },
36
+ triggers: @args[:triggers].map(&:build)
37
+ }
38
+ end
39
+
40
+ end
41
+
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,24 @@
1
+ module HttpStub
2
+ module Configurer
3
+ module Server
4
+
5
+ class BufferedCommandProcessor
6
+
7
+ def initialize(command_processor)
8
+ @command_processor = command_processor
9
+ @commands = []
10
+ end
11
+
12
+ def process(command)
13
+ @commands << command
14
+ end
15
+
16
+ def flush
17
+ @commands.each { |command| @command_processor.process(command) }
18
+ end
19
+
20
+ end
21
+
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,18 @@
1
+ module HttpStub
2
+ module Configurer
3
+ module Server
4
+
5
+ class Command
6
+
7
+ attr_reader :request, :description
8
+
9
+ def initialize(args)
10
+ @request = args[:request]
11
+ @description = args[:description]
12
+ end
13
+
14
+ end
15
+
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,38 @@
1
+ module HttpStub
2
+ module Configurer
3
+ module Server
4
+
5
+ class CommandProcessor
6
+
7
+ def initialize(configurer)
8
+ @configurer = configurer
9
+ end
10
+
11
+ def process(command)
12
+ begin
13
+ response = Net::HTTP.start(host, port) { |http| http.request(command.request) }
14
+ raise "#{error_message_prefix(command)}: #{response.code} #{response.message}" unless response.code == "200"
15
+ rescue Exception => exc
16
+ raise "#{error_message_prefix(command)}: #{exc}"
17
+ end
18
+ end
19
+
20
+ private
21
+
22
+ def host
23
+ @configurer.get_host
24
+ end
25
+
26
+ def port
27
+ @configurer.get_port
28
+ end
29
+
30
+ def error_message_prefix(command)
31
+ "Error occurred #{command.description} whilst configuring #{@configurer.get_base_uri}: "
32
+ end
33
+
34
+ end
35
+
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,56 @@
1
+ module HttpStub
2
+ module Configurer
3
+ module Server
4
+
5
+ class DSL
6
+
7
+ def initialize(server_facade)
8
+ @server_facade = server_facade
9
+ end
10
+
11
+ def has_started!
12
+ @server_facade.server_has_started
13
+ end
14
+
15
+ def build_stub(&block)
16
+ builder = HttpStub::Configurer::Request::StubPayloadBuilder.new
17
+ block.call(builder) if block_given?
18
+ builder
19
+ end
20
+
21
+ def add_stub!(builder=nil, &block)
22
+ resolved_builder = builder || self.build_stub(&block)
23
+ @server_facade.stub_response(HttpStub::Configurer::Request::Stub.new(resolved_builder.build))
24
+ end
25
+
26
+ def add_activator!(&block)
27
+ builder = HttpStub::Configurer::Request::StubActivatorPayloadBuilder.new
28
+ block.call(builder)
29
+ @server_facade.stub_activator(HttpStub::Configurer::Request::StubActivator.new(builder.build))
30
+ end
31
+
32
+ def activate!(uri)
33
+ @server_facade.activate(uri)
34
+ end
35
+
36
+ def remember_stubs
37
+ @server_facade.remember_stubs
38
+ end
39
+
40
+ def recall_stubs!
41
+ @server_facade.recall_stubs
42
+ end
43
+
44
+ def clear_stubs!
45
+ @server_facade.clear_stubs
46
+ end
47
+
48
+ def clear_activators!
49
+ @server_facade.clear_activators
50
+ end
51
+
52
+ end
53
+
54
+ end
55
+ end
56
+ end