-
Notifications
You must be signed in to change notification settings - Fork 0
/
pochi_example_test.go
113 lines (102 loc) · 3.01 KB
/
pochi_example_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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package pochi_test
import (
"fmt"
"io"
"log/slog"
"net/http"
"net/http/httptest"
"os"
"time"
"github.com/lestrrat-go/accesslog"
"github.com/lestrrat-go/pochi"
"github.com/lestrrat-go/pochi/middleware"
)
// This function creates an accesslog middleware that emits static values
// for the purposes of testing
func exampleAccessLog() *accesslog.Middleware {
return middleware.AccessLog().
// Use a static clock to get static output for testing
Clock(accesslog.StaticClock(time.Time{})).
Logger(
slog.New(
slog.NewJSONHandler(os.Stdout,
&slog.HandlerOptions{
ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
switch a.Key {
case slog.TimeKey:
// replace time to get static output for testing
return slog.Time(slog.TimeKey, time.Time{})
case "remote_addr":
// replace value to get static output for testing
return slog.String("remote_addr", "127.0.0.1:99999")
}
return a
},
},
),
),
)
}
func ExampleRouter() {
r := pochi.NewRouter()
if err := r.Route(
pochi.Path("/foo/bar/").
Use(exampleAccessLog()),
pochi.Path("/foo/bar/regular").
Get(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "Hello, World! (inherits from /)")
})),
pochi.Path("/foo/bar/nomiddlewares").
Inherit(false).
Get(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "Hello, World! (no accesslog)")
})),
); err != nil {
fmt.Println(err)
return
}
// Traverse all routes
fmt.Println("--- All registered paths ---")
pochi.Walk(r, pochi.RouteVisitFunc(func(fullpath string, spec *pochi.PathSpec) bool {
fmt.Printf("Path: %s\n", fullpath)
return true
}))
srv := httptest.NewServer(r)
defer srv.Close()
for _, path := range []string{"/foo/bar/regular", "/foo/bar/nomiddlewares"} {
fmt.Printf("Issuing GET request to %q\n", path)
res, err := srv.Client().Get(srv.URL + path)
if err != nil {
fmt.Println(err)
return
}
defer res.Body.Close()
fmt.Println(res.StatusCode)
buf, _ := io.ReadAll(res.Body)
fmt.Println(string(buf))
}
// This would fail because the path "/foo/" does not have a handler
res, err := srv.Client().Get(srv.URL + "/foo/bar")
if err != nil {
fmt.Println(err)
return
}
defer res.Body.Close()
if res.StatusCode != http.StatusNotFound {
fmt.Printf("Expected status code 404, but got %d\n", res.StatusCode)
}
// OUTPUT:
// --- All registered paths ---
// Path: /foo/bar/
// Path: /foo/bar/nomiddlewares
// Path: /foo/bar/regular
// Issuing GET request to "/foo/bar/regular"
// {"time":"0001-01-01T00:00:00Z","level":"INFO","msg":"access","remote_addr":"127.0.0.1:99999","http_method":"GET","path":"/foo/bar/regular","status":200,"body_bytes_sent":31,"http_referer":"","http_user_agent":"Go-http-client/1.1"}
// 200
// Hello, World! (inherits from /)
// Issuing GET request to "/foo/bar/nomiddlewares"
// 200
// Hello, World! (no accesslog)
}