Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow API redirect responses #542

Merged
merged 1 commit into from
Sep 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Twilio/Clients/TwilioRestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ private static Response ProcessResponse(Response response)
throw new ApiConnectionException("Connection Error: No response received.");
}

if (response.StatusCode >= HttpStatusCode.OK && response.StatusCode < HttpStatusCode.Ambiguous)
if (response.StatusCode >= HttpStatusCode.OK && response.StatusCode < HttpStatusCode.BadRequest)
eshanholtz marked this conversation as resolved.
Show resolved Hide resolved
{
return response;
}
Expand Down
1 change: 1 addition & 0 deletions src/Twilio/Http/Net35/WebRequestWrappers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public class HttpWebRequestWrapper : IHttpWebRequestWrapper
public HttpWebRequestWrapper(Uri uri)
{
this._httpWebRequest = (HttpWebRequest) WebRequest.Create(uri);
this._httpWebRequest.AllowAutoRedirect=false;
}

public string UserAgent
Expand Down
5 changes: 3 additions & 2 deletions src/Twilio/Http/SystemNetHttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class SystemNetHttpClient : HttpClient
/// <param name="httpClient">HTTP client to use</param>
public SystemNetHttpClient(System.Net.Http.HttpClient httpClient = null)
{
_httpClient = httpClient ?? new System.Net.Http.HttpClient();
_httpClient = httpClient ?? new System.Net.Http.HttpClient(new HttpClientHandler() { AllowAutoRedirect = false });
}

/// <summary>
Expand Down Expand Up @@ -94,7 +94,8 @@ private HttpRequestMessage BuildHttpRequest(Request request)
var libraryVersion = "twilio-csharp/" + AssemblyInfomation.AssemblyInformationalVersion + PlatVersion;
httpRequest.Headers.TryAddWithoutValidation("User-Agent", libraryVersion);

foreach (var header in request.HeaderParams) {
foreach (var header in request.HeaderParams)
{
httpRequest.Headers.TryAddWithoutValidation(header.Key, header.Value);
}

Expand Down
40 changes: 31 additions & 9 deletions test/Twilio.Test/Clients/TwilioRestClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,20 @@ public void TestCantConnect()
// Exception type doesn't matter, just needs to match in IsInstanceOf below.
client.MakeRequest(Arg.Any<Request>()).Throws(new InvalidOperationException());

try {
try
{
TwilioRestClient.ValidateSslCertificate(client);
Assert.Fail("Should have failed ssl verification");
} catch (CertificateValidationException e) {
}
catch (CertificateValidationException e)
{
Assert.IsInstanceOf(typeof(InvalidOperationException), e.GetBaseException());
Assert.AreEqual("Connection to api.twilio.com:8443 failed", e.Message);
Assert.IsNull(e.Response);
Assert.IsNotNull(e.Request);
} catch (Exception) {
}
catch (Exception)
{
Assert.Fail("Threw an unknown exception");
}
}
Expand All @@ -52,14 +57,19 @@ public void TestNotOkResponse()
{
client.MakeRequest(Arg.Any<Request>()).Returns(new Response(HttpStatusCode.SwitchingProtocols, "NOTOK"));

try {
try
{
TwilioRestClient.ValidateSslCertificate(client);
Assert.Fail("Should have failed ssl verification");
} catch (CertificateValidationException e) {
}
catch (CertificateValidationException e)
{
Assert.AreEqual("Unexpected response from certificate endpoint", e.Message);
Assert.IsNotNull(e.Response);
Assert.IsNotNull(e.Request);
} catch (Exception) {
}
catch (Exception)
{
Assert.Fail("Threw an unknown exception");
}
}
Expand All @@ -76,12 +86,15 @@ public void TestBadResponseWithDetails()
""foo"": ""bar""
}}";
client.MakeRequest(Arg.Any<Request>()).Returns(new Response(HttpStatusCode.BadRequest, jsonResponse));
try {
Request request = new Request(HttpMethod.Get,"https://www.contoso.com");
try
{
Request request = new Request(HttpMethod.Get, "https://www.contoso.com");
TwilioRestClient twilioClient = new TwilioRestClient("foo", "bar", null, null, client);
twilioClient.Request(request);
Assert.Fail("Should have failed");
} catch (ApiException e) {
}
catch (ApiException e)
{
Assert.AreEqual("Bad request", e.Message);
Assert.AreEqual(20001, e.Code);
Assert.AreEqual("https://www.twilio.com/docs/errors/20001", e.MoreInfo);
Expand All @@ -91,5 +104,14 @@ public void TestBadResponseWithDetails()
Assert.AreEqual(expectedDetails, e.Details);
}
}

[Test]
public void TestRedirectResponse()
{
client.MakeRequest(Arg.Any<Request>()).Returns(new Response(HttpStatusCode.RedirectKeepVerb, "REDIRECT"));
Request request = new Request(HttpMethod.Get, "https://www.contoso.com");
TwilioRestClient twilioClient = new TwilioRestClient("foo", "bar", null, null, client);
twilioClient.Request(request);
}
}
}