Пример #1
0
// LearnMore takes the email from the user and sends them a confirmation email
// and sends me an alert they signed up.
func LearnMore(w http.ResponseWriter, r *http.Request) {
	var post struct {
		Email string `json:"email"`
	}

	body, err := ioutil.ReadAll(r.Body)
	if err != nil {
		errStr := "Error reading request body."

		fmt.Printf(errStr)
		http.Error(w, errStr, http.StatusInternalServerError)

		return
	}

	err = json.Unmarshal(body, &post)
	if err != nil || post.Email == "" {
		errStr := "Improperly formatted request - must specify an email."

		fmt.Printf(errStr)
		http.Error(w, errStr, http.StatusInternalServerError)

		return
	}

	// Kick off two email jobs. These jobs won't delay returning the http
	// status, because they are run on a separate thread. Pass the Mailer
	// so depending on the environment, different mailers can be used.
	go services.EmailNewSignup(post.Email, HandlersMailer)
	go services.AlertNewSignup(post.Email, HandlersMailer)

	// Return a status code of 201.
	w.WriteHeader(http.StatusCreated)
}
// TestEmailNewSignup tests sending an email to a new signup.
func TestEmailNewSignup(t *testing.T) {
	mailer := mailers.NewTestMailer()

	services.EmailNewSignup("*****@*****.**", mailer)

	if mailer.Delivered() != 1 {
		t.Fatal("The mailer should have delivered a single welcome email.")
	}
}