This repository has been archived by the owner on Apr 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #84 from mercari/notification_test
test: Add test cases for a part of notification.go's functions
- Loading branch information
Showing
1 changed file
with
81 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package gaurun | ||
|
||
import ( | ||
"errors" | ||
"io/ioutil" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestValidateNotification(t *testing.T) { | ||
cases := []struct { | ||
Notification RequestGaurunNotification | ||
Expected error | ||
}{ | ||
// positive cases | ||
{ | ||
RequestGaurunNotification{ | ||
Tokens: []string{"test token"}, | ||
Platform: 1, | ||
Message: "test message", | ||
}, | ||
nil, | ||
}, | ||
{ | ||
RequestGaurunNotification{ | ||
Tokens: []string{"test token"}, | ||
Platform: 2, | ||
Message: "test message", | ||
}, | ||
nil, | ||
}, | ||
|
||
// negative cases | ||
{ | ||
RequestGaurunNotification{ | ||
Tokens: []string{""}, | ||
}, | ||
errors.New("empty token"), | ||
}, | ||
{ | ||
RequestGaurunNotification{ | ||
Tokens: []string{"test token"}, | ||
Platform: 100, /* neither iOS nor Android */ | ||
}, | ||
errors.New("invalid platform"), | ||
}, | ||
{ | ||
RequestGaurunNotification{ | ||
Tokens: []string{"test token"}, | ||
Platform: 1, | ||
Message: "", | ||
}, | ||
errors.New("empty message"), | ||
}, | ||
} | ||
|
||
for _, c := range cases { | ||
actual := validateNotification(&c.Notification) | ||
assert.Equal(t, actual, c.Expected) | ||
} | ||
} | ||
|
||
func TestSendResponse(t *testing.T) { | ||
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
sendResponse(w, "valid message", http.StatusOK) | ||
return | ||
})) | ||
defer s.Close() | ||
|
||
res, err := http.Get(s.URL) | ||
assert.Nil(t, err) | ||
assert.Equal(t, res.StatusCode, http.StatusOK) | ||
|
||
body, err := ioutil.ReadAll(res.Body) | ||
assert.Nil(t, err) | ||
assert.Equal(t, string(body), "{\"message\":\"valid message\"}\n") | ||
} | ||
|