Example #1
0
// Return a reverse proxy that forwards requests to the given port.
func NewHarness() *Harness {
	// Get a template loader to render errors.
	// Prefer the app's views/errors directory, and fall back to the stock error pages.
	revel.MainTemplateLoader = revel.NewTemplateLoader(
		[]string{path.Join(revel.RevelPath, "templates")})
	revel.MainTemplateLoader.Refresh()

	addr := revel.HttpAddr
	port := revel.Config.IntDefault("harness.port", 0)
	scheme := "http"
	if revel.HttpSsl {
		scheme = "https"
	}

	// If the server is running on the wildcard address, use "localhost"
	if addr == "" {
		addr = "localhost"
	}

	if port == 0 {
		port = getFreePort()
	}

	serverUrl, _ := url.ParseRequestURI(fmt.Sprintf(scheme+"://%s:%d", addr, port))

	harness := &Harness{
		port:       port,
		serverHost: serverUrl.String()[len(scheme+"://"):],
		proxy:      httputil.NewSingleHostReverseProxy(serverUrl),
	}

	if revel.HttpSsl {
		harness.proxy.Transport = &http.Transport{
			TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
		}
	}
	return harness
}
Example #2
0
func TestSendFromTemplate(t *testing.T) {
	server := `220 hello world
502 EH?
250 mx.google.com at your service
250 Ok resetting state
250 Sender ok
250 Receiver ok
250 Receiver ok
354 Go ahead
250 Data ok
221 Goodbye
`

	snippet1 := `Hello 世界

Welcome Ark, please click the link:
http://www.arkxu.com
`

	snippet2 := `<h1>Hello 世界</h1>

<p>Welcome Ark, please click the link:</p>
<a href="http://www.arkxu.com">http://www.arkxu.com</a>
`
	mailer := &Mailer{Server: "smtp.gmail.com", Port: 587, UserName: "******", Password: "******"}
	mailer.Sender = &Sender{From: "*****@*****.**"}

	// reset the revel template loader for testing purpose
	viewPath, _ := os.Getwd()
	revel.MainTemplateLoader = revel.NewTemplateLoader([]string{viewPath})
	revel.MainTemplateLoader.Refresh()

	// arguments used for template rendering
	var args = make(map[string]interface{})
	args["world"] = "世界"
	args["user"] = struct {
		Name string
		Link string
	}{
		"Ark",
		"http://www.arkxu.com",
	}

	message := &Message{To: []string{"*****@*****.**"}, Subject: "from template 6", Cc: []string{"*****@*****.**"}}
	err := message.RenderTemplate("testdata/testTemplate", args)
	if err != nil {
		fmt.Println(err)
	}

	actualcmds, err := mailer.SendTestMessage(server, message)
	if err != nil {
		fmt.Println(err)
	}

	if strings.Contains(actualcmds, snippet1) {
		t.Errorf("it should contains %s\n", snippet1)
	}

	if strings.Contains(actualcmds, snippet2) {
		t.Errorf("it should contains %s\n", snippet2)
	}

}