-
-
Notifications
You must be signed in to change notification settings - Fork 852
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add async feedback hook option (#414)
* feat: add async feedback hook option * remove unnecessary return values * review: check feedback's resp state * fix embedmd error * fix config test * add feedback tests * fix errcheck issues
- Loading branch information
Showing
10 changed files
with
163 additions
and
16 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
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,38 @@ | ||
package gorush | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"errors" | ||
"net/http" | ||
) | ||
|
||
// DispatchFeedback sends a feedback to the configured gateway. | ||
func DispatchFeedback(log LogPushEntry, url string) error { | ||
|
||
if url == "" { | ||
return errors.New("The url can't be empty") | ||
} | ||
|
||
payload, err := json.Marshal(log) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
req, _ := http.NewRequest("POST", url, bytes.NewBuffer(payload)) | ||
req.Header.Set("Content-Type", "application/json; charset=utf-8") | ||
|
||
HTTPClient := &http.Client{} | ||
resp, err := HTTPClient.Do(req) | ||
|
||
if resp != nil { | ||
defer resp.Body.Close() | ||
} | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
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,75 @@ | ||
package gorush | ||
|
||
import ( | ||
"log" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/appleboy/gorush/config" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestEmptyFeedbackURL(t *testing.T) { | ||
// PushConf, _ = config.LoadConf("") | ||
logEntry := LogPushEntry{ | ||
ID: "", | ||
Type: "", | ||
Platform: "", | ||
Token: "", | ||
Message: "", | ||
Error: "", | ||
} | ||
|
||
err := DispatchFeedback(logEntry, PushConf.Core.FeedbackURL) | ||
assert.NotNil(t, err) | ||
} | ||
|
||
func TestHTTPErrorInFeedbackCall(t *testing.T) { | ||
config, _ := config.LoadConf("") | ||
config.Core.FeedbackURL = "http://test.example.com/api/" | ||
logEntry := LogPushEntry{ | ||
ID: "", | ||
Type: "", | ||
Platform: "", | ||
Token: "", | ||
Message: "", | ||
Error: "", | ||
} | ||
|
||
err := DispatchFeedback(logEntry, config.Core.FeedbackURL) | ||
assert.NotNil(t, err) | ||
} | ||
|
||
func TestSuccessfulFeedbackCall(t *testing.T) { | ||
|
||
// Mock http server | ||
httpMock := httptest.NewServer( | ||
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
if r.URL.Path == "/dispatch" { | ||
w.Header().Add("Content-Type", "application/json") | ||
_, err := w.Write([]byte(`{}`)) | ||
|
||
if err != nil { | ||
log.Println(err) | ||
panic(err) | ||
} | ||
} | ||
}), | ||
) | ||
defer httpMock.Close() | ||
|
||
config, _ := config.LoadConf("") | ||
config.Core.FeedbackURL = httpMock.URL | ||
logEntry := LogPushEntry{ | ||
ID: "", | ||
Type: "", | ||
Platform: "", | ||
Token: "", | ||
Message: "", | ||
Error: "", | ||
} | ||
|
||
err := DispatchFeedback(logEntry, config.Core.FeedbackURL) | ||
assert.Nil(t, err) | ||
} |
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
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