Created
June 10, 2014 18:14
-
-
Save benerdin/ed6014d80ab021eb436d to your computer and use it in GitHub Desktop.
RestSharp and ServiceStack nUnit tests for POSTing form data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [TestFixture] | |
| class RestSharpIntegration | |
| { | |
| [Test] | |
| public void EnsureFormIsPosted() | |
| { | |
| // Arrange | |
| var request = CreateRequest(); | |
| // Act | |
| var response = _client.Execute(request); | |
| // Assert | |
| Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK)); | |
| } | |
| private RestRequest CreateRequest() | |
| { | |
| var request = new RestRequest("endpoint", Method.POST); | |
| // TODO: Populate request object | |
| return request; | |
| } | |
| private const string baseUri = "http://todo/api"; | |
| private static readonly IRestClient _client = new RestClient(baseUri) | |
| { | |
| Proxy = new System.Net.WebProxy("localhost", 8888), // Fiddler Proxy | |
| FollowRedirects = false, | |
| Authenticator = new HttpBasicAuthenticator("username", "password") | |
| }; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [TestFixture] | |
| class ServiceStackIntegration | |
| { | |
| [Test] | |
| public void EnsureFormIsPosted() | |
| { | |
| // Arrange | |
| var formData = GenericFactory.GetOfficeVpSignupModel(); | |
| // Act | |
| // "PostToUrl" is an extension method available via | |
| // the ServiceStack namespace. | |
| var response = baseUri.PostToUrl(formData, accept: "text/html"); | |
| // Assert | |
| Assert.Fail("Need to find a way to assert on the response. response is just a string."); | |
| } | |
| [Test] | |
| public void EnsureFormIsPostedViaJson() | |
| { | |
| // Arrange | |
| // Act | |
| var response = _client.Post( | |
| new { input1 = "value1", input2 = "value2" } | |
| ); | |
| // Assert | |
| Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK)); | |
| } | |
| private const string baseUri = "http://todo/api"; | |
| private static readonly IRestClient _client = new JsonServiceClient(baseUri) | |
| { | |
| Proxy = new System.Net.WebProxy("localhost", 8888), // Fiddler Proxy | |
| AllowAutoRedirect = false, | |
| UserName = "username", | |
| Password = "password" | |
| }; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment