Skip to content
This repository has been archived by the owner on Apr 11, 2024. It is now read-only.

Commit

Permalink
Merge pull request #84 from mercari/notification_test
Browse files Browse the repository at this point in the history
test: Add test cases for a part of notification.go's functions
  • Loading branch information
cubicdaiya authored Sep 29, 2017
2 parents a31b57e + fc4f377 commit 91eda29
Showing 1 changed file with 81 additions and 0 deletions.
81 changes: 81 additions & 0 deletions gaurun/notification_test.go
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")
}

0 comments on commit 91eda29

Please sign in to comment.