From bc469dbba57d9679c5a4c410d2cf0a6b54f3637a Mon Sep 17 00:00:00 2001 From: Brennon Loveless Date: Sun, 3 May 2020 20:41:18 -0400 Subject: [PATCH] Pass golang-ci --- client.go | 16 ++++++++++++++-- request.go | 31 +++++++++++++++++++++---------- request_test.go | 37 +++++-------------------------------- 3 files changed, 40 insertions(+), 44 deletions(-) diff --git a/client.go b/client.go index 36ca7f4..2a64724 100644 --- a/client.go +++ b/client.go @@ -74,6 +74,9 @@ func (c Client) OAuthRequestTokenGet(input OAuthRequestTokenInput) (OAuthRequest var output = OAuthRequestTokenOutput{} var decoder = schema.NewDecoder() err = decoder.Decode(&output, values) + if err != nil { + return OAuthRequestTokenOutput{}, err + } return output, nil } @@ -97,6 +100,9 @@ func (c Client) OAuthAccessTokenGet(input OAuthAccessTokenInput) (OAuthAccessTok var output = OAuthAccessTokenOutput{} var decoder = schema.NewDecoder() err = decoder.Decode(&output, values) + if err != nil { + return OAuthAccessTokenOutput{}, err + } return output, nil } @@ -118,7 +124,10 @@ func (c Client) StatusesUpdatePost(input StatusesUpdateInput) (StatusesUpdateOut } output := StatusesUpdateOutput{} - json.Unmarshal(resBytes, &output) + err = json.Unmarshal(resBytes, &output) + if err != nil { + return StatusesUpdateOutput{}, err + } return output, nil } @@ -153,7 +162,10 @@ func (c Client) StatusesUserTimelineGet(input StatusesUserTimelineInput) ([]Stat } var output []StatusesUserTimelineOutput - json.Unmarshal(resBytes, &output) + err = json.Unmarshal(resBytes, &output) + if err != nil { + return []StatusesUserTimelineOutput{}, err + } return output, nil } diff --git a/request.go b/request.go index 4124795..9dd0640 100644 --- a/request.go +++ b/request.go @@ -49,17 +49,17 @@ func processParams(input interface{}) url.Values { if value != nil { // convert to string based on underlying type - switch value.(type) { + switch value := value.(type) { case string: - params.Add(name, value.(string)) + params.Add(name, value) case bool: - params.Add(name, strconv.FormatBool(value.(bool))) + params.Add(name, strconv.FormatBool(value)) case int: - params.Add(name, strconv.FormatInt(int64(value.(int)), 10)) + params.Add(name, strconv.FormatInt(int64(value), 10)) case int64: - params.Add(name, strconv.FormatInt(value.(int64), 10)) + params.Add(name, strconv.FormatInt(value, 10)) case float64: - params.Add(name, strconv.FormatFloat(value.(float64), 'f', -1, 64)) + params.Add(name, strconv.FormatFloat(value, 'f', -1, 64)) } } } @@ -141,6 +141,9 @@ func (c Client) getSignedRequest(method, uri string, params url.Values) (*http.R u.RawQuery = sr.params.Encode() req, err = http.NewRequest(sr.method, u.String(), nil) + if err != nil { + return nil, err + } } if err != nil { @@ -191,14 +194,22 @@ func (c Client) signature(sr signatureRequest) (string, error) { signingKey := url.QueryEscape(c.OAuthConsumerSecret) + "&" + url.QueryEscape(c.OAuthAccessTokenSecret) - return calculateSignature(signatureBaseString, signingKey), nil + sig, err := calculateSignature(signatureBaseString, signingKey) + if err != nil { + return "", err + } + + return sig, nil } -func calculateSignature(base, key string) string { +func calculateSignature(base, key string) (string, error) { hash := hmac.New(sha1.New, []byte(key)) - hash.Write([]byte(base)) + _, err := hash.Write([]byte(base)) + if err != nil { + return "", err + } signature := hash.Sum(nil) - return base64.StdEncoding.EncodeToString(signature) + return base64.StdEncoding.EncodeToString(signature), nil } type headerParameters struct { diff --git a/request_test.go b/request_test.go index 998c402..f5e158e 100644 --- a/request_test.go +++ b/request_test.go @@ -1,7 +1,6 @@ package tweetgo import ( - "errors" "fmt" "net/http" "net/url" @@ -9,32 +8,6 @@ import ( "testing" ) -type mockClient struct { - responses []*http.Response -} - -func (c *mockClient) Do(req *http.Request) (*http.Response, error) { - if len(c.responses) > 0 { - response := c.responses[0] - c.responses = c.responses[1:] - return response, nil - } - - return nil, errors.New("no response found to return") -} - -type mockNonce struct{} - -func (n mockNonce) Generate() string { - return "kYjzVBB8Y0ZFabxSWbWovY3uYSQ2pTgmZeNu2VS4cg\n" -} - -type mockCurrentTimer struct{} - -func (t mockCurrentTimer) GetCurrentTime() int64 { - return 1588432148 -} - func TestCanProcessParamsAndOmitNilValues(t *testing.T) { type testStruct struct { TestString *string `schema:"test_string"` @@ -128,8 +101,8 @@ func TestCorrectlyCalculatesSignatureForStatusesUpdate(t *testing.T) { uri: "https://api.twitter.com/1.1/statuses/update.json", nonce: "kYjzVBB8Y0ZFabxSWbWovY3uYSQ2pTgmZeNu2VS4cg", timestamp: "1318622958", - params: url.Values{ - "status": {"Hello Ladies + Gentlemen, a signed OAuth request!"}, + params: url.Values{ + "status": {"Hello Ladies + Gentlemen, a signed OAuth request!"}, "include_entities": {"true"}, }, } @@ -159,7 +132,7 @@ func TestCorrectlyCalculatesSignatureForStatusesUpdateWithGetParameters(t *testi uri: "https://api.twitter.com/1.1/statuses/update.json?include_entities=true", nonce: "kYjzVBB8Y0ZFabxSWbWovY3uYSQ2pTgmZeNu2VS4cg", timestamp: "1318622958", - params: url.Values{ + params: url.Values{ "status": {"Hello Ladies + Gentlemen, a signed OAuth request!"}, }, } @@ -190,8 +163,8 @@ func TestCorrectlyCalculatesSignatureForStatusesUpdateWithDuplicatedGetParameter uri: "https://api.twitter.com/1.1/statuses/update.json?include_entities=true", nonce: "kYjzVBB8Y0ZFabxSWbWovY3uYSQ2pTgmZeNu2VS4cg", timestamp: "1318622958", - params: url.Values{ - "status": {"Hello Ladies + Gentlemen, a signed OAuth request!"}, + params: url.Values{ + "status": {"Hello Ladies + Gentlemen, a signed OAuth request!"}, "include_entities": {"true"}, }, }