http_stub 0.1.4 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,74 @@
1
+ describe HttpStub::Controllers::StubController do
2
+
3
+ let(:request_body) { "Some request body" }
4
+ let(:request) { double("HttpRequest", body: double("RequestBody", read: request_body)) }
5
+ let(:response) { double(HttpStub::Response) }
6
+ let(:the_stub) { double(HttpStub::Models::Stub, response: response) }
7
+ let(:registry) { double(HttpStub::Models::Registry).as_null_object }
8
+ let(:controller) { HttpStub::Controllers::StubController.new(registry) }
9
+
10
+ describe "#register" do
11
+
12
+ before(:each) do
13
+ HttpStub::Models::Stub.stub!(:new).and_return(the_stub)
14
+ end
15
+
16
+ it "should create a stub from the request body" do
17
+ HttpStub::Models::Stub.should_receive(:new).with(request_body).and_return(the_stub)
18
+
19
+ controller.register(request)
20
+ end
21
+
22
+ it "should add the stub to the stub registry" do
23
+ registry.should_receive(:add).with(the_stub, request)
24
+
25
+ controller.register(request)
26
+ end
27
+
28
+ it "should return a success response" do
29
+ controller.register(request).should eql(HttpStub::Response::SUCCESS)
30
+ end
31
+
32
+ end
33
+
34
+ describe "#replay" do
35
+
36
+ describe "when a stub has been registered that should be replayed for the request" do
37
+
38
+ before(:each) do
39
+ registry.stub!(:find_for).with(request).and_return(the_stub)
40
+ end
41
+
42
+ it "should return the stubs response" do
43
+ the_stub.should_receive(:response).and_return(response)
44
+
45
+ controller.replay(request).should eql(response)
46
+ end
47
+
48
+ end
49
+
50
+ describe "when no stub should be replayed for the request" do
51
+
52
+ before(:each) do
53
+ registry.stub!(:find_for).with(request).and_return(nil)
54
+ end
55
+
56
+ it "should return an empty response" do
57
+ controller.replay(request).should eql(HttpStub::Response::EMPTY)
58
+ end
59
+
60
+ end
61
+
62
+ end
63
+
64
+ describe "#clear" do
65
+
66
+ it "should clear the stub registry" do
67
+ registry.should_receive(:clear).with(request)
68
+
69
+ controller.clear(request)
70
+ end
71
+
72
+ end
73
+
74
+ end
@@ -0,0 +1,58 @@
1
+ describe HttpStub::Models::Alias do
2
+
3
+ let(:alias_uri) { "/some/alias/uri" }
4
+ let(:alias_body) do
5
+ { "alias_uri" => alias_uri }.to_json
6
+ end
7
+ let(:the_alias) { HttpStub::Models::Alias.new(alias_body) }
8
+
9
+ before(:each) { HttpStub::Models::Stub.stub!(:new).and_return(double(HttpStub::Models::Stub)) }
10
+
11
+ describe "#satisfies?" do
12
+
13
+ let(:request) { double("HttpRequest", path_info: request_path_info) }
14
+
15
+ describe "when the request uri exactly matches the aliases activation uri" do
16
+
17
+ let(:request_path_info) { alias_uri }
18
+
19
+ it "should return true" do
20
+ the_alias.satisfies?(request).should be_true
21
+ end
22
+
23
+ end
24
+
25
+ describe "when the alias activation uri is a substring of the request uri" do
26
+
27
+ let(:request_path_info) { "#{alias_uri}/with/additional/paths" }
28
+
29
+ it "should return false" do
30
+ the_alias.satisfies?(request).should be_false
31
+ end
32
+
33
+ end
34
+
35
+ describe "when the request uri is completely different to the aliases activation uri" do
36
+
37
+ let(:request_path_info) { "/completely/different/path" }
38
+
39
+ it "should return false" do
40
+ the_alias.satisfies?(request).should be_false
41
+ end
42
+
43
+ end
44
+
45
+ end
46
+
47
+ describe "#the_stub" do
48
+
49
+ it "should return a HttpStub::Models::Stub constructed from the request body" do
50
+ stub = double(HttpStub::Models::Stub)
51
+ HttpStub::Models::Stub.should_receive(:new).with(alias_body).and_return(stub)
52
+
53
+ the_alias.the_stub.should eql(stub)
54
+ end
55
+
56
+ end
57
+
58
+ end
@@ -0,0 +1,101 @@
1
+ describe HttpStub::Models::Registry do
2
+
3
+ let(:registry) { HttpStub::Models::Registry.new("a_model") }
4
+
5
+ let(:logger) { double("Logger").as_null_object }
6
+ let(:request) { double("HttpRequest", logger: logger, inspect: "Request inspect result") }
7
+
8
+ describe "#add" do
9
+
10
+ it "should log that the model has been registered" do
11
+ model = double("HttpStub::Models::Model", to_s: "Model as String")
12
+ logger.should_receive(:info).with(/Model as String/)
13
+
14
+ registry.add(model, request)
15
+ end
16
+
17
+ end
18
+
19
+ describe "#find_for" do
20
+
21
+ describe "when multiple models have been registered" do
22
+
23
+ let(:models) do
24
+ (1..3).map { |i| double("HttpStub::Models::Model#{i}", :satisfies? => false) }
25
+ end
26
+
27
+ before(:each) do
28
+ models.each { |model| registry.add(model, request) }
29
+ end
30
+
31
+ describe "and one registered model satisfies the request" do
32
+
33
+ let(:matching_model) { models[1] }
34
+
35
+ before(:each) { matching_model.stub!(:satisfies?).and_return(true) }
36
+
37
+ it "should return the model" do
38
+ registry.find_for(request).should eql(matching_model)
39
+ end
40
+
41
+ describe "and the registry is subsequently cleared" do
42
+
43
+ before(:each) { registry.clear(request) }
44
+
45
+ it "should return nil" do
46
+ registry.find_for(request).should be_nil
47
+ end
48
+
49
+ end
50
+
51
+ end
52
+
53
+ describe "and multiple registered models satisfy the request" do
54
+
55
+ before(:each) do
56
+ [0, 2].each { |i| models[i].stub!(:satisfies?).and_return(true) }
57
+ end
58
+
59
+ it "should support model overrides by returning the last model registered" do
60
+ registry.find_for(request).should eql(models[2])
61
+ end
62
+
63
+ end
64
+
65
+ describe "and no registered models match the request" do
66
+
67
+ it "should return nil" do
68
+ registry.find_for(request).should be_nil
69
+ end
70
+
71
+ end
72
+
73
+ end
74
+
75
+ describe "when no model has been registered" do
76
+
77
+ it "should return nil" do
78
+ registry.find_for(request).should be_nil
79
+ end
80
+
81
+ end
82
+
83
+ it "it should log model discovery diagnostics that includes the complete details of the request" do
84
+ logger.should_receive(:info).with(/Request inspect result/)
85
+
86
+ registry.find_for(request)
87
+ end
88
+
89
+ end
90
+
91
+ describe "#clear" do
92
+
93
+ it "should log that the models are being cleared" do
94
+ logger.should_receive(:info).with(/clearing a_model/i)
95
+
96
+ registry.clear(request)
97
+ end
98
+
99
+ end
100
+
101
+ end
@@ -1,4 +1,4 @@
1
- describe HttpStub::Stub do
1
+ describe HttpStub::Models::Stub do
2
2
 
