http_stub 0.15.5 → 0.16.0.pre1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (137) hide show
  1. checksums.yaml +4 -4
  2. data/lib/http_stub.rb +37 -27
  3. data/lib/http_stub/configurer.rb +2 -2
  4. data/lib/http_stub/configurer/dsl/deprecated.rb +60 -0
  5. data/lib/http_stub/configurer/{server/dsl.rb → dsl/sanctioned.rb} +18 -18
  6. data/lib/http_stub/configurer/dsl/scenario_activator.rb +15 -0
  7. data/lib/http_stub/configurer/dsl/scenario_builder.rb +37 -0
  8. data/lib/http_stub/configurer/dsl/stub_activator_builder.rb +24 -0
  9. data/lib/http_stub/configurer/{request → dsl}/stub_builder.rb +4 -3
  10. data/lib/http_stub/configurer/dsl/stub_builder_producer.rb +21 -0
  11. data/lib/http_stub/configurer/request/http/factory.rb +8 -2
  12. data/lib/http_stub/configurer/request/scenario.rb +29 -0
  13. data/lib/http_stub/configurer/server/facade.rb +7 -7
  14. data/lib/http_stub/server/application.rb +21 -14
  15. data/lib/http_stub/server/registry.rb +7 -3
  16. data/lib/http_stub/server/response.rb +3 -3
  17. data/lib/http_stub/server/scenario.rb +12 -0
  18. data/lib/http_stub/server/scenario/activator.rb +25 -0
  19. data/lib/http_stub/server/scenario/controller.rb +36 -0
  20. data/lib/http_stub/server/scenario/instance.rb +36 -0
  21. data/lib/http_stub/server/scenario/request_parser.rb +19 -0
  22. data/lib/http_stub/server/stub.rb +3 -23
  23. data/lib/http_stub/server/stub/controller.rb +30 -0
  24. data/lib/http_stub/server/stub/exact_value_matcher.rb +15 -0
  25. data/lib/http_stub/server/stub/hash_with_string_value_matchers.rb +22 -0
  26. data/lib/http_stub/server/stub/headers.rb +19 -0
  27. data/lib/http_stub/server/stub/instance.rb +31 -0
  28. data/lib/http_stub/server/stub/method.rb +23 -0
  29. data/lib/http_stub/server/stub/omitted_value_matcher.rb +21 -0
  30. data/lib/http_stub/server/stub/payload_file_consolidator.rb +18 -0
  31. data/lib/http_stub/server/stub/regexp_value_matcher.rb +16 -0
  32. data/lib/http_stub/server/stub/registry.rb +31 -0
  33. data/lib/http_stub/server/stub/request_header_parser.rb +19 -0
  34. data/lib/http_stub/server/stub/request_headers.rb +28 -0
  35. data/lib/http_stub/server/stub/request_parameters.rb +23 -0
  36. data/lib/http_stub/server/stub/request_parser.rb +17 -0
  37. data/lib/http_stub/server/stub/response.rb +16 -0
  38. data/lib/http_stub/server/stub/response/base.rb +64 -0
  39. data/lib/http_stub/server/stub/response/file.rb +36 -0
  40. data/lib/http_stub/server/stub/response/text.rb +19 -0
  41. data/lib/http_stub/server/stub/string_value_matcher.rb +31 -0
  42. data/lib/http_stub/server/stub/triggers.rb +25 -0
  43. data/lib/http_stub/server/stub/uri.rb +23 -0
  44. data/lib/http_stub/server/views/_scenario.haml +16 -0
  45. data/lib/http_stub/server/views/application.sass +4 -0
  46. data/lib/http_stub/server/views/scenarios.haml +3 -0
  47. data/lib/http_stub/version.rb +1 -1
  48. data/spec/acceptance/activator_spec.rb +81 -0
  49. data/spec/acceptance/configurer_initialization_spec.rb +158 -0
  50. data/spec/acceptance/scenario_spec.rb +91 -0
  51. data/spec/acceptance/stub_control_values_spec.rb +160 -0
  52. data/spec/acceptance/stub_spec.rb +290 -0
  53. data/spec/acceptance/stub_trigger_spec.rb +144 -0
  54. data/spec/lib/http_stub/configurer/{deprecated_dsl_spec.rb → dsl/deprecated_spec.rb} +21 -13
  55. data/spec/lib/http_stub/configurer/{server/dsl_spec.rb → dsl/sanctioned_spec.rb} +74 -97
  56. data/spec/lib/http_stub/configurer/dsl/scenario_activator_spec.rb +62 -0
  57. data/spec/lib/http_stub/configurer/dsl/scenario_builder_spec.rb +146 -0
  58. data/spec/lib/http_stub/configurer/dsl/stub_activator_builder_spec.rb +120 -0
  59. data/spec/lib/http_stub/configurer/dsl/stub_builder_producer_spec.rb +105 -0
  60. data/spec/lib/http_stub/configurer/{request → dsl}/stub_builder_spec.rb +33 -5
  61. data/spec/lib/http_stub/configurer/request/controllable_value_spec.rb +1 -1
  62. data/spec/lib/http_stub/configurer/request/http/factory_spec.rb +46 -5
  63. data/spec/lib/http_stub/configurer/request/omittable_spec.rb +1 -1
  64. data/spec/lib/http_stub/configurer/request/scenario_spec.rb +69 -0
  65. data/spec/lib/http_stub/configurer/request/stub_spec.rb +2 -2
  66. data/spec/lib/http_stub/configurer/server/facade_spec.rb +17 -17
  67. data/spec/lib/http_stub/configurer_spec.rb +1 -1
  68. data/spec/lib/http_stub/server/application_integration_spec.rb +23 -13
  69. data/spec/lib/http_stub/server/application_spec.rb +40 -38
  70. data/spec/lib/http_stub/server/daemon_spec.rb +2 -2
  71. data/spec/lib/http_stub/server/registry_spec.rb +66 -17
  72. data/spec/lib/http_stub/server/{request_parser_spec.rb → request_file_consolidator_spec.rb} +20 -33
  73. data/spec/lib/http_stub/server/response_pipeline_spec.rb +1 -1
  74. data/spec/lib/http_stub/server/scenario/activator_spec.rb +106 -0
  75. data/spec/lib/http_stub/server/scenario/controller_spec.rb +102 -0
  76. data/spec/lib/http_stub/server/scenario/instance_spec.rb +126 -0
  77. data/spec/lib/http_stub/server/scenario/request_parser_spec.rb +59 -0
  78. data/spec/lib/http_stub/server/scenario_spec.rb +24 -0
  79. data/spec/lib/http_stub/server/{stub_controller_spec.rb → stub/controller_spec.rb} +13 -17
  80. data/spec/lib/http_stub/server/{exact_value_matcher_spec.rb → stub/exact_value_matcher_spec.rb} +3 -3
  81. data/spec/lib/http_stub/server/{hash_with_string_value_matchers_spec.rb → stub/hash_with_string_value_matchers_spec.rb} +7 -5
  82. data/spec/lib/http_stub/server/{headers_spec.rb → stub/headers_spec.rb} +2 -2
  83. data/spec/lib/http_stub/server/stub/instance_spec.rb +201 -0
  84. data/spec/lib/http_stub/server/stub/method_spec.rb +95 -0
  85. data/spec/lib/http_stub/server/{omitted_value_matcher_spec.rb → stub/omitted_value_matcher_spec.rb} +3 -3
  86. data/spec/lib/http_stub/server/{regexp_value_matcher_spec.rb → stub/regexp_value_matcher_spec.rb} +3 -3
  87. data/spec/lib/http_stub/server/stub/registry_integration_spec.rb +56 -0
  88. data/spec/lib/http_stub/server/{stub_registry_spec.rb → stub/registry_spec.rb} +27 -14
  89. data/spec/lib/http_stub/server/{request_header_parser_spec.rb → stub/request_header_parser_spec.rb} +6 -6
  90. data/spec/lib/http_stub/server/stub/request_headers_spec.rb +68 -0
  91. data/spec/lib/http_stub/server/{stub_parameters_spec.rb → stub/request_parameters_spec.rb} +19 -17
  92. data/spec/lib/http_stub/server/stub/request_parser_spec.rb +56 -0
  93. data/spec/lib/http_stub/server/{stub_response → stub/response}/base_spec.rb +5 -5
  94. data/spec/lib/http_stub/server/{stub_response → stub/response}/file_spec.rb +3 -3
  95. data/spec/lib/http_stub/server/{stub_response → stub/response}/text_spec.rb +3 -3
  96. data/spec/lib/http_stub/server/{stub_response_spec.rb → stub/response_spec.rb} +8 -8
  97. data/spec/lib/http_stub/server/{string_value_matcher_spec.rb → stub/string_value_matcher_spec.rb} +25 -13
  98. data/spec/lib/http_stub/server/{stub_triggers_spec.rb → stub/triggers_spec.rb} +7 -9
  99. data/spec/lib/http_stub/server/stub/uri_spec.rb +43 -0
  100. data/spec/lib/http_stub/server/stub_spec.rb +10 -185
  101. data/spec/spec_helper.rb +10 -5
  102. data/spec/support/configurer_integration.rb +24 -0
  103. data/spec/support/scenario_fixture.rb +33 -0
  104. data/spec/support/stub_fixture.rb +1 -1
  105. metadata +126 -84
  106. data/lib/http_stub/configurer/deprecated_dsl.rb +0 -57
  107. data/lib/http_stub/configurer/request/stub_activator.rb +0 -26
  108. data/lib/http_stub/configurer/request/stub_activator_builder.rb +0 -25
  109. data/lib/http_stub/server/exact_value_matcher.rb +0 -13
  110. data/lib/http_stub/server/hash_with_string_value_matchers.rb +0 -20
  111. data/lib/http_stub/server/headers.rb +0 -17
  112. data/lib/http_stub/server/omitted_value_matcher.rb +0 -19
  113. data/lib/http_stub/server/regexp_value_matcher.rb +0 -14
  114. data/lib/http_stub/server/request_header_parser.rb +0 -17
  115. data/lib/http_stub/server/request_parser.rb +0 -29
  116. data/lib/http_stub/server/string_value_matcher.rb +0 -29
  117. data/lib/http_stub/server/stub_activator.rb +0 -30
  118. data/lib/http_stub/server/stub_activator_controller.rb +0 -34
  119. data/lib/http_stub/server/stub_controller.rb +0 -28
  120. data/lib/http_stub/server/stub_headers.rb +0 -24
  121. data/lib/http_stub/server/stub_parameters.rb +0 -21
  122. data/lib/http_stub/server/stub_registry.rb +0 -29
  123. data/lib/http_stub/server/stub_response.rb +0 -14
  124. data/lib/http_stub/server/stub_response/base.rb +0 -62
  125. data/lib/http_stub/server/stub_response/file.rb +0 -34
  126. data/lib/http_stub/server/stub_response/text.rb +0 -17
  127. data/lib/http_stub/server/stub_triggers.rb +0 -23
  128. data/lib/http_stub/server/stub_uri.rb +0 -21
  129. data/lib/http_stub/server/views/stub_activators.haml +0 -6
  130. data/spec/lib/http_stub/configurer/request/stub_activator_builder_spec.rb +0 -119
  131. data/spec/lib/http_stub/configurer/request/stub_activator_spec.rb +0 -45
  132. data/spec/lib/http_stub/configurer_integration_spec.rb +0 -837
  133. data/spec/lib/http_stub/server/stub_activator_controller_spec.rb +0 -95
  134. data/spec/lib/http_stub/server/stub_activator_spec.rb +0 -83
  135. data/spec/lib/http_stub/server/stub_headers_spec.rb +0 -68
  136. data/spec/lib/http_stub/server/stub_registry_integration_spec.rb +0 -62
  137. data/spec/lib/http_stub/server/stub_uri_spec.rb +0 -42
