|
55 | 55 | end |
56 | 56 |
|
57 | 57 | context 'when client is configured without retries' do |
58 | | - let(:client) { described_class.new(nil, Optimizely::CmabRetryConfig.new(max_retries: 0), spy_logger) } |
| 58 | + let(:client) { described_class.new(http_client: nil, retry_config: Optimizely::CmabRetryConfig.new(max_retries: 0), logger: spy_logger) } |
59 | 59 |
|
60 | 60 | it 'should return the variation id on success' do |
61 | 61 | WebMock.stub_request(:post, expected_url) |
|
132 | 132 | end |
133 | 133 |
|
134 | 134 | context 'when client is configured with retries' do |
135 | | - let(:client_with_retry) { described_class.new(nil, retry_config, spy_logger) } |
| 135 | + let(:client_with_retry) { described_class.new(http_client: nil, retry_config: retry_config, logger: spy_logger) } |
136 | 136 |
|
137 | 137 | it 'should return the variation id on first try' do |
138 | 138 | WebMock.stub_request(:post, expected_url) |
|
195 | 195 | expect(spy_logger).to have_received(:log).with(Logger::ERROR, a_string_including('Max retries exceeded for CMAB request')) |
196 | 196 | end |
197 | 197 | end |
| 198 | + |
| 199 | + context 'when custom prediction endpoint is configured' do |
| 200 | + let(:custom_endpoint) { 'https://custom.endpoint.com/predict/%s' } |
| 201 | + let(:custom_url) { 'https://custom.endpoint.com/predict/test_rule' } |
| 202 | + let(:client_with_custom_endpoint) { described_class.new(http_client: nil, retry_config: Optimizely::CmabRetryConfig.new(max_retries: 0), logger: spy_logger, prediction_endpoint: custom_endpoint) } |
| 203 | + |
| 204 | + it 'should use the custom prediction endpoint' do |
| 205 | + WebMock.stub_request(:post, custom_url) |
| 206 | + .with(body: expected_body_for_webmock, headers: expected_headers) |
| 207 | + .to_return(status: 200, body: {'predictions' => [{'variation_id' => 'custom123'}]}.to_json, headers: {'Content-Type' => 'application/json'}) |
| 208 | + |
| 209 | + result = client_with_custom_endpoint.fetch_decision(rule_id, user_id, attributes, cmab_uuid) |
| 210 | + |
| 211 | + expect(result).to eq('custom123') |
| 212 | + expect(WebMock).to have_requested(:post, custom_url) |
| 213 | + .with(body: expected_body_for_webmock, headers: expected_headers).once |
| 214 | + end |
| 215 | + end |
| 216 | + |
| 217 | + context 'when no prediction endpoint is provided' do |
| 218 | + let(:client_with_default) { described_class.new(http_client: nil, retry_config: Optimizely::CmabRetryConfig.new(max_retries: 0), logger: spy_logger, prediction_endpoint: nil) } |
| 219 | + |
| 220 | + it 'should use the default prediction endpoint' do |
| 221 | + WebMock.stub_request(:post, expected_url) |
| 222 | + .with(body: expected_body_for_webmock, headers: expected_headers) |
| 223 | + .to_return(status: 200, body: {'predictions' => [{'variation_id' => 'default123'}]}.to_json, headers: {'Content-Type' => 'application/json'}) |
| 224 | + |
| 225 | + result = client_with_default.fetch_decision(rule_id, user_id, attributes, cmab_uuid) |
| 226 | + |
| 227 | + expect(result).to eq('default123') |
| 228 | + expect(WebMock).to have_requested(:post, expected_url) |
| 229 | + .with(body: expected_body_for_webmock, headers: expected_headers).once |
| 230 | + end |
| 231 | + end |
| 232 | + |
| 233 | + context 'when empty string prediction endpoint is provided' do |
| 234 | + let(:client_with_empty_endpoint) { described_class.new(http_client: nil, retry_config: Optimizely::CmabRetryConfig.new(max_retries: 0), logger: spy_logger, prediction_endpoint: '') } |
| 235 | + |
| 236 | + it 'should fall back to the default prediction endpoint' do |
| 237 | + WebMock.stub_request(:post, expected_url) |
| 238 | + .with(body: expected_body_for_webmock, headers: expected_headers) |
| 239 | + .to_return(status: 200, body: {'predictions' => [{'variation_id' => 'fallback123'}]}.to_json, headers: {'Content-Type' => 'application/json'}) |
| 240 | + |
| 241 | + result = client_with_empty_endpoint.fetch_decision(rule_id, user_id, attributes, cmab_uuid) |
| 242 | + |
| 243 | + expect(result).to eq('fallback123') |
| 244 | + expect(WebMock).to have_requested(:post, expected_url) |
| 245 | + .with(body: expected_body_for_webmock, headers: expected_headers).once |
| 246 | + end |
| 247 | + end |
198 | 248 | end |
0 commit comments