-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.go
55 lines (47 loc) · 1.36 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, opts ...GlobalOption) (*Router, Context) {
fox := New(opts...)
c := newTextContextOnly(fox, w, r)
return fox, c
}
// NewTestContextOnly returns a new [Context] designed only for testing purpose.
func NewTestContextOnly(w http.ResponseWriter, r *http.Request, opts ...GlobalOption) Context {
fox := New(opts...)
return newTextContextOnly(fox, w, r)
}
func newTextContextOnly(fox *Router, w http.ResponseWriter, r *http.Request) *cTx {
tree := fox.getRoot()
c := tree.allocateContext()
c.resetNil()
c.reset(w, r)
return c
}
func newTestContext(fox *Router) *cTx {
tree := fox.getRoot()
c := tree.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
}