@@ -0,0 +1,16 @@
1
+ module HttpStub
2
+ module Server
3
+ module Stub
4
+
5
+ class RegexpValueMatcher
6
+
7
+ def self.match?(stub_value, actual_value)
8
+ match_data = stub_value.match(/^regexp:(.*)/)
9
+ match_data && !!Regexp.new(match_data[1]).match(actual_value)
10
+ end
11
+
12
+ end
13
+
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,31 @@
1
+ module HttpStub
2
+ module Server
3
+ module Stub
4
+
5
+ class Registry
6
+
7
+ delegate :add, :concat, :all, :clear, to: :@registry
8
+
9
+ def initialize
10
+ @registry = HttpStub::Server::Registry.new("stub")
11
+ end
12
+
13
+ def find_for(request)
14
+ stub = @registry.find(criteria: request, request: request)
15
+ stub.triggers.add_to(self, request) if stub
16
+ stub
17
+ end
18
+
19
+ def remember
20
+ @remembered_stub = @registry.last
21
+ end
22
+
23
+ def recall
24
+ @registry.rollback_to(@remembered_stub) if @remembered_stub
25
+ end
26
+
27
+ end
28
+
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,19 @@
1
+ module HttpStub
2
+ module Server
3
+ module Stub
4
+
5
+ class RequestHeaderParser
6
+
7
+ def self.parse(request)
8
+ request.env.reduce({}) do |result, element|
9
+ match = element[0].match(/^(?:HTTP_)?([A-Z0-9_]+)$/)
10
+ result[match[1]] = element[1] if match
11
+ result
12
+ end
13
+ end
14
+
15
+ end
16
+
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,28 @@
1
+ module HttpStub
2
+ module Server
3
+ module Stub
4
+
5
+ class RequestHeaders < HttpStub::Server::Stub::Headers
6
+
7
+ def initialize(headers)
8
+ super(headers)
9
+ @headers = HttpStub::Server::Stub::HashWithStringValueMatchers.new(
10
+ (headers || {}).downcase_and_underscore_keys
11
+ )
12
+ end
13
+
14
+ def match?(request)
15
+ @headers.match?(headers_in(request).downcase_and_underscore_keys)
16
+ end
17
+
18
+ private
19
+
20
+ def headers_in(request)
21
+ HttpStub::Server::Stub::RequestHeaderParser.parse(request)
22
+ end
23
+
24
+ end
25
+
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,23 @@
1
+ module HttpStub
2
+ module Server
3
+ module Stub
4
+
5
+ class RequestParameters
6
+
7
+ def initialize(parameters)
8
+ @parameters = HttpStub::Server::Stub::HashWithStringValueMatchers.new(parameters || {})
9
+ end
10
+
11
+ def match?(request)
12
+ @parameters.match?(request.params)
13
+ end
14
+
15
+ def to_s
16
+ @parameters ? @parameters.map { |key_and_value| key_and_value.map(&:to_s).join("=") }.join("&") : ""
17
+ end
18
+
19
+ end
20
+
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,17 @@
1
+ module HttpStub
2
+ module Server
3
+ module Stub
4
+
5
+ class RequestParser
6
+
7
+ def self.parse(request)
8
+ HttpStub::Server::Stub::PayloadFileConsolidator.consolidate!(
9
+ JSON.parse(request.params["payload"] || request.body.read), request
10
+ )
11
+ end
12
+
13
+ end
14
+
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,16 @@
1
+ module HttpStub
2
+ module Server
3
+ module Stub
4
+
5
+ module Response
6
+
7
+ def self.create(args)
8
+ args["body"].is_a?(Hash) ?
9
+ HttpStub::Server::Stub::Response::File.new(args) : HttpStub::Server::Stub::Response::Text.new(args)
10
+ end
11
+
12
+ end
13
+
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,64 @@
1
+ module HttpStub
2
+ module Server
3
+ module Stub
4
+ module Response
5
+
6
+ class Base
7
+
8
+ private
9
+
10
+ DEFAULT_ARGS = { "status" => 200, "delay_in_seconds" => 0 }.freeze
11
+
12
+ public
13
+
14
+ class << self
15
+
16
+ def add_default_headers(headers)
17
+ default_headers.merge!(headers)
18
+ end
19
+
20
+ def merge_defaults(args)
21
+ args.clone.tap do |result|
22
+ headers = result["headers"] ||= {}
23
+ DEFAULT_ARGS.each { |key, value| result[key] = value if !result[key] || result[key] == "" }
24
+ default_headers.each { |key, value| headers[key] = value if !headers[key] || headers[key] == "" }
25
+ end
26
+ end
27
+
28
+ private
29
+
30
+ def default_headers
31
+ @default_headers ||= {}
32
+ end
33
+
34
+ end
35
+
36
+ attr_reader :status, :body, :delay_in_seconds, :headers
37
+
38
+ def initialize(args={})
39
+ @original_args = args
40
+ resolved_args = self.class.merge_defaults(args)
41
+ @status = resolved_args["status"]
42
+ @body = resolved_args["body"]
43
+ @delay_in_seconds = resolved_args["delay_in_seconds"]
44
+ @headers = HttpStub::Server::Stub::Headers.new(resolved_args["headers"])
45
+ end
46
+
47
+ def empty?
48
+ @original_args.empty?
49
+ end
50
+
51
+ def type
52
+ self.class.name.demodulize.underscore
53
+ end
54
+
55
+ def to_s
56
+ @original_args.to_s
57
+ end
58
+
59
+ end
60
+
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,36 @@
1
+ module HttpStub
2
+ module Server
3
+ module Stub
4
+ module Response
5
+
6
+ class File < HttpStub::Server::Stub::Response::Base
7
+
8
+ add_default_headers "content-type" => "application/octet-stream"
9
+
10
+ attr_reader :uri
11
+
12
+ def initialize(args)
13
+ @file = args["body"][:tempfile]
14
+ @uri = "file://#{@file.path}"
15
+ super(args.merge("body" => ""))
16
+ end
17
+
18
+ def serve_on(server)
19
+ server.send_file(@file.path, send_options)
20
+ end
21
+
22
+ private
23
+
24
+ def send_options
25
+ { status: @status, type: @headers["content-type"] }.tap do |options|
26
+ options[:last_modified] = @headers["last-modified"] if @headers["last-modified"]
27
+ options[:disposition] = @headers["content-disposition"] if @headers["content-disposition"]
28
+ end
29
+ end
30
+
31
+ end
32
+
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,19 @@
1
+ module HttpStub
2
+ module Server
3
+ module Stub
4
+ module Response
5
+
6
+ class Text < HttpStub::Server::Stub::Response::Base
7
+
8
+ add_default_headers "content-type" => "application/json"
9
+
10
+ def serve_on(server)
11
+ server.halt(@status, @headers.to_hash, @body)
12
+ end
13
+
14
+ end
15
+
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,31 @@
1
+ module HttpStub
2
+ module Server
3
+ module Stub
4
+
5
+ class StringValueMatcher
6
+
7
+ private
8
+
9
+ MATCHERS = [ HttpStub::Server::Stub::OmittedValueMatcher,
10
+ HttpStub::Server::Stub::RegexpValueMatcher,
11
+ HttpStub::Server::Stub::ExactValueMatcher ].freeze
12
+
13
+ public
14
+
15
+ def initialize(stub_value)
16
+ @stub_match_value = stub_value ? stub_value.to_s : stub_value
17
+ end
18
+
19
+ def match?(actual_value)
20
+ !!MATCHERS.find { |matcher| matcher.match?(@stub_match_value, actual_value) }
21
+ end
22
+
23
+ def to_s
24
+ @stub_match_value
25
+ end
26
+
27
+ end
28
+
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,25 @@
1
+ module HttpStub
2
+ module Server
3
+ module Stub
4
+
5
+ class Triggers
6
+
7
+ delegate :each, to: :@triggers
8
+
9
+ def initialize(triggers)
10
+ @triggers = (triggers || []).map { |trigger| HttpStub::Server::Stub.create(trigger) }
11
+ end
12
+
13
+ def add_to(registry, request)
14
+ @triggers.each { |trigger| registry.add(trigger, request) }
15
+ end
16
+
17
+ def to_s
18
+ @triggers.reduce("") { |result, trigger| "#{result}\n#{trigger}" }
19
+ end
20
+
21
+ end
22
+
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,23 @@
1
+ module HttpStub
2
+ module Server
3
+ module Stub
4
+
5
+ class Uri
6
+
7
+ def initialize(uri)
8
+ @uri = HttpStub::Server::Stub::StringValueMatcher.new(uri)
9
+ end
10
+
11
+ def match?(request)
12
+ @uri.match?(request.path_info)
13
+ end
14
+
15
+ def to_s
16
+ @uri.to_s
17
+ end
18
+
19
+ end
20
+
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,16 @@
1
+ %table{ class: :scenario }
2
+ %tr
3
+ %td Name:
4
+ %td
5
+ %a{ href: scenario.uri, class: :scenario }= scenario.name
6
+ %tr
7
+ %td Stubs:
8
+ %td
9
+ - scenario.stubs.each do |the_stub|
10
+ = partial :stub, locals: { the_stub: the_stub }
11
+ %tr
12
+ %td Triggered Scenario's:
13
+ %td
14
+ - scenario.triggered_scenarios.each do |triggered_scenario_name, triggered_scenario_uri|
15
+ %a{ href: triggered_scenario_uri, class: :triggered_scenario }= triggered_scenario_name
16
+ %br
@@ -1,2 +1,6 @@
1
1
  table
