http_stub 0.14.2 → 0.15.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (118) hide show
  1. checksums.yaml +4 -4
  2. data/lib/http_stub/configurer/request/http/basic.rb +21 -0
  3. data/lib/http_stub/configurer/request/http/factory.rb +43 -0
  4. data/lib/http_stub/configurer/request/http/multipart.rb +33 -0
  5. data/lib/http_stub/configurer/request/stub.rb +24 -8
  6. data/lib/http_stub/configurer/request/stub_activator.rb +12 -8
  7. data/lib/http_stub/configurer/request/{stub_activator_payload_builder.rb → stub_activator_builder.rb} +4 -4
  8. data/lib/http_stub/configurer/request/stub_builder.rb +34 -0
  9. data/lib/http_stub/configurer/request/stub_response.rb +40 -0
  10. data/lib/http_stub/configurer/request/stub_response_file.rb +20 -0
  11. data/lib/http_stub/configurer/server/command.rb +5 -1
  12. data/lib/http_stub/configurer/server/command_processor.rb +1 -1
  13. data/lib/http_stub/configurer/server/dsl.rb +4 -4
  14. data/lib/http_stub/configurer/server/facade.rb +25 -11
  15. data/lib/http_stub/extensions/core/hash.rb +26 -0
  16. data/lib/http_stub/rake/server_daemon_tasks.rb +1 -1
  17. data/lib/http_stub/rake/server_tasks.rb +1 -1
  18. data/lib/http_stub/rake/task_generators.rb +3 -3
  19. data/lib/http_stub/server/application.rb +117 -0
  20. data/lib/http_stub/server/daemon.rb +39 -0
  21. data/lib/http_stub/{models → server}/exact_value_matcher.rb +1 -1
  22. data/lib/http_stub/{models → server}/hash_with_string_value_matchers.rb +2 -2
  23. data/lib/http_stub/{models → server}/headers.rb +1 -1
  24. data/lib/http_stub/{models → server}/omitted_value_matcher.rb +1 -1
  25. data/lib/http_stub/{models → server}/regexp_value_matcher.rb +1 -1
  26. data/lib/http_stub/{models → server}/registry.rb +10 -3
  27. data/lib/http_stub/{models → server}/request_header_parser.rb +1 -1
  28. data/lib/http_stub/server/response.rb +13 -0
  29. data/lib/http_stub/server/response_pipeline.rb +18 -0
  30. data/lib/http_stub/{models → server}/string_value_matcher.rb +4 -4
  31. data/lib/http_stub/server/stub.rb +37 -0
  32. data/lib/http_stub/{models → server}/stub_activator.rb +10 -2
  33. data/lib/http_stub/{controllers → server}/stub_activator_controller.rb +5 -5
  34. data/lib/http_stub/{controllers → server}/stub_controller.rb +4 -4
  35. data/lib/http_stub/server/stub_factory.rb +30 -0
  36. data/lib/http_stub/{models → server}/stub_headers.rb +4 -4
  37. data/lib/http_stub/{models → server}/stub_parameters.rb +2 -2
  38. data/lib/http_stub/{models → server}/stub_registry.rb +4 -4
  39. data/lib/http_stub/server/stub_response/base.rb +62 -0
  40. data/lib/http_stub/server/stub_response/file.rb +48 -0
  41. data/lib/http_stub/server/stub_response/text.rb +17 -0
  42. data/lib/http_stub/server/stub_response.rb +14 -0
  43. data/lib/http_stub/{models → server}/stub_triggers.rb +6 -2
  44. data/lib/http_stub/{models → server}/stub_uri.rb +2 -2
  45. data/lib/http_stub/{views → server/views}/_stub.haml +0 -0
  46. data/lib/http_stub/{views → server/views}/application.sass +0 -0
  47. data/lib/http_stub/{views → server/views}/layout.haml +0 -0
  48. data/lib/http_stub/{views → server/views}/stub_activators.haml +0 -0
  49. data/lib/http_stub/{views → server/views}/stubs.haml +0 -0
  50. data/lib/http_stub/version.rb +1 -1
  51. data/lib/http_stub.rb +41 -24
  52. data/spec/lib/http_stub/configurer/deprecated_dsl_spec.rb +2 -2
  53. data/spec/lib/http_stub/configurer/request/http/basic_spec.rb +15 -0
  54. data/spec/lib/http_stub/configurer/request/http/factory_spec.rb +148 -0
  55. data/spec/lib/http_stub/configurer/request/http/multipart_spec.rb +74 -0
  56. data/spec/lib/http_stub/configurer/request/stub_activator_builder_spec.rb +119 -0
  57. data/spec/lib/http_stub/configurer/request/stub_activator_spec.rb +24 -22
  58. data/spec/lib/http_stub/configurer/request/stub_builder_spec.rb +154 -0
  59. data/spec/lib/http_stub/configurer/request/stub_response_spec.rb +136 -0
  60. data/spec/lib/http_stub/configurer/request/stub_spec.rb +238 -17
  61. data/spec/lib/http_stub/configurer/server/command_processor_integration_spec.rb +7 -3
  62. data/spec/lib/http_stub/configurer/server/command_spec.rb +7 -6
  63. data/spec/lib/http_stub/configurer/server/dsl_spec.rb +58 -94
  64. data/spec/lib/http_stub/configurer/server/facade_spec.rb +56 -41
  65. data/spec/lib/http_stub/configurer_integration_spec.rb +180 -81
  66. data/spec/lib/http_stub/{hash_extensions_spec.rb → extensions/core/hash_spec.rb} +1 -1
  67. data/spec/lib/http_stub/rake/server_daemon_tasks_smoke_spec.rb +1 -1
  68. data/spec/lib/http_stub/{server_integration_spec.rb → server/application_integration_spec.rb} +1 -1
  69. data/spec/lib/http_stub/server/application_spec.rb +205 -0
  70. data/spec/lib/http_stub/server/daemon_integration_spec.rb +7 -0
  71. data/spec/lib/http_stub/{server_daemon_spec.rb → server/daemon_spec.rb} +4 -4
  72. data/spec/lib/http_stub/{models → server}/exact_value_matcher_spec.rb +2 -2
  73. data/spec/lib/http_stub/{models → server}/hash_with_string_value_matchers_spec.rb +5 -5
  74. data/spec/lib/http_stub/{models → server}/headers_spec.rb +2 -2
  75. data/spec/lib/http_stub/{models → server}/omitted_value_matcher_spec.rb +2 -2
  76. data/spec/lib/http_stub/{models → server}/regexp_value_matcher_spec.rb +2 -2
  77. data/spec/lib/http_stub/{models → server}/registry_spec.rb +52 -55
  78. data/spec/lib/http_stub/{models → server}/request_header_parser_spec.rb +4 -4
  79. data/spec/lib/http_stub/server/response_pipeline_spec.rb +29 -0
  80. data/spec/lib/http_stub/server/response_spec.rb +41 -0
  81. data/spec/lib/http_stub/{models → server}/string_value_matcher_spec.rb +11 -11
  82. data/spec/lib/http_stub/server/stub_activator_controller_spec.rb +85 -0
  83. data/spec/lib/http_stub/server/stub_activator_spec.rb +124 -0
  84. data/spec/lib/http_stub/server/stub_controller_spec.rb +74 -0
  85. data/spec/lib/http_stub/server/stub_factory_spec.rb +117 -0
  86. data/spec/lib/http_stub/{models → server}/stub_headers_spec.rb +9 -9
  87. data/spec/lib/http_stub/{models → server}/stub_parameters_spec.rb +6 -6
  88. data/spec/lib/http_stub/{models → server}/stub_registry_integration_spec.rb +3 -3
  89. data/spec/lib/http_stub/{models → server}/stub_registry_spec.rb +15 -35
  90. data/spec/lib/http_stub/server/stub_response/base_spec.rb +225 -0
  91. data/spec/lib/http_stub/server/stub_response/file_spec.rb +140 -0
  92. data/spec/lib/http_stub/server/stub_response/text_spec.rb +103 -0
  93. data/spec/lib/http_stub/server/stub_response_spec.rb +49 -0
  94. data/spec/lib/http_stub/{models → server}/stub_spec.rb +34 -18
  95. data/spec/lib/http_stub/{models → server}/stub_triggers_spec.rb +26 -16
  96. data/spec/lib/http_stub/{models → server}/stub_uri_spec.rb +5 -5
  97. data/spec/resources/sample.pdf +0 -0
  98. data/spec/resources/sample.txt +1 -0
  99. data/spec/spec_helper.rb +13 -4
  100. data/spec/support/stub_fixture.rb +106 -0
  101. metadata +148 -102
  102. data/lib/http_stub/configurer/request/stub_payload_builder.rb +0 -46
  103. data/lib/http_stub/hash_extensions.rb +0 -22
  104. data/lib/http_stub/models/request_pipeline.rb +0 -13
  105. data/lib/http_stub/models/response.rb +0 -43
  106. data/lib/http_stub/models/stub.rb +0 -35
  107. data/lib/http_stub/server.rb +0 -114
  108. data/lib/http_stub/server_daemon.rb +0 -36
  109. data/spec/lib/http_stub/configurer/request/stub_activator_payload_builder_spec.rb +0 -103
  110. data/spec/lib/http_stub/configurer/request/stub_payload_builder_spec.rb +0 -304
  111. data/spec/lib/http_stub/controllers/stub_activator_controller_spec.rb +0 -94
  112. data/spec/lib/http_stub/controllers/stub_controller_spec.rb +0 -83
  113. data/spec/lib/http_stub/models/request_pipeline_spec.rb +0 -17
  114. data/spec/lib/http_stub/models/response_spec.rb +0 -206
  115. data/spec/lib/http_stub/models/stub_activator_spec.rb +0 -76
  116. data/spec/lib/http_stub/server_daemon_integration_spec.rb +0 -7
  117. data/spec/lib/http_stub/server_spec.rb +0 -234
  118. data/spec/support/http_stub/configurer/request/stub_payload_builder_shared_context.rb +0 -27
