-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
return formated error struct for status code 400
Signed-off-by: Leslie Wang <[email protected]>
- Loading branch information
1 parent
a6bfe4a
commit 3c9421a
Showing
3 changed files
with
141 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// | ||
// SPDX-License-Identifier: BSD-3-Clause | ||
// | ||
|
||
package gofish | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/stmcginnis/gofish/common" | ||
) | ||
|
||
const ( | ||
errMsg = `{ | ||
"code": "Base.1.0.GeneralError", | ||
"message": "A general error has occurred. See ExtendedInfo for more information.", | ||
"@Message.ExtendedInfo": [ | ||
{ | ||
"MessageId": "Base.1.0.PropertyValueNotInList", | ||
"Message": "The value Red for the property IndicatorLED is not in the list of acceptable values", | ||
"MessageArgs": [ | ||
"RED", | ||
"IndicatorLED" | ||
], | ||
"Severity": "Warning", | ||
"Resolution": "Remove the property from the request body and resubmit the request if the operation failed" | ||
}, | ||
{ | ||
"MessageId": "Base.1.0.PropertyNotWriteable", | ||
"Message": "The property SKU is a read only property and cannot be assigned a value", | ||
"MessageArgs": [ | ||
"SKU" | ||
], | ||
"Severity": "Warning", | ||
"Resolution": "Remove the property from the request body and resubmit the request if the operation failed" | ||
} | ||
] | ||
}` | ||
expectErrorStatus400 = `{"error": ` + errMsg + "}" | ||
expectErrorStatus404 = `404: {"error": ` + errMsg + "}" | ||
) | ||
|
||
// TestError400 tests the parsing of error reply. | ||
func TestError400(t *testing.T) { | ||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(400) | ||
w.Write([]byte(expectErrorStatus400)) | ||
})) | ||
defer ts.Close() | ||
|
||
_, err := Connect(ClientConfig{Endpoint: ts.URL, HTTPClient: ts.Client()}) | ||
if err == nil { | ||
t.Error("Update call should fail") | ||
} | ||
errStruct, ok := err.(*common.Error) | ||
if !ok { | ||
t.Errorf("400 should return known error type: %v", err) | ||
} | ||
errBody, err := json.MarshalIndent(errStruct, " ", " ") | ||
if err != nil { | ||
t.Errorf("Marshall error %v got: %s", errStruct, err) | ||
} | ||
if errMsg != string(errBody) { | ||
t.Errorf("Expect:\n%s\nGot:\n%s", errMsg, string(errBody)) | ||
} | ||
} | ||
|
||
// TestErrorNon400 tests the parsing of error reply for non 400 reply. | ||
func TestErrorNon400(t *testing.T) { | ||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(404) | ||
w.Write([]byte(expectErrorStatus400)) | ||
})) | ||
defer ts.Close() | ||
|
||
_, err := Connect(ClientConfig{Endpoint: ts.URL, HTTPClient: ts.Client()}) | ||
if err == nil { | ||
t.Error("Update call should fail") | ||
} | ||
_, ok := err.(*common.Error) | ||
if ok { | ||
t.Errorf("404 should not return known error type: %v", err) | ||
} | ||
if expectErrorStatus404 != err.Error() { | ||
t.Errorf("Expect:\n%s\nGot:\n%s", expectErrorStatus404, err.Error()) | ||
} | ||
} |
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