3
3
  let(:stub_uri) { "/a_path" }
4
4
  let(:stub_method) { "get" }
@@ -14,10 +14,9 @@ describe HttpStub::Stub do
14
14
  }
15
15
  }.to_json
16
16
  end
17
- let(:stub_request) { double("HttpRequest", :body => double("HttpRequestBody", :read => stub_body)) }
18
- let(:stub_instance) { HttpStub::Stub.new(stub_request) }
17
+ let(:the_stub) { HttpStub::Models::Stub.new(stub_body) }
19
18
 
20
- describe "#stubs?" do
19
+ describe "#satisfies?" do
21
20
 
22
21
  let(:request_uri) { stub_uri }
23
22
  let(:request_method) { stub_method }
@@ -43,7 +42,7 @@ describe HttpStub::Stub do
43
42
  describe "and the request parameters are identical" do
44
43
 
45
44
  it "should return true" do
46
- stub_instance.stubs?(request).should be_true
45
+ the_stub.satisfies?(request).should be_true
47
46
  end
48
47
 
49
48
  end
@@ -53,7 +52,7 @@ describe HttpStub::Stub do
53
52
  let(:request_parameters) { stub_parameters.merge("param4" => "value4") }
54
53
 
55
54
  it "should return true" do
56
- stub_instance.stubs?(request).should be_true
55
+ the_stub.satisfies?(request).should be_true
57
56
  end
