http_stub 0.16.0.pre1 → 0.17.0.pre1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. checksums.yaml +13 -5
  2. data/lib/http_stub.rb +10 -5
  3. data/lib/http_stub/configurer.rb +1 -21
  4. data/lib/http_stub/configurer/dsl/{sanctioned.rb → server.rb} +7 -1
  5. data/lib/http_stub/configurer/dsl/stub_builder.rb +4 -0
  6. data/lib/http_stub/configurer/request/stub.rb +1 -0
  7. data/lib/http_stub/configurer/server/command_processor.rb +3 -3
  8. data/lib/http_stub/rake/server_tasks.rb +1 -1
  9. data/lib/http_stub/server/daemon.rb +3 -1
  10. data/lib/http_stub/server/stub/instance.rb +3 -2
  11. data/lib/http_stub/server/stub/json_request_body.rb +35 -0
  12. data/lib/http_stub/server/stub/request_body.rb +44 -0
  13. data/lib/http_stub/server/stub/simple_request_body.rb +23 -0
  14. data/lib/http_stub/server/stub/truthy_request_matcher.rb +23 -0
  15. data/lib/http_stub/server/views/_stub.haml +3 -0
  16. data/lib/http_stub/version.rb +1 -1
  17. data/spec/acceptance/configurer_initialization_spec.rb +1 -1
  18. data/spec/acceptance/stub_body_schema_validation_spec.rb +67 -0
  19. data/spec/acceptance/stub_control_values_spec.rb +2 -2
  20. data/spec/acceptance/stub_spec.rb +1 -1
  21. data/spec/lib/http_stub/configurer/dsl/{sanctioned_spec.rb → server_spec.rb} +34 -15
  22. data/spec/lib/http_stub/configurer/dsl/stub_builder_spec.rb +18 -0
  23. data/spec/lib/http_stub/configurer/request/stub_spec.rb +52 -20
  24. data/spec/lib/http_stub/configurer/server/command_processor_integration_spec.rb +7 -4
  25. data/spec/lib/http_stub/rake/server_tasks_smoke_spec.rb +54 -13
  26. data/spec/lib/http_stub/server/application_integration_spec.rb +22 -2
  27. data/spec/lib/http_stub/server/daemon_integration_spec.rb +1 -1
  28. data/spec/lib/http_stub/server/daemon_spec.rb +50 -2
  29. data/spec/lib/http_stub/server/stub/instance_spec.rb +44 -10
  30. data/spec/lib/http_stub/server/stub/json_request_body_spec.rb +102 -0
  31. data/spec/lib/http_stub/server/stub/request_body_spec.rb +120 -0
  32. data/spec/lib/http_stub/server/stub/simple_request_body_spec.rb +43 -0
  33. data/spec/lib/http_stub/server/stub/string_value_matcher_spec.rb +2 -2
  34. data/spec/lib/http_stub/server/stub/truthy_request_matcher_spec.rb +23 -0
  35. data/spec/lib/http_stub/server/stub/uri_spec.rb +1 -1
  36. data/spec/spec_helper.rb +3 -2
  37. data/spec/support/configurer_integration.rb +2 -2
  38. data/spec/support/stub_fixture.rb +2 -1
  39. metadata +307 -281
  40. data/spec/lib/http_stub/configurer_spec.rb +0 -22
@@ -21,6 +21,7 @@ describe HttpStub::Server::Stub::Instance do
21
21
  "method" => "post",
22
22
  "headers" => { "triggered_header" => "triggered_header_value" },
23
23
  "parameters" => { "triggered_parameter" => "triggered_parameter_value" },
