-
Notifications
You must be signed in to change notification settings - Fork 9
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 #24 from koladev32/19-add-tests-suite-for-the-webh…
…ook-sender-package 19 add tests suite for the webhook sender package
- Loading branch information
Showing
6 changed files
with
187 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package sender | ||
|
||
/* | ||
This package contains mostly test functions for the utils used to send webhooks. If these utils works, | ||
we can ensure that the send webhook function will function normally too. For a whole test suite for the sender function, | ||
check out the webhook_test.go file. | ||
*/ | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"net/http" | ||
"testing" | ||
) | ||
|
||
type MockClient struct { | ||
MockDo func(req *http.Request) (*http.Response, error) | ||
} | ||
|
||
func (m *MockClient) Do(req *http.Request) (*http.Response, error) { | ||
return m.MockDo(req) | ||
} | ||
|
||
// Testing the MarshalJSON method | ||
func TestMarshalJSON(t *testing.T) { | ||
data := map[string]string{ | ||
"key": "value", | ||
} | ||
expectedJSON := `{"key":"value"}` | ||
|
||
jsonBytes, err := marshalJSON(data) | ||
if err != nil { | ||
t.Fatalf("Expected no error, but got: %v", err) | ||
} | ||
|
||
if string(jsonBytes) != expectedJSON { | ||
t.Fatalf("Expected %s, but got %s", expectedJSON, string(jsonBytes)) | ||
} | ||
} | ||
|
||
// Test for prepareRequest | ||
func TestPrepareRequest(t *testing.T) { | ||
url := "http://example.com/webhook" | ||
jsonBytes := []byte(`{"key":"value"}`) | ||
secretHash := "secret123" | ||
|
||
req, err := prepareRequest(url, jsonBytes, secretHash) | ||
if err != nil { | ||
t.Fatalf("Expected no error, but got: %v", err) | ||
} | ||
|
||
if req.Header.Get("Content-Type") != "application/json" { | ||
t.Fatalf("Expected header Content-Type to be application/json but got %s", req.Header.Get("Content-Type")) | ||
} | ||
|
||
if req.Header.Get("X-Secret-Hash") != secretHash { | ||
t.Fatalf("Expected header X-Secret-Hash to be %s but got %s", secretHash, req.Header.Get("X-Secret-Hash")) | ||
} | ||
} | ||
|
||
func TestSendRequest(t *testing.T) { | ||
HTTPClient = &MockClient{ | ||
MockDo: func(req *http.Request) (*http.Response, error) { | ||
return &http.Response{ | ||
StatusCode: 200, | ||
Body: io.NopCloser(bytes.NewBufferString("OK")), | ||
}, nil | ||
}, | ||
} | ||
|
||
req, _ := http.NewRequest("GET", "http://example.com", nil) | ||
resp, err := sendRequest(req) | ||
if err != nil { | ||
t.Fatalf("Expected no error, but got: %v", err) | ||
} | ||
|
||
body, _ := io.ReadAll(resp.Body) | ||
if string(body) != "OK" { | ||
t.Fatalf("Expected body to be OK but got %s", string(body)) | ||
} | ||
} |
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,92 @@ | ||
package sender | ||
|
||
import ( | ||
"errors" | ||
"net/http" | ||
"testing" | ||
"webhook/logging" | ||
) | ||
|
||
var ( | ||
marshalJSONOrig = marshalJSON | ||
prepareRequestOrig = prepareRequest | ||
sendRequestOrig = sendRequest | ||
processResponseOrig = processResponse | ||
webhookLoggerInvoked = false | ||
) | ||
|
||
func TestSendWebhook(t *testing.T) { | ||
logging.WebhookLogger = func(errorType string, errorMessage error) error { | ||
webhookLoggerInvoked = true | ||
return nil | ||
} | ||
|
||
t.Run("Successful webhook sending", func(t *testing.T) { | ||
resetMocks() // Reset all mocks to original functions | ||
|
||
err := SendWebhook(nil, "http://dummy.com", "webhookId", "secretHash") | ||
if err != nil { | ||
t.Fatalf("Expected no error, but got: %v", err) | ||
} | ||
}) | ||
|
||
t.Run("Failed webhook due to marshaling errors", func(t *testing.T) { | ||
resetMocks() | ||
marshalJSON = func(data interface{}) ([]byte, error) { | ||
return nil, errors.New("marshaling error") | ||
} | ||
|
||
err := SendWebhook(nil, "http://dummy.com", "webhookId", "secretHash") | ||
if err == nil || err.Error() != "marshaling error" { | ||
t.Fatalf("Expected marshaling error, but got: %v", err) | ||
} | ||
}) | ||
|
||
t.Run("Failed webhook due to request preparation errors", func(t *testing.T) { | ||
resetMocks() | ||
prepareRequest = func(url string, jsonBytes []byte, secretHash string) (*http.Request, error) { | ||
return nil, errors.New("request preparation error") | ||
} | ||
|
||
err := SendWebhook(nil, "http://dummy.com", "webhookId", "secretHash") | ||
if err == nil || err.Error() != "request preparation error" { | ||
t.Fatalf("Expected request preparation error, but got: %v", err) | ||
} | ||
}) | ||
|
||
t.Run("Failed webhook due to response processing errors", func(t *testing.T) { | ||
resetMocks() | ||
processResponse = func(resp *http.Response) (string, []byte, error) { | ||
return "failed", nil, errors.New("response processing error") | ||
} | ||
|
||
err := SendWebhook(nil, "http://dummy.com", "webhookId", "secretHash") | ||
if err == nil || err.Error() != "response processing error" { | ||
t.Fatalf("Expected response processing error, but got: %v", err) | ||
} | ||
}) | ||
|
||
t.Run("Logging on failed webhook delivery", func(t *testing.T) { | ||
resetMocks() | ||
processResponse = func(resp *http.Response) (string, []byte, error) { | ||
return "failed", []byte("error body"), nil | ||
} | ||
|
||
webhookLoggerInvoked = false | ||
err := SendWebhook(nil, "http://dummy.com", "webhookId", "secretHash") | ||
if err == nil || err.Error() != "failed" { | ||
t.Fatalf("Expected failed status, but got: %v", err) | ||
} | ||
|
||
if !webhookLoggerInvoked { | ||
t.Fatalf("Expected WebhookLogger to be invoked") | ||
} | ||
}) | ||
} | ||
|
||
func resetMocks() { | ||
marshalJSON = marshalJSONOrig | ||
prepareRequest = prepareRequestOrig | ||
sendRequest = sendRequestOrig | ||
processResponse = processResponseOrig | ||
} |