58
57
 
59
58
  end
@@ -69,7 +68,7 @@ describe HttpStub::Stub do
69
68
  end
70
69
 
71
70
  it "should return false" do
72
- stub_instance.stubs?(request).should be_false
71
+ the_stub.satisfies?(request).should be_false
73
72
  end
74
73
 
75
74
  end
@@ -84,7 +83,7 @@ describe HttpStub::Stub do
84
83
  end
85
84
 
86
85
  it "should be false" do
87
- stub_instance.stubs?(request).should be_false
86
+ the_stub.satisfies?(request).should be_false
88
87
  end
89
88
 
90
89
  end
@@ -100,7 +99,7 @@ describe HttpStub::Stub do
100
99
  end
101
100
 
102
101
  it "should be true" do
103
- stub_instance.stubs?(request).should be_true
102
+ the_stub.satisfies?(request).should be_true
104
103
  end
105
104
 
106
105
  end
@@ -116,7 +115,7 @@ describe HttpStub::Stub do
116
115
  let(:request_uri) { "/a_different_path" }
117
116
 
118
117
  it "should return false" do
119
- stub_instance.stubs?(request).should be_false
118
+ the_stub.satisfies?(request).should be_false
120
119
  end
121
120
 
122
121
  end
@@ -126,7 +125,7 @@ describe HttpStub::Stub do
126
125
  let(:request_method) { "post" }
127
126
 
128
127
  it "should return false" do
129
- stub_instance.stubs?(request).should be_false
128
+ the_stub.satisfies?(request).should be_false
130
129
  end
131
130
 
132
131
  end
@@ -136,11 +135,11 @@ describe HttpStub::Stub do
136
135
  describe "#response" do
137
136
 
138
137
  it "should expose the provided response status" do
139
- stub_instance.response.status.should eql(201)
138
+ the_stub.response.status.should eql(201)
140
139
  end
141
140
 
142
141
  it "should expose the provided response body" do
143
- stub_instance.response.body.should eql("Foo")
142
+ the_stub.response.body.should eql("Foo")
144
143
  end
145
144
 
146
145
  end
@@ -156,26 +155,26 @@ describe HttpStub::Stub do
156
155
  end
157
156
 
158
157
  it "should return a string containing the stubbed uri" do
159
- stub_instance.to_s.should match(/\/a_path/)
158
+ the_stub.to_s.should match(/\/a_path/)
160
159
  end
161
160
 
162
161
  it "should return a string containing the stubbed request method" do
163
- stub_instance.to_s.should match(/get/)
162
+ the_stub.to_s.should match(/get/)
164
163
  end
165
164
 
166
165
  it "should return a string containing the stubbed parameters" do
167
166
  stub_parameters.each_pair do |key, value|
