diff --git a/CHANGELOG.md b/CHANGELOG.md index c42c762..fb9bbc3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed +## [1.3.1] - 2024-02-09 + +### Added + +### Changed + +- Fix bug that resulted in the error "content is empty" being returned instead of HTTP status information if the request returned no content and an unsuccessful status code. + ## [1.3.0] - 2024-01-22 ### Added diff --git a/nethttp_request_adapter.go b/nethttp_request_adapter.go index bfd0a0b..c140636 100644 --- a/nethttp_request_adapter.go +++ b/nethttp_request_adapter.go @@ -702,6 +702,11 @@ func (a *NetHttpRequestAdapter) SendNoContent(ctx context.Context, requestInfo * func (a *NetHttpRequestAdapter) getRootParseNode(ctx context.Context, response *nethttp.Response, spanForAttributes trace.Span) (absser.ParseNode, context.Context, error) { ctx, span := otel.GetTracerProvider().Tracer(a.observabilityOptions.GetTracerInstrumentationName()).Start(ctx, "getRootParseNode") defer span.End() + + if response.ContentLength == 0 { + return nil, ctx, nil + } + body, err := io.ReadAll(response.Body) if err != nil { spanForAttributes.RecordError(err) diff --git a/nethttp_request_adapter_test.go b/nethttp_request_adapter_test.go index 5ac0215..c8592e7 100644 --- a/nethttp_request_adapter_test.go +++ b/nethttp_request_adapter_test.go @@ -250,6 +250,35 @@ func TestSendReturnNilOnNoContent(t *testing.T) { } } +func TestSendReturnErrOnNoContent(t *testing.T) { + // Subset of status codes this applies to since there's many of them. This + // could be switched to ranges if full coverage is desired. + statusCodes := []int{nethttp.StatusBadRequest, nethttp.StatusInternalServerError} + + for _, code := range statusCodes { + testServer := httptest.NewServer(nethttp.HandlerFunc(func(res nethttp.ResponseWriter, req *nethttp.Request) { + res.WriteHeader(code) + })) + defer func() { testServer.Close() }() + + authProvider := &absauth.AnonymousAuthenticationProvider{} + adapter, err := NewNetHttpRequestAdapter(authProvider) + assert.Nil(t, err) + assert.NotNil(t, adapter) + + uri, err := url.Parse(testServer.URL) + assert.Nil(t, err) + assert.NotNil(t, uri) + request := abs.NewRequestInformation() + request.SetUri(*uri) + request.Method = abs.GET + + res, err2 := adapter.Send(context.TODO(), request, internal.MockEntityFactory, nil) + assert.Error(t, err2) + assert.Nil(t, res) + } +} + func TestSendReturnsObjectOnContent(t *testing.T) { statusCodes := []int{200, 201, 202, 203, 204, 205} diff --git a/user_agent_handler.go b/user_agent_handler.go index def0aed..552aca6 100644 --- a/user_agent_handler.go +++ b/user_agent_handler.go @@ -42,7 +42,7 @@ func NewUserAgentHandlerOptions() *UserAgentHandlerOptions { return &UserAgentHandlerOptions{ Enabled: true, ProductName: "kiota-go", - ProductVersion: "1.1.0", + ProductVersion: "1.3.1", } }