-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into BCF-2612-ChainReader-Next
- Loading branch information
Showing
12 changed files
with
364 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package eautils | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
) | ||
|
||
type AdapterStatus struct { | ||
ErrorMessage *string `json:"errorMessage"` | ||
Error any `json:"error"` | ||
StatusCode *int `json:"statusCode"` | ||
ProviderStatusCode *int `json:"providerStatusCode"` | ||
} | ||
|
||
func BestEffortExtractEAStatus(responseBytes []byte) (code int, ok bool) { | ||
var status AdapterStatus | ||
err := json.Unmarshal(responseBytes, &status) | ||
if err != nil { | ||
return 0, false | ||
} | ||
|
||
if status.StatusCode == nil { | ||
return 0, false | ||
} | ||
|
||
if *status.StatusCode != http.StatusOK { | ||
return *status.StatusCode, true | ||
} | ||
|
||
if status.ProviderStatusCode != nil && *status.ProviderStatusCode != http.StatusOK { | ||
return *status.ProviderStatusCode, true | ||
} | ||
|
||
if status.Error != nil { | ||
return http.StatusInternalServerError, true | ||
} | ||
|
||
return *status.StatusCode, true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package eautils | ||
|
||
import ( | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestBestEffortExtractEAStatus(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
arg []byte | ||
expectCode int | ||
expectOk bool | ||
}{ | ||
{ | ||
name: "invalid object", | ||
arg: []byte(`{"error": "invalid json object" `), | ||
expectCode: 0, | ||
expectOk: false, | ||
}, | ||
{ | ||
name: "no status code in object", | ||
arg: []byte(`{}`), | ||
expectCode: 0, | ||
expectOk: false, | ||
}, | ||
{ | ||
name: "invalid status code", | ||
arg: []byte(`{"statusCode":400}`), | ||
expectCode: http.StatusBadRequest, | ||
expectOk: true, | ||
}, | ||
{ | ||
name: "invalid provider status code", | ||
arg: []byte(`{"statusCode":200, "providerStatusCode":500}`), | ||
expectCode: http.StatusInternalServerError, | ||
expectOk: true, | ||
}, | ||
{ | ||
name: "valid statuses with error message", | ||
arg: []byte(`{"statusCode":200, "providerStatusCode":200, "error": "unexpected error"}`), | ||
expectCode: http.StatusInternalServerError, | ||
expectOk: true, | ||
}, | ||
{ | ||
name: "valid status code", | ||
arg: []byte(`{"statusCode":200}`), | ||
expectCode: http.StatusOK, | ||
expectOk: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
code, ok := BestEffortExtractEAStatus(tt.arg) | ||
assert.Equal(t, tt.expectCode, code) | ||
assert.Equal(t, tt.expectOk, ok) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.