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,158 @@
1
+ describe "Configurer initialization acceptance" do
2
+ include_context "configurer integration"
3
+
4
+ let(:configurer) { HttpStub::Examples::ConfigurerWithComplexInitializer.new }
5
+
6
+ context "when a configurer is initialized" do
7
+
8
+ before(:example) { configurer.class.initialize! }
9
+
10
+ context "that contains a class stub" do
11
+
12
+ let(:configurer) { HttpStub::Examples::ConfigurerWithStub.new }
13
+
14
+ it "registers the stub" do
15
+ response = HTTParty.get("#{server_uri}/a_class_stub")
16
+
17
+ expect(response.code).to eql(201)
18
+ expect(response.body).to eql("Class stub body")
19
+ end
20
+
21
+ context "and the stub is overridden" do
22
+
23
+ before(:example) do
24
+ configurer.stub_response!("/a_class_stub", method: :get, response: { body: "Other class stub body" })
25
+ end
26
+
27
+ context "and the configurer is re-initialized" do
28
+
29
+ before(:example) do
30
+ configurer.class.initialize!
31
+ end
32
+
33
+ it "re-establishes the class stub as having priority" do
34
+ response = HTTParty.get("#{server_uri}/a_class_stub")
35
+
36
+ expect(response.code).to eql(201)
37
+ expect(response.body).to eql("Class stub body")
38
+ end
39
+
40
+ end
41
+
42
+ end
43
+
44
+ end
45
+
46
+ context "and the initializer contains conventional stubs" do
47
+
48
+ let(:configurer) { HttpStub::Examples::ConfigurerWithComplexInitializer.new }
49
+
50
+ it "registers the stub" do
51
+ response = HTTParty.get("#{server_uri}/stubbed_during_initialization_path")
52
+
53
+ expect(response.code).to eql(200)
54
+ expect(response.body).to eql("Stubbed during initialization body")
55
+ end
56
+
57
+ context "and another stub is registered" do
58
+
59
+ before(:example) do
60
+ configurer.stub_response!("/another_stub", method: :get, response: { body: "Another stub body" })
61
+ end
62
+
63
+ context "and the servers remembered stubs are recalled" do
64
+
65
+ before(:example) { configurer.recall_stubs! }
66
+
67
+ it "removes the stub registered post-initialization" do
68
+ response = HTTParty.get("#{server_uri}/another_stub")
69
+
70
+ expect(response.code).to eql(404)
71
+ end
72
+
73
+ it "retains the stub registered during initialization" do
74
+ response = HTTParty.get("#{server_uri}/stubbed_during_initialization_path")
75
+
76
+ expect(response.code).to eql(200)
77
+ expect(response.body).to eql("Stubbed during initialization body")
78
+ end
79
+
80
+ end
81
+
82
+ end
83
+
84
+ end
85
+
86
+ context "and the configurer contains an on initialize callback" do
87
+
88
+ let(:configurer) { HttpStub::Examples::ConfigurerWithInitializeCallback.new }
89
+
90
+ it "executes the callback" do
91
+ response = HTTParty.get("#{server_uri}/stubbed_on_initialize_path")
92
+
93
+ expect(response.code).to eql(200)
94
+ expect(response.body).to eql("Stubbed on initialize body")
95
+ end
96
+
97
+ end
98
+
99
+ end
100
+
101
+ context "when a configurer is uninitialized" do
102
+
103
+ context "and the configurer is informed that the server has started" do
104
+
105
+ before(:example) { configurer.server_has_started! }
106
+
107
+ it "does not initialize the configurer" do
108
+ activation_lambda = lambda { configurer.activate!("an_activator") }
109
+
110
+ expect(activation_lambda).to raise_error(/error occurred activating 'an_activator'/i)
111
+ end
112
+
113
+ context "and an attempt is made to register a stub" do
114
+
115
+ before(:example) do
116
+ configurer.stub_response!("/some_stub_path", method: :get, response: { body: "Some stub body" })
117
+ end
118
+
119
+ it "registers the stub" do
120
+ response = HTTParty.get("#{server_uri}/some_stub_path")
121
+
122
+ expect(response.code).to eql(200)
123
+ expect(response.body).to eql("Some stub body")
124
+ end
125
+
126
+ end
127
+
128
+ end
129
+
130
+ context "and the configurer has not been informed that the server has started" do
131
+
132
+ context "and an attempt is made to activate a stub" do
133
+
134
+ it "raises an exception indicating an error occurred during activation" do
135
+ activation_lambda = lambda { configurer.activate!("an_activator") }
136
+
137
+ expect(activation_lambda).to raise_error(/error occurred activating 'an_activator'/i)
138
+ end
139
+
140
+ end
141
+
142
+ end
143
+
144
+ context "and the configurer contains an on initialize callback" do
145
+
146
+ let(:configurer) { HttpStub::Examples::ConfigurerWithInitializeCallback.new }
147
+
148
+ it "does not execute the callback" do
149
+ response = HTTParty.get("#{server_uri}/stubbed_on_initialize_path")
150
+
151
+ expect(response.code).to eql(404)
152
+ end
153
+
154
+ end
155
+
156
+ end
157
+
158
+ end
@@ -0,0 +1,91 @@
1
+ describe "Scenario acceptance" do
2
+ include_context "configurer integration"
3
+
4
+ context "when a configurer that contains scenario's is initialized" do
5
+
6
+ let(:configurer) { HttpStub::Examples::ConfigurerWithTrivialScenarios.new }
7
+
8
+ before(:example) { configurer.class.initialize! }
9
+
10
+ context "and a scenario is activated" do
11
+
12
+ context "containing stubs" do
13
+
14
+ before(:example) { configurer.activate!("scenario_1") }
15
+
16
+ (1..3).each do |stub_number|
17
+
18
+ context "and scenario stub request ##{stub_number} is made" do
19
+
20
+ let(:response) { HTTParty.get("#{server_uri}/scenario_stub_path_#{stub_number}") }
21
+
22
+ it "replays the stubbed response" do
23
+ expect(response.code).to eql(200 + stub_number)
24
+ expect(response.body).to eql("Scenario stub #{stub_number} body")
25
+ end
26
+
27
+ end
28
+
29
+ end
30
+
31
+ end
32
+
33
+ context "containing triggered scenarios" do
34
+
35
+ before(:example) { configurer.activate!("scenario_activating_another_scenario") }
36
+
37
+ (1..3).each do |stub_number|
38
+
39
+ context "and a request is made matching stub ##{stub_number} within the triggered scenario" do
40
+
41
+ let(:response) { HTTParty.get("#{server_uri}/scenario_stub_path_#{stub_number}") }
42
+
43
+ it "triggers all related scenarios" do
44
+ expect(response.code).to eql(200 + stub_number)
45
+ expect(response.body).to eql("Scenario stub #{stub_number} body")
46
+ end
47
+
48
+ end
49
+
50
+ end
51
+
52
+ end
53
+
54
+ end
55
+
56
+ context "and the scenario is not activated" do
57
+
58
+ context "and a scenario stub request is made" do
59
+
60
+ let(:response) { HTTParty.get("#{server_uri}/scenario_stub_path_1") }
61
+
62
+ it "responds with a 404 status code" do
63
+ expect(response.code).to eql(404)
64
+ end
65
+
66
+ end
67
+
68
+ end
69
+
70
+ context "and the response contains a file" do
71
+
72
+ let(:configurer) { HttpStub::Examples::ConfigurerWithFileResponses.new }
73
+
74
+ before(:example) { configurer.activate!("scenario_with_file") }
75
+
76
+ context "and the stub request is made" do
77
+
78
+ let(:response) { HTTParty.get("#{server_uri}/activated_response_with_file") }
79
+
80
+ it "replays the stubbed response" do
81
+ expect(response.code).to eql(200)
82
+ expect_response_to_contain_file(HttpStub::Examples::ConfigurerWithFileResponses::FILE_PATH)
83
+ end
84
+
85
+ end
86
+
87
+ end
88
+
89
+ end
90
+
91
+ end
@@ -0,0 +1,160 @@
1
+ describe "Stub control value acceptance" do
2
+ include_context "configurer integration"
3
+
4
+ let(:configurer) { HttpStub::Examples::ConfigurerWithStub.new }
5
+
6
+ before(:example) { configurer.class.initialize! }
7
+
8
+ context "when a stub is submitted" do
9
+
10
+ context "with a stub uri that is regular expression containing meta characters" do
11
+
12
+ before(:example) do
13
+ configurer.stub_response!(/\/stub\/regexp\/\$key=value/, method: :get, response: { body: "Stub body" })
14
+ end
15
+
16
+ context "and a request is made whose uri matches the regular expression" do
17
+
18
+ let(:response) { HTTParty.get("#{server_uri}/match/stub/regexp/$key=value") }
19
+
20
+ it "replays the stubbed body" do
21
+ expect(response.body).to eql("Stub body")
22
+ end
23
+
24
+ end
25
+
26
+ context "and a request is made whose uri does not match the regular expression" do
27
+
28
+ let(:response) { HTTParty.get("#{server_uri}/stub/no_match/regexp") }
29
+
30
+ it "responds with a 404 status code" do
31
+ expect(response.code).to eql(404)
32
+ end
33
+
34
+ end
35
+
36
+ end
37
+
38
+ context "with headers whose values are regular expressions" do
39
+
40
+ before(:example) do
41
+ configurer.stub_response!(
42
+ "/stub_with_headers", method: :get, headers: { key: /^match.*/ },
43
+ response: { status: 202, body: "Another stub body" }
44
+ )
45
+ end
46
+
47
+ context "and a request that matches is made" do
48
+
49
+ let(:response) do
50
+ HTTParty.get("#{server_uri}/stub_with_headers", headers: { "key" => "matching_value" })
51
+ end
52
+
53
+ it "replays the stubbed response" do
54
+ expect(response.code).to eql(202)
55
+ expect(response.body).to eql("Another stub body")
56
+ end
57
+
58
+ end
59
+
60
+ context "and a request that does not match is made" do
61
+
62
+ let(:response) do
63
+ HTTParty.get("#{server_uri}/stub_with_headers", headers: { "key" => "does_not_match_value" })
64
+ end
65
+
66
+ it "responds with a 404 status code" do
67
+ expect(response.code).to eql(404)
68
+ end
69
+
70
+ end
71
+
72
+ end
73
+
74
+ context "with parameters" do
75
+
76
+ context "whose values are regular expressions" do
77
+
78
+ before(:example) do
79
+ configurer.stub_response!(
80
+ "/stub_with_parameters", method: :get, parameters: { key: /^match.*/ },
81
+ response: { status: 202, body: "Another stub body" }
82
+ )
83
+ end
84
+
85
+ context "and a request that matches is made" do
86
+
87
+ let(:response) { HTTParty.get("#{server_uri}/stub_with_parameters?key=matching_value") }
88
+
89
+ it "replays the stubbed response" do
90
+ expect(response.code).to eql(202)
91
+ expect(response.body).to eql("Another stub body")
92
+ end
93
+
94
+ end
95
+
96
+ context "and a request that does not match is made" do
97
+
98
+ let(:response) { HTTParty.get("#{server_uri}/stub_with_parameters?key=does_not_match_value") }
99
+
100
+ it "responds with a 404 status code" do
101
+ expect(response.code).to eql(404)
102
+ end
103
+
104
+ end
105
+
106
+ end
107
+
108
+ context "whose values indicate the parameters must be omitted" do
109
+
110
+ before(:example) do
111
+ configurer.stub_response!(
112
+ "/stub_with_omitted_parameters", method: :get, parameters: { key: :omitted },
113
+ response: { status: 202, body: "Omitted parameter stub body" }
114
+ )
115
+ end
116
+
117
+ context "and a request that matches is made" do
118
+
119
+ let(:response) { HTTParty.get("#{server_uri}/stub_with_omitted_parameters") }
120
+
121
+ it "replays the stubbed response" do
122
+ expect(response.code).to eql(202)
123
+ expect(response.body).to eql("Omitted parameter stub body")
124
+ end
125
+
126
+ end
127
+
128
+ context "and a request that does not match is made" do
129
+
130
+ let(:response) { HTTParty.get("#{server_uri}/stub_with_omitted_parameters?key=must_be_omitted") }
131
+
132
+ it "responds with a 404 status code" do
133
+ expect(response.code).to eql(404)
134
+ end
135
+
136
+ end
137
+
138
+ end
139
+
140
+ end
141
+
142
+ context "with a response delay" do
143
+
144
+ before(:example) do
145
+ configurer.stub_response!("/some_stub_path", method: :get, response: { delay_in_seconds: 2 })
146
+ end
147
+
148
+ it "delays to response by the time provided" do
149
+ start_time = Time.now
150
+
151
+ response = HTTParty.get("#{server_uri}/some_stub_path")
152
+
153
+ expect(Time.now - start_time).to be >= 2.0
154
+ end
155
+
156
+ end
157
+
158
+ end
159
+
160
+ end
@@ -0,0 +1,290 @@
1
+ describe "Stub basics acceptance" do
2
+ include_context "configurer integration"
3
+
4
+ let(:configurer) { HttpStub::Examples::ConfigurerWithStub.new }
5
+
6
+ before(:example) { configurer.class.initialize! }
7
+
8
+ context "when a stub is submitted" do
9
+
10
+ context "that contains no request headers or parameters" do
11
+
12
+ context "and it contains a response status" do
13
+
14
+ before(:example) do
15
+ stub_server.add_stub! do
16
+ match_requests("/stub_with_status", method: :get).respond_with(status: 201, body: "Stub body")
17
+ end
18
+ end
19
+
20
+ context "and a matching request is made" do
21
+
22
+ let(:response) { HTTParty.get("#{server_uri}/stub_with_status") }
23
+
24
+ it "responds with the stubbed status" do
25
+ expect(response.code).to eql(201)
26
+ end
27
+
28
+ it "replays the stubbed body" do
29
+ expect(response.body).to eql("Stub body")
30
+ end
31
+
32
+ end
33
+
34
+ context "and stubs are cleared" do
35
+
36
+ before(:example) { configurer.clear_stubs! }
37
+
38
+ context "and a matching request is made" do
39
+
40
+ let(:response) { HTTParty.get("#{server_uri}/stub_with_status") }
41
+
42
+ it "responds with a 404 status code" do
43
+ expect(response.code).to eql(404)
44
+ end
45
+
46
+ end
47
+
48
+ end
49
+
50
+ end
51
+
52
+ context "and it does not contain a response status" do
53
+
54
+ before(:example) do
55
+ stub_server.add_stub! { match_requests("/stub_without_status", method: :get).respond_with(body: "Stub body") }
56
+ end
57
+
58
+ context "and a matching request is made" do
59
+
60
+ let(:response) { HTTParty.get("#{server_uri}/stub_without_status") }
61
+
62
+ it "replays the stubbed body" do
63
+ expect(response.body).to eql("Stub body")
64
+ end
65
+
66
+ end
67
+
68
+ end
69
+
70
+ context "and it does not contain a request method" do
71
+
72
+ before(:example) do
73
+ stub_server.add_stub! { match_requests("/stub_without_method").respond_with(body: "Stub body") }
74
+ end
75
+
76
+ context "and a request is made with a matching uri" do
77
+
78
+ let(:response) { HTTParty.get("#{server_uri}/stub_without_method") }
79
+
80
+ it "replays the stubbed body" do
81
+ expect(response.body).to eql("Stub body")
82
+ end
83
+
84
+ end
85
+
86
+ end
87
+
88
+ end
89
+
90
+ context "that contains request headers" do
91
+
92
+ context "whose values are strings" do
93
+
94
+ before(:example) do
95
+ stub_server.add_stub! do
96
+ match_requests("/stub_with_headers", method: :get, headers: { key: "value" })
97
+ respond_with(status: 202, body: "Another stub body")
98
+ end
99
+ end
100
+
101
+ context "and that request is made" do
102
+
103
+ let(:response) { HTTParty.get("#{server_uri}/stub_with_headers", headers: { "key" => "value" }) }
104
+
105
+ it "replays the stubbed response" do
106
+ expect(response.code).to eql(202)
107
+ expect(response.body).to eql("Another stub body")
108
+ end
109
+
110
+ end
111
+
112
+ context "and a request with different headers is made" do
113
+
114
+ let(:response) { HTTParty.get("#{server_uri}/stub_with_headers", headers: { "key" => "other_value" }) }
115
+
116
+ it "responds with a 404 status code" do
117
+ expect(response.code).to eql(404)
118
+ end
119
+
120
+ end
121
+
122
+ end
123
+
124
+ end
125
+
126
+ context "that contains request parameters" do
127
+
128
+ context "whose values are strings" do
129
+
130
+ before(:example) do
131
+ stub_server.add_stub! do
132
+ match_requests("/stub_with_parameters", method: :get, parameters: { key: "value" })
133
+ respond_with(status: 202, body: "Another stub body")
134
+ end
135
+ end
136
+
137
+ context "and that request is made" do
138
+
139
+ let(:response) { HTTParty.get("#{server_uri}/stub_with_parameters?key=value") }
140
+
141
+ it "replays the stubbed response" do
142
+ expect(response.code).to eql(202)
143
+ expect(response.body).to eql("Another stub body")
144
+ end
145
+
146
+ end
147
+
148
+ context "and a request with different parameters is made" do
149
+
150
+ let(:response) { HTTParty.get("#{server_uri}/stub_with_parameters?key=another_value") }
151
+
152
+ it "responds with a 404 status code" do
153
+ expect(response.code).to eql(404)
154
+ end
155
+
156
+ end
157
+
158
+ end
159
+
160
+ context "whose values are numbers" do
161
+
162
+ before(:example) do
163
+ stub_server.add_stub! do
164
+ match_requests("/stub_with_parameters", method: :get, parameters: { key: 88 })
165
+ respond_with(status: 203, body: "Body for parameter number")
166
+ end
167
+ end
168
+
169
+ context "and that request is made" do
170
+
171
+ let(:response) { HTTParty.get("#{server_uri}/stub_with_parameters?key=88") }
172
+
173
+ it "replays the stubbed response" do
174
+ expect(response.code).to eql(203)
175
+ expect(response.body).to eql("Body for parameter number")
176
+ end
177
+
178
+ end
179
+
180
+ end
181
+
182
+ end
183
+
184
+ context "that contains response defaults" do
185
+
186
+ let(:configurer) { HttpStub::Examples::ConfigurerWithResponseDefaults.new }
187
+
188
+ it "includes the defaults in each response" do
189
+ response = Net::HTTP.get_response(server_host, "/response_with_defaults", server_port)
190
+
191
+ expect(response["defaulted_header"]).to eql("Header value")
192
+ end
193
+
194
+ end
195
+
196
+ describe "that contains response headers" do
197
+
198
+ context "with a content-type header" do
199
+
200
+ before(:example) do
201
+ stub_server.add_stub! do
202
+ match_requests("/some_stub_path", method: :get)
203
+ respond_with(body: "Some stub body", headers: { "content-type" => "application/xhtml" })
204
+ end
205
+ end
206
+
207
+ it "registers the stub" do
208
+ response = HTTParty.get("#{server_uri}/some_stub_path")
209
+
210
+ expect(response.content_type).to eql("application/xhtml")
211
+ end
212
+
213
+ end
214
+
215
+ context "which are custom" do
216
+
217
+ describe "and an attempt is made to register a response with a other headers" do
218
+
219
+ let(:response_headers) do
220
+ {
221
+ "some_header" => "some value",
222
+ "another_header" => "another value",
223
+ "yet_another_header" => "yet another value"
224
+ }
225
+ end
226
+
227
+ before(:example) do
228
+ stub_server.add_stub! do |stub|
229
+ stub.match_requests("/some_stub_path", method: :get)
230
+ stub.respond_with(body: "Some stub body", headers: response_headers)
231
+ end
232
+ end
233
+
234
+ it "registers the stub" do
235
+ response = Net::HTTP.get_response("localhost", "/some_stub_path", 8001)
236
+
237
+ response_headers.each { |key, value| expect(response[key]).to eql(value) }
238
+ end
239
+
240
+ end
241
+
242
+ end
243
+
244
+ end
245
+
246
+ context "that contains a response body that is a file" do
247
+
248
+ let(:configurer) { HttpStub::Examples::ConfigurerWithFileResponses.new }
249
+
250
+ context "and a request that matches is made" do
251
+
252
+ context "that matches a stub with a custom content-type" do
253
+
254
+ let(:response) { HTTParty.get("#{server_uri}/stub_response_with_file") }
255
+
256
+ it "responds with the configured status code" do
257
+ expect(response.code).to eql(200)
258
+ end
259
+
260
+ it "responds with the configured content type" do
261
+ expect(response.content_type).to eql("application/pdf")
262
+ end
263
+
264
+ it "responds with the configured file" do
265
+ expect_response_to_contain_file(HttpStub::Examples::ConfigurerWithFileResponses::FILE_PATH)
266
+ end
267
+
268
+ end
269
+
270
+ context "that matches a stub with no content-type" do
271
+
272
+ let(:response) { HTTParty.get("#{server_uri}/stub_response_with_file_and_no_content_type") }
273
+
274
+ it "responds with a default content type of 'application/octet-stream'" do
275
+ expect(response.content_type).to eql("application/octet-stream")
276
+ end
277
+
278
+ it "responds with the configured response" do
279
+ expect_response_to_contain_file(HttpStub::Examples::ConfigurerWithFileResponses::FILE_PATH)
280
+ end
281
+
282
+ end
283
+
284
+ end
285
+
286
+ end
287
+
288
+ end
289
+
290
+ end