forked from go-playground/lars
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil_test.go
94 lines (76 loc) · 2.33 KB
/
util_test.go
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
package lars
import (
"fmt"
"net/http"
"reflect"
"strconv"
"testing"
. "gopkg.in/go-playground/assert.v1"
)
// NOTES:
// - Run "go test" to run tests
// - Run "gocov test | gocov report" to report on test converage by file
// - Run "gocov test | gocov annotate -" to report on all code and functions, those ,marked with "MISS" were never called
//
// or
//
// -- may be a good idea to change to output path to somewherelike /tmp
// go test -coverprofile cover.out && go tool cover -html=cover.out -o cover.html
//
// LogResponseWritter wraps the standard http.ResponseWritter allowing for more
// verbose logging
type logResponseWritter struct {
status int
size int
http.ResponseWriter
}
// Status provides an easy way to retrieve the status code
func (w *logResponseWritter) Status() int {
return w.status
}
// Size provides an easy way to retrieve the response size in bytes
func (w *logResponseWritter) Size() int {
return w.size
}
// Header returns & satisfies the http.ResponseWriter interface
func (w *logResponseWritter) Header() http.Header {
return w.ResponseWriter.Header()
}
// Write satisfies the http.ResponseWriter interface and
// captures data written, in bytes
func (w *logResponseWritter) Write(data []byte) (int, error) {
written, err := w.ResponseWriter.Write(data)
w.size += written
return written, err
}
// WriteHeader satisfies the http.ResponseWriter interface and
// allows us to cach the status code
func (w *logResponseWritter) WriteHeader(statusCode int) {
w.status = statusCode
w.ResponseWriter.WriteHeader(statusCode)
}
func loggingRecoveryHandler(w http.ResponseWriter, r *http.Request) {
res := w.(*Response)
wr := &logResponseWritter{status: 200, ResponseWriter: res.Writer()}
res.SetWriter(wr)
}
func TestOverridingResponseWriterNative(t *testing.T) {
l := New()
l.Use(loggingRecoveryHandler)
l.Get("/test", func(c Context) {
if _, err := c.Response().Write([]byte(fmt.Sprint(reflect.TypeOf(c.Response().ResponseWriter)))); err != nil {
panic(err)
}
})
code, body := request(GET, "/test", l)
Equal(t, code, http.StatusOK)
Equal(t, body, "*lars.logResponseWritter")
}
func TestTooManyParams(t *testing.T) {
s := "/"
for i := 0; i < 256; i++ {
s += ":id" + strconv.Itoa(i)
}
l := New()
PanicMatches(t, func() { l.Get(s, func(c Context) {}) }, "too many parameters defined in path, max is 255")
}