internal CosmosItemResponse <T> CreateItemResponse <T>(
     CosmosResponseMessage cosmosResponseMessage)
 {
     return(CosmosItemResponse <T> .CreateResponse <T>(
                cosmosResponseMessage,
                this.jsonSerializer));
 }
Exemple #2
0
        /// <summary>
        /// Creates a CosmosClient with a mock TransportHandler that will capture and detect Exceptions happening inside ProcessChangesAsync.
        /// Since we want to be Exception-less, this will help detect if the Store Model is throwing or not.
        /// </summary>
        /// <param name="goThroughGateway">Whether or not to run the scenario using Gateway. If false, Direct will be used.</param>
        private static async Task <MockTransportHandler> TransportHandlerRunScenario(int responseStatusCode, bool goThroughGateway = true)
        {
            Func <HttpRequestMessage, Task <HttpResponseMessage> > sendFunc = async httpRequest => await Task.FromResult(new HttpResponseMessage((HttpStatusCode)responseStatusCode) {
                Content        = new StringContent("{}"),
                RequestMessage = httpRequest
            });

            Func <Uri, DocumentServiceRequest, StoreResponse> sendDirectFunc = (uri, request) => new StoreResponse()
            {
                ResponseBody         = Stream.Null,
                Status               = responseStatusCode,
                ResponseHeaderNames  = Array.Empty <string>(),
                ResponseHeaderValues = Array.Empty <string>()
            };

            // This is needed because in order to Mock a TransportClient we previously need an instance of CosmosClient
            CosmosClient internalClient = MockDocumentClient.CreateMockCosmosClient();

            internalClient.DocumentClient.GatewayStoreModel = MockGatewayStoreModel(sendFunc);
            internalClient.DocumentClient.StoreModel        = MockServerStoreModel(internalClient.DocumentClient.Session, sendDirectFunc);


            RetryHandler         retryHandler     = new RetryHandler(internalClient.DocumentClient.ResetSessionTokenRetryPolicy);
            MockTransportHandler transportHandler = new MockTransportHandler(internalClient);

            CosmosClient client = MockDocumentClient.CreateMockCosmosClient(
                (builder) => {
                builder
                .AddCustomHandlers(retryHandler, transportHandler);
            });

            try
            {
                if (goThroughGateway)
                {
                    CosmosDatabaseResponse response = await client.Databases.CreateDatabaseAsync("test");
                }
                else
                {
                    CosmosItemResponse <dynamic> response = await client.Databases["test"].Containers["test"].Items.CreateItemAsync <dynamic>(partitionKey: "id", item: new { id = "id" });
                }
            }
            catch (CosmosException)
            {
                // Swallow CosmosExceptions as the point is to test the TransportHandler
            }

            return(transportHandler);
        }