Пример #1
0
        public async void MockHandlerTest()
        {
            var mockHandler = new MockHttpClientHandler();

            mockHandler.AddMockResponse(new Uri("http://example.org/test"), new HttpResponseMessage(HttpStatusCode.OK));

            var httpClient = new HttpClient(mockHandler);

            var response1 = await httpClient.GetAsync("http://example.org/notthere");

            var response2 = await httpClient.GetAsync("http://example.org/test");

            Assert.Equal(response1.StatusCode, HttpStatusCode.NotFound);
            Assert.Equal(response2.StatusCode, HttpStatusCode.OK);
        }
Пример #2
0
        public async void QueryAsync()
        {
            var mockHandler = new MockHttpClientHandler();

            HttpResponseMessage respMsg = MockResponse.GetResponse("querybatch/batch-1.json", HttpStatusCode.OK);
            Uri requestUri = new Uri(@"https://na15.salesforce.com/services/data/v41.0/query?q=SELECT%20Id%20FROM%20Contact%20LIMIT%20800");

            mockHandler.AddMockResponse(requestUri, respMsg);

            HttpResponseMessage respMsg2 = MockResponse.GetResponse("querybatch/batch-2.json", HttpStatusCode.OK);
            Uri requestUri2 = new Uri(@"https://na15.salesforce.com/services/data/v41.0/query/01gC000006QJJ7fIAH-200");

            mockHandler.AddMockResponse(requestUri2, respMsg2);

            HttpResponseMessage respMsg3 = MockResponse.GetResponse("querybatch/batch-3.json", HttpStatusCode.OK);
            Uri requestUri3 = new Uri(@"https://na15.salesforce.com/services/data/v41.0/query/01gC000006QJJ7fIAH-400");

            mockHandler.AddMockResponse(requestUri3, respMsg3);

            HttpResponseMessage respMsg4 = MockResponse.GetResponse("querybatch/batch-4.json", HttpStatusCode.OK);
            Uri requestUri4 = new Uri(@"https://na15.salesforce.com/services/data/v41.0/query/01gC000006QJJ7fIAH-600");

            mockHandler.AddMockResponse(requestUri4, respMsg4);

            HttpClient httpClient = new HttpClient(mockHandler);

            ForceClient client = new ForceClient("https://na15.salesforce.com", "v41.0", "dummyToken", httpClient);

            var contactsEnumerable = client.QueryAsync <SfContact>("SELECT Id FROM Contact LIMIT 800", batchSize: 200);

            List <SfContact> contacts = new List <SfContact>(800);

            using (IAsyncEnumerator <SfContact> contactsEnumerator = contactsEnumerable.GetEnumerator())
            {
                while (await contactsEnumerator.MoveNext())
                {
                    contacts.Add(contactsEnumerator.Current);
                }
            }

            Assert.Equal(800, contacts.Count);
            Assert.Equal("003XXXXXXXXXXXXXXX", contacts[0].Id);
            Assert.Equal("003XXXXXXXXXXXXXXX", contacts[799].Id);
        }
Пример #3
0
        private ForceClient CreateMockClient(
            string instanceUrl,
            string apiVersion,
            string requestUrl,
            string responseFilename,
            HttpStatusCode responseCode)
        {
            var mockHandler = new MockHttpClientHandler();

            HttpResponseMessage respMsg = MockResponse.GetResponse(responseFilename, responseCode);

            Uri requestUri = new Uri(requestUrl);

            mockHandler.AddMockResponse(requestUri, respMsg);

            var httpClient = new HttpClient(mockHandler);

            return(new ForceClient(instanceUrl, apiVersion, "dummyToken", httpClient));
        }
