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

Return error and status not content is empty #152

Merged
merged 2 commits into from
Feb 9, 2024
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
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions nethttp_request_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
29 changes: 29 additions & 0 deletions nethttp_request_adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}

Expand Down
2 changes: 1 addition & 1 deletion user_agent_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func NewUserAgentHandlerOptions() *UserAgentHandlerOptions {
return &UserAgentHandlerOptions{
Enabled: true,
ProductName: "kiota-go",
ProductVersion: "1.1.0",
ProductVersion: "1.3.1",
}
}

Expand Down
Loading