public void TestSuccessWhenHeadersDefined() { var headers = new NameValueCollection { { "X-Header-Name", "Header_Value" } }; using (var httpMock = HttpMocks.New(DefaultMockUrl)) { httpMock .WhenRequestGet("/") .Headers(headers) .ThenResponse(200); } var url = BuildUrl(DefaultMockUrl, "/"); var responseA = Send(url, "GET"); responseA.StatusCode.ShouldBeEquivalentTo(500); var responseB = Send(url, "GET", headers: headers); responseB.StatusCode.ShouldBeEquivalentTo(200); HttpMocks.Invoking(m => m.VerifyAll()) .ShouldThrowExactly <AssertHttpMockException>() .WithMessage("Actual request GET /, but not expected."); }
public void TestSuccessWhenQueryDefined() { var query = new NameValueCollection { { "qp", "qv" } }; using (var httpMock = HttpMocks.New(DefaultMockUrl)) { httpMock .WhenRequestGet("/") .Query(query) .ThenResponse(200); } var responseA = Send(BuildUrl(DefaultMockUrl, "/"), "GET"); responseA.StatusCode.ShouldBeEquivalentTo(500); var responseB = Send(BuildUrl(DefaultMockUrl, "/", query), "GET"); responseB.StatusCode.ShouldBeEquivalentTo(200); HttpMocks.Invoking(m => m.VerifyAll()) .ShouldThrowExactly <AssertHttpMockException>() .WithMessage("Actual request GET /, but not expected."); }
public void TestSuccess() { IHttpMock httpMock1; using (httpMock1 = HttpMocks.New("localhost", 3465)) { httpMock1 .WhenRequestGet("/bills/1") .ThenResponse(200); } IHttpMock httpMock2; using (httpMock2 = HttpMocks.New("localhost", 3466)) { httpMock2 .WhenRequestGet("/bills/2") .ThenResponse(200); } Send(BuildUrl(httpMock1.MockUri, "/bills/1"), "GET").StatusCode.ShouldBeEquivalentTo(200); Send(BuildUrl(httpMock2.MockUri, "/bills/2"), "GET").StatusCode.ShouldBeEquivalentTo(200); HttpMocks.VerifyAll(); }
public void TestSuccessWhenResponseFromAsyncDelegate() { var paths = new List <string>(); Func <HttpRequest, Task <HttpResponse> > processRequestInfoAsync = request => { paths.Add(request.Path); return(Task.FromResult(HttpResponse.Create(200))); }; using (var httpMock = HttpMocks.New(DefaultMockUrl)) { httpMock .WhenRequestGet("/bills/@guid") .ThenResponse(i => processRequestInfoAsync(i)); } var guid = Guid.NewGuid(); var url = BuildUrl(DefaultMockUrl, $"/bills/{guid}"); var response = Send(url, "GET"); response.StatusCode.ShouldBeEquivalentTo(200); response.ContentBytes.Length.ShouldBeEquivalentTo(0); paths.Count.ShouldBeEquivalentTo(1); paths[0].ShouldBeEquivalentTo($"/bills/{guid}"); HttpMocks.VerifyAll(); }
public void TestSuccess() { IHttpMock httpMock1; using (httpMock1 = HttpMocks.NewCluster("localhost", 2)) { httpMock1 .WhenRequestGet("/bills/1") .ThenResponse(200); } Send(BuildUrl(httpMock1.MockUri, "/bills/1"), "GET").StatusCode.ShouldBeEquivalentTo(200); }
public void SetUp() { var routes = new RouteCollection(); routes.Clear(); var routeData = new RouteData(); RegisterRoutesTo(routes); var requestContext = new RequestContext(HttpMocks.HttpContext(), routeData); _urlHelper = new UrlHelper(requestContext, routes); }
public void TestNotActualWhenRepeatCountMoreActualCount() { IHttpMock httpMock1; using (httpMock1 = HttpMocks.New("localhost", 3465)) { httpMock1 .WhenRequestGet("/bills/1") .ThenResponse(200) .Repeat(2); } Send(BuildUrl(httpMock1.MockUri, "/bills/1"), "GET").StatusCode.ShouldBeEquivalentTo(200); HttpMocks.Invoking(x => x.VerifyAll()).ShouldThrow <AssertHttpMockException>(); }
public void TestSuccessWhenGetReturn302() { using (var httpMock = HttpMocks.New(DefaultMockUrl)) { httpMock .WhenRequestGet("/bills") .ThenResponse(302); } var url = BuildUrl(DefaultMockUrl, "/bills"); var response = Send(url, "GET"); response.StatusCode.ShouldBeEquivalentTo(302); response.ContentBytes.Length.ShouldBeEquivalentTo(0); HttpMocks.VerifyAll(); }
public void TestFailWhenAnyActualRepeatMoreThatExpected() { using (var httpMock = HttpMocks.New(DefaultMockUrl)) { httpMock .WhenRequestGet("/bills") .ThenResponse(200) .RepeatAny(); } var url = BuildUrl(DefaultMockUrl, "/bills"); Send(url, "GET").StatusCode.ShouldBeEquivalentTo(200); Send(url, "GET").StatusCode.ShouldBeEquivalentTo(200); HttpMocks.VerifyAll(); }
public void TestFailWhenActualIsNotExpectedRequest() { using (var httpMock = HttpMocks.New(DefaultMockUrl)) { httpMock .WhenRequestGet("/bills/@guid") .ThenResponse(302); } var url = BuildUrl(DefaultMockUrl, "/bills"); var response = Send(url, "GET"); response.StatusCode.ShouldBeEquivalentTo(500); response.ContentBytes.Length.ShouldBeEquivalentTo(0); HttpMocks .Invoking(m => m.VerifyAll()) .ShouldThrowExactly <AssertHttpMockException>(); }
public void TestFailWhenDefaultActualRepeatMoreThatExpected() { using (var httpMock = HttpMocks.New(DefaultMockUrl)) { httpMock .WhenRequestGet("/bills") .ThenResponse(200); } var url = BuildUrl(DefaultMockUrl, "/bills"); Send(url, "GET").StatusCode.ShouldBeEquivalentTo(200); Send(url, "GET").StatusCode.ShouldBeEquivalentTo(500); HttpMocks.Invoking(m => m.VerifyAll()) .ShouldThrowExactly <AssertHttpMockException>() .WithMessage("Actual request GET /bills repeat count 2, but max expected repeat count 1."); }
public void TestSuccessThenGetReturn302() { var postContentBytes = new byte[100]; const string contentType = "application/text"; using (var httpMock = HttpMocks.New(DefaultMockUrl)) { httpMock .WhenRequestPost("/bills") .Content(postContentBytes, contentType) .ThenResponse(302); } var url = BuildUrl(DefaultMockUrl, "/bills"); var response = Send(url, "POST", postContentBytes, contentType); response.StatusCode.ShouldBeEquivalentTo(302); response.ContentBytes.Length.ShouldBeEquivalentTo(0); }
public void TestSuccessWhenGetReturn200AndResult() { const string testDataString = "Test data"; var content = Encoding.UTF8.GetBytes(testDataString); using (var httpMock = HttpMocks.New(DefaultMockUrl)) { httpMock .WhenRequestGet("/bills") .ThenResponse(200) .Content(content, "text/plain"); } var url = BuildUrl(DefaultMockUrl, "/bills"); var response = Send(url, "GET"); response.StatusCode.ShouldBeEquivalentTo(200); response.ContentBytes.Length.ShouldBeEquivalentTo(content.Length); Encoding.UTF8.GetString(response.ContentBytes).ShouldBeEquivalentTo(testDataString); HttpMocks.VerifyAll(); }