hibri / HttpMock

A library for creating Http servers on the fly in tests and stubbing responses
MIT License
128 stars 44 forks source link

Removed log4net dependency #70

Closed zhdusurfin closed 7 years ago

hibri commented 7 years ago

Hi @zhdusurfin, Could you please fix the line endings, so that I can read the PR. The build fails as well. Please provide a bit more explanation in the PR as well Thanks

zhdusurfin commented 7 years ago

Hi @hibri! I fixed line endings and build errors. Here is a little details on PR: In case to remove from Log4Net dependency I refactored HttpMock by adding new ILog interface and LogFactory class, By default it implements to void logger - no logs write out. For users whom needs log4net I added a HttpMock.Log4NetLogger project (package).

Example to use it:

public void Test()
{
    Log4NetFactory.UseLog4Net();

    // test code here...
}

So it also need to be published to Nuget.

hibri commented 7 years ago

Thank you @zhdusurfin 👍 I'll review and publish this in the next couple of days.

zhdusurfin commented 7 years ago

@hibri I've rebased and fixed branch as you requested.

hibri commented 7 years ago

Could you replace the Log4Net configuration here, and show how to use the new library. It will also act as an example on how to use the logging library.

zhdusurfin commented 7 years ago

So this is an example to use a HttpMock.Logging.Log4Net:

using System;
using System.Net;
using HttpMock;
using HttpMock.Logging.Log4Net;
using log4net.Config;
using NUnit.Framework;

namespace HttpMockExample
{
    public class WebClientTests
    {
        [Test]
        public void DownloadStringTest()
        {
            // Set up a simple configuration that logs on the console.
            BasicConfigurator.Configure();

            // Register log4net within HttpMock.
            Log4NetFactory.UseLog4Net();

            // Test
            using (var server = new HttpServer(new Uri("http://localhost:9000")))
            {
                server.Start();
                server.Stub(x => x.Get("/hello")).Return("hello world").OK();

                var c = new WebClient();
                var result = c.DownloadString("http://localhost:9000/hello");

                Assert.AreEqual(result, "hello world");
            }
        }
    }
}

It will produce something like this:

1254 [27] DEBUG HttpMock.RequestProcessor (null) - Start Processing request for : GET:/hello
1296 [27] DEBUG HttpMock.RequestProcessor (null) - Matched a handler GET:/hello 
1298 [27] DEBUG HttpMock.RequestProcessor (null) - End Processing request for : GET:/hello
zhdusurfin commented 7 years ago

@hibri done

hibri commented 7 years ago

Thanks @zhdusurfin I've merged these changes. Thank you very much