Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7676599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:14:10+00:00 2026-05-31T17:14:10+00:00

I need to do some connectivity simulations to see that my code handles various

  • 0

I need to do some connectivity simulations to see that my code handles various connectivity errors to Facebook. I want to be able to simulate 500s, timeouts etc.

The easiest way to do that is to use Fiddler, but it seems to not be working with HTTPS (I get 403s when I try).

Is ther a way to force the SDK to work with HTTP instead of HTTPS for debugging purposes?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T17:14:11+00:00Added an answer on May 31, 2026 at 5:14 pm

    Facebook C# SDK supports your scenario for mocking the entire HttpWebRequest and HttpWebResponse. In fact we actually use that internally in our unit tests so that every single line of the code in Facebook C# SDK actually gets executed and the result is always the same. https://github.com/facebook-csharp-sdk/facebook-csharp-sdk/blob/v5/Source/Facebook.Tests/TestExtensions.cs For now you will need to check these tests in v5 branch as we haven’t yet migrated those tests to v6.

    For v5, you will need to override the protected CreateHttpWebRequest method in FacebookClient.

    Here is an example for v5 when there is no internet connection. There are three hidden classes HttpWebRequestWrapper, HttpWebResponseWrapper and WebExceptionWrapper that you will need to make use of.

        public static void NoInternetConnection(this Mock<Facebook.FacebookClient> facebookClient, out Mock<HttpWebRequestWrapper> mockRequest, out Mock<WebExceptionWrapper> mockWebException)
        {
            mockRequest = new Mock<HttpWebRequestWrapper>();
            mockWebException = new Mock<WebExceptionWrapper>();
            var mockAsyncResult = new Mock<IAsyncResult>();
    
            var request = mockRequest.Object;
            var webException = mockWebException.Object;
            var asyncResult = mockAsyncResult.Object;
    
            mockRequest.SetupProperty(r => r.Method);
            mockRequest.SetupProperty(r => r.ContentType);
            mockRequest.SetupProperty(r => r.ContentLength);
            mockAsyncResult
                .Setup(ar => ar.AsyncWaitHandle)
                .Returns((ManualResetEvent)null);
    
            mockWebException
                .Setup(e => e.GetResponse())
                .Returns<HttpWebResponseWrapper>(null);
    
            mockRequest
                .Setup(r => r.GetResponse())
                .Throws(webException);
    
            mockRequest
                .Setup(r => r.EndGetResponse(It.IsAny<IAsyncResult>()))
                .Throws(webException);
    
            AsyncCallback callback = null;
    
            mockRequest
                .Setup(r => r.BeginGetResponse(It.IsAny<AsyncCallback>(), It.IsAny<object>()))
                .Callback<AsyncCallback, object>((c, s) =>
                                                     {
                                                         callback = c;
                                                     })
                .Returns(() =>
                             {
                                 callback(asyncResult);
                                 return asyncResult;
                             });
    
            var mockRequestCopy = mockRequest;
            var mockWebExceptionCopy = mockWebException;
    
            facebookClient.Protected()
                .Setup<HttpWebRequestWrapper>("CreateHttpWebRequest", ItExpr.IsAny<Uri>())
                .Callback<Uri>(uri =>
                                   {
                                       mockRequestCopy.Setup(r => r.RequestUri).Returns(uri);
                                       mockWebExceptionCopy.Setup(e => e.Message).Returns(string.Format("The remote name could not be resolved: '{0}'", uri.Host));
                                   })
                .Returns(request);
        }
    

    You can then write your tests as below.

        [Fact]
        public void SyncWhenThereIsNotInternetConnectionAndFiddlerIsNotOpen_ThrowsWebExceptionWrapper()
        {
            var mockFb = new Mock<FacebookClient> { CallBase = true };
            Mock<HttpWebRequestWrapper> mockRequest;
            Mock<WebExceptionWrapper> mockWebException;
    
            mockFb.NoInternetConnection(out mockRequest, out mockWebException);
    
            Exception exception = null;
    
            try
            {
                var fb = mockFb.Object;
                fb.Get(_parameters);
            }
            catch (Exception ex)
            {
                exception = ex;
            }
    
            mockFb.VerifyCreateHttpWebRequest(Times.Once());
            mockRequest.VerifyGetResponse();
            mockWebException.VerifyGetReponse();
    
            Assert.IsAssignableFrom<WebExceptionWrapper>(exception);
        }
    

    In v6 we have made mocking the HttpWebRequest and HttpWebResponse much easier.

    Create your custom HttpWebRequest and HttpWebResponse by inheriting HttpWebRequestWrapper and HttpWebReponseWrapper.

    Then change the default http web request factory for Facebook C# SDK. Here is the sample of the default factory.

    FacebookClient.SetDefaultHttpWebRequestFactory(uri => new HttpWebRequestWrapper((HttpWebRequest)WebRequest.Create(uri)));
    

    If you want to change the HttpWebRequestFactor per FacebookClient instance then use the following code.

    var fb = new FacebookClient();
    fb.HttpWebRequestFactory = uri=> new MyHttpWebRequestWrapper(uri); 
    

    Note: HttpWebRequestWrapper, HttpWebResponseWrapper, WebExceptionWrapper, FacebookClient.SetDefaultHttpWebRequestFactory and FacebookClient.HttpWebRequestFactory has the attribute [EditorBrowsable(EditorBrowsableState.Never)] so you might not see it in the intellisense.

    Things like no internet connection that you mention should actually be a part of facebook c# sdk tests and not your app unit tests. The sdk should guarantee that when there is not internet conenction it always throws WebExceptionWrapper and your app unit tests should actually be handling the WebExceptionWrapper exception and not mocking the entire httpwebrequest and httpwebresponse.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to write some code to trigger internet connectivity on a computer. By
need some help! I'm trying to write some code in objective-c that requires part-of-speech
We need to simulate an unstable network connection to try to debug some connectivity
Need some help about with Memcache. I have created a class and want to
Need some help assigning a mouseover event to display some icons that start out
I need a way to simulate connectivity problems in an automated test suite, on
Need some help on a script I am doing... what I want to accomplish
Need some advice on architecture. I've built a chess site, and want to add
Need some help, please. I have a line of horizontal thumbnails loaded as ONE
Need some help to solve this. I have a gridview and inside the gridview

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.