Beispiel #1
0
func ExampleLog() {
	logger := log.New(os.Stdout, "[go-web] ", log.LstdFlags)
	mux := httpmux.New()
	mux.UseFunc(httplog.DefaultFormat(logger))
	mux.GET("/", func(w http.ResponseWriter, r *http.Request) {
		httplog.Errorf(r, "Todos são manos, eeei. Todos são hu-manos.")
	})
	http.ListenAndServe(":8080", mux)
}
Beispiel #2
0
func Example() {
	// curl -i localhost:8080
	// curl -i -XPOST --basic -u foobar:foobared localhost:8080/auth/login
	root := httpmux.New()
	root.GET("/", func(w http.ResponseWriter, r *http.Request) {
		io.WriteString(w, "hello, world\n")
	})
	auth := httpmux.New()
	{
		auth.UseFunc(authHandler)
		auth.POST("/login", func(w http.ResponseWriter, r *http.Request) {
			u := httpmux.Context(r).Value("user")
			fmt.Fprintln(w, "hello,", u)
		})
	}
	root.Append("/auth", auth)
	http.ListenAndServe(":8080", root)
}
Beispiel #3
0
func TestErrorln(t *testing.T) {
	var ctx context.Context
	mux := httpmux.New()
	mux.GET("/", func(w http.ResponseWriter, r *http.Request) {
		Errorln(r, "hello", "world")
		ctx = httpmux.Context(r)
	})
	r := &http.Request{Method: "GET", URL: &url.URL{Path: "/"}}
	mux.ServeHTTP(&httptest.ResponseRecorder{}, r)
	if ctx.Value(ErrorID) != "hello world\n" {
		t.Fatalf("Unexpected value. Want \"hello world\\n\", have %v", ctx.Value("error"))
	}
}
Beispiel #4
0
func TestApacheCombinedFormat(t *testing.T) {
	var b bytes.Buffer
	l := log.New(&b, "", 0)
	mux := httpmux.New()
	mux.UseFunc(ApacheCommonFormat(l))
	mux.GET("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(http.StatusNotFound)
		io.WriteString(w, "foobar")
	}))
	r := &http.Request{
		Method: "GET",
		URL:    &url.URL{Path: "/"},
	}
	w := &httptest.ResponseRecorder{}
	mux.ServeHTTP(w, r)
	// TODO: Test the format.
}
Beispiel #5
0
func TestDefaultFormat(t *testing.T) {
	var b bytes.Buffer
	l := log.New(&b, "", 0)
	mux := httpmux.New()
	mux.UseFunc(DefaultFormat(l))
	mux.GET("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(http.StatusNotFound)
		io.WriteString(w, "foobar")
	}))
	r := &http.Request{
		Proto:  "HTTP/1.1",
		Method: "GET",
		URL:    &url.URL{Path: "/"},
	}
	w := &httptest.ResponseRecorder{}
	mux.ServeHTTP(w, r)
	// TODO: Test the format.
	if !strings.HasPrefix(b.String(), "HTTP/1.1 404") {
		t.Fatalf("Unexpected data. Want HTTP/1.1 404, have %q", b.String())
	}
}