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.
- checksums.yaml +4 -4
- data/lib/http_stub/configurer/request/http/basic.rb +21 -0
- data/lib/http_stub/configurer/request/http/factory.rb +43 -0
- data/lib/http_stub/configurer/request/http/multipart.rb +33 -0
- data/lib/http_stub/configurer/request/stub.rb +24 -8
- data/lib/http_stub/configurer/request/stub_activator.rb +12 -8
- data/lib/http_stub/configurer/request/{stub_activator_payload_builder.rb → stub_activator_builder.rb} +4 -4
- data/lib/http_stub/configurer/request/stub_builder.rb +34 -0
- data/lib/http_stub/configurer/request/stub_response.rb +40 -0
- data/lib/http_stub/configurer/request/stub_response_file.rb +20 -0
- data/lib/http_stub/configurer/server/command.rb +5 -1
- data/lib/http_stub/configurer/server/command_processor.rb +1 -1
- data/lib/http_stub/configurer/server/dsl.rb +4 -4
- data/lib/http_stub/configurer/server/facade.rb +25 -11
- data/lib/http_stub/extensions/core/hash.rb +26 -0
- data/lib/http_stub/rake/server_daemon_tasks.rb +1 -1
- data/lib/http_stub/rake/server_tasks.rb +1 -1
- data/lib/http_stub/rake/task_generators.rb +3 -3
- data/lib/http_stub/server/application.rb +117 -0
- data/lib/http_stub/server/daemon.rb +39 -0
- data/lib/http_stub/{models → server}/exact_value_matcher.rb +1 -1
- data/lib/http_stub/{models → server}/hash_with_string_value_matchers.rb +2 -2
- data/lib/http_stub/{models → server}/headers.rb +1 -1
- data/lib/http_stub/{models → server}/omitted_value_matcher.rb +1 -1
- data/lib/http_stub/{models → server}/regexp_value_matcher.rb +1 -1
- data/lib/http_stub/{models → server}/registry.rb +10 -3
- data/lib/http_stub/{models → server}/request_header_parser.rb +1 -1
- data/lib/http_stub/server/response.rb +13 -0
- data/lib/http_stub/server/response_pipeline.rb +18 -0
- data/lib/http_stub/{models → server}/string_value_matcher.rb +4 -4
- data/lib/http_stub/server/stub.rb +37 -0
- data/lib/http_stub/{models → server}/stub_activator.rb +10 -2
- data/lib/http_stub/{controllers → server}/stub_activator_controller.rb +5 -5
- data/lib/http_stub/{controllers → server}/stub_controller.rb +4 -4
- data/lib/http_stub/server/stub_factory.rb +30 -0
- data/lib/http_stub/{models → server}/stub_headers.rb +4 -4
- data/lib/http_stub/{models → server}/stub_parameters.rb +2 -2
- data/lib/http_stub/{models → server}/stub_registry.rb +4 -4
- data/lib/http_stub/server/stub_response/base.rb +62 -0
- data/lib/http_stub/server/stub_response/file.rb +48 -0
- data/lib/http_stub/server/stub_response/text.rb +17 -0
- data/lib/http_stub/server/stub_response.rb +14 -0
- data/lib/http_stub/{models → server}/stub_triggers.rb +6 -2
- data/lib/http_stub/{models → server}/stub_uri.rb +2 -2
- data/lib/http_stub/{views → server/views}/_stub.haml +0 -0
- data/lib/http_stub/{views → server/views}/application.sass +0 -0
- data/lib/http_stub/{views → server/views}/layout.haml +0 -0
- data/lib/http_stub/{views → server/views}/stub_activators.haml +0 -0
- data/lib/http_stub/{views → server/views}/stubs.haml +0 -0
- data/lib/http_stub/version.rb +1 -1
- data/lib/http_stub.rb +41 -24
- data/spec/lib/http_stub/configurer/deprecated_dsl_spec.rb +2 -2
- data/spec/lib/http_stub/configurer/request/http/basic_spec.rb +15 -0
- data/spec/lib/http_stub/configurer/request/http/factory_spec.rb +148 -0
- data/spec/lib/http_stub/configurer/request/http/multipart_spec.rb +74 -0
- data/spec/lib/http_stub/configurer/request/stub_activator_builder_spec.rb +119 -0
- data/spec/lib/http_stub/configurer/request/stub_activator_spec.rb +24 -22
- data/spec/lib/http_stub/configurer/request/stub_builder_spec.rb +154 -0
- data/spec/lib/http_stub/configurer/request/stub_response_spec.rb +136 -0
- data/spec/lib/http_stub/configurer/request/stub_spec.rb +238 -17
- data/spec/lib/http_stub/configurer/server/command_processor_integration_spec.rb +7 -3
- data/spec/lib/http_stub/configurer/server/command_spec.rb +7 -6
- data/spec/lib/http_stub/configurer/server/dsl_spec.rb +58 -94
- data/spec/lib/http_stub/configurer/server/facade_spec.rb +56 -41
- data/spec/lib/http_stub/configurer_integration_spec.rb +180 -81
- data/spec/lib/http_stub/{hash_extensions_spec.rb → extensions/core/hash_spec.rb} +1 -1
- data/spec/lib/http_stub/rake/server_daemon_tasks_smoke_spec.rb +1 -1
- data/spec/lib/http_stub/{server_integration_spec.rb → server/application_integration_spec.rb} +1 -1
- data/spec/lib/http_stub/server/application_spec.rb +205 -0
- data/spec/lib/http_stub/server/daemon_integration_spec.rb +7 -0
- data/spec/lib/http_stub/{server_daemon_spec.rb → server/daemon_spec.rb} +4 -4
- data/spec/lib/http_stub/{models → server}/exact_value_matcher_spec.rb +2 -2
- data/spec/lib/http_stub/{models → server}/hash_with_string_value_matchers_spec.rb +5 -5
- data/spec/lib/http_stub/{models → server}/headers_spec.rb +2 -2
- data/spec/lib/http_stub/{models → server}/omitted_value_matcher_spec.rb +2 -2
- data/spec/lib/http_stub/{models → server}/regexp_value_matcher_spec.rb +2 -2
- data/spec/lib/http_stub/{models → server}/registry_spec.rb +52 -55
- data/spec/lib/http_stub/{models → server}/request_header_parser_spec.rb +4 -4
- data/spec/lib/http_stub/server/response_pipeline_spec.rb +29 -0
- data/spec/lib/http_stub/server/response_spec.rb +41 -0
- data/spec/lib/http_stub/{models → server}/string_value_matcher_spec.rb +11 -11
- data/spec/lib/http_stub/server/stub_activator_controller_spec.rb +85 -0
- data/spec/lib/http_stub/server/stub_activator_spec.rb +124 -0
- data/spec/lib/http_stub/server/stub_controller_spec.rb +74 -0
- data/spec/lib/http_stub/server/stub_factory_spec.rb +117 -0
- data/spec/lib/http_stub/{models → server}/stub_headers_spec.rb +9 -9
- data/spec/lib/http_stub/{models → server}/stub_parameters_spec.rb +6 -6
- data/spec/lib/http_stub/{models → server}/stub_registry_integration_spec.rb +3 -3
- data/spec/lib/http_stub/{models → server}/stub_registry_spec.rb +15 -35
- data/spec/lib/http_stub/server/stub_response/base_spec.rb +225 -0
- data/spec/lib/http_stub/server/stub_response/file_spec.rb +140 -0
- data/spec/lib/http_stub/server/stub_response/text_spec.rb +103 -0
- data/spec/lib/http_stub/server/stub_response_spec.rb +49 -0
- data/spec/lib/http_stub/{models → server}/stub_spec.rb +34 -18
- data/spec/lib/http_stub/{models → server}/stub_triggers_spec.rb +26 -16
- data/spec/lib/http_stub/{models → server}/stub_uri_spec.rb +5 -5
- data/spec/resources/sample.pdf +0 -0
- data/spec/resources/sample.txt +1 -0
- data/spec/spec_helper.rb +13 -4
- data/spec/support/stub_fixture.rb +106 -0
- metadata +148 -102
- data/lib/http_stub/configurer/request/stub_payload_builder.rb +0 -46
- data/lib/http_stub/hash_extensions.rb +0 -22
- data/lib/http_stub/models/request_pipeline.rb +0 -13
- data/lib/http_stub/models/response.rb +0 -43
- data/lib/http_stub/models/stub.rb +0 -35
- data/lib/http_stub/server.rb +0 -114
- data/lib/http_stub/server_daemon.rb +0 -36
- data/spec/lib/http_stub/configurer/request/stub_activator_payload_builder_spec.rb +0 -103
- data/spec/lib/http_stub/configurer/request/stub_payload_builder_spec.rb +0 -304
- data/spec/lib/http_stub/controllers/stub_activator_controller_spec.rb +0 -94
- data/spec/lib/http_stub/controllers/stub_controller_spec.rb +0 -83
- data/spec/lib/http_stub/models/request_pipeline_spec.rb +0 -17
- data/spec/lib/http_stub/models/response_spec.rb +0 -206
- data/spec/lib/http_stub/models/stub_activator_spec.rb +0 -76
- data/spec/lib/http_stub/server_daemon_integration_spec.rb +0 -7
- data/spec/lib/http_stub/server_spec.rb +0 -234
- data/spec/support/http_stub/configurer/request/stub_payload_builder_shared_context.rb +0 -27
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 77eb1d522a9c4187caae1203528f66741295bffe
|
4
|
+
data.tar.gz: f089d9c6e085126c6be87386a77cc1295b142e9c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5c367ce983d21581034478d9accfcffc23d1d150fe49053fd72992fd2a7e8106eae1cb3a7d710111bf9ffad5ec073623785102efca130b0a4b64251e9d5f93f4
|
7
|
+
data.tar.gz: d3f5a65a799a7afebc73feb58f155196fce11acacb827d3706243b836abb5261f75cf04f9cb6156a6deb2462e4d23db80efd90759e638af8f673edab278c6ba2
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module HttpStub
|
2
|
+
module Configurer
|
3
|
+
module Request
|
4
|
+
module Http
|
5
|
+
|
6
|
+
class Basic
|
7
|
+
|
8
|
+
def initialize(http_request)
|
9
|
+
@http_request = http_request
|
10
|
+
end
|
11
|
+
|
12
|
+
def to_http_request
|
13
|
+
@http_request
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
module HttpStub
|
2
|
+
module Configurer
|
3
|
+
module Request
|
4
|
+
module Http
|
5
|
+
|
6
|
+
class Factory
|
7
|
+
|
8
|
+
class << self
|
9
|
+
|
10
|
+
def stub(model)
|
11
|
+
HttpStub::Configurer::Request::Http::Multipart.new("/stubs", model)
|
12
|
+
end
|
13
|
+
|
14
|
+
def stub_activator(model)
|
15
|
+
HttpStub::Configurer::Request::Http::Multipart.new("/stubs/activators", model)
|
16
|
+
end
|
17
|
+
|
18
|
+
def get(path)
|
19
|
+
to_basic_request(Net::HTTP::Get.new(path))
|
20
|
+
end
|
21
|
+
|
22
|
+
def post(path)
|
23
|
+
to_basic_request(Net::HTTP::Post.new(path).tap { |request| request.body = "" })
|
24
|
+
end
|
25
|
+
|
26
|
+
def delete(path)
|
27
|
+
to_basic_request(Net::HTTP::Delete.new(path))
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def to_basic_request(http_request)
|
33
|
+
HttpStub::Configurer::Request::Http::Basic.new(http_request)
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module HttpStub
|
2
|
+
module Configurer
|
3
|
+
module Request
|
4
|
+
module Http
|
5
|
+
|
6
|
+
class Multipart
|
7
|
+
|
8
|
+
def initialize(path, model)
|
9
|
+
@path = path
|
10
|
+
@model = model
|
11
|
+
end
|
12
|
+
|
13
|
+
def to_http_request
|
14
|
+
Net::HTTP::Post::Multipart.new(@path, parameters)
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def parameters
|
20
|
+
{ payload: @model.payload.to_json }.tap do |parameters|
|
21
|
+
@model.response_files.each do |response_file|
|
22
|
+
parameters["response_file_#{response_file.id}"] =
|
23
|
+
UploadIO.new(response_file.path, response_file.type, response_file.name)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -2,17 +2,33 @@ module HttpStub
|
|
2
2
|
module Configurer
|
3
3
|
module Request
|
4
4
|
|
5
|
-
class Stub
|
5
|
+
class Stub
|
6
6
|
|
7
|
-
def initialize(
|
8
|
-
|
9
|
-
@
|
10
|
-
|
11
|
-
|
7
|
+
def initialize(args)
|
8
|
+
@id = SecureRandom.uuid
|
9
|
+
@request = args[:request]
|
10
|
+
@response = HttpStub::Configurer::Request::StubResponse.new(@id, args[:response])
|
11
|
+
@triggers = args[:triggers].map(&:build)
|
12
12
|
end
|
13
13
|
|
14
|
-
def
|
15
|
-
|
14
|
+
def payload
|
15
|
+
{
|
16
|
+
id: @id,
|
17
|
+
uri: HttpStub::Configurer::Request::ControllableValue.format(@request[:uri]),
|
18
|
+
method: @request[:method],
|
19
|
+
headers: HttpStub::Configurer::Request::ControllableValue.format(@request[:headers] || {}),
|
20
|
+
parameters: HttpStub::Configurer::Request::ControllableValue.format(@request[:parameters] || {}),
|
21
|
+
response: @response.payload,
|
22
|
+
triggers: @triggers.map(&:payload)
|
23
|
+
}
|
24
|
+
end
|
25
|
+
|
26
|
+
def response_files
|
27
|
+
([ @response.file ] + @triggers.map(&:response_files)).flatten.compact
|
28
|
+
end
|
29
|
+
|
30
|
+
def to_s
|
31
|
+
@request[:uri]
|
16
32
|
end
|
17
33
|
|
18
34
|
end
|
@@ -2,17 +2,21 @@ module HttpStub
|
|
2
2
|
module Configurer
|
3
3
|
module Request
|
4
4
|
|
5
|
-
class StubActivator
|
5
|
+
class StubActivator
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
7
|
+
delegate :response_files, to: :@stub
|
8
|
+
|
9
|
+
def initialize(args)
|
10
|
+
@activation_uri = args[:activation_uri]
|
11
|
+
@stub = args[:stub]
|
12
|
+
end
|
13
|
+
|
14
|
+
def payload
|
15
|
+
{ activation_uri: @activation_uri }.merge(@stub.payload)
|
12
16
|
end
|
13
17
|
|
14
|
-
def
|
15
|
-
@
|
18
|
+
def to_s
|
19
|
+
@activation_uri
|
16
20
|
end
|
17
21
|
|
18
22
|
end
|
@@ -2,12 +2,12 @@ module HttpStub
|
|
2
2
|
module Configurer
|
3
3
|
module Request
|
4
4
|
|
5
|
-
class
|
5
|
+
class StubActivatorBuilder
|
6
6
|
|
7
|
-
delegate :match_requests, :respond_with, :trigger, to: :@
|
7
|
+
delegate :match_requests, :respond_with, :trigger, to: :@stub_builder
|
8
8
|
|
9
9
|
def initialize(response_defaults)
|
10
|
-
@
|
10
|
+
@stub_builder = HttpStub::Configurer::Request::StubBuilder.new(response_defaults)
|
11
11
|
end
|
12
12
|
|
13
13
|
def on(uri)
|
@@ -15,7 +15,7 @@ module HttpStub
|
|
15
15
|
end
|
16
16
|
|
17
17
|
def build
|
18
|
-
|
18
|
+
HttpStub::Configurer::Request::StubActivator.new(activation_uri: @activation_uri, stub: @stub_builder.build)
|
19
19
|
end
|
20
20
|
|
21
21
|
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module HttpStub
|
2
|
+
module Configurer
|
3
|
+
module Request
|
4
|
+
|
5
|
+
class StubBuilder
|
6
|
+
|
7
|
+
def initialize(response_defaults)
|
8
|
+
@response = response_defaults ? response_defaults.clone : {}
|
9
|
+
@triggers = []
|
10
|
+
end
|
11
|
+
|
12
|
+
def match_requests(uri, args={})
|
13
|
+
@request = { uri: uri }.merge(args)
|
14
|
+
end
|
15
|
+
|
16
|
+
def respond_with(args)
|
17
|
+
@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
|
+
@triggers.concat(resolved_builders)
|
24
|
+
end
|
25
|
+
|
26
|
+
def build
|
27
|
+
HttpStub::Configurer::Request::Stub.new(request: @request, response: @response, triggers: @triggers)
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module HttpStub
|
2
|
+
module Configurer
|
3
|
+
module Request
|
4
|
+
|
5
|
+
class StubResponse
|
6
|
+
|
7
|
+
def initialize(id, args)
|
8
|
+
@id = id
|
9
|
+
@args = args
|
10
|
+
end
|
11
|
+
|
12
|
+
def payload
|
13
|
+
{
|
14
|
+
status: @args[:status] || "",
|
15
|
+
headers: @args[:headers] || {},
|
16
|
+
body: @args[:body],
|
17
|
+
delay_in_seconds: @args[:delay_in_seconds] || ""
|
18
|
+
}
|
19
|
+
end
|
20
|
+
|
21
|
+
def file
|
22
|
+
if contains_file?
|
23
|
+
args = { id: @id, type: @args[:headers]["content-type"] }.merge(@args[:body][:file])
|
24
|
+
HttpStub::Configurer::Request::StubResponseFile.new(args)
|
25
|
+
else
|
26
|
+
nil
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def contains_file?
|
33
|
+
@args[:body].is_a?(Hash) && @args[:body].key?(:file)
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module HttpStub
|
2
|
+
module Configurer
|
3
|
+
module Request
|
4
|
+
|
5
|
+
class StubResponseFile
|
6
|
+
|
7
|
+
attr_reader :id, :path, :name, :type
|
8
|
+
|
9
|
+
def initialize(args)
|
10
|
+
@id = args[:id]
|
11
|
+
@path = args[:path]
|
12
|
+
@name = args[:name]
|
13
|
+
@type = args[:type]
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -4,13 +4,17 @@ module HttpStub
|
|
4
4
|
|
5
5
|
class Command
|
6
6
|
|
7
|
-
attr_reader :
|
7
|
+
attr_reader :description
|
8
8
|
|
9
9
|
def initialize(args)
|
10
10
|
@request = args[:request]
|
11
11
|
@description = args[:description]
|
12
12
|
end
|
13
13
|
|
14
|
+
def http_request
|
15
|
+
@request.to_http_request
|
16
|
+
end
|
17
|
+
|
14
18
|
end
|
15
19
|
|
16
20
|
end
|
@@ -10,7 +10,7 @@ module HttpStub
|
|
10
10
|
|
11
11
|
def process(command)
|
12
12
|
begin
|
13
|
-
response = Net::HTTP.start(host, port) { |http| http.request(command.
|
13
|
+
response = Net::HTTP.start(host, port) { |http| http.request(command.http_request) }
|
14
14
|
raise "#{error_message_prefix(command)}: #{response.code} #{response.message}" unless response.code == "200"
|
15
15
|
rescue Exception => exc
|
16
16
|
raise "#{error_message_prefix(command)}: #{exc}"
|
@@ -18,14 +18,14 @@ module HttpStub
|
|
18
18
|
end
|
19
19
|
|
20
20
|
def build_stub(&block)
|
21
|
-
builder = HttpStub::Configurer::Request::
|
21
|
+
builder = HttpStub::Configurer::Request::StubBuilder.new(@response_defaults)
|
22
22
|
block.call(builder) if block_given?
|
23
23
|
builder
|
24
24
|
end
|
25
25
|
|
26
26
|
def add_stub!(builder=nil, &block)
|
27
27
|
resolved_builder = builder || self.build_stub(&block)
|
28
|
-
@server_facade.stub_response(
|
28
|
+
@server_facade.stub_response(resolved_builder.build)
|
29
29
|
end
|
30
30
|
|
31
31
|
def add_stubs!(builders)
|
@@ -33,9 +33,9 @@ module HttpStub
|
|
33
33
|
end
|
34
34
|
|
35
35
|
def add_activator!(&block)
|
36
|
-
builder = HttpStub::Configurer::Request::
|
36
|
+
builder = HttpStub::Configurer::Request::StubActivatorBuilder.new(@response_defaults)
|
37
37
|
block.call(builder)
|
38
|
-
@server_facade.stub_activator(
|
38
|
+
@server_facade.stub_activator(builder.build)
|
39
39
|
end
|
40
40
|
|
41
41
|
def activate!(uri)
|
@@ -8,37 +8,51 @@ module HttpStub
|
|
8
8
|
@request_processor = HttpStub::Configurer::Server::RequestProcessor.new(configurer)
|
9
9
|
end
|
10
10
|
|
11
|
-
def stub_response(
|
12
|
-
@request_processor.submit(
|
11
|
+
def stub_response(model)
|
12
|
+
@request_processor.submit(
|
13
|
+
request: HttpStub::Configurer::Request::Http::Factory.stub(model),
|
14
|
+
description: "stubbing '#{model}'"
|
15
|
+
)
|
13
16
|
end
|
14
17
|
|
15
|
-
def stub_activator(
|
16
|
-
@request_processor.submit(
|
18
|
+
def stub_activator(model)
|
19
|
+
@request_processor.submit(
|
20
|
+
request: HttpStub::Configurer::Request::Http::Factory.stub_activator(model),
|
21
|
+
description: "registering activator '#{model}'"
|
22
|
+
)
|
17
23
|
end
|
18
24
|
|
19
25
|
def activate(uri)
|
20
|
-
@request_processor.submit(
|
26
|
+
@request_processor.submit(
|
27
|
+
request: HttpStub::Configurer::Request::Http::Factory.get(uri),
|
28
|
+
description: "activating '#{uri}'"
|
29
|
+
)
|
21
30
|
end
|
22
31
|
|
23
32
|
def remember_stubs
|
24
|
-
|
25
|
-
|
26
|
-
|
33
|
+
@request_processor.submit(
|
34
|
+
request: HttpStub::Configurer::Request::Http::Factory.post("/stubs/memory"),
|
35
|
+
description: "committing stubs to memory"
|
36
|
+
)
|
27
37
|
end
|
28
38
|
|
29
39
|
def recall_stubs
|
30
40
|
@request_processor.submit(
|
31
|
-
request:
|
41
|
+
request: HttpStub::Configurer::Request::Http::Factory.get("/stubs/memory"),
|
42
|
+
description: "recalling stubs in memory"
|
32
43
|
)
|
33
44
|
end
|
34
45
|
|
35
46
|
def clear_stubs
|
36
|
-
@request_processor.submit(
|
47
|
+
@request_processor.submit(
|
48
|
+
request: HttpStub::Configurer::Request::Http::Factory.delete("/stubs"),
|
49
|
+
description: "clearing stubs")
|
37
50
|
end
|
38
51
|
|
39
52
|
def clear_activators
|
40
53
|
@request_processor.submit(
|
41
|
-
request:
|
54
|
+
request: HttpStub::Configurer::Request::Http::Factory.delete("/stubs/activators"),
|
55
|
+
description: "clearing activators"
|
42
56
|
)
|
43
57
|
end
|
44
58
|
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module HttpStub
|
2
|
+
module Extensions
|
3
|
+
module Core
|
4
|
+
|
5
|
+
module Hash
|
6
|
+
|
7
|
+
def downcase_and_underscore_keys
|
8
|
+
self.reduce({}) do |result, element|
|
9
|
+
result[element[0].is_a?(::String) ? element[0].downcase.gsub(/-/, '_') : element[0]] = element[1]
|
10
|
+
result
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def has_hash?(other_hash)
|
15
|
+
other_hash.nil? || other_hash.reduce(true) do |result, element|
|
16
|
+
result && (self[element[0]] == element[1])
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
::Hash.send(:include, HttpStub::Extensions::Core::Hash)
|
@@ -5,7 +5,7 @@ module HttpStub
|
|
5
5
|
|
6
6
|
def initialize(args)
|
7
7
|
HttpStub::Rake::ServerTasks.new(args)
|
8
|
-
namespace(args[:name]) { HttpServerManager::Rake::ServerTasks.new(HttpStub::
|
8
|
+
namespace(args[:name]) { HttpServerManager::Rake::ServerTasks.new(HttpStub::Server::Daemon.new(args)) }
|
9
9
|
end
|
10
10
|
|
11
11
|
end
|
@@ -16,7 +16,7 @@ module HttpStub
|
|
16
16
|
namespace :start do
|
17
17
|
desc "Starts stub #{args[:name]} in the foreground"
|
18
18
|
task(:foreground) do
|
19
|
-
HttpStub::Server.instance_eval do
|
19
|
+
HttpStub::Server::Application.instance_eval do
|
20
20
|
set :environment, :test
|
21
21
|
set :port, args[:port]
|
22
22
|
run!
|
@@ -1,7 +1,7 @@
|
|
1
|
-
require File.expand_path('../../../http_stub', __FILE__)
|
1
|
+
require ::File.expand_path('../../../http_stub', __FILE__)
|
2
2
|
require 'rake/tasklib' unless defined? (::Rake::TaskLib)
|
3
3
|
|
4
4
|
require 'http_server_manager/rake/task_generators'
|
5
5
|
|
6
|
-
require File.expand_path('../server_tasks', __FILE__)
|
7
|
-
require File.expand_path('../server_daemon_tasks', __FILE__)
|
6
|
+
require ::File.expand_path('../server_tasks', __FILE__)
|
7
|
+
require ::File.expand_path('../server_daemon_tasks', __FILE__)
|
@@ -0,0 +1,117 @@
|
|
1
|
+
module HttpStub
|
2
|
+
module Server
|
3
|
+
|
4
|
+
class Application < Sinatra::Base
|
5
|
+
|
6
|
+
register Sinatra::Partial
|
7
|
+
|
8
|
+
enable :dump_errors, :logging, :partial_underscores
|
9
|
+
|
10
|
+
def initialize
|
11
|
+
super()
|
12
|
+
@stub_registry = HttpStub::Server::StubRegistry.new
|
13
|
+
@stub_activator_registry = HttpStub::Server::Registry.new("stub_activator")
|
14
|
+
@stub_controller = HttpStub::Server::StubController.new(@stub_registry)
|
15
|
+
@stub_activator_controller =
|
16
|
+
HttpStub::Server::StubActivatorController.new(@stub_activator_registry, @stub_registry)
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
SUPPORTED_REQUEST_TYPES = [ :get, :post, :put, :delete, :patch, :options ].freeze
|
22
|
+
|
23
|
+
def self.any_request_type(path, opts={}, &block)
|
24
|
+
SUPPORTED_REQUEST_TYPES.each { |type| self.send(type, path, opts, &block) }
|
25
|
+
end
|
26
|
+
|
27
|
+
before { @response_pipeline = HttpStub::Server::ResponsePipeline.new(self) }
|
28
|
+
|
29
|
+
public
|
30
|
+
|
31
|
+
# Sample request body:
|
32
|
+
# {
|
33
|
+
# "uri": "/some/path",
|
34
|
+
# "method": "get",
|
35
|
+
# "headers": {
|
36
|
+
# "key": "value",
|
37
|
+
# ...
|
38
|
+
# },
|
39
|
+
# "parameters": {
|
40
|
+
# "key": "value",
|
41
|
+
# ...
|
42
|
+
# },
|
43
|
+
# "response": {
|
44
|
+
# "status": "200",
|
45
|
+
# "body": "Hello World"
|
46
|
+
# }
|
47
|
+
# }
|
48
|
+
post "/stubs" do
|
49
|
+
response = @stub_controller.register(request)
|
50
|
+
@response_pipeline.process(response)
|
51
|
+
end
|
52
|
+
|
53
|
+
get "/stubs" do
|
54
|
+
haml :stubs, {}, stubs: @stub_registry.all
|
55
|
+
end
|
56
|
+
|
57
|
+
delete "/stubs" do
|
58
|
+
@stub_controller.clear(request)
|
59
|
+
halt 200, "OK"
|
60
|
+
end
|
61
|
+
|
62
|
+
post "/stubs/memory" do
|
63
|
+
@stub_registry.remember
|
64
|
+
halt 200, "OK"
|
65
|
+
end
|
66
|
+
|
67
|
+
get "/stubs/memory" do
|
68
|
+
@stub_registry.recall
|
69
|
+
halt 200, "OK"
|
70
|
+
end
|
71
|
+
|
72
|
+
# Sample request body:
|
73
|
+
# {
|
74
|
+
# "activation_uri": "/some/path",
|
75
|
+
# ... see /stub ...
|
76
|
+
# }
|
77
|
+
post "/stubs/activators" do
|
78
|
+
response = @stub_activator_controller.register(request)
|
79
|
+
@response_pipeline.process(response)
|
80
|
+
end
|
81
|
+
|
82
|
+
get "/stubs/activators" do
|
83
|
+
haml :stub_activators, {}, stub_activators: @stub_activator_registry.all.sort_by(&:activation_uri)
|
84
|
+
end
|
85
|
+
|
86
|
+
delete "/stubs/activators" do
|
87
|
+
@stub_activator_controller.clear(request)
|
88
|
+
halt 200, "OK"
|
89
|
+
end
|
90
|
+
|
91
|
+
get "/application.css" do
|
92
|
+
sass :application
|
93
|
+
end
|
94
|
+
|
95
|
+
any_request_type(//) { handle_request }
|
96
|
+
|
97
|
+
helpers do
|
98
|
+
|
99
|
+
def h(text)
|
100
|
+
Rack::Utils.escape_html(text)
|
101
|
+
end
|
102
|
+
|
103
|
+
end
|
104
|
+
|
105
|
+
private
|
106
|
+
|
107
|
+
def handle_request
|
108
|
+
response = @stub_controller.replay(request)
|
109
|
+
response = @stub_activator_controller.activate(request) if response.empty?
|
110
|
+
response = HttpStub::Server::Response::ERROR if response.empty?
|
111
|
+
@response_pipeline.process(response)
|
112
|
+
end
|
113
|
+
|
114
|
+
end
|
115
|
+
|
116
|
+
end
|
117
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module HttpStub
|
2
|
+
module Server
|
3
|
+
|
4
|
+
class Daemon < HttpServerManager::Server
|
5
|
+
|
6
|
+
class << self
|
7
|
+
|
8
|
+
def pid_dir=(dir)
|
9
|
+
HttpServerManager.pid_dir = dir
|
10
|
+
end
|
11
|
+
|
12
|
+
def log_dir=(dir)
|
13
|
+
HttpServerManager.log_dir = dir
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
def initialize(args)
|
19
|
+
super({ host: "localhost" }.merge(args))
|
20
|
+
@configurer = args[:configurer]
|
21
|
+
end
|
22
|
+
|
23
|
+
def start!
|
24
|
+
super
|
25
|
+
if @configurer
|
26
|
+
@configurer.initialize!
|
27
|
+
logger.info "#{@name} initialized"
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def start_command
|
32
|
+
"rake #{@name}:start:foreground"
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
@@ -1,10 +1,10 @@
|
|
1
1
|
module HttpStub
|
2
|
-
module
|
2
|
+
module Server
|
3
3
|
|
4
4
|
class HashWithStringValueMatchers < Hash
|
5
5
|
|
6
6
|
def initialize(stub_hash)
|
7
|
-
stub_hash.each_pair { |key, value| self[key] = HttpStub::
|
7
|
+
stub_hash.each_pair { |key, value| self[key] = HttpStub::Server::StringValueMatcher.new(value) }
|
8
8
|
end
|
9
9
|
|
10
10
|
def match?(actual_hash)
|