-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Adjust styling to match internal patterns * Add some test cases for Reply kinds
- Loading branch information
Showing
8 changed files
with
128 additions
and
115 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package fleetlock | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
// List of ReplyKind | ||
const ( | ||
KindMethodNotAllowed ReplyKind = "method_not_allowed" | ||
KindMissingHeader ReplyKind = "missing_header" | ||
KindDecodeError ReplyKind = "decode_error" | ||
KindInternalError ReplyKind = "internal_error" | ||
KindLockHeld ReplyKind = "lock_held" | ||
) | ||
|
||
// ReplyKind is used as a Zincati metrics label. | ||
type ReplyKind string | ||
|
||
// Reply represents a Fleetlock protocol reply. | ||
type Reply struct { | ||
// reply identifier | ||
Kind ReplyKind `json:"kind"` | ||
// human-friendly reply message | ||
Value string `json:"value"` | ||
} | ||
|
||
// NewReply creates an Reply with a specific kind and a formatted message. | ||
func NewReply(kind ReplyKind, format string, a ...interface{}) Reply { | ||
return Reply{ | ||
Kind: kind, | ||
Value: fmt.Sprintf(format, a...), | ||
} | ||
} | ||
|
||
// encodeReply writes response with the given Reply and HTTP code. | ||
func encodeReply(w http.ResponseWriter, reply Reply) error { | ||
w.Header().Set("Content-Type", "application/json") | ||
w.Header().Set("X-Content-Type-Options", "nosniff") | ||
|
||
switch reply.Kind { | ||
case KindMethodNotAllowed: | ||
w.WriteHeader(http.StatusMethodNotAllowed) | ||
case KindDecodeError, KindMissingHeader: | ||
w.WriteHeader(http.StatusBadRequest) | ||
case KindInternalError: | ||
w.WriteHeader(http.StatusInternalServerError) | ||
case KindLockHeld: | ||
w.WriteHeader(http.StatusLocked) | ||
default: | ||
w.WriteHeader(http.StatusOK) | ||
} | ||
|
||
return json.NewEncoder(w).Encode(reply) | ||
} |
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,60 @@ | ||
package fleetlock | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestEncodeReply(t *testing.T) { | ||
cases := []struct { | ||
reply Reply | ||
expectedStatus int | ||
expectedResponse string | ||
}{ | ||
{ | ||
reply: NewReply(KindMethodNotAllowed, "required method POST"), | ||
expectedStatus: 405, | ||
expectedResponse: `{"kind": "method_not_allowed", "value": "required method POST"}`, | ||
}, | ||
{ | ||
reply: NewReply(KindMissingHeader, "missing required header %s: %s", fleetLockHeaderKey, "true"), | ||
expectedStatus: 400, | ||
expectedResponse: `{"kind": "missing_header", "value": "missing required header fleet-lock-protocol: true"}`, | ||
}, | ||
{ | ||
reply: NewReply(KindDecodeError, "error decoding message"), | ||
expectedStatus: 400, | ||
expectedResponse: `{"kind": "decode_error", "value": "error decoding message"}`, | ||
}, | ||
{ | ||
reply: NewReply(KindInternalError, "error getting reboot lease"), | ||
expectedStatus: 500, | ||
expectedResponse: `{"kind": "internal_error", "value": "error getting reboot lease"}`, | ||
}, | ||
{ | ||
reply: NewReply(KindLockHeld, "reboot lease unavailable, held by %s", "e0f3745b108f471cbd4883c6fbed8cdd"), | ||
expectedStatus: 423, | ||
expectedResponse: `{"kind": "lock_held", "value": "reboot lease unavailable, held by e0f3745b108f471cbd4883c6fbed8cdd"}`, | ||
}, | ||
{ | ||
reply: NewReply("other", "message"), | ||
expectedStatus: 200, | ||
expectedResponse: `{"kind": "other", "value": "message"}`, | ||
}, | ||
} | ||
for _, c := range cases { | ||
t.Run(fmt.Sprintf("%v-%v", c.reply.Kind, c.reply.Value), func(t *testing.T) { | ||
w := httptest.NewRecorder() | ||
encodeReply(w, c.reply) | ||
|
||
assert.Equal(t, c.expectedStatus, w.Code, "Expected status code %v", c.expectedStatus) | ||
|
||
body, _ := ioutil.ReadAll(w.Body) | ||
assert.JSONEq(t, c.expectedResponse, string(body), "Unexpected JSON output in response") | ||
}) | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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