-
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
Changes from 4 commits
84f6d28
66e087e
239be51
4c0a47a
8124231
97d5573
bcabec3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
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() { | ||
|
||
// Send Mail | ||
sendMail() | ||
|
||
// 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 | ||
|
||
// add mock value | ||
mock.Add(&mock.Mock{ | ||
StatusCode: 200, | ||
}) | ||
sendMail() // 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 | ||
|
||
// start mocks server | ||
mock.StartTestServer() | ||
|
||
sendMail() // Response with mock data | ||
|
||
// clear mock | ||
mock.Flush() | ||
sendMail() // Send Mail | ||
|
||
// 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("Nahuel", "[email protected]") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please revert to the example name and email. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hello @thinkingserious, mock.StartTestServer() // start mock server
mock.StopTestServer() // end mock server Do I write documentation in a markdown file? Thanks you! |
||
m.SetFrom(from) | ||
|
||
m.AddContent(content) | ||
|
||
personalization := mail.NewPersonalization() | ||
|
||
personalization.AddTos(mail.NewEmail("Nahuel", "[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() | ||
} |
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 | ||
} |
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() | ||
} |
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