public void ShouldPostMultuPartWithBase64Content() { using (FakeHttpServer server = new FakeHttpServer("http://localhost:8080/api/customers")) { MultiPartBodyExtractor extractor = new MultiPartBodyExtractor(); server.UseBodyExtractor(extractor); server.Listen(); string message = "Hello, World!Ӽ!"; byte[] messageRaw = Encoding.UTF8.GetBytes(message); string base64 = Convert.ToBase64String(messageRaw); byte[] base64Raw = Encoding.UTF8.GetBytes(base64); NameValueCollection headers = new NameValueCollection(); headers.Add("Content-Transfer-Encoding", "base64"); RestClient client = new RestClient("http://localhost:8080"); client.Post("api/customers") .WithMultiPartBody(b => { b.WithFile("file", "file.txt", base64Raw, "text/plain", headers); }) .Execute(); var file = extractor.Files.GetFiles("file").Single(); CollectionAssert.AreEqual(base64Raw, file.Contents); } }
public void ShouldRespondWithStringBasedOnConfiguration() { using (FakeHttpServer server = new FakeHttpServer()) { server.Host = "localhost"; server.Port = 8080; server.Path = "api/v1/accounts"; server.ReturnString("Hello, World!!!"); server.StatusCode = HttpStatusCode.OK; server.StatusDescription = "Okey, Dokey"; server.Headers.Add("When", new DateTime(2014, 06, 16).ToString("MM/dd/yyyy")); server.Listen(); WebRequest request = WebRequest.Create("http://localhost:8080/api/v1/accounts"); using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { Assert.AreEqual(server.StatusCode, response.StatusCode, "The wrong status code was returned."); Assert.AreEqual(server.StatusDescription, response.StatusDescription, "The wrong status description was returned."); Assert.AreEqual("06/16/2014", response.Headers["When"], "The wrong header was returned"); string responseText = getResponseString(response); Assert.AreEqual("Hello, World!!!", responseText); } } }
public void ShouldRespondWithStringBasedOnConfiguration() { using (FakeHttpServer server = new FakeHttpServer()) { server.Host = "localhost"; server.Port = 8080; server.Path = "api/v1/accounts"; server.ReturnString("Hello, World!!!"); server.StatusCode = HttpStatusCode.OK; server.StatusDescription = "Okey, Dokey"; server.Headers.Add("When", new DateTime(2014, 06, 16).ToString("MM/dd/yyyy")); StringBodyExtractor extractor = new StringBodyExtractor(); server.UseBodyExtractor(extractor); server.Listen(); WebRequest request = WebRequest.Create("http://localhost:8080/api/v1/accounts"); request.Method = "POST"; using (StreamWriter writer = new StreamWriter(request.GetRequestStream())) { writer.Write("Hello, Universe!!!"); } using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { Assert.AreEqual(server.StatusCode, response.StatusCode, "The wrong status code was returned."); Assert.AreEqual(server.StatusDescription, response.StatusDescription, "The wrong status description was returned."); Assert.AreEqual("06/16/2014", response.Headers["When"], "The wrong header was returned"); string responseText = new StreamReader(response.GetResponseStream()).ReadToEnd(); Assert.AreEqual("Hello, World!!!", responseText); Assert.AreEqual("Hello, Universe!!!", extractor.Content); } } }
public async Task ShouldPOSTMultiPartDataAsync() { using (FakeHttpServer server = new FakeHttpServer("http://localhost:8080/api/customers")) { MultiPartBodyExtractor extractor = new MultiPartBodyExtractor(); server.UseBodyExtractor(extractor); server.Listen(); RestClient client = new RestClient("http://localhost:8080"); await client.Post("api/customers") .WithMultiPartBody(b => { b.WithFormData(ub => ub.WithParameter("name", "John Smith")); b.WithFile("file1", "path", Encoding.UTF8.GetBytes("Hello, world"), "text/plain"); }) .ExecuteAsync(); Assert.AreEqual("John Smith", extractor.Parameters["name"], "The form data was not transfered."); var file = extractor.Files.GetFiles("file1").SingleOrDefault(); Assert.AreEqual("file1", file.Name); Assert.AreEqual("path", file.FileName); Assert.AreEqual("text/plain", file.ContentType); Assert.AreEqual("Hello, world", Encoding.UTF8.GetString(file.Contents)); } }
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 ShouldGetInt32() { using (FakeHttpServer server = new FakeHttpServer("http://localhost:8080/numbers")) { server.ReturnString("4"); server.Listen(); RestClient client = new RestClient("http://localhost:8080"); var response = client.Get("numbers") .WhenSuccess(r => r.FromString <int>()) .Execute(); Assert.IsTrue(response.IsSuccessStatusCode, "An error occurred getting the number."); Assert.AreEqual(4, response.GetResult <int>()); } }
public void ShouldGETWithQueryParametersObject() { using (FakeHttpServer server = new FakeHttpServer("http://localhost:8080/api/customers")) { var extractor = new RequestExtractor(); server.UseBodyExtractor(extractor); server.Listen(); RestClient client = new RestClient(); var response = client.Get("http://localhost:8080/api/customers") .WithQueryParameters(new { customerid = 123 }) .Execute(); Assert.AreEqual("123", extractor.QueryString["customerid"], "The ID was not passed."); } }
public void ShouldGET() { using (FakeHttpServer server = new FakeHttpServer("http://localhost:8080/api/customers")) { server.StatusCode = HttpStatusCode.OK; server.Listen(); RestClient client = new RestClient(); var response = client.Get("http://localhost:8080/api/customers").Execute(); Assert.AreEqual(HttpStatusCode.OK, response.StatusCode, "The wrong status code was returned."); Assert.IsTrue(response.IsSuccessStatusCode, "A success code was returned. There should be no error."); Assert.IsNull(response.Result, "No WHEN handler was defined. The result should be null."); } }
public void ShouldGETWithCustomHeader() { using (FakeHttpServer server = new FakeHttpServer("http://localhost:8080/api/customers")) { var extractor = new RequestExtractor(); server.UseBodyExtractor(extractor); server.Listen(); RestClient client = new RestClient(); var response = client.Get("http://localhost:8080/api/customers") .WithHeader("test_header", "test_value") .Execute(); var value = extractor.Headers.Get("test_header"); Assert.AreEqual("test_value", value, "The header was not passed to the server."); } }
public void ShouldPOSTWithNoBody() { using (FakeHttpServer server = new FakeHttpServer("http://localhost:8080/api/customers")) { RequestExtractor extractor = new RequestExtractor(); server.UseBodyExtractor(extractor); server.Listen(); RestClient client = new RestClient(); var response = client.Post("http://localhost:8080/api/customers") .WhenError(r => { throw new Exception(r.FromString <string>()); }) .Execute(); string contentLength = extractor.Headers["Content-Length"]; Assert.AreEqual("0", contentLength, "The content length was not specified."); } }
public void ShouldGETWithSimpleTemplate() { using (FakeHttpServer server = new FakeHttpServer("http://localhost:8080/api/customers")) { var extractor = new RequestExtractor(); server.UseBodyExtractor(extractor); server.Listen(); RestClient client = new RestClient(); var response = client.Get("http://localhost:8080/api/customers/{customerId}", new { customerId = 123 }).Execute(); Assert.IsTrue(extractor.Url.ToString().EndsWith("123"), "The ID was not passed."); } }
public void ShouldExtractMultiPartFormData() { using (FakeHttpServer server = new FakeHttpServer()) { server.Host = "localhost"; server.Port = 8080; server.Path = "api/v1/accounts"; server.StatusCode = HttpStatusCode.OK; server.StatusDescription = "Success"; MultiPartBodyExtractor extractor = new MultiPartBodyExtractor(); server.UseBodyExtractor(extractor); server.Listen(); WebRequest request = WebRequest.Create("http://localhost:8080/api/v1/accounts"); request.Method = "POST"; request.ContentType = "multipart/form-data; boundary=taco"; using (StreamWriter writer = new StreamWriter(request.GetRequestStream())) { writer.Write("--taco\r\n"); writer.Write("Content-Disposition: form-data; name=name\r\n"); writer.Write("\r\n"); writer.Write("bob\r\n"); writer.Write("--taco\r\n"); writer.Write("Content-Disposition: form-data; name=file; filename=help.txt\r\n"); writer.Write("Content-Type: text/plain\r\n"); writer.Write("\r\n"); writer.Write("These are the contents of the file.\r\n"); writer.Write("--taco--\r\n"); } using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { NameValueCollection collection = extractor.Parameters; Assert.AreEqual("bob", collection.Get("name"), "Could not extract the name parameter."); var files = extractor.Files.GetFiles("file"); Assert.AreEqual(1, files.Count(), "The wrong number of files were extracted."); var file = files.First(); Assert.AreEqual("file", file.Name, "The name of the file part was not stored."); Assert.AreEqual("help.txt", file.FileName, "The file name was not stored."); Assert.AreEqual("text/plain", file.ContentType, "The file content type was not stored."); Assert.AreEqual("These are the contents of the file.", Encoding.Default.GetString(file.Contents), "The file contents were wrong."); } } }
public void ShouldPostMultuPartWithInvalidCharacter() { using (FakeHttpServer server = new FakeHttpServer("http://localhost:8080/api/customers")) { MultiPartBodyExtractor extractor = new MultiPartBodyExtractor(); server.UseBodyExtractor(extractor); server.Listen(); RestClient client = new RestClient("http://localhost:8080"); client.Post("api/customers") .WithMultiPartBody(b => { b.WithFormData(ub => ub.WithParameter("naӼme", "John Smith")); }) .Execute(); Assert.AreEqual("John Smith", extractor.Parameters["na?me"], "The form data was not transfered."); } }
public void ShouldPOSTWithJsonData() { using (FakeHttpServer server = new FakeHttpServer("http://localhost:8080/api/customers")) { var bodyExtractor = new JsonBodyExtractor <TestCustomer>(); server.UseBodyExtractor(bodyExtractor); server.Listen(); RestClient client = new RestClient(); var response = client.Post("http://localhost:8080/api/customers") .WithJsonBody(new TestCustomer() { Name = "Bob Smith", Age = 31, Title = "Mr." }) .Execute(); var customer = bodyExtractor.Result; Assert.AreEqual("Bob Smith", customer.Name, "The name was not sent."); Assert.AreEqual(31, customer.Age, "The age was not sent."); Assert.AreEqual("Mr.", customer.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."); } }
public void ShouldGetInt32() { using (FakeHttpServer server = new FakeHttpServer("http://localhost:8080/numbers")) { server.ReturnString("4"); server.Listen(); RestClient client = new RestClient("http://localhost:8080"); var response = client.Get("numbers") .WhenSuccess(r => r.FromString<int>()) .Execute(); Assert.IsTrue(response.IsSuccessStatusCode, "An error occurred getting the number."); Assert.AreEqual(4, response.GetResult<int>()); } }
public void ShouldPOSTWithNoBody() { using (FakeHttpServer server = new FakeHttpServer("http://localhost:8080/api/customers")) { RequestExtractor extractor = new RequestExtractor(); server.UseBodyExtractor(extractor); server.Listen(); RestClient client = new RestClient(); var response = client.Post("http://localhost:8080/api/customers") .WhenError(r => { throw new Exception(r.FromString<string>()); }) .Execute(); string contentLength = extractor.Headers["Content-Length"]; Assert.AreEqual("0", contentLength, "The content length was not specified."); } }
public void ShouldPOSTWithJsonData() { using (FakeHttpServer server = new FakeHttpServer("http://localhost:8080/api/customers")) { var bodyExtractor = new JsonBodyExtractor<TestCustomer>(); server.UseBodyExtractor(bodyExtractor); server.Listen(); RestClient client = new RestClient(); var response = client.Post("http://localhost:8080/api/customers") .WithJsonBody(new TestCustomer() { Name = "Bob Smith", Age = 31, Title = "Mr." }) .Execute(); var customer = bodyExtractor.Result; Assert.AreEqual("Bob Smith", customer.Name, "The name was not sent."); Assert.AreEqual(31, customer.Age, "The age was not sent."); Assert.AreEqual("Mr.", customer.Title, "The title was not sent."); } }