8

As Richard Willis suggests in http://blog.salamandersoft.co.uk/index.php/2009/10/how-to-mock-httpwebrequest-when-unit-testing/ i'm trying to call a web request moking the behavior.

For that (I asking me if I'm messing something here) I implemented an IWebRequestCreate and extended a WebRequest and a WebResponse. (more details in link codes)

But now in my code I had a test that register (WebRequest.RegisterPrefix) a prefix:

[Test]
public void Test() {
     var some = File.ReadAllBytes(@"TestData\WebService\admrond_13jan2011_14jan2011.xml");
     WebRequest.RegisterPrefix("mockPrefix", new WebRequestCreateMock());
     WebRequestFake request = WebRequestCreateMock.CreateRequestFake(some);

     _remoteRepository.PopulateWithMeterData(_meter);
     ... (error in line before)

Then, I got this error: Invalid URI: The hostname could not be parsed.

But why? In my PopulateWithMeterData(Meter meter) I have this call:

     WebRequest request = WebRequest.Create(urlListMeteringData);
     WebResponse ws = request.GetResponse();

Some suggestion? Is interesting post my class implementations?


EDIT: as @Matthew ask:

public class WebRequestCreateMock : IWebRequestCreate {

    static WebRequest _nextRequest;
    static readonly object LockObject = new object();

    static public WebRequest NextRequest {
        get { return _nextRequest; }
        set {
            lock (LockObject) {
                _nextRequest = value;
            }
        }
    }

    public WebRequest Create(Uri uri) {
        return _nextRequest;
    }

    public static WebRequestFake CreateRequestFake(byte[] xmlStream) {
        WebRequestFake webRequestFake = new WebRequestFake(xmlStream);
        NextRequest = webRequestFake;
        return webRequestFake;
    }

}

public class WebRequestFake : WebRequest {
    MemoryStream requestStream = new MemoryStream();
    MemoryStream responseStream;

    public override string Method { get; set; }
    public override string ContentType { get; set; }
    public override long ContentLength { get; set; }

    public WebRequestFake(byte[] response) {
        responseStream = new MemoryStream(response);
    }

    public override Stream GetRequestStream() {
        return requestStream;
    }

    public override WebResponse GetResponse() {
        return new WebReponseFake(responseStream);
    }
}

public class WebReponseFake : WebResponse {

    private readonly Stream _responseStream;

    public WebReponseFake(Stream responseStream) {
        _responseStream = responseStream;
    }

    public override Stream GetResponseStream() {
        return _responseStream;
    }
}

And the Url is something like: mockPrefix://NoMatterUrl

2
  • I'd flesh out your code more, and give an example of the url you are using to create your request. Commented Feb 17, 2011 at 19:51
  • @Custodio I want to use rtmp link to pass it to HTTPHanlder for vlc plugin video target source. Can you help me how to use your code in reading live streams to vlc plugin player My question here Commented Feb 21, 2014 at 7:14

3 Answers 3

3

Since the error is "Invalid URI: The hostname could not be parsed." you are probably screwing up your Uri "mockPrefix://NoMatterUrl"

I had this problem once because I forgot to add a "/" between the domain uri and the request parameters.

Can you post exactly what your "NoMatterUri" looks like?

Sign up to request clarification or add additional context in comments.

1 Comment

Perfect! I was messing the url, debugging I got: mockPrefix://NoMatterUrlADM_ROND&datainicio=20090101000000&datafim=20090102000000. So I put an "/" after NoMatterUrl and everything works fine!
1

You need to register your prefix with a colon (':'); as in:

WebRequest.RegisterPrefix("mockPrefix:", new WebRequestCreateMock());

Comments

0

I have found that it's necessary to include a trailing "/" in the prefix. For instance, "test://localhost/":

[TestClass]
public class WebRequestTests
{
    [TestMethod]
    public void TestWebRequestCreate()
    {
        const string uriString = "test://localhost/foo/bar.baz?a=b&c=d";
        var webRequest = new MockWebRequestCreateAssertUrl(uriString);
        Assert.IsTrue(WebRequest.RegisterPrefix("test://localhost/", webRequest),
                      "Failed to register prefix");
        Assert.IsNotNull(WebRequest.Create(uriString));
    }

    public class MockWebRequestCreateAssertUrl : IWebRequestCreate
    {
        private readonly Uri _expectedUri;

        public MockWebRequestCreateAssertUrl(string uriString)
        {
            _expectedUri = new Uri(uriString);
        }

        public WebRequest Create(Uri uri)
        {
            Assert.AreEqual(_expectedUri, uri, "uri parameter is wrong");
            return new MockWebRequestAssertUrl();
        }
    }

    public class MockWebRequestAssertUrl : WebRequest {}
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.