Beispiel #1
0
        public static IUnleash CreateUnleash(TestDefinition testDefinition, UnleashContextDefinition contextDefinition)
        {
            var fakeHttpClientFactory  = A.Fake <IHttpClientFactory>();
            var fakeHttpMessageHandler = new TestHttpMessageHandler();
            var httpClient             = new HttpClient(fakeHttpMessageHandler)
            {
                BaseAddress = new Uri("http://localhost")
            };
            var fakeScheduler  = A.Fake <IUnleashScheduledTaskManager>();
            var fakeFileSystem = new MockFileSystem();

            A.CallTo(() => fakeHttpClientFactory.Create(A <Uri> ._)).Returns(httpClient);
            A.CallTo(() => fakeScheduler.Configure(A <IEnumerable <IUnleashScheduledTask> > ._, A <CancellationToken> ._)).Invokes(action =>
            {
                var task = ((IEnumerable <IUnleashScheduledTask>)action.Arguments[0]).First();
                task.ExecuteAsync((CancellationToken)action.Arguments[1]).Wait();
            });

            fakeHttpMessageHandler.Response = new HttpResponseMessage
            {
                StatusCode = HttpStatusCode.OK,
                Content    = new StringContent(testDefinition.State.ToString(), Encoding.UTF8, "application/json"),
                Headers    =
                {
                    ETag = new EntityTagHeaderValue("\"123\"")
                }
            };

            var contextBuilder = new UnleashContext.Builder()
                                 .UserId(contextDefinition.UserId)
                                 .SessionId(contextDefinition.SessionId)
                                 .RemoteAddress(contextDefinition.RemoteAddress)
                                 .Environment(contextDefinition.Environment)
                                 .AppName(contextDefinition.AppName);

            if (contextDefinition.Properties != null)
            {
                foreach (var property in contextDefinition.Properties)
                {
                    contextBuilder.AddProperty(property.Key, property.Value);
                }
            }

            var settings = new UnleashSettings
            {
                AppName = testDefinition.Name,
                UnleashContextProvider = new DefaultUnleashContextProvider(contextBuilder.Build()),
                HttpClientFactory      = fakeHttpClientFactory,
                ScheduledTaskManager   = fakeScheduler,
                FileSystem             = fakeFileSystem
            };

            var unleash = new DefaultUnleash(settings);

            return(unleash);
        }
Beispiel #2
0
        private static Action CreateTestAction(TestDefinition testDefinition, TestCase testCase)
        {
            return(() =>
            {
                // Arrange
                var unleash = CreateUnleash(testDefinition, testCase.Context);

                // Act
                var result = unleash.IsEnabled(testCase.ToggleName);

                // Assert
                Assert.AreEqual(testCase.ExpectedResult, result, testCase.Description);
            });
        }
Beispiel #3
0
        private static Action CreateVariantTestAction(TestDefinition testDefinition, TestCaseVariant testCase)
        {
            return(() =>
            {
                // Arrange
                var unleash = CreateUnleash(testDefinition, testCase.Context);

                // Act
                var result = unleash.GetVariant(testCase.ToggleName);

                // Assert
                Assert.AreEqual(testCase.ExpectedResult.Name, result.Name, testCase.Description);
                Assert.AreEqual(testCase.ExpectedResult.IsEnabled, result.IsEnabled, testCase.Description);
                Assert.AreEqual(testCase.ExpectedResult.Payload, result.Payload, testCase.Description);
            });
        }