-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.go
55 lines (47 loc) · 1.34 KB
/
helpers.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
// Copyright 2022 Sylvain Müller. All rights reserved.
// Mount of this source code is governed by a Apache-2.0 license that can be found
// at https://github.com/tigerwill90/fox/blob/master/LICENSE.txt.
package fox
import (
"net/http"
"testing"
)
// NewTestContext returns a new Router and its associated Context, designed only for testing purpose.
func NewTestContext(w http.ResponseWriter, r *http.Request) (*Router, Context) {
fox := New()
c := NewTestContextOnly(fox, w, r)
return fox, c
}
// NewTestContextOnly returns a new Context associated with the provided Router, designed only for testing purpose.
func NewTestContextOnly(fox *Router, w http.ResponseWriter, r *http.Request) Context {
return newTextContextOnly(fox, w, r)
}
func newTextContextOnly(fox *Router, w http.ResponseWriter, r *http.Request) *cTx {
c := fox.Tree().allocateContext()
c.resetNil()
c.req = r
c.rec.reset(w)
c.w = &c.rec
c.scope = AllHandlers
return c
}
func newTestContextTree(t *Tree) *cTx {
c := t.allocateContext()
c.resetNil()
return c
}
func newResponseWriter(w http.ResponseWriter) ResponseWriter {
return &recorder{
ResponseWriter: w,
size: notWritten,
status: http.StatusOK,
}
}
func unwrapContext(t *testing.T, c Context) *cTx {
t.Helper()
cc, ok := c.(*cTx)
if !ok {
t.Fatal("unable to unwrap context")
}
return cc
}