/// <summary> /// Requires the incoming HTTP request to contain the specified text in the body. /// </summary> /// <param name="stump">The <see cref="Stump"/> intercepting incoming HTTP requests.</param> /// <param name="text">The text that must be contained within the body.</param> /// <returns>The calling <see cref="Stump"/>.</returns> /// <exception cref="ArgumentNullException"><paramref name="stump"/> is <c>null</c>.</exception> public static Stump MatchingBodyContaining(this Stump stump, string[] text) { stump = stump ?? throw new ArgumentNullException(nameof(stump)); stump.AddRule(new BodyContentRule(text)); return(stump); }
/// <summary> /// Requires the incoming HTTP request to match the specified HTTP method. /// </summary> /// <param name="stump">The <see cref="Stump"/> intercepting incoming HTTP requests.</param> /// <param name="httpMethod">The HTTP method to match.</param> /// <returns>The calling <see cref="Stump"/>.</returns> /// <exception cref="ArgumentNullException"><paramref name="stump"/> is <c>null</c>.</exception> public static Stump MatchingMethod(this Stump stump, string httpMethod) { stump = stump ?? throw new ArgumentNullException(nameof(stump)); stump.AddRule(new HttpMethodRule(httpMethod)); return(stump); }
public void AddRule_WithRule_AddedToCollection() { var stump = new Stump("ABC"); stump.AddRule(Substitute.For <IStumpRule>()); Assert.AreEqual(1, stump.Count); }
public void IsMatch_WithFailingRule_TriesAllRulesReturnsFalse() { var stump = new Stump("ABC"); var context = Substitute.For <IStumpsHttpContext>(); var request = Substitute.For <IStumpsHttpRequest>(); context.Request.Returns(request); var rule1 = Substitute.For <IStumpRule>(); rule1.IsMatch(request).Returns(true); var rule2 = Substitute.For <IStumpRule>(); rule2.IsMatch(request).Returns(false); stump.AddRule(rule1); stump.AddRule(rule2); stump.Responds(); var matches = stump.IsMatch(context); rule1.Received(1).IsMatch(request); rule2.Received(1).IsMatch(request); Assert.IsFalse(matches); }
/// <summary> /// Requires the incoming HTTP request to match the specified <see cref="IStumpRule"/>. /// </summary> /// <param name="stump">The <see cref="Stump"/> intercepting incoming HTTP requests.</param> /// <param name="rule">The <see cref="IStumpRule"/> required to match.</param> /// <returns>The calling <see cref="Stump"/>.</returns> /// <exception cref="ArgumentNullException"><paramref name="stump"/> is <c>null</c>.</exception> public static Stump MatchingRule(this Stump stump, IStumpRule rule) { stump = stump ?? throw new ArgumentNullException(nameof(stump)); stump.AddRule(rule); return(stump); }
public static Stump MatchingUrl(this Stump stump, string url) { stump = stump ?? throw new ArgumentNullException(nameof(stump)); stump.AddRule(new UrlRule(url)); return(stump); }
/// <summary> /// Requires the incoming HTTP request to match the specified header. /// </summary> /// <param name="stump">The <see cref="Stump"/> intercepting incoming HTTP requests.</param> /// <param name="headerName">The name of the header to match.</param> /// <param name="headerValue">The value of the header to match.</param> /// <returns>The calling <see cref="Stump"/>.</returns> /// <exception cref="ArgumentNullException"><paramref name="stump"/> is <c>null</c>.</exception> public static Stump MatchingHeader(this Stump stump, string headerName, string headerValue) { stump = stump ?? throw new ArgumentNullException(nameof(stump)); stump.AddRule(new HeaderRule(headerName, headerValue)); return(stump); }
public void IsMatch_WithoutRules_ReturnsFalse() { var stump = new Stump("ABC"); stump.Responds(); Assert.IsFalse(stump.IsMatch(Substitute.For <IStumpsHttpContext>())); }
/// <summary> /// Requires the incoming HTTP request to contain the specified text in the body. /// </summary> /// <param name="stump">The <see cref="Stump"/> intercepting incoming HTTP requests.</param> /// <param name="text">The text that must be contained within the body.</param> /// <returns>The calling <see cref="Stump"/>.</returns> /// <exception cref="ArgumentNullException"><paramref name="stump"/> is <c>null</c>.</exception> public static Stump MatchingBodyContaining(this Stump stump, string text) { stump = stump ?? throw new ArgumentNullException(nameof(stump)); var textArray = new[] { text }; var stumpResponse = stump.MatchingBodyContaining(textArray); return(stumpResponse); }
/// <summary> /// Requires the incoming HTTP request to match the specified body. /// </summary> /// <param name="stump">The <see cref="Stump"/> intercepting incoming HTTP requests.</param> /// <param name="buffer">The array of bytes for the body.</param> /// <returns>The calling <see cref="Stump"/>.</returns> /// <exception cref="ArgumentNullException"><paramref name="stump"/> is <c>null</c>.</exception> public static Stump MatchingBody(this Stump stump, byte[] buffer) { stump = stump ?? throw new ArgumentNullException(nameof(stump)); if (buffer != null) { stump.AddRule(new BodyMatchRule(buffer.Length, CreateMd5Hash(buffer))); } return(stump); }
/// <summary> /// Respondswithes the multiple. /// </summary> /// <param name="stump">The <see cref="Stump"/> intercepting incoming HTTP requests.</param> /// <param name="behavior">The behavior of the <see cref="StumpResponseFactory.CreateResponse(IStumpsHttpRequest)"/> method when retrieving the next <see cref="IStumpsHttpResponse"/>.</param> /// <returns>The calling <see cref="Stump"/>.</returns> /// <exception cref="ArgumentNullException"><paramref name="stump"/> is <c>null</c>.</exception> public static Stump ReturnsMultipleResponses(this Stump stump, ResponseFactoryBehavior behavior) { stump = stump ?? throw new ArgumentNullException(nameof(stump)); if (stump.Responses == null || stump.Responses.Behavior != behavior) { stump.Responses = new StumpResponseFactory(behavior); } return(stump); }
public void ResposeFactory_GetSet_ReturnsResponseFactory() { var responseFactory = new StumpResponseFactory(); var stump = new Stump("ABC") { Responses = responseFactory }; Assert.AreEqual(responseFactory, stump.Responses); }
/// <summary> /// Asserts that the <see cref="Stump"/> will respond with a <see cref="BasicHttpResponse"/>. /// </summary> /// <param name="stump">The <see cref="Stump"/> intercepting incoming HTTP requests.</param> /// <returns>A <see cref="BasicHttpResponse"/> created for the <paramref name="stump"/>.</returns> /// <exception cref="ArgumentNullException"><paramref name="stump"/> is <c>null</c>.</exception> public static BasicHttpResponse Responds(this Stump stump) { stump = stump ?? throw new ArgumentNullException(nameof(stump)); stump.Responses = stump.Responses ?? new StumpResponseFactory(); var response = new BasicHttpResponse(); stump.Responses.Add(response); return(response); }
public void IsMatch_WithNullResponse_ReturnsFalse() { var stump = new Stump("ABC"); var rule1 = Substitute.For <IStumpRule>(); rule1.IsMatch(null).Returns(true); stump.AddRule(rule1); Assert.IsFalse(stump.IsMatch(Substitute.For <IStumpsHttpContext>())); }
/// <summary> /// Defines the entry point of the application. /// </summary> /// <param name="args">The command-line arguments.</param> public static void Main(string[] args) { ConsoleHelper.ApplicationBanner("Hello World API"); // Create a new Stumps Server var server = new StumpsServer(); // An open port will be chosen automatically unless specified // server.ListensOnPort = 9100; // Create a new Stump for the server var stump = new Stump("HelloWorldStump"); // Add two rules that stumps out HTTP GET requests for the url /HeloWorld.htm stump.AddRule(new HttpMethodRule("GET")); stump.AddRule(new UrlRule("/HelloWorld.htm")); // Create a response for the rule var response = new BasicHttpResponse(); response.Headers["Content-Type"] = "text/html;charset=UTF-8"; response.AppendToBody( "<html><header><title>Stumps Hello World</title></header><body><p>Hello From Stumps</p></body></html>"); // Add the response to the stump stump.Response = response; // Add the stump to the server server.AddStump(stump); // Show the requests that are incomming server.RequestProcessed += (o, e) => ConsoleHelper.ShowHttpResponse(server, e); // Start the server and wait! server.Start(); // Show the URL to the user Console.WriteLine("Browse to http://localhost:{0}/HelloWorld.htm", server.ListeningPort); Console.WriteLine(); // Wait to exit ConsoleHelper.WaitForExit(); server.Shutdown(); server.Dispose(); }
public void IsMatch_WithResponseFactoryNoResponse_ReturnsFalse() { var stump = new Stump("ABC"); var context = Substitute.For <IStumpsHttpContext>(); var request = Substitute.For <IStumpsHttpRequest>(); context.Request.Returns(request); var rule1 = Substitute.For <IStumpRule>(); rule1.IsMatch(request).Returns(true); var matches = stump.IsMatch(context); rule1.DidNotReceive().IsMatch(request); Assert.IsFalse(matches); }
/// <summary> /// Adds a new <see cref="T:Stumps.Stump" /> to the collection. /// </summary> /// <param name="stump">The <see cref="T:Stumps.Stump" /> to add to the collection.</param> /// <exception cref="System.ArgumentNullException"><paramref name="stump"/> is <c>null</c>.</exception> /// <exception cref="System.ArgumentException">A <see cref="T:Stumps.Stump" /> with the same identifier already exists.</exception> public void AddStump(Stump stump) { if (stump == null) { throw new ArgumentNullException("stump"); } if (_stumpReference.ContainsKey(stump.StumpId)) { throw new ArgumentException(BaseResources.StumpAlreadyExistsError, "stump"); } _lock.EnterWriteLock(); _stumpList.Add(stump); _stumpReference.Add(stump.StumpId, stump); _lock.ExitWriteLock(); }
public void IsMatch_WithMultipleMatchingRules_TriesAllRulesReturnsTrue() { var stump = new Stump("ABC"); var context = Substitute.For<IStumpsHttpContext>(); var request = Substitute.For<IStumpsHttpRequest>(); context.Request.Returns(request); var rule1 = Substitute.For<IStumpRule>(); rule1.IsMatch(request).Returns(true); var rule2 = Substitute.For<IStumpRule>(); rule2.IsMatch(request).Returns(true); stump.AddRule(rule1); stump.AddRule(rule2); stump.Response = new BasicHttpResponse(); var matches = stump.IsMatch(context); rule1.Received(1).IsMatch(request); rule2.Received(1).IsMatch(request); Assert.IsTrue(matches); }
public void Constuctor_WithValidId_UpdatesProperty() { var stump = new Stump("ABC"); Assert.AreEqual("ABC", stump.StumpId); }
/// <summary> /// Populates the response of the HTTP context from the Stump. /// </summary> /// <param name="incommingHttpContext">The incomming HTTP context.</param> /// <param name="stump">The <see cref="T:Stumps.Stump"/> used to populate the response.</param> private void PopulateResponse(IStumpsHttpContext incommingHttpContext, Stump stump) { // Write the status code information incommingHttpContext.Response.StatusCode = stump.Response.StatusCode; incommingHttpContext.Response.StatusDescription = stump.Response.StatusDescription; // Write the headers incommingHttpContext.Response.Headers.Clear(); stump.Response.Headers.CopyTo(incommingHttpContext.Response.Headers); // Write the body incommingHttpContext.Response.ClearBody(); if (stump.Response.BodyLength > 0) { var buffer = stump.Response.GetBody(); if (stump.Response.Headers["Content-Encoding"] != null) { var encoder = new ContentEncoder(stump.Response.Headers["Content-Encoding"]); buffer = encoder.Encode(buffer); } incommingHttpContext.Response.AppendToBody(buffer); } }
public void AddRule_WithRule_AddedToCollection() { var stump = new Stump("ABC"); stump.AddRule(Substitute.For<IStumpRule>()); Assert.AreEqual(1, stump.Count); }
/// <summary> /// Adds a new <see cref="T:Stumps.Stump" /> with a specified identifier to the collection. /// </summary> /// <param name="stumpId">The unique identifier for the <see cref="T:Stumps.Stump" />.</param> /// <returns>A new <see cref="T:Stumps.Stump"/> with the specified <paramref name="stumpId"/>.</returns> /// <exception cref="System.ArgumentNullException"><paramref name="stumpId"/> is <c>null</c>.</exception> /// <exception cref="System.ArgumentException">A <see cref="T:Stumps.Stump" /> with the same identifier already exists.</exception> public Stump AddNewStump(string stumpId) { var stump = new Stump(stumpId); _stumpsManager.AddStump(stump); return stump; }
/// <summary> /// Adds a new <see cref="T:Stumps.Stump" /> to the collection. /// </summary> /// <param name="stump">The <see cref="T:Stumps.Stump" /> to add to the collection.</param> /// <exception cref="System.ArgumentNullException"><paramref name="stump"/> is <c>null</c>.</exception> /// <exception cref="System.ArgumentException">A <see cref="T:Stumps.Stump" /> with the same identifier already exists.</exception> public void AddStump(Stump stump) { _stumpsManager.AddStump(stump); }
public void IsMatch_WithoutRules_ReturnsFalse() { var stump = new Stump("ABC"); stump.Response = new BasicHttpResponse(); Assert.IsFalse(stump.IsMatch(Substitute.For<IStumpsHttpContext>())); }
public void Respose_GetSet_ReturnsResponse() { var response = new BasicHttpResponse(); var stump = new Stump("ABC"); stump.Response = response; Assert.AreEqual(response, stump.Response); }
public void IsMatch_WithNullResponse_ReturnsFalse() { var stump = new Stump("ABC"); var rule1 = Substitute.For<IStumpRule>(); rule1.IsMatch(null).Returns(true); stump.AddRule(rule1); Assert.IsFalse(stump.IsMatch(Substitute.For<IStumpsHttpContext>())); }