WebRequest request = WebRequest.Create("http://example.com/login"); request.Method = "POST"; string postData = "username=johndoe&password=secret"; byte[] byteArray = Encoding.UTF8.GetBytes(postData); request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = byteArray.Length; Stream dataStream = request.GetRequestStream(); dataStream.Write(byteArray, 0, byteArray.Length); dataStream.Close(); WebResponse response = WebTest.Run(request); string responseBody = new StreamReader(response.GetResponseStream()).ReadToEnd(); Assert.Contains("Welcome, John Doe", responseBody);
WebRequest request = WebRequest.Create("http://example.com/api/users/123"); request.Method = "GET"; WebResponse response = WebTest.Run(request); string responseBody = new StreamReader(response.GetResponseStream()).ReadToEnd(); JObject user = JObject.Parse(responseBody); Assert.Equal("John Doe", user["name"]);In this example, we test a REST API by sending a GET request to retrieve information about a user with ID 123. We then use the WebTest Run method to execute the request and parse the JSON response to check if the user's name is correct. Package library: MonoTests.SystemWeb.Framework