Created
December 15, 2011 12:23
-
-
Save RobertWithP/1480917 to your computer and use it in GitHub Desktop.
Testdriven Wrapper for HttpWebRequest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Text; | |
using System.Collections.Generic; | |
using System.Linq; | |
using Microsoft.VisualStudio.TestTools.UnitTesting; | |
using System.Net; | |
using System.IO; | |
using Moq; | |
using System.Runtime.Serialization; | |
namespace Tests.HttpWebRequestWrapper | |
{ | |
[TestClass] | |
public class HttpWebRequestWrapperTest | |
{ | |
[TestMethod] | |
public void MockedWebRequestOnGoogleDeShouldBeResponseNoGoogle() | |
{ | |
var mockWebResponse = new Mock<IHttpWebResponseWrapper>(); | |
mockWebResponse.Setup(c => c.StatusCode).Returns(HttpStatusCode.OK); | |
mockWebResponse.Setup(c => c.GetContent()).Returns("NO GOOGLE"); | |
var mockWebRequest = new Mock<IHttpWebRequestWrapper>(); | |
mockWebRequest.SetupSet<string>(c => c.Method = "GET").Verifiable(); | |
mockWebRequest.Setup(s => s.GetResponse()).Returns(mockWebResponse.Object); | |
var wrapper = mockWebRequest.Object; | |
wrapper.Method = "GET"; | |
Assert.IsTrue(GetContentAndAssertForHttpStatusOK(wrapper).Contains("NO GOOGLE")); | |
} | |
[TestMethod] | |
public void NotMockedWebRequestOnGoogleDeShouldBeResponseGoogleDeSourcen() | |
{ | |
var wrapper = new HttpWebRequestWrapper(new Uri("http://www.google.de/")); | |
Assert.IsTrue(GetContentAndAssertForHttpStatusOK(wrapper).Contains("<title>Google</title>")); | |
} | |
/// <summary> | |
/// Gets the content and assert for HTTP status OK. | |
/// </summary> | |
/// <param name="wrapper">The wrapper.</param> | |
/// <returns></returns> | |
private string GetContentAndAssertForHttpStatusOK(IHttpWebRequestWrapper wrapper) | |
{ | |
wrapper.Method = "GET"; | |
var content = wrapper.GetResponse().GetContent(); | |
Assert.AreEqual(HttpStatusCode.OK, wrapper.GetResponse().StatusCode); | |
return content; | |
} | |
} | |
public class HttpWebRequestWrapper : IHttpWebRequestWrapper | |
{ | |
private HttpWebRequest httpWebRequest; | |
/// <summary> | |
/// Initializes a new instance of the <see cref="HttpWebRequestWrapper"/> class. | |
/// </summary> | |
/// <param name="url">The URL.</param> | |
public HttpWebRequestWrapper(Uri url) | |
{ | |
this.httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url); | |
} | |
/// <summary> | |
/// Gets the request stream to write data. | |
/// </summary> | |
/// <returns></returns> | |
public Stream GetRequestStream() | |
{ | |
return this.httpWebRequest.GetRequestStream(); | |
} | |
/// <summary> | |
/// Gets or sets the method. | |
/// </summary> | |
/// <value>The method.</value> | |
public string Method | |
{ | |
get { return this.httpWebRequest.Method; } | |
set { this.httpWebRequest.Method = value; } | |
} | |
/// <summary> | |
/// Gets or sets the type of the content. | |
/// </summary> | |
/// <value>The type of the content.</value> | |
public string ContentType | |
{ | |
get { return this.httpWebRequest.ContentType; } | |
set { this.httpWebRequest.ContentType = value; } | |
} | |
/// <summary> | |
/// Gets the response. | |
/// </summary> | |
/// <returns></returns> | |
public IHttpWebResponseWrapper GetResponse() | |
{ | |
return new HttpWebResponseWrapper(this.httpWebRequest.GetResponse()); | |
} | |
} | |
public class HttpWebResponseWrapper : IHttpWebResponseWrapper | |
{ | |
private WebResponse webResponse; | |
/// <summary> | |
/// Initializes a new instance of the <see cref="HttpWebResponseWrapper"/> class. | |
/// </summary> | |
/// <param name="webResponse">The web response.</param> | |
public HttpWebResponseWrapper(WebResponse webResponse) | |
{ | |
this.webResponse = webResponse; | |
} | |
/// <summary> | |
/// Gets the status code. | |
/// </summary> | |
/// <value>The status code.</value> | |
public HttpStatusCode StatusCode | |
{ | |
get { return ((HttpWebResponse)webResponse).StatusCode; } | |
} | |
/// <summary> | |
/// Gets the content. | |
/// </summary> | |
/// <returns></returns> | |
public string GetContent() | |
{ | |
using (StreamReader stream = new StreamReader(webResponse.GetResponseStream())) | |
{ | |
return stream.ReadToEnd(); | |
} | |
} | |
} | |
public interface IHttpWebRequestWrapper | |
{ | |
string ContentType { get; set; } | |
Stream GetRequestStream(); | |
IHttpWebResponseWrapper GetResponse(); | |
string Method { get; set; } | |
} | |
public interface IHttpWebResponseWrapper | |
{ | |
string GetContent(); | |
HttpStatusCode StatusCode { get; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for sharing. How about the actual code? Do I need to use the same wrapper in the actual code as well ?