@@ -1,46 +0,0 @@
1
- module HttpStub
2
- module Configurer
3
- module Request
4
-
5
- class StubPayloadBuilder
6
-
7
- def initialize(response_defaults)
8
- @args = { response: response_defaults ? response_defaults.clone : {}, triggers: [] }
9
- end
10
-
11
- def match_requests(uri, args={})
12
- @uri = uri
13
- @args.merge!(args)
14
- end
15
-
16
- def respond_with(args)
17
- @args[:response].deep_merge!(args)
18
- end
19
-
20
- def trigger(stub_builder_or_builders)
21
- resolved_builders =
22
- stub_builder_or_builders.is_a?(Array) ? stub_builder_or_builders : [ stub_builder_or_builders ]
23
- @args[:triggers].concat(resolved_builders)
24
- end
25
-
26
- def build
27
- {
28
- uri: HttpStub::Configurer::Request::ControllableValue.format(@uri),
29
- method: @args[:method],
30
- headers: HttpStub::Configurer::Request::ControllableValue.format(@args[:headers] || {}),
31
- parameters: HttpStub::Configurer::Request::ControllableValue.format(@args[:parameters] || {}),
32
- response: {
33
- status: @args[:response][:status] || "",
34
- headers: @args[:response][:headers] || {},
35
- body: @args[:response][:body],
36
- delay_in_seconds: @args[:response][:delay_in_seconds] || ""
37
- },
38
- triggers: @args[:triggers].map(&:build)
39
- }
40
- end
41
-
42
- end
43
-
44
- end
45
- end
46
- end
@@ -1,22 +0,0 @@
1
- module HttpStub
2
-
3
- module HashExtensions
4
-
5
- def downcase_and_underscore_keys
6
- self.reduce({}) do |result, element|
7
- result[element[0].is_a?(::String) ? element[0].downcase.gsub(/-/, '_') : element[0]] = element[1]
8
- result
9
- end
10
- end
11
-
12
- def has_hash?(other_hash)
13
- other_hash.nil? || other_hash.reduce(true) do |result, element|
14
- result && (self[element[0]] == element[1])
15
- end
16
- end
17
-
18
- end
19
-
20
- end
21
-
22
- ::Hash.send(:include, HttpStub::HashExtensions)
@@ -1,13 +0,0 @@
1
- module HttpStub
2
- module Models
3
-
4
- class RequestPipeline
5
-
6
- def self.before_halt(response)
7
- sleep response.delay_in_seconds
8
- end
9
-
10
- end
11
-
12
- end
13
- end
@@ -1,43 +0,0 @@
1
- module HttpStub
2
- module Models
3
-
4
- class Response
5
-
6
- private
7
-
8
- DEFAULT_ARGS = { "status" => 200, "delay_in_seconds" => 0 }.freeze
9
- DEFAULT_HEADERS = { "content-type" => "application/json" }.freeze
10
-
11
- def merge_default_arguments(args)
12
- args.clone.tap do |result|
13
- headers = result["headers"] ||= {}
14
- DEFAULT_ARGS.each { |key, value| result[key] = value if !result[key] || result[key] == "" }
15
- DEFAULT_HEADERS.each { |key, value| headers[key] = value if !headers[key] || headers[key] == "" }
16
- end
17
- end
18
-
19
- public
20
-
21
- attr_reader :status, :body, :delay_in_seconds, :headers
22
-
23
- def initialize(args={})
24
- @original_args = args
25
- resolved_args = merge_default_arguments(args)
26
- @status = resolved_args["status"]
27
- @body = resolved_args["body"]
28
- @delay_in_seconds = resolved_args["delay_in_seconds"]
29
- @headers = HttpStub::Models::Headers.new(resolved_args["headers"])
30
- end
31
-
32
- SUCCESS = HttpStub::Models::Response.new("status" => 200, "body" => "OK")
33
- ERROR = HttpStub::Models::Response.new("status" => 404, "body" => "ERROR")
34
- EMPTY = HttpStub::Models::Response.new()
35
-
36
- def empty?
37
- @original_args.empty?
38
- end
39
-
40
- end
41
-
42
- end
43
- end
@@ -1,35 +0,0 @@
1
- module HttpStub
2
- module Models
3
-
4
- class Stub
5
-
6
- attr_reader :uri, :headers, :parameters, :response, :triggers
7
-
8
- def initialize(args)
9
- @args = args
10
- @uri = HttpStub::Models::StubUri.new(args["uri"])
11
- @headers = HttpStub::Models::StubHeaders.new(args["headers"])
12
- @parameters = HttpStub::Models::StubParameters.new(args["parameters"])
13
- @response = HttpStub::Models::Response.new(args["response"])
14
- @triggers = HttpStub::Models::StubTriggers.new(args["triggers"])
15
- end
16
-
17
- def satisfies?(request)
18
- @uri.match?(request) &&
19
- method.downcase == request.request_method.downcase &&
20
- @headers.match?(request) &&
21
- @parameters.match?(request)
22
- end
23
-
24
- def method
25
- @args["method"]
26
- end
27
-
28
- def to_s
29
- @args.to_s
30
- end
31
-
32
- end
33
-
34
- end
35
- end
@@ -1,114 +0,0 @@
1
- module HttpStub
2
-
3
- class Server < ::Sinatra::Base
4
-
5
- register Sinatra::Partial
6
-
7
- enable :dump_errors, :logging, :partial_underscores
8
-
9
- def initialize
10
- super()
11
- @stub_registry = HttpStub::Models::StubRegistry.new
12
- @stub_activator_registry = HttpStub::Models::Registry.new("stub_activator")
13
- @stub_controller = HttpStub::Controllers::StubController.new(@stub_registry)
14
- @stub_activator_controller =
15
- HttpStub::Controllers::StubActivatorController.new(@stub_activator_registry, @stub_registry)
16
- end
17
-
18
- private
19
-
20
- SUPPORTED_REQUEST_TYPES = [ :get, :post, :put, :delete, :patch, :options ].freeze
21
-
22
- def self.any_request_type(path, opts={}, &block)
23
- SUPPORTED_REQUEST_TYPES.each { |type| self.send(type, path, opts, &block) }
24
- end
25
-
26
- public
27
-
28
- # Sample request body:
29
- # {
30
- # "uri": "/some/path",
31
- # "method": "get",
32
- # "headers": {
33
- # "key": "value",
34
- # ...
35
- # },
36
- # "parameters": {
37
- # "key": "value",
38
- # ...
39
- # },
40
- # "response": {
41
- # "status": "200",
42
- # "body": "Hello World"
43
- # }
44
- # }
45
- post "/stubs" do
46
- response = @stub_controller.register(request)
47
- halt(response.status, response.body)
48
- end
49
-
50
- get "/stubs" do
51
- haml :stubs, {}, stubs: @stub_registry.all
52
- end
53
-
54
- delete "/stubs" do
55
- @stub_controller.clear(request)
56
- halt 200, "OK"
57
- end
58
-
59
- post "/stubs/memory" do
60
- @stub_registry.remember
61
- halt 200, "OK"
62
- end
63
-
64
- get "/stubs/memory" do
65
- @stub_registry.recall
66
- halt 200, "OK"
67
- end
68
-
69
- # Sample request body:
70
- # {
71
- # "activation_uri": "/some/path",
72
- # ... see /stub ...
73
- # }
74
- post "/stubs/activators" do
75
- response = @stub_activator_controller.register(request)
76
- halt(response.status, response.body)
77
- end
78
-
79
- get "/stubs/activators" do
80
- haml :stub_activators, {}, stub_activators: @stub_activator_registry.all.sort_by(&:activation_uri)
81
- end
82
-
83
- delete "/stubs/activators" do
84
- @stub_activator_controller.clear(request)
85
- halt 200, "OK"
86
- end
87
-
88
- get "/application.css" do
89
- sass :application
90
- end
91
-
92
- any_request_type(//) { handle_request }
93
-
94
- helpers do
95
-
96
- def h(text)
97
- Rack::Utils.escape_html(text)
98
- end
99
-
100
- end
101
-
102
- private
103
-
104
- def handle_request
105
- response = @stub_controller.replay(request)
106
- response = @stub_activator_controller.activate(request) if response.empty?
107
- response = HttpStub::Models::Response::ERROR if response.empty?
108
- HttpStub::Models::RequestPipeline.before_halt(response)
109
- halt(response.status, response.headers.to_hash, response.body)
110
- end
111
-
112
- end
113
-
114
- end
@@ -1,36 +0,0 @@
1
- module HttpStub
2
-
3
- class ServerDaemon < HttpServerManager::Server
4
-
5
- class << self
6
-
7
- def pid_dir=(dir)
8
- HttpServerManager.pid_dir = dir
9
- end
10
-
11
- def log_dir=(dir)
12
- HttpServerManager.log_dir = dir
13
- end
14
-
15
- end
16
-
17
- def initialize(args)
18
- super({ host: "localhost" }.merge(args))
19
- @configurer = args[:configurer]
20
- end
21
-
22
- def start!
23
- super
24
- if @configurer
25
- @configurer.initialize!
26
- logger.info "#{@name} initialized"
27
- end
28
- end
29
-
30
- def start_command
31
- "rake #{@name}:start:foreground"
32
- end
33
-
34
- end
35
-
36
- end
@@ -1,103 +0,0 @@
1
- describe HttpStub::Configurer::Request::StubActivatorPayloadBuilder do
2
-
3
- include_context "stub payload builder arguments"
4
-
5
- let(:response_defaults) { {} }
6
- let(:stub_payload) { { "stub_payload_key" => "stub payload value" } }
7
- let(:stub_builder) { instance_double(HttpStub::Configurer::Request::StubPayloadBuilder, build: stub_payload) }
8
-
9
- let(:builder) { HttpStub::Configurer::Request::StubActivatorPayloadBuilder.new(response_defaults) }
10
-
11
- before(:example) do
12
- allow(HttpStub::Configurer::Request::StubPayloadBuilder).to receive(:new).and_return(stub_builder)
13
- end
14
-
15
- describe "constructor" do
16
-
17
- subject { builder }
18
-
19
- it "creates a stub payload builder with the provided response defaults" do
20
- expect(HttpStub::Configurer::Request::StubPayloadBuilder).to receive(:new).with(response_defaults)
21
-
22
- subject
23
- end
24
-
25
- end
26
-
27
- describe "#match_requests" do
28
-
29
- it "delegates to a stub payload builder" do
30
- expect(stub_builder).to receive(:match_requests).with(uri, request_options)
31
-
32
- builder.match_requests(uri, request_options)
33
- end
34
-
35
- end
36
-
37
- describe "#respond_with" do
38
-
39
- it "delegates to a stub payload builder" do
40
- expect(stub_builder).to receive(:respond_with).with(response_options)
41
-
42
- builder.respond_with(response_options)
43
- end
44
-
45
- end
46
-
47
- describe "#trigger" do
48
-
49
- context "when one triggered stub is provided" do
50
-
51
- let(:trigger_builder) { instance_double(HttpStub::Configurer::Request::StubPayloadBuilder) }
52
-
53
- it "delegates to a stub payload builder" do
54
- expect(stub_builder).to receive(:trigger).with(trigger_builder)
55
-
56
- builder.trigger(trigger_builder)
57
- end
58
-
59
- end
60
-
61
- context "when many triggered stubs are provided" do
62
-
63
- let(:trigger_builders) { (1..3).map { instance_double(HttpStub::Configurer::Request::StubPayloadBuilder) } }
64
-
65
- it "delegates to a stub payload builder" do
66
- expect(stub_builder).to receive(:trigger).with(trigger_builders)
67
-
68
- builder.trigger(trigger_builders)
69
- end
70
-
71
- end
72
-
73
- end
74
-
75
- describe "#build" do
76
-
77
- subject { builder.build }
78
-
79
- context "when a path on which the activator is activated is established" do
80
-
81
- let(:activation_uri) { "http://some/activation/uri" }
82
-
83
- before(:example) { builder.on(activation_uri) }
84
-
85
- it "returns a payload that includes the activation uri" do
86
- expect(subject).to include(activation_uri: activation_uri)
87
- end
88
-
89
- it "builds a stub payload" do
90
- expect(stub_builder).to receive(:build)
91
-
92
- subject
93
- end
94
-
95
- it "returns a payload that includes the stub payload" do
96
- expect(subject).to include(stub_payload)
97
- end
98
-
99
- end
100
-
101
- end
102
-
103
- end