web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Unanswered

Mock Call httpWebRequest using XrmFakedContext D365

(1) ShareShare
ReportReport
Posted on by 5

Hello,

I wanted to mock the call to the HttpWebRequest service web in the unit tests using the XrmFakedContext; in this example I show you the code of the test function and the function that calls the web service Test Class:

[TestMethod, TestCategory("Unit example")]
    public void test()
    { 
    
    //Initialisation
    fakeContext.Initialize(new List()
            {  
                customer,
                 
            });

        
        var inputs = new Dictionary { { "CustomerId", customer.ToEntityReference() }};

        var result = fakeContext.ExecuteCodeActivity(inputs, null);


        Assert.AreEqual(result["Exist"], true);
    }


the function that calls the web service in the customWorkFlow :

private ReponseClass CallServiceWeb(string someParams)
    {
        try
        {

            var request = (HttpWebRequest)WebRequest.Create(someParams);

            request.Method = "GET";
            request.KeepAlive = true;
            request.Accept = @"*/*";

            var credential = new NetworkCredential(someParams, someParams, someParams);
            var credentialCache = new CredentialCache();
            credentialCache.Add(new Uri(url), "NTLM", credential);
            request.Credentials = credentialCache;

            string response = null;

            using (var responseDetail = (HttpWebResponse)request.GetResponse())
            {
                using (var sr = new StreamReader(responseDetail.GetResponseStream()))
                {
                    response = sr.ReadToEnd();
                }
            }

            return this.Deserialize(response);
        }
        catch (Exception e)
        {
            return new ReponseClass () { erreurAppel = e };
        }
    }

I have the same question (0)

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 70 Super User 2025 Season 2

#2
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 33 Most Valuable Professional

#3
Daniyal Khaleel Profile Picture

Daniyal Khaleel 32 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans