Skip to content

Commit

Permalink
Merge pull request #498 from microsoft/fix/redirect-with-no-location
Browse files Browse the repository at this point in the history
fix/redirect with no location
  • Loading branch information
baywet authored Dec 19, 2024
2 parents 3bc8cbd + c637e06 commit cfe9df9
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

- Fixed a bug where the client would fail on 301/302 responses with no location headers. [#272](https://github.com/microsoft/kiota-dotnet/issues/272)

## [1.16.0] - 2024-12-13

### Added
Expand Down
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 cfe9df9

Please sign in to comment.