示例#1
0
        public MemorySetup(
            object responseValue,
            Func <ConnectionConfiguration, ConnectionConfiguration> configSetup,
            Func <ConnectionConfiguration, Stream, ElasticsearchResponse <Stream> > responseSetup,
            Func <IElasticsearchClient, ElasticsearchResponse <T> > call = null
            )
        {
            this.Fake = new AutoFake(callsDoNothing: true);
            var connectionConfiguration = configSetup(new ConnectionConfiguration());

            this.ResponseStream = CreateServerExceptionResponse(responseValue);
            var response = responseSetup(connectionConfiguration, this.ResponseStream);

            this.Fake.Provide <IConnectionConfigurationValues>(connectionConfiguration);
            this.MemoryProvider = this.Fake.Resolve <IMemoryStreamProvider>();
            A.CallTo(() => this.MemoryProvider.New()).ReturnsLazily((o) =>
            {
                var memoryStream = new TrackableMemoryStream();
                this._createdMemoryStreams.Add(memoryStream);
                return(memoryStream);
            });

            FakeCalls.ProvideDefaultTransport(this.Fake, memoryStreamProvider: this.MemoryProvider);

            this.GetCall = FakeCalls.GetSyncCall(this.Fake);
            this.GetCall.Returns(response);

            var client = this.Fake.Resolve <ElasticsearchClient>();

            this.Result = call != null?call(client) : client.Info <T>();

            this.GetCall.MustHaveHappened(Repeated.Exactly.Once);
        }
示例#2
0
        public async Task <AsyncMemorySetup <T> > Init()
        {
            this.Fake = new AutoFake(callsDoNothing: true);
            var connectionConfiguration = _configSetup(new ConnectionConfiguration());

            this.ResponseStream = CreateServerExceptionResponse(_responseValue);
            var response = _responseSetup(connectionConfiguration, this.ResponseStream);

            this.Fake.Provide <IConnectionConfigurationValues>(connectionConfiguration);
            this.MemoryProvider = this.Fake.Resolve <IMemoryStreamProvider>();
            A.CallTo(() => this.MemoryProvider.New()).ReturnsLazily((o) =>
            {
                var memoryStream = new TrackableMemoryStream();
                this._createdMemoryStreams.Add(memoryStream);
                return(memoryStream);
            });

            FakeCalls.ProvideDefaultTransport(this.Fake, memoryStreamProvider: this.MemoryProvider);

            this.GetCall = FakeCalls.GetCall(this.Fake);
            this.GetCall.Returns(response);

            var client = this.Fake.Resolve <ElasticsearchClient>();

            this.Result = await(_call != null ? _call(client) : client.InfoAsync <T>());

            this.GetCall.MustHaveHappened(Repeated.Exactly.Once);
            return(this);
        }
示例#3
0
        protected TrackableMemoryStream CreateServerExceptionResponse(object responseValue)
        {
            if (responseValue is string)
            {
                responseValue = string.Format(CultureInfo.InvariantCulture, @"""{0}""", responseValue);
            }
            var format = @"{{ ""value"": {0} }}";

            this.ResponseBytes = Encoding.UTF8.GetBytes(string.Format(CultureInfo.InvariantCulture, format, responseValue));
            var stream = new TrackableMemoryStream(this.ResponseBytes);

            return(stream);
        }