public void ShouldExtractUrlEncodedData() { using (FakeHttpServer server = new FakeHttpServer()) { server.Host = "localhost"; server.Port = 8080; server.Path = "api/v1/accounts"; server.StatusCode = HttpStatusCode.OK; server.StatusDescription = "Success"; UrlEncodedBodyExtractor extractor = new UrlEncodedBodyExtractor(); server.UseBodyExtractor(extractor); server.Listen(); WebRequest request = WebRequest.Create("http://localhost:8080/api/v1/accounts"); request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; using (StreamWriter writer = new StreamWriter(request.GetRequestStream())) { writer.Write("name=bob&age=31"); } using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { NameValueCollection collection = extractor.Parameters; Assert.AreEqual("bob", collection.Get("name"), "Could not extract the name parameter."); Assert.AreEqual("31", collection.Get("age"), "Could not extract the age parameter."); } } }
public void ShouldPOSTWithFormDataObject() { using (FakeHttpServer server = new FakeHttpServer("http://localhost:8080/api/customers")) { var bodyExtractor = new UrlEncodedBodyExtractor(); var extractor = new RequestExtractor(bodyExtractor); server.UseBodyExtractor(extractor); server.Listen(); RestClient client = new RestClient(); var response = client.Post("http://localhost:8080/api/customers") .WithUrlEncodedBody(new { Name = "Bob Smith", Age = 31, Title = "Mr." }) .Execute(); string name = bodyExtractor.Parameters["Name"]; string age = bodyExtractor.Parameters["Age"]; string title = bodyExtractor.Parameters["Title"]; Assert.AreEqual("Bob Smith", name, "The name was not sent."); Assert.AreEqual("31", age, "The age was not sent."); Assert.AreEqual("Mr.", title, "The title was not sent."); } }
public void ShouldPOSTWithArrayFormData() { using (FakeHttpServer server = new FakeHttpServer("http://localhost:8080/api/customers")) { var bodyExtractor = new UrlEncodedBodyExtractor(); var extractor = new RequestExtractor(bodyExtractor); server.UseBodyExtractor(extractor); server.Listen(); RestClient client = new RestClient(); var response = client.Post("http://localhost:8080/api/customers") .WithUrlEncodedBody(b => b .WithParameter("CustomerId", 1) .WithParameter("CustomerId", 2) .WithParameter("CustomerId", 3)) .Execute(); string[] ids = bodyExtractor.Parameters.GetValues("CustomerId"); string[] expectedIds = new string[] { "1", "2", "3" }; CollectionAssert.AreEquivalent(expectedIds, ids, "The array of values were not sent."); } }