public void When_Request_Method_Is_POST_Then_Request_Body_Contains_Encoded_Parameters() { string body = "Foo=12345&Bar=abcde"; var sourcebytes = Encoding.UTF8.GetBytes(body); client = new RestClient(); client.BaseUrl = BASE_URL; var request = new RestRequest(); request.Method = "POST"; request.AddParameter("Foo", "12345"); request.AddParameter("Bar", "abcde"); var wrapper = new HttpWebRequestWrapper(); wrapper.Request = new FakeHttpWebRequest(); var webrequest = (FakeHttpWebRequest)wrapper.ConfigureRequest(client, request); var stream = webrequest.GetRequestStream(); stream.Position = 0; byte[] actualbytes = stream.ReadAsBytes(); Assert.AreEqual(sourcebytes.Length, webrequest.ContentLength); Assert.AreEqual(sourcebytes.Length, actualbytes.Length); CollectionAssert.AreEquivalent(sourcebytes, actualbytes); }
public async Task When_Http_Protocol_Error_Then_Response_Contains_Status_Code_And_Description() { client = new RestClient(); client.BaseUrl = BASE_URL; client.MessageHandler = new FakeHttpMessageHandler(HttpStatusCode.BadRequest); var request = new RestRequest(); var restresponse = await client.ExecuteAsync<RestRequest>(request); Assert.AreEqual(HttpStatusCode.BadRequest, restresponse.StatusCode); Assert.AreEqual("Bad Request", restresponse.StatusDescription); Assert.AreEqual(ResponseStatus.Completed, restresponse.ResponseStatus); }
public void When_Response_With_Invalid_Json_Needs_Deserialization_Then_Deserialize_To_Type() { var sourcecontent = "{ \"firstName\":'', \"lastName\":\"Doe\"}"; var sourcebytes = Encoding.UTF8.GetBytes(sourcecontent); var client = new RestClient(); var restrequest = new RestRequest(); var restresponse = new RestResponse(); restresponse.ResponseStatus = ResponseStatus.Completed; restresponse.RawBytes = sourcebytes; var actualuser = client.Deserialize<User>(restrequest, restresponse); Assert.IsNotNull(actualuser.ErrorException); }
public void When_A_Default_Header_Is_Present_Then_WebRequest_Includes_This_Header() { string token = AuthorizationToken; client = new RestClient(); client.BaseUrl = BASE_URL; client.DefaultParameters.Add(new Parameter() { Name = "Authorization", Value = token, Type = ParameterType.HttpHeader }); var request = new RestRequest(); var wrapper = new HttpWebRequestWrapper(); var webrequest = wrapper.ConfigureRequest(client, request); Assert.IsTrue(webrequest.Headers.AllKeys.Contains("Authorization")); Assert.AreEqual(token, webrequest.Headers["Authorization"]); }
public async Task When_Http_Request_Times_Out_Then_Populate_Exception_Properties() { var message = "A task was canceled."; client = new RestClient(); client.BaseUrl = BASE_URL; client.MessageHandler = new FakeHttpMessageHandler(new TaskCanceledException()); client.Timeout = 1; var request = new RestRequest(); var restresponse = await client.ExecuteAsync<RestRequest>(request); Assert.IsNotNull(restresponse.ErrorException); Assert.AreEqual(message, restresponse.ErrorMessage); Assert.AreEqual(ResponseStatus.Error, restresponse.ResponseStatus); }
public async Task When_Http_Protocol_Error_Then_Response_Contains_Content() { var sourcecontent = "{\"code\": 90011, \"message\": \"Param From must be specified.\", \"more_info\": \"https://www.twilio.com/docs/errors/90011\", \"status\": 400}"; var sourcebytes = Encoding.ASCII.GetBytes(sourcecontent); client = new RestClient(); client.BaseUrl = BASE_URL; client.MessageHandler = new FakeHttpMessageHandler(HttpStatusCode.BadRequest, sourcecontent); var request = new RestRequest(); var restresponse = await client.ExecuteAsync<RestRequest>(request); Assert.AreEqual(HttpStatusCode.BadRequest, restresponse.StatusCode); Assert.AreEqual((int)sourcecontent.Length, restresponse.RawBytes.Length); CollectionAssert.AreEquivalent(sourcebytes, restresponse.RawBytes); Assert.AreEqual(ResponseStatus.Completed, restresponse.ResponseStatus); }
public async Task When_Http_Request_Is_Canceled_Then_Populate_Exception_Properties() { var message = "A task was canceled."; client = new RestClient(); client.BaseUrl = BASE_URL; client.MessageHandler = new FakeHttpMessageHandler(new TaskCanceledException()); var request = new RestRequest(); var mCancellationTokenSource = new CancellationTokenSource(); var restresponse = await client.ExecuteAsync(request, mCancellationTokenSource.Token); mCancellationTokenSource.Cancel(); Assert.IsNotNull(restresponse.ErrorException); Assert.AreEqual(message, restresponse.ErrorMessage); Assert.AreEqual(ResponseStatus.Error, restresponse.ResponseStatus); }
public HttpWebRequest ConfigureRequest(RestClient client, RestRequest restrequest) { foreach (var param in client.DefaultParameters) { if (!restrequest.Parameters.Any(p => p.Name == param.Name)) { restrequest.Parameters.Add(param); } } var webrequest = HttpWebRequestFactory(Simple.UriBuilder.Build(client.BaseUrl, restrequest)); webrequest.Timeout = client.Timeout; if (client.Proxy != null) { webrequest.Proxy = client.Proxy; } webrequest.Headers.Add("Accept-charset", "utf-8"); webrequest.UserAgent = client.UserAgent; webrequest.Method = restrequest.Method; webrequest.Accept = "application/json"; webrequest.KeepAlive = true; foreach (var param in restrequest.Parameters.Where(p => p.Type == ParameterType.HttpHeader)) { webrequest.Headers.Add(param.Name, param.Value.ToString()); } if (restrequest.Method == "POST" || restrequest.Method == "PUT") { webrequest.ContentType = "application/x-www-form-urlencoded"; var querystring = Utilities.EncodeParameters(restrequest.Parameters); var bytes = Encoding.UTF8.GetBytes(querystring.ToString()); webrequest.ContentLength = bytes.Length; //TODO: Fails without network connection / DNS resolution fails var requestStream = webrequest.GetRequestStream(); requestStream.Write(bytes, 0, bytes.Length); } return webrequest; }
public void Setup() { client = new RestClient(); client.BaseUrl = BASE_URL; }
public async Task When_Http_Request_Completes_Successfully_Then_Extract_Response() { var sourcecontent = "{\"sid\": \"SMb2628b9fb5992e2f117891601451084b\", \"date_created\": \"Thu, 03 Apr 2014 02:11:55 +0000\", \"date_updated\": \"Thu, 03 Apr 2014 02:11:58 +0000\", \"date_sent\": \"Thu, 03 Apr 2014 02:11:58 +0000\", \"account_sid\": \"AC3137d76457814a5eabf7de62f346d39a\", \"to\": \"+13144586142\", \"from\": \"+19108638087\", \"body\": \"Enter '1234' to confirm your identity and access your account.\", \"status\": \"delivered\", \"num_segments\": \"1\", \"num_media\": null, \"direction\": \"outbound-api\", \"api_version\": \"2010-04-01\", \"price\": \"-0.00750\", \"price_unit\": \"USD\", \"uri\": \"/2010-04-01/Accounts/AC3137d76457814a5eabf7de62f346d39a/Messages/SMb2628b9fb5992e2f117891601451084b.json\", \"subresource_uris\": {\"media\": \"/2010-04-01/Accounts/AC3137d76457814a5eabf7de62f346d39a/Messages/SMb2628b9fb5992e2f117891601451084b/Media.json\"}}"; var sourcebytes = Encoding.ASCII.GetBytes(sourcecontent); client = new RestClient(); client.BaseUrl = BASE_URL; client.MessageHandler = new FakeHttpMessageHandler(HttpStatusCode.OK, sourcecontent); var request = new RestRequest(); var restresponse = await client.ExecuteAsync<RestRequest>(request); Assert.AreEqual(HttpStatusCode.OK, restresponse.StatusCode); Assert.AreEqual(sourcebytes.Length, restresponse.RawBytes.Length); CollectionAssert.AreEquivalent(sourcebytes, restresponse.RawBytes); Assert.AreEqual(ResponseStatus.Completed, restresponse.ResponseStatus); }