Skip to content
This repository
branch: weekly.2012-03…
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 109 lines (86 sloc) 2.64 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
package mango

import (
"net/http"
"testing"
)

func routingTestServer(env Env) (Status, Headers, Body) {
return 200, Headers{}, Body("Hello World!")
}

func routingATestServer(env Env) (Status, Headers, Body) {
return 200, Headers{}, Body("Server A")
}

func routingBTestServer(env Env) (Status, Headers, Body) {
return 200, Headers{}, Body("Server B")
}

func TestRoutingSuccess(t *testing.T) {
// Compile the stack
routingStack := new(Stack)
routes := make(map[string]App)
routes["/a"] = routingATestServer
routes["/b"] = routingBTestServer
routingStack.Middleware(Routing(routes))
routingApp := routingStack.Compile(routingTestServer)

// Request against A
request, err := http.NewRequest("GET", "http://localhost:3000/a", nil)
status, _, body := routingApp(Env{"mango.request": &Request{request}})

if err != nil {
t.Error(err)
}

if status != 200 {
t.Error("Expected status to equal 200, got:", status)
}

expected := "Server A"
if string(body) != expected {
t.Error("Expected body:", string(body), "to equal:", expected)
}

// Request against B
request, err = http.NewRequest("GET", "http://localhost:3000/b", nil)
status, _, body = routingApp(Env{"mango.request": &Request{request}})

if err != nil {
t.Error(err)
}

if status != 200 {
t.Error("Expected status to equal 200, got:", status)
}

expected = "Server B"
if string(body) != expected {
t.Error("Expected body:", string(body), "to equal:", expected)
}
}

func TestRoutingFailure(t *testing.T) {
// Compile the stack
routingStack := new(Stack)
routes := make(map[string]App)
routes["/a"] = routingATestServer
routes["/b"] = routingBTestServer
routingStack.Middleware(Routing(routes))
routingApp := routingStack.Compile(routingTestServer)

// Request against it
request, err := http.NewRequest("GET", "http://localhost:3000/c", nil)
status, _, body := routingApp(Env{"mango.request": &Request{request}})

if err != nil {
t.Error(err)
}

if status != 200 {
t.Error("Expected status to equal 200, got:", status)
}

expected := "Hello World!"
if string(body) != expected {
t.Error("Expected body:", string(body), "to equal:", expected)
}
}

func BenchmarkRouting(b *testing.B) {
b.StopTimer()

routingStack := new(Stack)
routes := make(map[string]App)
routes["/a"] = routingATestServer
routes["/b"] = routingBTestServer
routingStack.Middleware(Routing(routes))
routingApp := routingStack.Compile(routingTestServer)

request, _ := http.NewRequest("GET", "http://localhost:3000/a", nil)

b.StartTimer()
for i := 0; i < b.N; i++ {
routingApp(Env{"mango.request": &Request{request}})
}
b.StopTimer()
}
Something went wrong with that request. Please try again.