Skip to content

Commit aeadcea

Browse files
committed
TESTING: Get up to 95.6% coverage by testing IsRouted, RoutePath.
1 parent e8f3a2a commit aeadcea

File tree

5 files changed

+65
-6
lines changed

5 files changed

+65
-6
lines changed

TODO

-4
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@ TODO:
33
- AppResponseWriter
44
- HTTPMethod
55
- HTTPMethods
6-
- Code coverage
7-
- IsRouted, RoutePath
8-
- PanicHandler
9-
- Invalid handlers?
106
- Go lint
117
- Godoc -- make sure everything is commented.
128
- Investigate sync.Pool

error_test.go

+27
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package web
22

33
import (
4+
"bytes"
45
"fmt"
56
"github.com/stretchr/testify/assert"
7+
"log"
68
"net/http"
9+
"strings"
710
"testing"
811
)
912

@@ -132,6 +135,30 @@ func TestNonRootMiddlewarePanic(t *testing.T) {
132135
assertResponse(t, rw, "Admin Error", 500)
133136
}
134137

138+
func TestPanicLogging(t *testing.T) {
139+
var buf bytes.Buffer
140+
141+
// Set the panichandler to our own time, then set it back after the test is done:
142+
oldHandler := PanicHandler
143+
PanicHandler = logPanicReporter{
144+
log: log.New(&buf, "", 0),
145+
}
146+
defer func() {
147+
PanicHandler = oldHandler
148+
}()
149+
150+
router := New(Context{})
151+
router.Get("/action", (*Context).ErrorAction)
152+
153+
rw, req := newTestRequest("GET", "/action")
154+
router.ServeHTTP(rw, req)
155+
assertResponse(t, rw, "Application Error", 500)
156+
157+
if !strings.HasPrefix(buf.String(), "PANIC") {
158+
t.Error("Expected to have our PanicHandler be logged to.")
159+
}
160+
}
161+
135162
func TestConsistentContext(t *testing.T) {
136163
router := New(Context{})
137164
router.Error((*Context).ErrorHandler)

router_serve.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ func (rootRouter *Router) handlePanic(rw *AppResponseWriter, req *Request, err i
225225
if targetRouter.contextType != curContextStruct.Type() {
226226
context = curContextStruct.Field(0)
227227
if reflect.Indirect(context).Type() != targetRouter.contextType {
228-
panic("oshit why")
228+
panic("bug: shouldn't get here")
229229
}
230230
}
231231
}

tree_test.go renamed to routing_test.go

+36
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package web
33
import (
44
"bytes"
55
"fmt"
6+
"github.com/stretchr/testify/assert"
67
"net/http"
78
"net/http/httptest"
89
"sort"
@@ -325,3 +326,38 @@ func TestRouteVerbs(t *testing.T) {
325326
}
326327
}
327328
}
329+
330+
func TestIsRouted(t *testing.T) {
331+
router := New(Context{})
332+
router.Middleware(func(w ResponseWriter, r *Request, next NextMiddlewareFunc) {
333+
if r.IsRouted() {
334+
panic("shouldn't be routed yet")
335+
}
336+
if r.RoutePath() != "" {
337+
panic("shouldn't have a route path yet")
338+
}
339+
next(w, r)
340+
if !r.IsRouted() {
341+
panic("should be routed")
342+
}
343+
})
344+
subrouter := router.Subrouter(Context{}, "")
345+
subrouter.Middleware(func(w ResponseWriter, r *Request, next NextMiddlewareFunc) {
346+
if !r.IsRouted() {
347+
panic("should be routed")
348+
}
349+
next(w, r)
350+
if !r.IsRouted() {
351+
panic("should be routed")
352+
}
353+
})
354+
subrouter.Get("/a", func(w ResponseWriter, r *Request) {
355+
fmt.Fprintf(w, r.RoutePath())
356+
})
357+
358+
assert.NotPanics(t, func() {
359+
rw, req := newTestRequest("GET", "/a")
360+
router.ServeHTTP(rw, req)
361+
assertResponse(t, rw, "/a", 200)
362+
})
363+
}

tree.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ func (leaf *pathLeaf) match(wildcardValues []string) bool {
125125

126126
// Invariant:
127127
if len(leaf.regexps) != len(wildcardValues) {
128-
panic("bug of some sort")
128+
panic("bug: invariant violated")
129129
}
130130

131131
for i, r := range leaf.regexps {

0 commit comments

Comments
 (0)