2
2
  :border 1px solid black
3
+
4
+ table
5
+ table
6
+ :border 1px solid grey
@@ -0,0 +1,3 @@
1
+ - scenarios.each do |scenario|
2
+ = partial :scenario, locals: { scenario: scenario }
3
+ %br
@@ -1,3 +1,3 @@
1
1
  module HttpStub
2
- VERSION = "0.15.5".freeze
2
+ VERSION = "0.16.0.pre1".freeze
3
3
  end
@@ -0,0 +1,81 @@
1
+ describe "Stub activator acceptance" do
2
+ include_context "configurer integration"
3
+
4
+ context "when a configurer that contains a stub activator is initialised" do
5
+
6
+ let(:configurer) { HttpStub::Examples::ConfigurerWithDeprecatedActivator.new }
7
+
8
+ before(:example) { configurer.class.initialize! }
9
+
10
+ context "and the stub is activated" do
11
+
12
+ context "and the response contains text" do
13
+
14
+ before(:example) { configurer.activate!("an_activator") }
15
+
16
+ context "and the stub request is made" do
17
+
18
+ let(:response) { HTTParty.get("#{server_uri}/stub_path") }
19
+
20
+ it "replays the stubbed response" do
21
+ expect(response.code).to eql(200)
22
+ expect(response.body).to eql("Stub activator body")
23
+ end
24
+
25
+ end
26
+
27
+ end
28
+
29
+ end
30
+
31
+ context "and a stub is not activated" do
32
+
33
+ context "and the stub request is made" do
34
+
35
+ let(:response) { HTTParty.get("#{server_uri}/stub_path") }
36
+
37
+ it "responds with a 404 status code" do
38
+ expect(response.code).to eql(404)
39
+ end
40
+
41
+ end
42
+
43
+ end
44
+
45
+ context "and initialization activates activators" do
46
+
47
+ let(:configurer) { HttpStub::Examples::ConfigurerWithComplexInitializer.new }
48
+
49
+ it "registers the activated activator" do
50
+ response = HTTParty.get("#{server_uri}/activated_during_initialization_stub_path")
51
+
52
+ expect(response.code).to eql(200)
53
+ expect(response.body).to eql("Activated during initialization body")
54
+ end
55
+
56
+ context "and another stub is registered" do
57
+
58
+ before(:example) do
59
+ configurer.stub_response!("/another_stub", method: :get, response: { body: "Another stub body" })
60
+ end
61
+
62
+ context "and the servers remembered stubs are recalled" do
63
+
64
+ before(:example) { configurer.recall_stubs! }
65
+
66
+ it "retains the activated activator during initialization" do
67
+ response = HTTParty.get("#{server_uri}/activated_during_initialization_stub_path")
68
+
69
+ expect(response.code).to eql(200)
70
+ expect(response.body).to eql("Activated during initialization body")
71
+ end
72
+
73
+ end
74
+
75
+ end
76
+
77
+ end
78
+
79
+ end
80
+
81
+ end