mirror of
https://github.com/optim-enterprises-bv/vault.git
synced 2025-11-01 19:17:58 +00:00
http: respondCommon to do common responses
This commit is contained in:
@@ -57,9 +57,18 @@ func respondError(w http.ResponseWriter, status int, err error) {
|
|||||||
enc.Encode(resp)
|
enc.Encode(resp)
|
||||||
}
|
}
|
||||||
|
|
||||||
func respondErrorResponse(w http.ResponseWriter, resp *logical.Response) {
|
func respondCommon(w http.ResponseWriter, resp *logical.Response) bool {
|
||||||
err := fmt.Errorf("%s", resp.Data["error"].(string))
|
if resp == nil {
|
||||||
respondError(w, http.StatusBadRequest, err)
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
if resp.IsError() {
|
||||||
|
err := fmt.Errorf("%s", resp.Data["error"].(string))
|
||||||
|
respondError(w, http.StatusBadRequest, err)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func respondOk(w http.ResponseWriter, body interface{}) {
|
func respondOk(w http.ResponseWriter, body interface{}) {
|
||||||
|
|||||||
@@ -59,12 +59,11 @@ func handleLogical(core *vault.Core) http.Handler {
|
|||||||
respondError(w, http.StatusInternalServerError, err)
|
respondError(w, http.StatusInternalServerError, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if op == logical.ReadOperation && resp == nil {
|
if respondCommon(w, resp) {
|
||||||
respondError(w, http.StatusNotFound, nil)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if resp.IsError() {
|
if op == logical.ReadOperation && resp == nil {
|
||||||
respondErrorResponse(w, resp)
|
respondError(w, http.StatusNotFound, nil)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -26,7 +26,6 @@ func TestLogical(t *testing.T) {
|
|||||||
|
|
||||||
var actual map[string]interface{}
|
var actual map[string]interface{}
|
||||||
expected := map[string]interface{}{
|
expected := map[string]interface{}{
|
||||||
"vault_id": "",
|
|
||||||
"renewable": false,
|
"renewable": false,
|
||||||
"lease_duration": float64(0),
|
"lease_duration": float64(0),
|
||||||
"data": map[string]interface{}{
|
"data": map[string]interface{}{
|
||||||
@@ -35,6 +34,7 @@ func TestLogical(t *testing.T) {
|
|||||||
}
|
}
|
||||||
testResponseStatus(t, resp, 200)
|
testResponseStatus(t, resp, 200)
|
||||||
testResponseBody(t, resp, &actual)
|
testResponseBody(t, resp, &actual)
|
||||||
|
delete(actual, "vault_id")
|
||||||
if !reflect.DeepEqual(actual, expected) {
|
if !reflect.DeepEqual(actual, expected) {
|
||||||
t.Fatalf("bad: %#v", actual)
|
t.Fatalf("bad: %#v", actual)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user