mirror of
https://github.com/hamburghammer/gmon.git
synced 2024-05-05 23:45:27 +02:00
Augusto Dwenger J.
20ede605fb
The package testify offers assert and require for testing. The difference between assert and require are is that on require the test will immediately fail.
66 lines
1.5 KiB
Go
66 lines
1.5 KiB
Go
package alert
|
|
|
|
import (
|
|
"bytes"
|
|
"io/ioutil"
|
|
"net/http"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
type mockHTTPClient struct {
|
|
response string
|
|
statusCode int
|
|
req http.Request
|
|
}
|
|
|
|
func (mhc *mockHTTPClient) Do(req *http.Request) (*http.Response, error) {
|
|
mhc.req = *req
|
|
response := http.Response{
|
|
Body: ioutil.NopCloser(bytes.NewBufferString(mhc.response)),
|
|
StatusCode: mhc.statusCode,
|
|
}
|
|
return &response, nil
|
|
}
|
|
|
|
func getMockClientWithValidResponse() mockHTTPClient {
|
|
responseJSON := ""
|
|
return mockHTTPClient{response: responseJSON, statusCode: 200}
|
|
}
|
|
|
|
func TestGotifyClientRequestBuilding(t *testing.T) {
|
|
|
|
data := Data{
|
|
Title: "title",
|
|
Message: "message",
|
|
Priority: 0,
|
|
}
|
|
|
|
t.Run("should build url with the token as header", func(t *testing.T) {
|
|
mockClient := getMockClientWithValidResponse()
|
|
token := "xxx"
|
|
simpleClient := NewGotifyClient(token, "https://example.com")
|
|
simpleClient = simpleClient.WithHTTPClient(&mockClient)
|
|
|
|
err := simpleClient.Notify(data)
|
|
require.Nil(t, err)
|
|
|
|
require.Equal(t, token, mockClient.req.Header.Get("X-Gotify-Key"))
|
|
})
|
|
|
|
t.Run("should return error if url could not be parse", func(t *testing.T) {
|
|
mockClient := &mockHTTPClient{}
|
|
token := "xxx"
|
|
simpleClient := NewGotifyClient(token, " https:")
|
|
simpleClient = simpleClient.WithHTTPClient(mockClient)
|
|
|
|
err := simpleClient.Notify(data)
|
|
require.NotNil(t, err)
|
|
|
|
want := "parse \" https:\": first path segment in URL cannot contain colon"
|
|
|
|
require.Equal(t, want, err.Error())
|
|
})
|
|
}
|