[x/blog] blog: user- and test-friendly local server

Log a short message when the blog starts explaining that it's
listening and what port it's listening on. This is helpful to avoid
errors if you start the blog and refresh the page before the server
has started listening on the port.

Add a basic homepage rendering test that should help protect against
regressions.

Change-Id: Ic86c30652714230b730cd4a254e69c10d019246b
Reviewed-on: https://go-review.googlesource.com/49292
Reviewed-by: Chris Broadfoot <cbro@golang.org>
X-Blog-Commit: cad53bf66e7328f5f6bdf7bf02113464370f16a4
diff --git a/blog/blog/local.go b/blog/blog/local.go
index 2e7ab54..676acb5 100644
--- a/blog/blog/local.go
+++ b/blog/blog/local.go
@@ -11,6 +11,7 @@
 import (
 	"flag"
 	"log"
+	"net"
 	"net/http"
 
 	"golang.org/x/tools/blog"
@@ -24,22 +25,36 @@
 	reload       = flag.Bool("reload", false, "reload content on each page load")
 )
 
+func newServer(reload bool, staticPath string, config blog.Config) (http.Handler, error) {
+	mux := http.NewServeMux()
+	if reload {
+		mux.HandleFunc("/", reloadingBlogServer)
+	} else {
+		s, err := blog.NewServer(config)
+		if err != nil {
+			return nil, err
+		}
+		mux.Handle("/", s)
+	}
+	fs := http.FileServer(http.Dir(staticPath))
+	mux.Handle("/static/", http.StripPrefix("/static/", fs))
+	return mux, nil
+}
+
 func main() {
 	flag.Parse()
 	config.ContentPath = *contentPath
 	config.TemplatePath = *templatePath
-	if *reload {
-		http.HandleFunc("/", reloadingBlogServer)
-	} else {
-		s, err := blog.NewServer(config)
-		if err != nil {
-			log.Fatal(err)
-		}
-		http.Handle("/", s)
+	mux, err := newServer(*reload, *staticPath, config)
+	if err != nil {
+		log.Fatal(err)
 	}
-	fs := http.FileServer(http.Dir(*staticPath))
-	http.Handle("/static/", http.StripPrefix("/static/", fs))
-	log.Fatal(http.ListenAndServe(*httpAddr, nil))
+	ln, err := net.Listen("tcp", *httpAddr)
+	if err != nil {
+		log.Fatal(err)
+	}
+	log.Println("Listening on addr", *httpAddr)
+	log.Fatal(http.Serve(ln, mux))
 }
 
 // reloadingBlogServer is an handler that restarts the blog server on each page
diff --git a/blog/blog/local_test.go b/blog/blog/local_test.go
new file mode 100644
index 0000000..b2a4419
--- /dev/null
+++ b/blog/blog/local_test.go
@@ -0,0 +1,33 @@
+// +build !appengine
+
+package main
+
+import (
+	"net/http/httptest"
+	"strings"
+	"testing"
+
+	"golang.org/x/tools/blog"
+)
+
+func TestServer(t *testing.T) {
+	mux, err := newServer(false, "/static", blog.Config{
+		TemplatePath: "../template",
+	})
+	if err != nil {
+		t.Fatal(err)
+	}
+	r := httptest.NewRequest("GET", "/", nil)
+	w := httptest.NewRecorder()
+	mux.ServeHTTP(w, r)
+	if w.Code != 200 {
+		t.Errorf("GET /: want code 200, got %d")
+	}
+	want := "The Go Programming Language Blog"
+	if !strings.Contains(w.Body.String(), want) {
+		t.Errorf("GET /: want to find %q, got\n\n%q", want, w.Body.String())
+	}
+	if hdr := w.Header().Get("Content-Type"); hdr != "text/html; charset=utf-8" {
+		t.Errorf("GET /: want text/html content-type, got %q", hdr)
+	}
+}