Permalink
Browse files

Moving the logger tests into a seperate file

  • Loading branch information...
paulbellamy committed Jun 11, 2011
1 parent ad0be06 commit 1efa83ffe930f08b76ebe72478f30803feb5700f
Showing with 45 additions and 38 deletions.
  1. +45 −0 logger_test.go
  2. +0 −38 mango_test.go
View
@@ -0,0 +1,45 @@
package mango
import (
"bytes"
"http"
"log"
"testing"
"runtime"
)
var loggerBuffer = &bytes.Buffer{}
func loggerTestServer(env Env) (Status, Headers, Body) {
env.Logger().Println("Never gonna give you up")
return 200, Headers{}, Body("Hello World!")
}
func init() {
runtime.GOMAXPROCS(4)
}
func TestLogger(t *testing.T) {
// Compile the stack
loggerStack := new(Stack)
custom_logger := log.New(loggerBuffer, "prefixed:", 0)
loggerStack.Middleware(Logger(custom_logger))
loggerApp := loggerStack.Compile(loggerTestServer)
// Request against it
request, err := http.NewRequest("GET", "http://localhost:3000/", nil)
status, _, _ := loggerApp(Env{"mango.request": &Request{request}})
if err != nil {
t.Error(err)
}
if status != 200 {
t.Error("Expected status to equal 200, got:", status)
}
expected := "prefixed:Never gonna give you up\n"
if loggerBuffer.String() != expected {
t.Error("Expected logger to print: \"", expected, "\" got: \"", loggerBuffer.String(), "\"")
}
}
View
@@ -4,25 +4,17 @@ import (
"http"
"io/ioutil"
"testing"
"bytes"
"fmt"
"log"
"runtime"
)
var testServer = Stack{}
var loggerBuffer = &bytes.Buffer{}
var client = http.Client{}
func helloWorld(env Env) (Status, Headers, Body) {
return 200, Headers{}, Body("Hello World!")
}
func loggerTestServer(env Env) (Status, Headers, Body) {
env.Logger().Println("Never gonna give you up")
return 200, Headers{}, Body("Hello World!")
}
func showErrorsTestServer(env Env) (Status, Headers, Body) {
panic("foo!")
return 200, Headers{}, Body("Hello World!")
@@ -37,11 +29,6 @@ func init() {
testRoutes["/hello"] = new(Stack).Compile(helloWorld)
loggerStack := new(Stack)
custom_logger := log.New(loggerBuffer, "prefixed:", 0)
loggerStack.Middleware(Logger(custom_logger))
testRoutes["/logger"] = loggerStack.Compile(loggerTestServer)
showErrorsStack := new(Stack)
showErrorsStack.Middleware(ShowErrors("<html><body>{Error|html}</body></html>"))
testRoutes["/show_errors"] = showErrorsStack.Compile(showErrorsTestServer)
@@ -51,7 +38,6 @@ func init() {
fullStackTestRoutes["/full_stack/[0-9]+"] = routingATestServer
fullStackTestRoutes["/full_stack/[a-z]+"] = routingBTestServer
fullStack.Middleware(ShowErrors("<html><body>{Error|html}</body></html>"),
Logger(custom_logger),
Routing(fullStackTestRoutes))
testRoutes["/full_stack(.*)"] = fullStack.Compile(helloWorld)
@@ -84,30 +70,6 @@ func BenchmarkHelloWorld(b *testing.B) {
}
}
func TestLogger(t *testing.T) {
// Request against it
response, err := client.Get("http://localhost:3000/logger")
if err != nil {
t.Error(err)
}
if response.StatusCode != 200 {
t.Error("Expected status to equal 200, got:", response.StatusCode)
}
expected := "prefixed:Never gonna give you up\n"
if loggerBuffer.String() != expected {
t.Error("Expected logger to print: \"", expected, "\" got: \"", loggerBuffer.String(), "\"")
}
}
func BenchmarkLogger(b *testing.B) {
for i := 0; i < b.N; i++ {
client.Get("http://localhost:3000/logger")
}
}
func TestShowErrors(t *testing.T) {
// Request against it
response, err := client.Get("http://localhost:3000/show_errors")

0 comments on commit 1efa83f

Please sign in to comment.