|  | 
|  | 1 | +/*  | 
|  | 2 | +* Copyright 2025, Optimizely | 
|  | 3 | +* | 
|  | 4 | +* Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | +* you may not use this file except in compliance with the License. | 
|  | 6 | +* You may obtain a copy of the License at | 
|  | 7 | +* | 
|  | 8 | +* http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | +* | 
|  | 10 | +* Unless required by applicable law or agreed to in writing, software | 
|  | 11 | +* distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | +* See the License for the specific language governing permissions and | 
|  | 14 | +* limitations under the License. | 
|  | 15 | +*/ | 
|  | 16 | + | 
|  | 17 | +using System; | 
|  | 18 | +using System.Collections.Generic; | 
|  | 19 | +using System.Net; | 
|  | 20 | +using System.Net.Http; | 
|  | 21 | +using System.Threading; | 
|  | 22 | +using System.Threading.Tasks; | 
|  | 23 | +using Moq; | 
|  | 24 | +using Moq.Protected; | 
|  | 25 | +using NUnit.Framework; | 
|  | 26 | +using OptimizelySDK.Cmab; | 
|  | 27 | +using OptimizelySDK.ErrorHandler; | 
|  | 28 | +using OptimizelySDK.Exceptions; | 
|  | 29 | +using OptimizelySDK.Logger; | 
|  | 30 | + | 
|  | 31 | +namespace OptimizelySDK.Tests.CmabTests | 
|  | 32 | +{ | 
|  | 33 | +    [TestFixture] | 
|  | 34 | +    public class DefaultCmabClientTest | 
|  | 35 | +    { | 
|  | 36 | +        private class ResponseStep | 
|  | 37 | +        { | 
|  | 38 | +            public HttpStatusCode Status { get; private set; } | 
|  | 39 | +            public string Body { get; private set; } | 
|  | 40 | +            public ResponseStep(HttpStatusCode status, string body) | 
|  | 41 | +            { | 
|  | 42 | +                Status = status; | 
|  | 43 | +                Body = body; | 
|  | 44 | +            } | 
|  | 45 | +        } | 
|  | 46 | + | 
|  | 47 | +        private static HttpClient MakeClient(params ResponseStep[] sequence) | 
|  | 48 | +        { | 
|  | 49 | +            var handler = new Mock<HttpMessageHandler>(MockBehavior.Strict); | 
|  | 50 | +            var queue = new Queue<ResponseStep>(sequence); | 
|  | 51 | + | 
|  | 52 | +            handler.Protected().Setup<Task<HttpResponseMessage>>("SendAsync", | 
|  | 53 | +                ItExpr.IsAny<HttpRequestMessage>(), | 
|  | 54 | +                ItExpr.IsAny<CancellationToken>()).Returns((HttpRequestMessage _, CancellationToken __) => | 
|  | 55 | +            { | 
|  | 56 | +                if (queue.Count == 0) | 
|  | 57 | +                    throw new InvalidOperationException("No more mocked responses available."); | 
|  | 58 | + | 
|  | 59 | +                var step = queue.Dequeue(); | 
|  | 60 | +                var response = new HttpResponseMessage(step.Status); | 
|  | 61 | +                if (step.Body != null) | 
|  | 62 | +                { | 
|  | 63 | +                    response.Content = new StringContent(step.Body); | 
|  | 64 | +                } | 
|  | 65 | +                return Task.FromResult(response); | 
|  | 66 | +            }); | 
|  | 67 | + | 
|  | 68 | +            return new HttpClient(handler.Object); | 
|  | 69 | +        } | 
|  | 70 | + | 
|  | 71 | +        private static HttpClient MakeClientExceptionSequence(params Exception[] sequence) | 
|  | 72 | +        { | 
|  | 73 | +            var handler = new Mock<HttpMessageHandler>(MockBehavior.Strict); | 
|  | 74 | +            var queue = new Queue<Exception>(sequence); | 
|  | 75 | + | 
|  | 76 | +            handler.Protected().Setup<Task<HttpResponseMessage>>("SendAsync", | 
|  | 77 | +                ItExpr.IsAny<HttpRequestMessage>(), | 
|  | 78 | +                ItExpr.IsAny<CancellationToken>()).Returns((HttpRequestMessage _, CancellationToken __) => | 
|  | 79 | +            { | 
|  | 80 | +                if (queue.Count == 0) | 
|  | 81 | +                    throw new InvalidOperationException("No more mocked exceptions available."); | 
|  | 82 | + | 
|  | 83 | +                var ex = queue.Dequeue(); | 
|  | 84 | +                var tcs = new TaskCompletionSource<HttpResponseMessage>(); | 
|  | 85 | +                tcs.SetException(ex); | 
|  | 86 | +                return tcs.Task; | 
|  | 87 | +            }); | 
|  | 88 | + | 
|  | 89 | +            return new HttpClient(handler.Object); | 
|  | 90 | +        } | 
|  | 91 | + | 
|  | 92 | +        private static string ValidBody(string variationId = "v1") | 
|  | 93 | +            => $"{{\"predictions\":[{{\"variation_id\":\"{variationId}\"}}]}}"; | 
|  | 94 | + | 
|  | 95 | +        [Test] | 
|  | 96 | +        public void FetchDecisionReturnsSuccessNoRetry() | 
|  | 97 | +        { | 
|  | 98 | +            var http = MakeClient(new ResponseStep(HttpStatusCode.OK, ValidBody("v1"))); | 
|  | 99 | +            var client = new DefaultCmabClient(http, retryConfig: null, logger: new NoOpLogger(), errorHandler: new NoOpErrorHandler()); | 
|  | 100 | +            var result = client.FetchDecision("rule-1", "user-1", null, "uuid-1"); | 
|  | 101 | + | 
|  | 102 | +            Assert.AreEqual("v1", result); | 
|  | 103 | +        } | 
|  | 104 | + | 
|  | 105 | +        [Test] | 
|  | 106 | +        public void FetchDecisionHttpExceptionNoRetry() | 
|  | 107 | +        { | 
|  | 108 | +            var http = MakeClientExceptionSequence(new HttpRequestException("boom")); | 
|  | 109 | +            var client = new DefaultCmabClient(http, retryConfig: null); | 
|  | 110 | + | 
|  | 111 | +            Assert.Throws<CmabFetchException>(() => | 
|  | 112 | +                client.FetchDecision("rule-1", "user-1", null, "uuid-1")); | 
|  | 113 | +        } | 
|  | 114 | + | 
|  | 115 | +        [Test] | 
|  | 116 | +        public void FetchDecisionNon2xxNoRetry() | 
|  | 117 | +        { | 
|  | 118 | +            var http = MakeClient(new ResponseStep(HttpStatusCode.InternalServerError, null)); | 
|  | 119 | +            var client = new DefaultCmabClient(http, retryConfig: null); | 
|  | 120 | + | 
|  | 121 | +            Assert.Throws<CmabFetchException>(() => | 
|  | 122 | +                client.FetchDecision("rule-1", "user-1", null, "uuid-1")); | 
|  | 123 | +        } | 
|  | 124 | + | 
|  | 125 | +        [Test] | 
|  | 126 | +        public void FetchDecisionInvalidJsonNoRetry() | 
|  | 127 | +        { | 
|  | 128 | +            var http = MakeClient(new ResponseStep(HttpStatusCode.OK, "not json")); | 
|  | 129 | +            var client = new DefaultCmabClient(http, retryConfig: null); | 
|  | 130 | + | 
|  | 131 | +            Assert.Throws<CmabInvalidResponseException>(() => | 
|  | 132 | +                client.FetchDecision("rule-1", "user-1", null, "uuid-1")); | 
|  | 133 | +        } | 
|  | 134 | + | 
|  | 135 | +        [Test] | 
|  | 136 | +        public void FetchDecisionInvalidStructureNoRetry() | 
|  | 137 | +        { | 
|  | 138 | +            var http = MakeClient(new ResponseStep(HttpStatusCode.OK, "{\"predictions\":[]}")); | 
|  | 139 | +            var client = new DefaultCmabClient(http, retryConfig: null); | 
|  | 140 | + | 
|  | 141 | +            Assert.Throws<CmabInvalidResponseException>(() => | 
|  | 142 | +                client.FetchDecision("rule-1", "user-1", null, "uuid-1")); | 
|  | 143 | +        } | 
|  | 144 | + | 
|  | 145 | +        [Test] | 
|  | 146 | +        public void FetchDecisionSuccessWithRetryFirstTry() | 
|  | 147 | +        { | 
|  | 148 | +            var http = MakeClient(new ResponseStep(HttpStatusCode.OK, ValidBody("v2"))); | 
|  | 149 | +            var retry = new CmabRetryConfig(maxRetries: 2, initialBackoff: TimeSpan.Zero, maxBackoff: TimeSpan.FromSeconds(1), backoffMultiplier: 2.0); | 
|  | 150 | +            var client = new DefaultCmabClient(http, retry); | 
|  | 151 | +            var result = client.FetchDecision("rule-1", "user-1", null, "uuid-1"); | 
|  | 152 | + | 
|  | 153 | +            Assert.AreEqual("v2", result); | 
|  | 154 | +        } | 
|  | 155 | + | 
|  | 156 | +        [Test] | 
|  | 157 | +        public void FetchDecisionSuccessWithRetryThirdTry() | 
|  | 158 | +        { | 
|  | 159 | +            var http = MakeClient( | 
|  | 160 | +                new ResponseStep(HttpStatusCode.InternalServerError, null), | 
|  | 161 | +                new ResponseStep(HttpStatusCode.InternalServerError, null), | 
|  | 162 | +                new ResponseStep(HttpStatusCode.OK, ValidBody("v3")) | 
|  | 163 | +            ); | 
|  | 164 | +            var retry = new CmabRetryConfig(maxRetries: 2, initialBackoff: TimeSpan.Zero, maxBackoff: TimeSpan.FromSeconds(1), backoffMultiplier: 2.0); | 
|  | 165 | +            var client = new DefaultCmabClient(http, retry); | 
|  | 166 | +            var result = client.FetchDecision("rule-1", "user-1", null, "uuid-1"); | 
|  | 167 | + | 
|  | 168 | +            Assert.AreEqual("v3", result); | 
|  | 169 | +        } | 
|  | 170 | + | 
|  | 171 | +        [Test] | 
|  | 172 | +        public void FetchDecisionExhaustsAllRetries() | 
|  | 173 | +        { | 
|  | 174 | +            var http = MakeClient( | 
|  | 175 | +                new ResponseStep(HttpStatusCode.InternalServerError, null), | 
|  | 176 | +                new ResponseStep(HttpStatusCode.InternalServerError, null), | 
|  | 177 | +                new ResponseStep(HttpStatusCode.InternalServerError, null) | 
|  | 178 | +            ); | 
|  | 179 | +            var retry = new CmabRetryConfig(maxRetries: 2, initialBackoff: TimeSpan.Zero, maxBackoff: TimeSpan.FromSeconds(1), backoffMultiplier: 2.0); | 
|  | 180 | +            var client = new DefaultCmabClient(http, retry); | 
|  | 181 | + | 
|  | 182 | +            Assert.Throws<CmabFetchException>(() => | 
|  | 183 | +                client.FetchDecision("rule-1", "user-1", null, "uuid-1")); | 
|  | 184 | +        } | 
|  | 185 | +    } | 
|  | 186 | +} | 
0 commit comments