Skip to content

Commit

Permalink
fix: do not fail on empty location header
Browse files Browse the repository at this point in the history
  • Loading branch information
baywet committed Dec 18, 2024
1 parent 3bc8cbd commit 809f804
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/http/httpClient/HttpClientRequestAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ private static bool shouldReturnNull(HttpResponseMessage response)
private async Task ThrowIfFailedResponseAsync(HttpResponseMessage response, Dictionary<string, ParsableFactory<IParsable>>? errorMapping, Activity? activityForAttributes, CancellationToken cancellationToken)
{
using var span = activitySource?.StartActivity(nameof(ThrowIfFailedResponseAsync));
if(response.IsSuccessStatusCode) return;
if(response.IsSuccessStatusCode || response.StatusCode is HttpStatusCode.Moved or HttpStatusCode.MovedPermanently or HttpStatusCode.Found or HttpStatusCode.Redirect) return;

activityForAttributes?.SetStatus(ActivityStatusCode.Error, "received_error_response");

Expand Down
41 changes: 33 additions & 8 deletions tests/http/httpClient/RequestAdapterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ namespace Microsoft.Kiota.Http.HttpClientLibrary.Tests
public class HttpClientRequestAdapterTests
{
private readonly IAuthenticationProvider _authenticationProvider;
private readonly HttpClientRequestAdapter requestAdapter;
private readonly HttpClientRequestAdapter _requestAdapter;

public HttpClientRequestAdapterTests()
{
_authenticationProvider = new Mock<IAuthenticationProvider>().Object;
requestAdapter = new HttpClientRequestAdapter(new AnonymousAuthenticationProvider());
_requestAdapter = new HttpClientRequestAdapter(new AnonymousAuthenticationProvider());
}

[Fact]
Expand Down Expand Up @@ -82,15 +82,15 @@ public void EnablesBackingStore()
public async Task GetRequestMessageFromRequestInformationWithBaseUrlTemplate()
{
// Arrange
requestAdapter.BaseUrl = "http://localhost";
_requestAdapter.BaseUrl = "http://localhost";
var requestInfo = new RequestInformation
{
HttpMethod = Method.GET,
UrlTemplate = "{+baseurl}/me"
};

// Act
var requestMessage = await requestAdapter.ConvertToNativeRequestAsync<HttpRequestMessage>(requestInfo);
var requestMessage = await _requestAdapter.ConvertToNativeRequestAsync<HttpRequestMessage>(requestInfo);

// Assert
Assert.NotNull(requestMessage);
Expand All @@ -113,10 +113,10 @@ public async Task GetRequestMessageFromRequestInformationUsesBaseUrlFromAdapter(

};
// Change the baseUrl of the adapter
requestAdapter.BaseUrl = "http://localhost";
_requestAdapter.BaseUrl = "http://localhost";

// Act
var requestMessage = await requestAdapter.ConvertToNativeRequestAsync<HttpRequestMessage>(requestInfo);
var requestMessage = await _requestAdapter.ConvertToNativeRequestAsync<HttpRequestMessage>(requestInfo);

// Assert
Assert.NotNull(requestMessage);
Expand All @@ -140,7 +140,7 @@ public async Task GetRequestMessageFromRequestInformationSetsQueryParametersCorr
requestInfo.QueryParameters.Add(queryParam, queryParamObject!);

// Act
var requestMessage = await requestAdapter.ConvertToNativeRequestAsync<HttpRequestMessage>(requestInfo);
var requestMessage = await _requestAdapter.ConvertToNativeRequestAsync<HttpRequestMessage>(requestInfo);

// Assert
Assert.NotNull(requestMessage);
Expand All @@ -162,7 +162,7 @@ public async Task GetRequestMessageFromRequestInformationSetsContentHeaders()
requestInfo.SetStreamContent(new MemoryStream(Encoding.UTF8.GetBytes("contents")), "application/octet-stream");

// Act
var requestMessage = await requestAdapter.ConvertToNativeRequestAsync<HttpRequestMessage>(requestInfo);
var requestMessage = await _requestAdapter.ConvertToNativeRequestAsync<HttpRequestMessage>(requestInfo);

// Assert
Assert.NotNull(requestMessage);
Expand Down Expand Up @@ -207,6 +207,31 @@ public async Task SendMethodDoesNotThrowWithoutUrlTemplate()
Assert.Equal(4, response.Length);
}

[InlineData(HttpStatusCode.Redirect)]
[InlineData(HttpStatusCode.MovedPermanently)]
[Theory]
public async Task SendMethodDoesNotThrowOn3XXWithNoLocationAsync(HttpStatusCode httpStatusCode)
{
var mockHandler = new Mock<HttpMessageHandler>();
var client = new HttpClient(mockHandler.Object);
mockHandler.Protected()
.Setup<Task<HttpResponseMessage>>("SendAsync", ItExpr.IsAny<HttpRequestMessage>(), ItExpr.IsAny<CancellationToken>())
.ReturnsAsync(new HttpResponseMessage
{
StatusCode = httpStatusCode
});
var adapter = new HttpClientRequestAdapter(_authenticationProvider, httpClient: client);
var requestInfo = new RequestInformation
{
HttpMethod = Method.GET,
URI = new Uri("https://example.com")
};

var response = await adapter.SendAsync(requestInfo, MockEntity.Factory);

Assert.Null(response);
}

[InlineData(HttpStatusCode.OK)]
[InlineData(HttpStatusCode.Created)]
[InlineData(HttpStatusCode.Accepted)]
Expand Down

0 comments on commit 809f804

Please sign in to comment.