Skip to content

Instantly share code, notes, and snippets.

@timiles
Created November 15, 2012 15:51
Show Gist options
  • Select an option

  • Save timiles/4079321 to your computer and use it in GitHub Desktop.

Select an option

Save timiles/4079321 to your computer and use it in GitHub Desktop.

Revisions

  1. timiles revised this gist Nov 23, 2012. 1 changed file with 5 additions and 3 deletions.
    8 changes: 5 additions & 3 deletions MockWebProxyHelper.cs
    Original file line number Diff line number Diff line change
    @@ -27,10 +27,12 @@ public Response(string header = "HTTP/1.1 200 OK", string body = "", string cont
    public static Func<HttpMethods, string, Response> GetMockResponse = delegate { return new Response(); };
    public static Func<HttpMethods, string, bool> InterceptRequest = delegate { return true; };

    public static void SetUp()
    public static void SetUp(bool registerAsSystemProxy = false)
    {
    FiddlerApplication.Startup(7883, FiddlerCoreStartupFlags.None | FiddlerCoreStartupFlags.DecryptSSL);
    WebRequest.DefaultWebProxy = new WebProxy("localhost", 7883);
    const int port = 18833;
    FiddlerApplication.Startup(port, FiddlerCoreStartupFlags.DecryptSSL
    | (registerAsSystemProxy ? FiddlerCoreStartupFlags.RegisterAsSystemProxy : FiddlerCoreStartupFlags.None));
    WebRequest.DefaultWebProxy = new WebProxy("localhost", port);

    FiddlerApplication.BeforeRequest += BeforeRequest;
    }
  2. timiles revised this gist Nov 16, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion MockWebProxyHelper.cs
    Original file line number Diff line number Diff line change
    @@ -45,7 +45,7 @@ private static void BeforeRequest(Session session)

    var response = GetMockResponse(httpMethod, url);
    session.oResponse.headers = Parser.ParseResponse(response.Header);
    session.oResponse.headers.Add("Content-Type", "application/json");
    session.oResponse.headers.Add("Content-Type", response.ContentType);

    session.utilSetResponseBody(response.Body);
    }
  3. timiles revised this gist Nov 16, 2012. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion MockWebProxyHelper.cs
    Original file line number Diff line number Diff line change
    @@ -12,14 +12,16 @@ public enum HttpMethods

    public class Response
    {
    public Response(string header = "HTTP/1.1 200 OK", string body = "")
    public Response(string header = "HTTP/1.1 200 OK", string body = "", string contentType = "application/json")
    {
    Header = header;
    Body = body;
    ContentType = contentType;
    }

    public string Header { get; private set; }
    public string Body { get; private set; }
    public string ContentType { get; private set; }
    }

    public static Func<HttpMethods, string, Response> GetMockResponse = delegate { return new Response(); };
  4. timiles revised this gist Nov 16, 2012. 1 changed file with 11 additions and 5 deletions.
    16 changes: 11 additions & 5 deletions MockWebProxyHelper.cs
    Original file line number Diff line number Diff line change
    @@ -23,6 +23,7 @@ public Response(string header = "HTTP/1.1 200 OK", string body = "")
    }

    public static Func<HttpMethods, string, Response> GetMockResponse = delegate { return new Response(); };
    public static Func<HttpMethods, string, bool> InterceptRequest = delegate { return true; };

    public static void SetUp()
    {
    @@ -34,13 +35,18 @@ public static void SetUp()

    private static void BeforeRequest(Session session)
    {
    session.utilCreateResponseAndBypassServer();
    var httpMethod = GetHttpMethod(session);
    var url = session.url;
    if (InterceptRequest(httpMethod, url))
    {
    session.utilCreateResponseAndBypassServer();

    var response = GetMockResponse(GetHttpMethod(session), session.url);
    session.oResponse.headers = Parser.ParseResponse(response.Header);
    session.oResponse.headers.Add("Content-Type", "application/json");
    var response = GetMockResponse(httpMethod, url);
    session.oResponse.headers = Parser.ParseResponse(response.Header);
    session.oResponse.headers.Add("Content-Type", "application/json");

    session.utilSetResponseBody(response.Body);
    session.utilSetResponseBody(response.Body);
    }
    }

    private static HttpMethods GetHttpMethod(Session session)
  5. timiles created this gist Nov 15, 2012.
    59 changes: 59 additions & 0 deletions MockWebProxyHelper.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,59 @@
    using System;
    using System.Net;
    // http://www.fiddler2.com/fiddler/Core/
    using Fiddler;

    public static class MockWebProxyHelper
    {
    public enum HttpMethods
    {
    GET, POST, PUT, Unknown
    }

    public class Response
    {
    public Response(string header = "HTTP/1.1 200 OK", string body = "")
    {
    Header = header;
    Body = body;
    }

    public string Header { get; private set; }
    public string Body { get; private set; }
    }

    public static Func<HttpMethods, string, Response> GetMockResponse = delegate { return new Response(); };

    public static void SetUp()
    {
    FiddlerApplication.Startup(7883, FiddlerCoreStartupFlags.None | FiddlerCoreStartupFlags.DecryptSSL);
    WebRequest.DefaultWebProxy = new WebProxy("localhost", 7883);

    FiddlerApplication.BeforeRequest += BeforeRequest;
    }

    private static void BeforeRequest(Session session)
    {
    session.utilCreateResponseAndBypassServer();

    var response = GetMockResponse(GetHttpMethod(session), session.url);
    session.oResponse.headers = Parser.ParseResponse(response.Header);
    session.oResponse.headers.Add("Content-Type", "application/json");

    session.utilSetResponseBody(response.Body);
    }

    private static HttpMethods GetHttpMethod(Session session)
    {
    return session.HTTPMethodIs("GET") ? HttpMethods.GET
    : session.HTTPMethodIs("POST") ? HttpMethods.POST
    : session.HTTPMethodIs("PUT") ? HttpMethods.PUT : HttpMethods.Unknown;
    }

    public static void TearDown()
    {
    FiddlerApplication.BeforeRequest -= BeforeRequest;
    FiddlerApplication.oProxy.Detach();
    FiddlerApplication.Shutdown();
    }
    }