|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using Xunit; |
| 6 | +using RestSharp.IntegrationTests.Helpers; |
| 7 | +using System.Net; |
| 8 | +using System.Threading; |
| 9 | + |
| 10 | +namespace RestSharp.IntegrationTests |
| 11 | +{ |
| 12 | + public class NonProtocolExceptionHandlingTests |
| 13 | + { |
| 14 | + |
| 15 | + /// <summary> |
| 16 | + /// Success of this test is based largely on the behavior of your current DNS. |
| 17 | + /// For example, if you're using OpenDNS this will test will fail; ResponseStatus will be Completed. |
| 18 | + /// </summary> |
| 19 | + [Fact] |
| 20 | + public void Handles_Non_Existent_Domain() |
| 21 | + { |
| 22 | + var client = new RestClient("http://nonexistantdomainimguessing.org"); |
| 23 | + var request = new RestRequest("foo"); |
| 24 | + var response = client.Execute(request); |
| 25 | + |
| 26 | + Assert.Equal(ResponseStatus.Error, response.ResponseStatus); |
| 27 | + } |
| 28 | + |
| 29 | + /// <summary> |
| 30 | + /// Tests that RestSharp properly handles a non-protocol error. |
| 31 | + /// Simulates a server timeout, then verifies that the ErrorException |
| 32 | + /// property is correctly populated. |
| 33 | + /// </summary> |
| 34 | + [Fact] |
| 35 | + public void Handles_Server_Timeout_Error() |
| 36 | + { |
| 37 | + const string baseUrl = "http://localhost:8080/"; |
| 38 | + using (SimpleServer.Create(baseUrl, TimeoutHandler)) |
| 39 | + { |
| 40 | + var client = new RestClient(baseUrl); |
| 41 | + var request = new RestRequest("404"); |
| 42 | + var response = client.Execute(request); |
| 43 | + |
| 44 | + Assert.NotNull(response.ErrorException); |
| 45 | + Assert.IsAssignableFrom(typeof(WebException), response.ErrorException); |
| 46 | + Assert.Equal(response.ErrorException.Message, "The operation has timed out"); |
| 47 | + |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + [Fact] |
| 52 | + public void Handles_Server_Timeout_Error_Async() |
| 53 | + { |
| 54 | + const string baseUrl = "http://localhost:8080/"; |
| 55 | + var resetEvent = new ManualResetEvent(false); |
| 56 | + |
| 57 | + using (SimpleServer.Create(baseUrl, TimeoutHandler)) |
| 58 | + { |
| 59 | + var client = new RestClient(baseUrl); |
| 60 | + var request = new RestRequest("404"); |
| 61 | + client.ExecuteAsync(request, response => { |
| 62 | + |
| 63 | + Assert.NotNull(response.ErrorException); |
| 64 | + Assert.IsAssignableFrom(typeof(WebException), response.ErrorException); |
| 65 | + Assert.Equal(response.ErrorException.Message, "The operation has timed out"); |
| 66 | + resetEvent.Set(); |
| 67 | + }); |
| 68 | + resetEvent.WaitOne(); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + /// <summary> |
| 73 | + /// Tests that RestSharp properly handles a non-protocol error. |
| 74 | + /// Simulates a server timeout, then verifies that the ErrorException |
| 75 | + /// property is correctly populated. |
| 76 | + /// </summary> |
| 77 | + [Fact] |
| 78 | + public void Handles_Server_Timeout_Error_With_Deserializer() |
| 79 | + { |
| 80 | + const string baseUrl = "http://localhost:8080/"; |
| 81 | + using (SimpleServer.Create(baseUrl, TimeoutHandler)) |
| 82 | + { |
| 83 | + var client = new RestClient(baseUrl); |
| 84 | + var request = new RestRequest("404"); |
| 85 | + var response = client.Execute<Response>(request); |
| 86 | + |
| 87 | + Assert.Null(response.Data); |
| 88 | + Assert.NotNull(response.ErrorException); |
| 89 | + Assert.IsAssignableFrom(typeof(WebException), response.ErrorException); |
| 90 | + Assert.Equal(response.ErrorException.Message, "The operation has timed out"); |
| 91 | + |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + |
| 96 | + /// <summary> |
| 97 | + /// Simulates a long server process that should result in a client timeout |
| 98 | + /// </summary> |
| 99 | + /// <param name="context"></param> |
| 100 | + public static void TimeoutHandler(HttpListenerContext context) |
| 101 | + { |
| 102 | + System.Threading.Thread.Sleep(101000); |
| 103 | + } |
| 104 | + |
| 105 | + |
| 106 | + } |
| 107 | +} |
0 commit comments