24
+ "body" => { "schema" => { "json" => "trigger schema definition" } },
24
25
  "response" => {
25
26
  "status" => 203,
26
27
  "body" => "Triggered body"
@@ -33,6 +34,7 @@ describe HttpStub::Server::Stub::Instance do
33
34
  "method" => stub_method,
34
35
  "headers" => request_header_payload,
35
36
  "parameters" => request_parameter_payload,
37
+ "body" => { "schema" => { "json" => "stub schema definition" } },
36
38
  "response" => {
37
39
  "status" => 201,
38
40
  "body" => "Some body"
@@ -44,6 +46,7 @@ describe HttpStub::Server::Stub::Instance do
44
46
  let(:uri) { instance_double(HttpStub::Server::Stub::Uri, match?: true) }
45
47
  let(:request_headers) { instance_double(HttpStub::Server::Stub::RequestHeaders, match?: true) }
46
48
  let(:request_parameters) { instance_double(HttpStub::Server::Stub::RequestParameters, match?: true) }
49
+ let(:request_body) { double("HttpStub::Server::Stub::SomeRequestBody", match?: true) }
47
50
  let(:response) { instance_double(HttpStub::Server::Stub::Response::Base) }
48
51
  let(:triggers) { instance_double(HttpStub::Server::Stub::Triggers) }
49
52
 
@@ -54,6 +57,7 @@ describe HttpStub::Server::Stub::Instance do
54
57
  allow(HttpStub::Server::Stub::Uri).to receive(:new).and_return(uri)
55
58
  allow(HttpStub::Server::Stub::RequestHeaders).to receive(:new).and_return(request_headers)
56
59
  allow(HttpStub::Server::Stub::RequestParameters).to receive(:new).and_return(request_parameters)
60
+ allow(HttpStub::Server::Stub::RequestBody).to receive(:create).and_return(request_body)
57
61
  allow(HttpStub::Server::Stub::Response).to receive(:create).and_return(response)
58
62
  allow(HttpStub::Server::Stub::Triggers).to receive(:new).and_return(triggers)
59
63
  end
@@ -64,6 +68,8 @@ describe HttpStub::Server::Stub::Instance do
64
68
  let(:request_uri) { "/a_request_uri" }
65
69
  let(:request) { instance_double(Rack::Request, :request_method => request_method_payload) }
66
70
 
71
+ subject { the_stub.satisfies?(request) }
72
+
67
73
  describe "when the request uri matches" do
68
74
 
69
75
  before(:example) { allow(uri).to receive(:match?).with(request).and_return(true) }
@@ -82,8 +88,18 @@ describe HttpStub::Server::Stub::Instance do
82
88
 
83
89
  before(:example) { allow(request_parameters).to receive(:match?).with(request).and_return(true) }
84
90
 
85
- it "returns true" do
86
- expect(the_stub.satisfies?(request)).to be_truthy
91
+ describe "and a body match is configured" do
92
+
93
+ describe "that matches" do
94
+
95
+ before(:example) { allow(request_body).to receive(:match?).with(request).and_return(true) }
96
+
97
+ it "returns true" do
98
+ expect(subject).to be(true)
99
+ end
100
+
101
+ end
102
+
87
103
  end
88
104
 
89
105
  end
@@ -98,22 +114,22 @@ describe HttpStub::Server::Stub::Instance do
98
114
 
99
115
  end
100
116
 
101
- describe "when the request method does not match" do
117
+ describe "when the request uri does not match" do
102
118
 
103
- before(:example) { allow(stub_method).to receive(:match?).with(request).and_return(false) }
119
+ before(:example) { allow(uri).to receive(:match?).with(request).and_return(false) }
104
120
 
105
121
  it "returns false" do
106
- expect(the_stub.satisfies?(request)).to be(false)
122
+ expect(subject).to be(false)
107
123
  end
108
124
 
109
125
  end
110
126
 
111
- describe "when the request uri does not match" do
127
+ describe "when the request method does not match" do
112
128
 
113
- before(:example) { allow(uri).to receive(:match?).with(request).and_return(false) }
129
+ before(:example) { allow(stub_method).to receive(:match?).with(request).and_return(false) }
114
130
 
115
131
  it "returns false" do
116
- expect(the_stub.satisfies?(request)).to be(false)
132
+ expect(subject).to be(false)
117
133
  end
118
134
 
119
135
  end
@@ -123,7 +139,7 @@ describe HttpStub::Server::Stub::Instance do
123
139
  before(:example) { allow(request_headers).to receive(:match?).with(request).and_return(false) }
124
140
 
125
141
  it "returns false" do
126
- expect(the_stub.satisfies?(request)).to be(false)
142
+ expect(subject).to be(false)
127
143
  end
128
144
 
129
145
  end
@@ -133,7 +149,17 @@ describe HttpStub::Server::Stub::Instance do
133
149
  before(:example) { allow(request_parameters).to receive(:match?).with(request).and_return(false) }
134
150
 
135
151
  it "returns false" do
136
- expect(the_stub.satisfies?(request)).to be(false)
152
+ expect(subject).to be(false)
153
+ end
154
+
155
+ end
156
+
157
+ describe "when the bodies do not match" do
158
+
159
+ before(:example) { allow(request_body).to receive(:match?).with(request).and_return(false) }
160
+
161
+ it "returns false" do
162
+ expect(subject).to be(false)
137
163
  end
138
164
 
139
165
  end
@@ -172,6 +198,14 @@ describe HttpStub::Server::Stub::Instance do
172
198
 
173
199
  end
174
200
 
201
+ describe "#body" do
202
+
203
+ it "returns the body model encapsulating the body provided in the request body" do
204
+ expect(the_stub.body).to eql(request_body)
205
+ end
206
+
207
+ end
208
+
175
209
  describe "#response" do
176
210
 
177
211
  it "exposes the response model encapsulating the response provided in the request body" do
@@ -0,0 +1,102 @@
1
+ describe HttpStub::Server::Stub::JsonRequestBody do
2
+
3
+ let(:stubbed_schema_definition) { { "type" => "object", "properties" => { "some_property" => "some_type" } } }
4
+
5
+ let(:json_request_body) { HttpStub::Server::Stub::JsonRequestBody.new(stubbed_schema_definition) }
6
+
7
+ describe "#match?" do
8
+
9
+ let(:logger) { double("Logger").as_null_object }
10
+ let(:raw_request_body) { { "some_json_property" => "some_json_value" }.to_json }
11
+ let(:request_body) { double("RackRequestBody", read: raw_request_body) }
12
+ let(:request) { instance_double(Rack::Request, logger: logger, body: request_body) }
13
+
14
+ let(:validation_errors) { [] }
15
+
16
+ subject { json_request_body.match?(request) }
17
+
18
+ before(:example) { allow(JSON::Validator).to receive(:fully_validate).and_return(validation_errors) }
19
+
20
+ it "validates the request body using the stubbed schema definition" do
21
+ expect(JSON::Validator).to receive(:fully_validate).with(stubbed_schema_definition, raw_request_body, anything)
22
+
23
+ subject
24
+ end
25
+
26
+ it "validates against raw JSON" do
27
+ expect(JSON::Validator).to receive(:fully_validate).with(anything, anything, hash_including(json: true))
28
+
29
+ subject
30
+ end
31
+
32
+ it "ensures the schema definition is valid" do
33
+ expect(JSON::Validator).to(
34
+ receive(:fully_validate).with(anything, anything, hash_including(validate_schema: true))
35
+ )
36
+
37
+ subject
38
+ end
39
+
40
+ context "when body is valid" do
41
+
42
+ let(:validation_errors) { [] }
43
+
44
+ it "returns true" do
45
+ expect(subject).to be(true)
46
+ end
47
+
48
+ it "logs nothing" do
49
+ expect(logger).not_to receive(:info)
50
+
51
+ subject
52
+ end
53
+
54
+ end
55
+
56
+ context "when the body is invalid" do
57
+
58
+ let(:validation_errors) { (1..3).map { |i| "The property '#/#{i}' failed validation"} }
59
+
60
+ it "returns false" do
61
+ expect(subject).to be(false)
62
+ end
63
+
64
+ it "logs each validation error" do
65
+ validation_errors.each { |validation_error| expect(logger).to receive(:info).with(validation_error) }
66
+
67
+ subject
68
+ end
69
+
70
+ end
71
+
72
+ context "when an error occurs validating the body" do
73
+
74
+ let(:error) { JSON::ParserError.new }
75
+
76
+ before(:example) { allow(JSON::Validator).to receive(:fully_validate).and_raise(JSON::ParserError.new) }
77
+
78
+ it "returns false" do
79
+ expect(subject).to be(false)
80
+ end
81
+
82
+ it "logs the error" do
83
+ expect(logger).to receive(:info).with(error.message)
84
+
85
+ subject
86
+ end
87
+
88
+ end
89
+
90
+ end
91
+
92
+ describe "#to_s" do
93
+
94
+ subject { json_request_body.to_s }
95
+
96
+ it "returns the JSON representation of the schema definition" do
97
+ expect(subject).to eql(stubbed_schema_definition.to_json)
98
+ end
99
+
100
+ end
101
+
102
+ end
@@ -0,0 +1,120 @@
1
+ describe HttpStub::Server::Stub::RequestBody do
2
+
3
+ describe "::create" do
4
+
5
+ subject { HttpStub::Server::Stub::RequestBody.create(stubbed_body) }
6
+
7
+ shared_context "a raw request body that causes a simple request body to be created" do
8
+
9
+ it "creates a simple request body with the body" do
10
+ expect(HttpStub::Server::Stub::SimpleRequestBody).to receive(:new).with(stubbed_body)
11
+
12
+ subject
13
+ end
14
+
15
+ it "returns the created request body" do
16
+ simple_request_body = instance_double(HttpStub::Server::Stub::SimpleRequestBody)
17
+ allow(HttpStub::Server::Stub::SimpleRequestBody).to receive(:new).and_return(simple_request_body)
18
+
19
+ expect(subject).to eql(simple_request_body)
20
+ end
21
+
22
+ end
23
+
24
+ context "when the stubbed body contains a schema" do
25
+
26
+ let(:stubbed_body) { { "schema" => raw_schema } }
27
+
28
+ context "that is JSON" do
29
+
30
+ let(:json_schema) { "some json schema" }
31
+ let(:raw_schema) { { "type" => "json", "definition" => json_schema } }
32
+
33
+ it "creates a JSON request body with the schema" do
34
+ expect(HttpStub::Server::Stub::JsonRequestBody).to receive(:new).with(json_schema)
35
+
36
+ subject
37
+ end
38
+
39
+ it "returns the created request body" do
40
+ json_request_body = instance_double(HttpStub::Server::Stub::JsonRequestBody)
41
+ allow(HttpStub::Server::Stub::JsonRequestBody).to receive(:new).and_return(json_request_body)
42
+
43
+ expect(subject).to eql(json_request_body)
44
+ end
45
+
46
+ end
47
+
48
+ context "that is not JSON" do
49
+
50
+ let(:raw_schema) { { "type" => "xml", "definition" => "some xml schema" } }
51
+
52
+ it "raises an error indicating the body is invalid" do
53
+ expect { subject }.to(
54
+ raise_error("Stub request body schema #{raw_schema} is invalid: xml schema is not supported")
55
+ )
56
+ end
57
+
58
+ end
59
+
60
+ context "that does not have a type" do
61
+
62
+ let(:raw_schema) { { "definition" => "some defintion" } }
63
+
64
+ it "raises an error indicating the body is invalid" do
65
+ expect { subject }.to raise_error("Stub request body schema #{raw_schema} is invalid: type expected")
66
+ end
67
+
68
+ end
69
+
70
+ context "that does not have a definition" do
71
+
72
+ let(:raw_schema) { { "type" => "some type" } }
73
+
74
+ it "raises an error indicating the body is invalid" do
75
+ expect { subject }.to raise_error("Stub request body schema #{raw_schema} is invalid: definition expected")
76
+ end
77
+
78
+ end
79
+
80
+ end
81
+
82
+ context "when the stubbed body contains a string" do
83
+
84
+ let(:stubbed_body) { "some string" }
85
+
86
+ it_behaves_like "a raw request body that causes a simple request body to be created"
87
+
88
+ end
89
+
90
+ context "when the stubbed body contains a regular expression control value" do
91
+
92
+ let(:stubbed_body) { "regexp:some regex" }
93
+
94
+ it_behaves_like "a raw request body that causes a simple request body to be created"
95
+
96
+ end
97
+
98
+ context "when the stubbed body is empty" do
99
+
100
+ let(:stubbed_body) { "" }
101
+
102
+ it "returns a truthy request matcher" do
103
+ expect(subject).to eql(HttpStub::Server::Stub::TruthyRequestMatcher)
104
+ end
105
+
106
+ end
107
+
108
+ context "when the stubbed body is nil" do
109
+
110
+ let(:stubbed_body) { nil }
111
+
112
+ it "returns a truthy request matcher" do
113
+ expect(subject).to eql(HttpStub::Server::Stub::TruthyRequestMatcher)
114
+ end
115
+
116
+ end
117
+
118
+ end
119
+
120
+ end
@@ -0,0 +1,43 @@
1
+ describe HttpStub::Server::Stub::SimpleRequestBody do
2
+
3
+ let(:raw_body) { "some body" }
4
+ let(:value_matcher) { instance_double(HttpStub::Server::Stub::StringValueMatcher).as_null_object }
5
+
6
+ let(:simple_request_body) { HttpStub::Server::Stub::SimpleRequestBody.new(raw_body) }
7
+
8
+ before(:example) { allow(HttpStub::Server::Stub::StringValueMatcher).to receive(:new).and_return(value_matcher) }
9
+
10
+ describe "constructor" do
11
+
12
+ it "creates a value matcher for the provided body" do
13
+ expect(HttpStub::Server::Stub::StringValueMatcher).to receive(:new).with(raw_body)
14
+
15
+ simple_request_body
16
+ end
17
+
18
+ end
19
+
20
+ describe "#match?" do
21
+
22
+ let(:request_body) { "some request body" }
23
+ let(:request) { instance_double(Rack::Request, body: double("Rack::Body", read: request_body)) }
24
+
25
+ it "delegates to the value matcher to match the request body" do
26
+ expect(value_matcher).to receive(:match?).with(request_body).and_return(true)
27
+
28
+ expect(simple_request_body.match?(request)).to be(true)
29
+ end
30
+
31
+ end
32
+
33
+ describe "#to_s" do
34
+
35
+ it "delegates to the value matcher representation of the provided body" do
36
+ expect(value_matcher).to receive(:to_s).and_return("some value matcher string")
37
+
38
+ expect(simple_request_body.to_s).to eql("some value matcher string")
39
+ end
40
+
41
+ end
42
+
43
+ end
@@ -65,7 +65,7 @@ describe HttpStub::Server::Stub::StringValueMatcher do
65
65
  let(:stub_value) { "some stub value" }
66
66
  let(:expected_stub_match_value) { "some stub value" }
67
67
 
68
- it_should_behave_like "a StringValueMatcher that matches an expected stub value"
68
+ it_behaves_like "a StringValueMatcher that matches an expected stub value"
69
69
 
70
70
  end
71
71
 
@@ -74,7 +74,7 @@ describe HttpStub::Server::Stub::StringValueMatcher do
74
74
  let(:stub_value) { 88 }
75
75
  let(:expected_stub_match_value) { "88" }
76
76
 
77
- it_should_behave_like "a StringValueMatcher that matches an expected stub value"
77
+ it_behaves_like "a StringValueMatcher that matches an expected stub value"
78
78
 
79
79
  end
80
80
 
@@ -0,0 +1,23 @@
1
+ describe HttpStub::Server::Stub::TruthyRequestMatcher do
2
+
3
+ let(:truthy_request_matcher) { HttpStub::Server::Stub::TruthyRequestMatcher }
4
+
5
+ describe "::match?" do
6
+
7
+ let(:request) { instance_double(Rack::Request) }
8
+
9
+ it "returns true" do
10
+ expect(truthy_request_matcher.match?(request)).to be(true)
11
+ end
12
+
13
+ end
14
+
15
+ describe "::to_s" do
16
+
17
+ it "returns an empty string" do
18
+ expect(truthy_request_matcher.to_s).to eql("")
19
+ end
20
+
21
+ end
22
+
23
+ end
@@ -25,7 +25,7 @@ describe HttpStub::Server::Stub::Uri do
25
25
  it "delegates to the value matcher representation of the provided uri" do
26
26
  expect(value_matcher).to receive(:match?).with(request_uri).and_return(true)
27
27
 
28
- expect(uri.match?(request)).to be_truthy
28
+ expect(uri.match?(request)).to be(true)
29
29
  end
30
30
 
31
31
  end