-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add encoder/decoder for GetItem Handler #44
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9adc445
sample code for splitting decoders/encoders
joe94 cc2ecb5
Add constant values for header keys
joe94 5ae3f00
add unit test for getItemDecoder
joe94 3cdfbf8
Add encodeGetItemResponse unit tests
joe94 91498be
Add change notes
joe94 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,15 @@ | ||
package store | ||
|
||
import "net/http" | ||
|
||
type BadRequestErr struct { | ||
Message string | ||
} | ||
|
||
func (bre BadRequestErr) Error() string { | ||
return bre.Message | ||
} | ||
|
||
func (bre BadRequestErr) StatusCode() int { | ||
return http.StatusBadRequest | ||
} |
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,97 @@ | ||
package store | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"errors" | ||
"net/http" | ||
|
||
kithttp "github.com/go-kit/kit/transport/http" | ||
"github.com/gorilla/mux" | ||
"github.com/xmidt-org/argus/model" | ||
) | ||
|
||
// request URL path keys | ||
const ( | ||
bucketVarKey = "bucket" | ||
idVarKey = "id" | ||
) | ||
|
||
const ( | ||
bucketVarMissingMsg = "{bucket} URL path parameter missing" | ||
idVarMissingMsg = "{id} URL path parameter missing" | ||
) | ||
|
||
// Request and Response Headers | ||
const ( | ||
ItemOwnerHeaderKey = "X-Xmidt-Owner" | ||
XmidtErrorHeaderKey = "X-Xmidt-Error" | ||
) | ||
|
||
var ErrCastingEncodeGetItemResponse = errors.New("casting error in encodeGetItemResponse") | ||
|
||
// TODO: since GET and DELETE are so similar, we could make them share at least the | ||
// decoders | ||
type getItemRequest struct { | ||
key model.Key | ||
owner string | ||
} | ||
|
||
func decodeGetItemRequest(ctx context.Context, r *http.Request) (interface{}, error) { | ||
vars := mux.Vars(r) | ||
bucket, ok := vars[bucketVarKey] | ||
if !ok { | ||
return nil, &BadRequestErr{Message: bucketVarMissingMsg} | ||
} | ||
|
||
id, ok := vars[idVarKey] | ||
|
||
if !ok { | ||
return nil, &BadRequestErr{Message: idVarMissingMsg} | ||
} | ||
|
||
return &getItemRequest{ | ||
key: model.Key{ | ||
Bucket: bucket, | ||
ID: id, | ||
}, | ||
owner: r.Header.Get(ItemOwnerHeaderKey), | ||
}, nil | ||
} | ||
|
||
func encodeGetItemResponse(ctx context.Context, rw http.ResponseWriter, response interface{}) error { | ||
item, ok := response.(*OwnableItem) | ||
if !ok { | ||
return ErrCastingEncodeGetItemResponse | ||
} | ||
|
||
if item.TTL <= 0 { | ||
rw.WriteHeader(http.StatusNotFound) | ||
return nil | ||
} | ||
|
||
data, err := json.Marshal(&item.Item) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
rw.Header().Add("Content-Type", "application/json") | ||
rw.Write(data) | ||
return nil | ||
} | ||
|
||
func encodeError(ctx context.Context, err error, w http.ResponseWriter) { | ||
w.Header().Set(XmidtErrorHeaderKey, err.Error()) | ||
if headerer, ok := err.(kithttp.Headerer); ok { | ||
for k, values := range headerer.Headers() { | ||
for _, v := range values { | ||
w.Header().Add(k, v) | ||
} | ||
} | ||
} | ||
code := http.StatusInternalServerError | ||
if sc, ok := err.(kithttp.StatusCoder); ok { | ||
code = sc.StatusCode() | ||
} | ||
w.WriteHeader(code) | ||
} |
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,151 @@ | ||
package store | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/gorilla/mux" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/xmidt-org/argus/model" | ||
) | ||
|
||
func TestDecodeGetItemRequest(t *testing.T) { | ||
testCases := []struct { | ||
Name string | ||
URLVars map[string]string | ||
Headers map[string][]string | ||
ExpectedDecodedRequest interface{} | ||
ExpectedErr error | ||
}{ | ||
{ | ||
Name: "Missing id", | ||
URLVars: map[string]string{ | ||
"bucket": "california", | ||
}, | ||
ExpectedErr: &BadRequestErr{Message: idVarMissingMsg}, | ||
}, | ||
{ | ||
Name: "Missing bucket", | ||
URLVars: map[string]string{ | ||
"id": "san francisco", | ||
}, | ||
ExpectedErr: &BadRequestErr{Message: bucketVarMissingMsg}, | ||
}, | ||
{ | ||
Name: "Happy path - No owner", | ||
URLVars: map[string]string{ | ||
"bucket": "california", | ||
"id": "san francisco", | ||
}, | ||
ExpectedDecodedRequest: &getItemRequest{ | ||
key: model.Key{ | ||
Bucket: "california", | ||
ID: "san francisco", | ||
}, | ||
}, | ||
}, | ||
{ | ||
Name: "Happy path", | ||
URLVars: map[string]string{ | ||
"bucket": "california", | ||
"id": "san francisco", | ||
}, | ||
|
||
ExpectedDecodedRequest: &getItemRequest{ | ||
key: model.Key{ | ||
Bucket: "california", | ||
ID: "san francisco", | ||
}, | ||
owner: "SF Giants", | ||
}, | ||
Headers: map[string][]string{ | ||
ItemOwnerHeaderKey: []string{"SF Giants"}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, testCase := range testCases { | ||
t.Run(testCase.Name, func(t *testing.T) { | ||
assert := assert.New(t) | ||
r := httptest.NewRequest(http.MethodGet, "http://localhost/test", nil) | ||
transferHeaders(testCase.Headers, r) | ||
|
||
r = mux.SetURLVars(r, testCase.URLVars) | ||
decodedRequest, err := decodeGetItemRequest(context.Background(), r) | ||
|
||
assert.Equal(testCase.ExpectedDecodedRequest, decodedRequest) | ||
assert.Equal(testCase.ExpectedErr, err) | ||
}) | ||
} | ||
} | ||
|
||
func TestEncodeGetItemResponse(t *testing.T) { | ||
testCases := []struct { | ||
Name string | ||
ItemResponse interface{} | ||
ExpectedHeaders http.Header | ||
ExpectedCode int | ||
ExpectedBody string | ||
ExpectedErr error | ||
}{ | ||
{ | ||
Name: "Unexpected casting error", | ||
ItemResponse: nil, | ||
ExpectedHeaders: make(http.Header), | ||
ExpectedErr: ErrCastingEncodeGetItemResponse, | ||
// used due to limitations in httptest. In reality, any non-nil error promises nothing | ||
// would be written to the response writer | ||
ExpectedCode: 200, | ||
}, | ||
{ | ||
Name: "Expired item", | ||
ItemResponse: &OwnableItem{ | ||
Item: model.Item{ | ||
TTL: 0, | ||
}, | ||
}, | ||
ExpectedCode: http.StatusNotFound, | ||
ExpectedHeaders: make(http.Header), | ||
}, | ||
{ | ||
Name: "Happy path", | ||
ItemResponse: &OwnableItem{ | ||
Owner: "xmidt", | ||
Item: model.Item{ | ||
TTL: 20, | ||
Identifier: "id01", | ||
Data: map[string]interface{}{ | ||
"key": 10, | ||
}, | ||
}, | ||
}, | ||
ExpectedBody: `{"identifier":"id01","data":{"key":10},"ttl":20}`, | ||
ExpectedCode: 200, | ||
ExpectedHeaders: http.Header{ | ||
"Content-Type": []string{"application/json"}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, testCase := range testCases { | ||
t.Run(testCase.Name, func(t *testing.T) { | ||
assert := assert.New(t) | ||
recorder := httptest.NewRecorder() | ||
err := encodeGetItemResponse(context.Background(), recorder, testCase.ItemResponse) | ||
assert.Equal(testCase.ExpectedErr, err) | ||
assert.Equal(testCase.ExpectedBody, recorder.Body.String()) | ||
assert.Equal(testCase.ExpectedHeaders, recorder.HeaderMap) | ||
assert.Equal(testCase.ExpectedCode, recorder.Code) | ||
}) | ||
} | ||
} | ||
|
||
func transferHeaders(headers map[string][]string, r *http.Request) { | ||
for k, values := range headers { | ||
for _, value := range values { | ||
r.Header.Add(k, value) | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think it belongs in this pr, but can we make these headers configurable? If argus is used in a context outside of Xmidt, they seem out of place.