Skip to content

Commit

Permalink
Merge pull request #152 from alcionai/151-return-status-on-no-content
Browse files Browse the repository at this point in the history
Return error and status not content is empty
  • Loading branch information
baywet authored Feb 9, 2024
2 parents 1496924 + 5721034 commit 07ce92f
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 1 deletion.
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

0 comments on commit 07ce92f

Please sign in to comment.