168
- stub_instance.to_s.should match(/#{Regexp.escape(key)}/)
169
- stub_instance.to_s.should match(/#{Regexp.escape(value)}/)
167
+ the_stub.to_s.should match(/#{Regexp.escape(key)}/)
168
+ the_stub.to_s.should match(/#{Regexp.escape(value)}/)
170
169
  end
171
170
  end
172
171
 
173
172
  it "should return a string containing the intended response code" do
174
- stub_instance.to_s.should match(/201/)
173
+ the_stub.to_s.should match(/201/)
175
174
  end
176
175
 
177
176
  it "should return a string containing the intended response body" do
178
- stub_instance.to_s.should match(/Foo/)
177
+ the_stub.to_s.should match(/Foo/)
179
178
  end
180
179
 
181
180
  end
@@ -0,0 +1,73 @@
1
+ describe HttpStub::Response do
2
+
3
+ describe "::SUCCESS" do
4
+
5
+ let(:response) { HttpStub::Response::SUCCESS }
6
+
7
+ it "should have a status of 200" do
8
+ response.status.should eql(200)
9
+ end
10
+
11
+ it "should have an empty body" do
12
+ response.body.should eql("")
13
+ end
14
+
15
+ end
16
+
17
+ describe "::ERROR" do
18
+
19
+ let(:response) { HttpStub::Response::ERROR }
20
+
21
+ it "should have a status of 404" do
22
+ response.status.should eql(404)
23
+ end
24
+
25
+ it "should have an empty body" do
26
+ response.body.should eql("")
27
+ end
28
+
29
+ end
30
+
31
+ describe "::EMPTY" do
32
+
33
+ let(:response) { HttpStub::Response::EMPTY }
34
+
35
+ it "should have a nil status" do
36
+ response.status.should be_nil
37
+ end
38
+
39
+ it "should have a nil body" do
40
+ response.body.should be_nil
41
+ end
42
+
43
+ end
44
+
45
+ describe "#empty?" do
46
+
47
+ describe "when the response is EMPTY" do
48
+
49
+ it "should return true" do
50
+ HttpStub::Response::EMPTY.should be_empty
51
+ end
52
+
53
+ end
54
+
55
+ describe "when the response is not EMPTY but contains no values" do
56
+
57
+ it "should return true" do
58
+ HttpStub::Response.new.should be_empty
59
+ end
60
+
61
+ end
62
+
63
+ describe "when the response is not EMPTY" do
64
+
65
+ it "should return false" do
66
+ HttpStub::Response::SUCCESS.should_not be_empty
67
+ end
68
+
69
+ end
70
+
71
+ end
72
+
73
+ end
@@ -1,90 +1,163 @@
1
1
  describe HttpStub::Server do
2
2
  include Rack::Test::Methods
3
3
 
4
+ let(:response) { last_response }
5
+ let(:response_body) { response.body.to_s }
6
+
7
+ let(:stub_registry) { double(HttpStub::Models::Registry).as_null_object }
8
+ let(:alias_registry) { double(HttpStub::Models::Registry).as_null_object }
9
+
10
+ let(:stub_controller) { double(HttpStub::Controllers::StubController).as_null_object }
11
+ let(:alias_controller) { double(HttpStub::Controllers::AliasController).as_null_object }
12
+
4
13
  let(:app) { HttpStub::Server.new }
5
14
 
6
- let(:registry) { double(HttpStub::Registry).as_null_object }
7
- before(:each) { HttpStub::Registry.stub!(:new).and_return(registry) }
15
+ before(:each) do
16
+ HttpStub::Models::Registry.stub!(:new).with("stub").and_return(stub_registry)
17
+ HttpStub::Models::Registry.stub!(:new).with("alias").and_return(alias_registry)
18
+ HttpStub::Controllers::StubController.stub!(:new).and_return(stub_controller)
19
+ HttpStub::Controllers::AliasController.stub!(:new).and_return(alias_controller)
20
+ end
8
21
 
9
- let(:response) { last_response }
10
- let(:response_body) { response.body.to_s }
22
+ describe "when a stub insertion is received" do
23
+
24
+ it "should register the insertion via the stub controller" do
25
+ stub_controller.should_receive(:register).and_return(HttpStub::Response::SUCCESS)
11
26
 
12
- describe "when a stub request is received" do
27
+ issue_stub_request
28
+ end
13
29
 
14
- it "should register a stub encapsulating the request" do
15
- stub = double(HttpStub::Stub)
16
- HttpStub::Stub.should_receive(:new).and_return(stub)
17
- registry.should_receive(:add).with(stub, anything)
30
+ it "should respond with the response provided by the controller" do
31
+ stub_controller.stub!(:register).and_return(HttpStub::Response.new(status: 202, body: ""))
18
32
 
19
33
  issue_stub_request
34
+
35
+ response.status.should eql(202)
36
+ end
37
+
38
+ def issue_stub_request
39
+ post "/stubs", {
40
+ "uri" => "/a_path",
41
+ "method" => "a method",
42
+ "response" => {
43
+ "status" => 200,
44
+ "body" => "Foo"
45
+ }
46
+ }.to_json
47
+ end
48
+
49
+ end
50
+
51
+ describe "when a stub alias insertion request is received" do
52
+
53
+ it "should register the insertion via the alias controller" do
54
+ alias_controller.should_receive(:register).and_return(HttpStub::Response::SUCCESS)
55
+
56
+ issue_stub_alias_request
57
+ end
58
+
59
+ it "should respond with the response provided by the controller" do
60
+ alias_controller.stub!(:register).and_return(HttpStub::Response.new(status: 302, body: ""))
61
+
62
+ issue_stub_alias_request
63
+
64
+ response.status.should eql(302)
65
+ end
66
+
67
+ def issue_stub_alias_request
68
+ post "/stubs/aliases", {
69
+ "alias_uri" => "/an_alias_path",
70
+ "uri" => "/a_path",
71
+ "method" => "a method",
72
+ "response" => {
73
+ "status" => 200,
74
+ "body" => "Foo"
75
+ }
76
+ }.to_json
77
+ end
78
+
79
+ end
80
+
81
+ describe "when a request to clear the server has been received" do
82
+
83
+ it "should delegate clearing to the stub controller" do
84
+ stub_controller.should_receive(:clear)
85
+
86
+ delete "/stubs"
87
+ end
88
+
89
+ it "should respond with a 200 status code" do
90
+ delete "/stubs"
91
+
92
+ response.status.should eql(200)
20
93
  end
21
94
 
22
95
  end
23
96
 
24
- describe "when a replay request is received" do
97
+ describe "when another type of request is received" do
25
98
 
26
- describe "and the request has been stubbed" do
99
+ describe "and the stub controller replays a response" do
27
100
 
28
101
  before(:each) do
29
- registry.stub!(:find_for).and_return(
30
- double(HttpStub::Stub, response: double("StubResponse", status: 500, body: "Some text")))
102
+ stub_controller.stub!(:replay).and_return(HttpStub::Response.new(status: 222, body: "Some body"))
31
103
  end
32
104
 
33
- it "should respond with the configured status" do
105
+ it "should respond with the replay status code" do
34
106
  get "/a_path"
35
107
 
36
- response.status.should eql(500)
108
+ response.status.should eql(222)
37
109
  end
38
110
 
39
- it "should respond with the configured body" do
111
+ it "should respond with the replay body" do
40
112
  get "/a_path"
41
113
 
42
- response_body.should eql("Some text")
114
+ response_body.should eql("Some body")
43
115
  end
44
116
 
45
117
  end
46
118
 
47
- describe "and the request has not been stubbed" do
119
+ describe "and the stub controller does not replay a response" do
48
120
 
49
121
  before(:each) do
50
- registry.stub!(:find_for).and_return(nil)
122
+ stub_controller.stub!(:replay).and_return(HttpStub::Response::EMPTY)
51
123
  end
52
124
 
53
- it "should respond with a 404 status code" do
54
- get "/a_path"
125
+ describe "but the alias controller activates a stub" do
55
126
 
56
- response.status.should eql(404)
57
- end
127
+ before(:each) do
128
+ alias_controller.stub!(:activate).and_return(HttpStub::Response.new(status: 300, body: "A body"))
129
+ end
58
130
 
59
- end
131
+ it "should respond with the activation response status code" do
132
+ get "/a_path"
60
133
 
61
- end
134
+ response.status.should eql(300)
135
+ end
62
136
 
63
- describe "when a request to clear the stub has been received" do
137
+ it "should respond with the activation response body" do
138
+ get "/a_path"
64
139
 
65
- it "should clear the contents of the registry" do
66
- registry.should_receive(:clear)
140
+ response_body.should eql("A body")
141
+ end
67
142
 
68
- delete "/stubs"
69
- end
143
+ end
70
144
 
71
- it "should respond with a 200 status code" do
72
- delete "/stubs"
145
+ describe "and the alias controller does not activate a stub" do
73
146
 
74
- response.status.should eql(200)
75
- end
147
+ before(:each) do
148
+ alias_controller.stub!(:activate).and_return(HttpStub::Response::EMPTY)
149
+ end
76
150
 
77
- end
151
+ it "should respond with a 404 status code" do
152
+ get "/a_path"
153
+
154
+ response.status.should eql(404)
155
+ end
156
+
157
+ end
158
+
159
+ end
78
160
 
79
- def issue_stub_request
80
- post "/stub", {
81
- "uri" => "/a_path",
82
- "method" => "a method",
83
- "response" => {
84
- "status" => 200,
85
- "body" => "Foo"
86
- }
87
- }.to_json
88
161
  end
89
162
 
90
163
  end