Skip to content

Commit

Permalink
Adds support for XX status code
Browse files Browse the repository at this point in the history
  • Loading branch information
rkodev committed Jan 22, 2024
1 parent 7751d0c commit 764a6ea
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

## [1.2.0] - 2024-01-22

### Added

- Adds support for XXX status code.

## [1.1.1] - 2023-11-22

### Added
Expand Down
6 changes: 6 additions & 0 deletions internal/mock_parse_node_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ func (e *MockParseNode) GetCollectionOfEnumValues(parser absser.EnumFactory) ([]
return nil, nil
}
func (e *MockParseNode) GetObjectValue(ctor absser.ParsableFactory) (absser.Parsable, error) {
if ctor != nil {
_, err := ctor(e)
if err != nil {
return nil, err
}
}
return &MockEntity{}, nil
}
func (e *MockParseNode) GetStringValue() (*string, error) {
Expand Down
4 changes: 3 additions & 1 deletion nethttp_request_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,7 @@ 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()
body, err := ioutil.ReadAll(response.Body)
body, err := io.ReadAll(response.Body)
if err != nil {
spanForAttributes.RecordError(err)
return nil, ctx, err
Expand Down Expand Up @@ -754,6 +754,8 @@ func (a *NetHttpRequestAdapter) throwIfFailedResponse(ctx context.Context, respo
if len(errorMappings) != 0 {
if errorMappings[statusAsString] != nil {
errorCtor = errorMappings[statusAsString]
} else if errorMappings["XXX"] != nil && response.StatusCode >= 400 && response.StatusCode < 600 {
errorCtor = errorMappings["XXX"]
} else if response.StatusCode >= 400 && response.StatusCode < 500 && errorMappings["4XX"] != nil {
errorCtor = errorMappings["4XX"]
} else if response.StatusCode >= 500 && response.StatusCode < 600 && errorMappings["5XX"] != nil {
Expand Down
35 changes: 35 additions & 0 deletions nethttp_request_adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package nethttplibrary

import (
"context"
"github.com/microsoft/kiota-abstractions-go/serialization"
nethttp "net/http"
httptest "net/http/httptest"
"net/url"
Expand Down Expand Up @@ -75,6 +76,40 @@ func TestItThrowsApiError(t *testing.T) {
assert.Equal(t, "example-guid", apiError.ResponseHeaders.Get("client-request-id")[0])
}

func TestGenericError(t *testing.T) {
testServer := httptest.NewServer(nethttp.HandlerFunc(func(res nethttp.ResponseWriter, req *nethttp.Request) {
res.WriteHeader(500)
res.Write([]byte("test"))
}))
defer func() { testServer.Close() }()
authProvider := &absauth.AnonymousAuthenticationProvider{}
adapter, err := NewNetHttpRequestAdapterWithParseNodeFactory(authProvider, &internal.MockParseNodeFactory{})
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

result := 0
errorMapping := abs.ErrorMappings{
"XXX": func(parseNode serialization.ParseNode) (serialization.Parsable, error) {
result++
return nil, &abs.ApiError{
Message: "test XXX message",
}
},
}

_, err2 := adapter.SendPrimitive(context.TODO(), request, "[]byte", errorMapping)
assert.NotNil(t, err2)
assert.Equal(t, 1, result)
assert.Equal(t, "test XXX message", err2.Error())
}

func TestImplementationHonoursInterface(t *testing.T) {
authProvider := &absauth.AnonymousAuthenticationProvider{}
adapter, err := NewNetHttpRequestAdapter(authProvider)
Expand Down

0 comments on commit 764a6ea

Please sign in to comment.