[email] More tests.

Bug: skia:13284
Change-Id: I48e6ed062a9b6f3dfc4ef4d4866baac1a7fa836c
Reviewed-on: https://skia-review.googlesource.com/c/buildbot/+/552071
Reviewed-by: Ravi Mistry <rmistry@google.com>
Commit-Queue: Joe Gregorio <jcgregorio@google.com>
diff --git a/email/go/emailservice/BUILD.bazel b/email/go/emailservice/BUILD.bazel
index 0bccc1d..29104bd 100644
--- a/email/go/emailservice/BUILD.bazel
+++ b/email/go/emailservice/BUILD.bazel
@@ -27,6 +27,7 @@
     deps = [
         "//go/metrics2",
         "//go/testutils/unittest",
+        "@com_github_sendgrid_sendgrid_go//:sendgrid-go",
         "@com_github_sendgrid_sendgrid_go//helpers/mail",
         "@com_github_stretchr_testify//require",
     ],
diff --git a/email/go/emailservice/emailservice.go b/email/go/emailservice/emailservice.go
index a4cd701..72c8dd7 100644
--- a/email/go/emailservice/emailservice.go
+++ b/email/go/emailservice/emailservice.go
@@ -143,7 +143,7 @@
 
 // Response is the JSON format of the body the SendGrid API returns.
 type Response struct {
-	Errors []Error `json:"errors"`
+	Errors []Error `json:"errors,omitempty"`
 }
 
 // Handle incoming POST's of RFC2822 formatted emails, which are then parsed and
@@ -157,7 +157,7 @@
 
 	resp, err := a.sendgridClient.Send(m)
 	if err != nil {
-		a.reportSendError(w, err, fmt.Sprintf("Failed to send via API: %q", resp.Body))
+		a.reportSendError(w, err, "Failed to send via API")
 		return
 	}
 
diff --git a/email/go/emailservice/emailservice_test.go b/email/go/emailservice/emailservice_test.go
index 3d38ed0..0c06674 100644
--- a/email/go/emailservice/emailservice_test.go
+++ b/email/go/emailservice/emailservice_test.go
@@ -2,17 +2,21 @@
 
 import (
 	"bytes"
+	"encoding/json"
 	"fmt"
 	"net/http"
 	"net/http/httptest"
 	"testing"
 
+	"github.com/sendgrid/sendgrid-go"
 	"github.com/sendgrid/sendgrid-go/helpers/mail"
 	"github.com/stretchr/testify/require"
 	"go.skia.org/infra/go/metrics2"
 	"go.skia.org/infra/go/testutils/unittest"
 )
 
+const myMesageID = "<abcdef>"
+
 var errMyMockError = fmt.Errorf("my mock error")
 
 const (
@@ -40,7 +44,7 @@
 `
 )
 
-func createAppForTest(t *testing.T) *App {
+func createAppForTest(t *testing.T, handler http.Handler) *App {
 	ret := &App{
 		sendSuccess: metrics2.GetCounter("emailservice_send_success"),
 		sendFailure: metrics2.GetCounter("emailservice_send_failure"),
@@ -48,12 +52,40 @@
 	ret.sendFailure.Reset()
 	ret.sendSuccess.Reset()
 
+	if handler != nil {
+		s := httptest.NewServer(handler)
+		t.Cleanup(s.Close)
+		ret.sendgridClient = &sendgrid.Client{
+			Request: sendgrid.GetRequest("my key", "/v3/mail/send", s.URL),
+		}
+	}
+
 	return ret
 }
 
+func TestAppIncomingEmaiHandler_HappyPath(t *testing.T) {
+	unittest.SmallTest(t)
+	app := createAppForTest(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+		w.Header().Add("X-Message-Id", myMesageID)
+		resp := Response{
+			// No errors in response means the request was a success.
+		}
+		err := json.NewEncoder(w).Encode(resp)
+		require.NoError(t, err)
+	}))
+	w := httptest.NewRecorder()
+	r := httptest.NewRequest("POST", "/send", bytes.NewBufferString(validMessage))
+
+	app.incomingEmaiHandler(w, r)
+	require.Equal(t, http.StatusOK, w.Code)
+	require.Equal(t, myMesageID, w.Header().Get("X-Message-Id"))
+	require.Equal(t, int64(0), app.sendFailure.Get())
+	require.Equal(t, int64(1), app.sendSuccess.Get())
+}
+
 func TestAppIncomingEmaiHandler_RequestBodyIsInvalidRFC2822Message_ReturnsHTTPError(t *testing.T) {
 	unittest.SmallTest(t)
-	app := createAppForTest(t)
+	app := createAppForTest(t, nil)
 	w := httptest.NewRecorder()
 	r := httptest.NewRequest("POST", "/send", bytes.NewBufferString(""))
 
@@ -64,6 +96,30 @@
 	require.Equal(t, int64(0), app.sendSuccess.Get())
 }
 
+func TestAppIncomingEmaiHandler_ServerReturnsError_ReturnsHTTPError(t *testing.T) {
+	unittest.SmallTest(t)
+	app := createAppForTest(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+		resp := Response{
+			Errors: []Error{
+				{
+					Message: "something failed on the server",
+				},
+			},
+		}
+		err := json.NewEncoder(w).Encode(resp)
+		require.NoError(t, err)
+		w.WriteHeader(http.StatusInternalServerError)
+	}))
+	w := httptest.NewRecorder()
+	r := httptest.NewRequest("POST", "/send", bytes.NewBufferString(validMessage))
+
+	app.incomingEmaiHandler(w, r)
+	require.Equal(t, http.StatusBadRequest, w.Code)
+	require.Contains(t, w.Body.String(), "something failed on the server")
+	require.Equal(t, int64(1), app.sendFailure.Get())
+	require.Equal(t, int64(0), app.sendSuccess.Get())
+}
+
 func TestConvertRFC2822ToSendGrid_HappyPath(t *testing.T) {
 	unittest.SmallTest(t)
 	body := bytes.NewBufferString(`From: Alert Service <alerts@skia.org>