-
Notifications
You must be signed in to change notification settings - Fork 260
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: create mocks helper #420
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
84f6d28
feat: create mocks helper
ncostamagna 66e087e
Merge branch 'main' into feat/mocks
239be51
Merge branch 'main' into feat/mocks
eshanholtz 4c0a47a
feat: create start & end test server
ncostamagna 8124231
feat: create SendGrid interface & NewSendClientMock function
ncostamagna 97d5573
feat: update mocks example
ncostamagna bcabec3
Merge branch 'main' into feat/mocks
JenniferMah File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/sendgrid/sendgrid-go" | ||
"github.com/sendgrid/sendgrid-go/helpers/mail" | ||
"github.com/sendgrid/sendgrid-go/helpers/mock" | ||
) | ||
|
||
func main() { | ||
|
||
sendMail() // Send Mail | ||
simpleSendMail() // Response Error | ||
|
||
// start mocks server | ||
mock.StartTestServer() | ||
|
||
// add mock value | ||
mock.Add(&mock.Mock{ | ||
StatusCode: 400, | ||
Body: `{ "errors":[{ "message":"Example error.", "field":"example field" }] }`, | ||
}) | ||
sendMail() // Response with mock data | ||
simpleSendMail() // Response with mock data | ||
|
||
// add mock value | ||
mock.Add(&mock.Mock{ | ||
StatusCode: 200, | ||
}) | ||
sendMail() // Response with mock data | ||
simpleSendMail() // Response with mock data | ||
|
||
// add mock value | ||
mock.Add(&mock.Mock{ | ||
Err: errors.New("Has been an mock error"), | ||
}) | ||
|
||
// stop mocks server | ||
mock.StopTestServer() | ||
|
||
sendMail() // Send Mail | ||
simpleSendMail() // Response Error | ||
// start mocks server | ||
mock.StartTestServer() | ||
|
||
sendMail() // Response with mock data | ||
simpleSendMail() // Response with mock data | ||
|
||
// clear mock | ||
mock.Flush() | ||
sendMail() // Send Mail | ||
simpleSendMail() // Response Error | ||
|
||
// stop mocks server | ||
mock.StopTestServer() | ||
|
||
} | ||
|
||
func sendMail() { | ||
m := mail.NewV3Mail() | ||
content := mail.NewContent("text/html", "<h1>Hello world</h1>This is an example") | ||
|
||
from := mail.NewEmail("envalidMail", "[email protected]") | ||
m.SetFrom(from) | ||
|
||
m.AddContent(content) | ||
|
||
personalization := mail.NewPersonalization() | ||
|
||
personalization.AddTos(mail.NewEmail("envalidMail", "[email protected]")) | ||
personalization.Subject = "Example" | ||
|
||
m.AddPersonalizations(personalization) | ||
|
||
request := sendgrid.GetRequest(os.Getenv("SENDGRID_API_KEY"), "/v3/mail/send", "https://api.sendgrid.com") | ||
request.Method = "POST" | ||
request.Body = mail.GetRequestBody(m) | ||
response, err := sendgrid.API(request) | ||
|
||
if err != nil { | ||
fmt.Println("Sengird Error: ") | ||
fmt.Println(err) | ||
fmt.Println("________________________________") | ||
fmt.Println() | ||
return | ||
} | ||
|
||
fmt.Println("Sengird Response: ") | ||
fmt.Println(response.StatusCode) | ||
fmt.Println(response.Body) | ||
fmt.Println(response.Headers) | ||
fmt.Println("________________________________") | ||
fmt.Println() | ||
} | ||
|
||
func simpleSendMail() { | ||
from := mail.NewEmail("Example User", "[email protected]") | ||
subject := "Sending with Twilio SendGrid is Fun" | ||
to := mail.NewEmail("Example User", "[email protected]") | ||
plainTextContent := "and easy to do anywhere, even with Go" | ||
htmlContent := "<strong>and easy to do anywhere, even with Go</strong>" | ||
message := mail.NewSingleEmail(from, subject, to, plainTextContent, htmlContent) | ||
client := sendgrid.NewSendClientMock(os.Getenv("SENDGRID_API_KEY")) | ||
response, err := client.Send(message) | ||
if err != nil { | ||
fmt.Println("Simple Sengird Error: ") | ||
fmt.Println(err) | ||
fmt.Println("________________________________") | ||
fmt.Println() | ||
} else { | ||
fmt.Println("Simple Sengird Response: ") | ||
fmt.Println(response.StatusCode) | ||
fmt.Println(response.Body) | ||
fmt.Println(response.Headers) | ||
fmt.Println("________________________________") | ||
fmt.Println() | ||
} | ||
} |
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,64 @@ | ||
package mock | ||
|
||
import "github.com/sendgrid/rest" | ||
|
||
// Mock - Mock struct, has Response Code, Response Body and errors | ||
type Mock struct { | ||
StatusCode int | ||
Body string | ||
Err error | ||
} | ||
|
||
var ( | ||
mock *Mock | ||
isMocked bool | ||
) | ||
|
||
// Add - add mock method | ||
func Add(m *Mock) { | ||
mock = m | ||
} | ||
|
||
// Flush - Flush mock method | ||
func Flush() { | ||
mock = nil | ||
} | ||
|
||
// Get - Get mock method | ||
func Get() *Mock { | ||
return mock | ||
} | ||
|
||
// StartTestServer - start mock server | ||
func StartTestServer() { | ||
isMocked = true | ||
} | ||
|
||
// StopTestServer - start mock server | ||
func StopTestServer() { | ||
isMocked = false | ||
} | ||
|
||
// IsMocked - return true if the mocks server was started | ||
func IsMocked() bool { | ||
return isMocked | ||
} | ||
|
||
// Request - return mock sengrid response and error | ||
func Request() (*rest.Response, error) { | ||
|
||
if mock == nil { | ||
panic("There isn't a mock") | ||
} | ||
|
||
if mock.Err != nil { | ||
return nil, mock.Err | ||
} | ||
|
||
r := &rest.Response{ | ||
StatusCode: mock.StatusCode, | ||
Body: mock.Body, | ||
} | ||
|
||
return r, 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
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 |
---|---|---|
|
@@ -2,6 +2,7 @@ package sendgrid | |
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
|
@@ -14,6 +15,7 @@ import ( | |
|
||
"github.com/sendgrid/rest" | ||
"github.com/sendgrid/sendgrid-go/helpers/mail" | ||
"github.com/sendgrid/sendgrid-go/helpers/mock" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
|
@@ -3742,3 +3744,111 @@ func Test_test_whitelabel_links__link_id__subuser_post(t *testing.T) { | |
} | ||
assert.Equal(t, 200, response.StatusCode, "Wrong status code returned") | ||
} | ||
|
||
// send mail function example to used in Mock test | ||
func sendMailExample() (code int, body string, err error) { | ||
m := mail.NewV3Mail() | ||
|
||
m.SetFrom(mail.NewEmail("Invalid From", "[email protected]")) | ||
m.AddContent(mail.NewContent("text/html", "<h1>Hello world</h1>This is an example")) | ||
|
||
personalization := mail.NewPersonalization() | ||
|
||
// invalid mail | ||
personalization.AddTos(mail.NewEmail("Nahuel", "nlcostamagna")) | ||
personalization.Subject = "Example invalid email" | ||
|
||
m.AddPersonalizations(personalization) | ||
|
||
request := GetRequest(os.Getenv("SENDGRID_API_KEY"), "/v3/mail/send", "https://api.sendgrid.com") | ||
request.Method = "POST" | ||
request.Body = mail.GetRequestBody(m) | ||
response, err := API(request) | ||
|
||
if err != nil { | ||
return 0, "", err | ||
} | ||
|
||
return response.StatusCode, response.Body, nil | ||
} | ||
|
||
func TestMockStatusCode400(t *testing.T) { | ||
mock.StartTestServer() | ||
|
||
mock.Add(&mock.Mock{ | ||
StatusCode: 400, | ||
Body: `{ "errors":[{ "message":"Example error.", "field":"example field" }] }`, | ||
}) | ||
|
||
m := mock.Get() | ||
code, body, err := sendMailExample() | ||
|
||
assert.EqualValues(t, code, 400) | ||
assert.EqualValues(t, m.StatusCode, 400) | ||
assert.EqualValues(t, body, `{ "errors":[{ "message":"Example error.", "field":"example field" }] }`) | ||
assert.EqualValues(t, m.Body, `{ "errors":[{ "message":"Example error.", "field":"example field" }] }`) | ||
assert.Nil(t, err) | ||
assert.Nil(t, m.Err) | ||
|
||
mock.StopTestServer() | ||
} | ||
|
||
func TestMockStatusCode202(t *testing.T) { | ||
mock.StartTestServer() | ||
|
||
mock.Add(&mock.Mock{ | ||
StatusCode: 202, | ||
}) | ||
|
||
m := mock.Get() | ||
code, body, err := sendMailExample() | ||
|
||
assert.EqualValues(t, code, 202) | ||
assert.EqualValues(t, m.StatusCode, 202) | ||
assert.EqualValues(t, body, "") | ||
assert.EqualValues(t, m.Body, "") | ||
assert.Nil(t, err) | ||
assert.Nil(t, m.Err) | ||
|
||
mock.StopTestServer() | ||
} | ||
|
||
func TestMockErr(t *testing.T) { | ||
mock.StartTestServer() | ||
|
||
mock.Add(&mock.Mock{ | ||
Err: errors.New("Has been an error in TestMockErr"), | ||
}) | ||
|
||
m := mock.Get() | ||
code, body, err := sendMailExample() | ||
|
||
assert.EqualValues(t, err.Error(), "Has been an error in TestMockErr") | ||
assert.EqualValues(t, m.Err.Error(), "Has been an error in TestMockErr") | ||
assert.EqualValues(t, body, "") | ||
assert.EqualValues(t, m.Body, "") | ||
assert.EqualValues(t, code, 0) | ||
assert.EqualValues(t, m.StatusCode, 0) | ||
|
||
mock.StopTestServer() | ||
} | ||
|
||
func TestMockFlush(t *testing.T) { | ||
mock.StartTestServer() | ||
|
||
mock.Add(&mock.Mock{ | ||
StatusCode: 202, | ||
}) | ||
m1 := mock.Get() | ||
mock.Flush() | ||
m2 := mock.Get() | ||
|
||
code, _, err := sendMailExample() | ||
|
||
assert.EqualValues(t, m1.StatusCode, 202) | ||
assert.EqualValues(t, code, 401) | ||
assert.Nil(t, m2) | ||
assert.Nil(t, err) | ||
|
||
mock.StopTestServer() | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't like adding this dependency in non-dev environments.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello @thinkingserious, how are you?
Yes, I think the same but is only place where we could intercept the request
I haven't found other place where we could intercept the request with the Mocks.
Where we could intercept the request with the mocks?
Thank you very much and greetings!
Nahuel
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm good, thanks for asking. I trust the same is true with you!
I think we should first create the interface per issue #421.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello @thinkingserious
I created a SendGrid interface for test integration,
this example is in examples/mocks