Пример #4
0
        public async void QuerySingleObjectFail()
        {
            var mockHandler = new MockHttpClientHandler();

            HttpResponseMessage respMsg = MockResponse.GetResponse("query_case_result.json", HttpStatusCode.OK);

            Uri requestUri = new Uri(@"https://na15.salesforce.com/services/data/v41.0/query?q=SELECT%20Id,CaseNumber,SystemModstamp,Account.Name,Account.SystemModstamp,Contact.Name,Contact.SystemModstamp%20FROM%20Case%20WHERE%20CaseNumber%20LIKE%20%270000%25%27");

            mockHandler.AddMockResponse(requestUri, respMsg);

            HttpClient httpClient = new HttpClient(mockHandler);

            ForceClient client = new ForceClient("https://na15.salesforce.com", "v41.0", "dummyToken", httpClient);

            Exception ex = await Assert.ThrowsAsync <Exception>(
                async() => await client.QuerySingle <SfCase>("SELECT Id,CaseNumber,SystemModstamp,Account.Name,Account.SystemModstamp,Contact.Name,Contact.SystemModstamp FROM Case WHERE CaseNumber LIKE '0000%'")
                );

            Assert.NotNull(ex);
        }
Пример #5
0
        public async void QuerySingleObject()
        {
            var mockHandler = new MockHttpClientHandler();

            HttpResponseMessage respMsg = MockResponse.GetResponse("query_case_single_result.json", HttpStatusCode.OK);

            Uri requestUri = new Uri(@"https://na15.salesforce.com/services/data/v41.0/query?q=SELECT%20Id,CaseNumber,SystemModstamp,Account.Name,Account.SystemModstamp,Contact.Name,Contact.SystemModstamp%20FROM%20Case%20WHERE%20CaseNumber%20%3D%20%2700001000%27");

            mockHandler.AddMockResponse(requestUri, respMsg);

            HttpClient httpClient = new HttpClient(mockHandler);

            ForceClient client = new ForceClient("https://na15.salesforce.com", "v41.0", "dummyToken", httpClient);

            SfCase singleCase = await client.QuerySingle <SfCase>("SELECT Id,CaseNumber,SystemModstamp,Account.Name,Account.SystemModstamp,Contact.Name,Contact.SystemModstamp FROM Case WHERE CaseNumber = '00001000'");

            Assert.False(singleCase == null);
            Assert.Equal("00001000", singleCase.CaseNumber);
            Assert.Equal("Edge Communications", singleCase.Account.Name);
            Assert.Equal("Rose Gonzalez", singleCase.Contact.Name);
        }
Пример #6
0
        public async void ObjectById()
        {
            var mockHandler = new MockHttpClientHandler();

            HttpResponseMessage respMsg = MockResponse.GetResponse("account.json", HttpStatusCode.OK);

            const string objectId = "001i000002C8QTIAA3";

            Uri requestUri = new Uri("https://na15.salesforce.com/services/data/v41.0/sobjects/Account/001i000002C8QTIAA3");

            mockHandler.AddMockResponse(requestUri, respMsg);

            HttpClient httpClient = new HttpClient(mockHandler);

            ForceClient client = new ForceClient("https://na15.salesforce.com", "v41.0", "dummyToken", httpClient);

            SfAccount acct = await client.GetObjectById <SfAccount>("Account", objectId);

            Assert.False(acct == null);

            Assert.Equal(objectId, acct.Id);
            Assert.Equal("NY", acct.BillingAddress.State);
        }
Пример #7
0
        public async void QueryNestedObjects()
        {
            var mockHandler = new MockHttpClientHandler();

            HttpResponseMessage respMsg = MockResponse.GetResponse("query_case_result.json", HttpStatusCode.OK);

            Uri requestUri = new Uri(@"https://na15.salesforce.com/services/data/v41.0/query?q=SELECT%20Id,CaseNumber,SystemModstamp,Account.Name,Account.SystemModstamp,Contact.Name,Contact.SystemModstamp%20FROM%20Case");

            mockHandler.AddMockResponse(requestUri, respMsg);

            HttpClient httpClient = new HttpClient(mockHandler);

            ForceClient client = new ForceClient("https://na15.salesforce.com", "v41.0", "dummyToken", httpClient);

            List <SfCase> cases = await client.Query <SfCase>("SELECT Id,CaseNumber,SystemModstamp,Account.Name,Account.SystemModstamp,Contact.Name,Contact.SystemModstamp FROM Case");

            SfCase firstCase = cases[0];

            Assert.False(cases == null);
            Assert.Equal("00001000", cases[0].CaseNumber);
            Assert.Equal("Edge Communications", cases[0].Account.Name);
            Assert.Equal("Rose Gonzalez", cases[0].Contact.Name);
            Assert.Equal(26, cases.Count);
        }