How to unit test a method with HttpWebRequest/Response dependencies

£可爱£侵袭症+ 提交于 2019-12-25 07:05:44

问题


Been trying to unit test this method but can't figure out how to do it.

public bool ValidateCaptcha(string captchaResponse, string captchaChallenge,
                            string hostip)
    {
        var strPrivateKey = _secConfiguration.CaptchaPrivateKey;
            var strParameters = "verify?privatekey=" + strPrivateKey +
                "&remoteip=" + hostip +
                "&challenge=" + captchaChallenge+
                "&response=" + captchaResponse;

            var url = CaptchaUrl + strParameters;
            var request = CreateHttpWebRequest(url);
            request.Proxy.Credentials = CredentialCache.DefaultCredentials;
            request.Method = "POST";
            request.ContentType = "text/html";
            request.ContentLength = 0;

            var response = GetHttpWebResponse(request);                
            var writer = response.GetResponseStream();                
            var reader = new StreamReader(writer);
            var responseFromServer = reader.ReadToEnd();

            var serverResponse = responseFromServer.Split('\n');
            return serverResponse[0] == "true";
    }

    private HttpWebResponse GetHttpWebResponse(HttpWebRequest request)
    {
        return (HttpWebResponse) request.GetResponse();
    }

    private HttpWebRequest CreateHttpWebRequest(string url)
    {
        return (HttpWebRequest) WebRequest.Create(url);
    }

I had planned to moq the dependencies and have a couple of wrap classes

public class WrapHttpWebRequest : IHttpWebRequest
{
    private readonly HttpWebRequest _request;

    public WrapHttpWebRequest(HttpWebRequest request)
    {
        _request = request;
    }

    public string Method
    {
        get { return _request.Method; }
        set { _request.Method = value; }
    }

    public IHttpWebResponse GetResponse()
    {
        return new WrapHttpWebResponse((HttpWebResponse)_request.GetResponse());
    }

}

and

public class WrapHttpWebResponse : IHttpWebResponse
{
    private WebResponse _response;

    public WrapHttpWebResponse(HttpWebResponse response)
    {
        _response = response;
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    private void Dispose(bool disposing)
    {
        if (disposing)
        {
            if (_response != null)
            {
                ((IDisposable)_response).Dispose();
                _response = null;
            }
        }
    }

    public Stream GetResponseStream()
    {
        return _response.GetResponseStream();
    }
}

But can't find a way to inject them. Any ideas how can I do this? Thanks.


回答1:


You could create testable version of your class under test. Make the method virtual and override it in the testable version.

protected virtual HttpWebRequest CreateHttpWebRequest(string url)
{
    return (HttpWebRequest)WebRequest.Create(url);
}

public class TesableClassUnderTest : ClassUnderTest
{
    public HttpWebRequest HttpWebRequestFake { get; set; }

    protected override HttpWebRequest CreateHttpWebRequest(string url)
    {
        if (HttpWebRequestFake != null)
            return HttpWebRequestFake;
        return base.CreateHttpWebRequest(url);
    }
} 

Then in test set the fake object to your own value:

[TestMethod]
public void test()
{
    TesableClassUnderTest cut = new TesableClassUnderTest();
    cut.HttpWebRequestFake = CreateFakeHttpWebRequest();
    cut.ValidateCaptcha(...)
    Assert...
}


来源:https://stackoverflow.com/questions/25746107/how-to-unit-test-a-method-with-httpwebrequest-response-dependencies

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!