Harbor format: 871 coding_baseline tasks (part 20)
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
- tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/environment/source/middleware/nocache.go +59 -0
- tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/environment/source/middleware/page_route.go +20 -0
- tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/environment/source/middleware/path_rewrite.go +16 -0
- tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/environment/source/middleware/profiler.go +49 -0
- tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/environment/source/middleware/realip.go +56 -0
- tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/environment/source/middleware/recoverer.go +203 -0
- tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/environment/source/middleware/request_id.go +96 -0
- tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/environment/source/middleware/request_size.go +18 -0
- tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/environment/source/middleware/route_headers.go +145 -0
- tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/environment/source/middleware/strip.go +70 -0
- tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/environment/source/middleware/sunset.go +25 -0
- tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/environment/source/middleware/supress_notfound.go +27 -0
- tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/environment/source/middleware/terminal.go +63 -0
- tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/environment/source/middleware/throttle.go +140 -0
- tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/environment/source/middleware/timeout.go +48 -0
- tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/environment/source/middleware/url_format.go +75 -0
- tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/environment/source/middleware/value.go +17 -0
- tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/environment/source/middleware/wrap_writer.go +241 -0
- tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/environment/source/mux.go +527 -0
- tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/environment/source/path_value.go +21 -0
- tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/environment/source/path_value_fallback.go +19 -0
- tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/environment/source/tree.go +890 -0
- tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/instruction.md +33 -0
- tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/provenance.json +653 -0
- tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/solution/solve.sh +12 -0
- tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/task.toml +11 -0
- tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/tests/frozen/context_test.go +93 -0
- tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/tests/frozen/middleware/compress_test.go +219 -0
- tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/tests/frozen/middleware/content_charset_test.go +127 -0
- tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/tests/frozen/middleware/content_encoding_test.go +82 -0
- tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/tests/frozen/middleware/content_type_test.go +82 -0
- tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/tests/frozen/middleware/get_head_test.go +66 -0
- tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/tests/frozen/middleware/logger_test.go +51 -0
- tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/tests/frozen/middleware/middleware_test.go +154 -0
- tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/tests/frozen/middleware/realip_test.go +115 -0
- tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/tests/frozen/middleware/recoverer_test.go +67 -0
- tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/tests/frozen/middleware/request_id_test.go +71 -0
- tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/tests/frozen/middleware/strip_test.go +273 -0
- tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/tests/frozen/middleware/sunset_test.go +104 -0
- tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/tests/frozen/middleware/throttle_test.go +301 -0
- tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/tests/frozen/middleware/url_format_test.go +69 -0
- tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/tests/frozen/middleware/wrap_writer_test.go +86 -0
- tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/tests/frozen/mux_test.go +2042 -0
- tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/tests/frozen/path_value_test.go +77 -0
- tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/tests/frozen/testdata/cert.pem +19 -0
- tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/tests/frozen/testdata/key.pem +27 -0
- tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/tests/frozen/tree_test.go +523 -0
- tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/tests/grade.py +157 -0
- tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/tests/manifest.json +752 -0
- tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/tests/test.sh +3 -0
tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/environment/source/middleware/nocache.go
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
package middleware
|
| 2 |
+
|
| 3 |
+
// Ported from Goji's middleware, source:
|
| 4 |
+
// https://github.com/zenazn/goji/tree/master/web/middleware
|
| 5 |
+
|
| 6 |
+
import (
|
| 7 |
+
"net/http"
|
| 8 |
+
"time"
|
| 9 |
+
)
|
| 10 |
+
|
| 11 |
+
// Unix epoch time
|
| 12 |
+
var epoch = time.Unix(0, 0).UTC().Format(http.TimeFormat)
|
| 13 |
+
|
| 14 |
+
// Taken from https://github.com/mytrile/nocache
|
| 15 |
+
var noCacheHeaders = map[string]string{
|
| 16 |
+
"Expires": epoch,
|
| 17 |
+
"Cache-Control": "no-cache, no-store, no-transform, must-revalidate, private, max-age=0",
|
| 18 |
+
"Pragma": "no-cache",
|
| 19 |
+
"X-Accel-Expires": "0",
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
var etagHeaders = []string{
|
| 23 |
+
"ETag",
|
| 24 |
+
"If-Modified-Since",
|
| 25 |
+
"If-Match",
|
| 26 |
+
"If-None-Match",
|
| 27 |
+
"If-Range",
|
| 28 |
+
"If-Unmodified-Since",
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
// NoCache is a simple piece of middleware that sets a number of HTTP headers to prevent
|
| 32 |
+
// a router (or subrouter) from being cached by an upstream proxy and/or client.
|
| 33 |
+
//
|
| 34 |
+
// As per http://wiki.nginx.org/HttpProxyModule - NoCache sets:
|
| 35 |
+
//
|
| 36 |
+
// Expires: Thu, 01 Jan 1970 00:00:00 UTC
|
| 37 |
+
// Cache-Control: no-cache, private, max-age=0
|
| 38 |
+
// X-Accel-Expires: 0
|
| 39 |
+
// Pragma: no-cache (for HTTP/1.0 proxies/clients)
|
| 40 |
+
func NoCache(h http.Handler) http.Handler {
|
| 41 |
+
fn := func(w http.ResponseWriter, r *http.Request) {
|
| 42 |
+
|
| 43 |
+
// Delete any ETag headers that may have been set
|
| 44 |
+
for _, v := range etagHeaders {
|
| 45 |
+
if r.Header.Get(v) != "" {
|
| 46 |
+
r.Header.Del(v)
|
| 47 |
+
}
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
// Set our NoCache headers
|
| 51 |
+
for k, v := range noCacheHeaders {
|
| 52 |
+
w.Header().Set(k, v)
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
h.ServeHTTP(w, r)
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
return http.HandlerFunc(fn)
|
| 59 |
+
}
|
tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/environment/source/middleware/page_route.go
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
package middleware
|
| 2 |
+
|
| 3 |
+
import (
|
| 4 |
+
"net/http"
|
| 5 |
+
"strings"
|
| 6 |
+
)
|
| 7 |
+
|
| 8 |
+
// PageRoute is a simple middleware which allows you to route a static GET request
|
| 9 |
+
// at the middleware stack level.
|
| 10 |
+
func PageRoute(path string, handler http.Handler) func(http.Handler) http.Handler {
|
| 11 |
+
return func(next http.Handler) http.Handler {
|
| 12 |
+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
| 13 |
+
if r.Method == "GET" && strings.EqualFold(r.URL.Path, path) {
|
| 14 |
+
handler.ServeHTTP(w, r)
|
| 15 |
+
return
|
| 16 |
+
}
|
| 17 |
+
next.ServeHTTP(w, r)
|
| 18 |
+
})
|
| 19 |
+
}
|
| 20 |
+
}
|
tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/environment/source/middleware/path_rewrite.go
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
package middleware
|
| 2 |
+
|
| 3 |
+
import (
|
| 4 |
+
"net/http"
|
| 5 |
+
"strings"
|
| 6 |
+
)
|
| 7 |
+
|
| 8 |
+
// PathRewrite is a simple middleware which allows you to rewrite the request URL path.
|
| 9 |
+
func PathRewrite(old, new string) func(http.Handler) http.Handler {
|
| 10 |
+
return func(next http.Handler) http.Handler {
|
| 11 |
+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
| 12 |
+
r.URL.Path = strings.Replace(r.URL.Path, old, new, 1)
|
| 13 |
+
next.ServeHTTP(w, r)
|
| 14 |
+
})
|
| 15 |
+
}
|
| 16 |
+
}
|
tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/environment/source/middleware/profiler.go
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
//go:build !tinygo
|
| 2 |
+
// +build !tinygo
|
| 3 |
+
|
| 4 |
+
package middleware
|
| 5 |
+
|
| 6 |
+
import (
|
| 7 |
+
"expvar"
|
| 8 |
+
"net/http"
|
| 9 |
+
"net/http/pprof"
|
| 10 |
+
|
| 11 |
+
"github.com/go-chi/chi/v5"
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
// Profiler is a convenient subrouter used for mounting net/http/pprof. ie.
|
| 15 |
+
//
|
| 16 |
+
// func MyService() http.Handler {
|
| 17 |
+
// r := chi.NewRouter()
|
| 18 |
+
// // ..middlewares
|
| 19 |
+
// r.Mount("/debug", middleware.Profiler())
|
| 20 |
+
// // ..routes
|
| 21 |
+
// return r
|
| 22 |
+
// }
|
| 23 |
+
func Profiler() http.Handler {
|
| 24 |
+
r := chi.NewRouter()
|
| 25 |
+
r.Use(NoCache)
|
| 26 |
+
|
| 27 |
+
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
| 28 |
+
http.Redirect(w, r, r.RequestURI+"/pprof/", http.StatusMovedPermanently)
|
| 29 |
+
})
|
| 30 |
+
r.HandleFunc("/pprof", func(w http.ResponseWriter, r *http.Request) {
|
| 31 |
+
http.Redirect(w, r, r.RequestURI+"/", http.StatusMovedPermanently)
|
| 32 |
+
})
|
| 33 |
+
|
| 34 |
+
r.HandleFunc("/pprof/*", pprof.Index)
|
| 35 |
+
r.HandleFunc("/pprof/cmdline", pprof.Cmdline)
|
| 36 |
+
r.HandleFunc("/pprof/profile", pprof.Profile)
|
| 37 |
+
r.HandleFunc("/pprof/symbol", pprof.Symbol)
|
| 38 |
+
r.HandleFunc("/pprof/trace", pprof.Trace)
|
| 39 |
+
r.Handle("/vars", expvar.Handler())
|
| 40 |
+
|
| 41 |
+
r.Handle("/pprof/goroutine", pprof.Handler("goroutine"))
|
| 42 |
+
r.Handle("/pprof/threadcreate", pprof.Handler("threadcreate"))
|
| 43 |
+
r.Handle("/pprof/mutex", pprof.Handler("mutex"))
|
| 44 |
+
r.Handle("/pprof/heap", pprof.Handler("heap"))
|
| 45 |
+
r.Handle("/pprof/block", pprof.Handler("block"))
|
| 46 |
+
r.Handle("/pprof/allocs", pprof.Handler("allocs"))
|
| 47 |
+
|
| 48 |
+
return r
|
| 49 |
+
}
|
tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/environment/source/middleware/realip.go
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
package middleware
|
| 2 |
+
|
| 3 |
+
// Ported from Goji's middleware, source:
|
| 4 |
+
// https://github.com/zenazn/goji/tree/master/web/middleware
|
| 5 |
+
|
| 6 |
+
import (
|
| 7 |
+
"net"
|
| 8 |
+
"net/http"
|
| 9 |
+
"strings"
|
| 10 |
+
)
|
| 11 |
+
|
| 12 |
+
var trueClientIP = http.CanonicalHeaderKey("True-Client-IP")
|
| 13 |
+
var xForwardedFor = http.CanonicalHeaderKey("X-Forwarded-For")
|
| 14 |
+
var xRealIP = http.CanonicalHeaderKey("X-Real-IP")
|
| 15 |
+
|
| 16 |
+
// RealIP is a middleware that sets a http.Request's RemoteAddr to the results
|
| 17 |
+
// of parsing either the True-Client-IP, X-Real-IP or the X-Forwarded-For headers
|
| 18 |
+
// (in that order).
|
| 19 |
+
//
|
| 20 |
+
// This middleware should be inserted fairly early in the middleware stack to
|
| 21 |
+
// ensure that subsequent layers (e.g., request loggers) which examine the
|
| 22 |
+
// RemoteAddr will see the intended value.
|
| 23 |
+
//
|
| 24 |
+
// You should only use this middleware if you can trust the headers passed to
|
| 25 |
+
// you (in particular, the three headers this middleware uses), for example
|
| 26 |
+
// because you have placed a reverse proxy like HAProxy or nginx in front of
|
| 27 |
+
// chi. If your reverse proxies are configured to pass along arbitrary header
|
| 28 |
+
// values from the client, or if you use this middleware without a reverse
|
| 29 |
+
// proxy, malicious clients will be able to make you very sad (or, depending on
|
| 30 |
+
// how you're using RemoteAddr, vulnerable to an attack of some sort).
|
| 31 |
+
func RealIP(h http.Handler) http.Handler {
|
| 32 |
+
fn := func(w http.ResponseWriter, r *http.Request) {
|
| 33 |
+
if rip := realIP(r); rip != "" {
|
| 34 |
+
r.RemoteAddr = rip
|
| 35 |
+
}
|
| 36 |
+
h.ServeHTTP(w, r)
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
return http.HandlerFunc(fn)
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
func realIP(r *http.Request) string {
|
| 43 |
+
var ip string
|
| 44 |
+
|
| 45 |
+
if tcip := r.Header.Get(trueClientIP); tcip != "" {
|
| 46 |
+
ip = tcip
|
| 47 |
+
} else if xrip := r.Header.Get(xRealIP); xrip != "" {
|
| 48 |
+
ip = xrip
|
| 49 |
+
} else if xff := r.Header.Get(xForwardedFor); xff != "" {
|
| 50 |
+
ip, _, _ = strings.Cut(xff, ",")
|
| 51 |
+
}
|
| 52 |
+
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
package middleware
|
| 2 |
+
|
| 3 |
+
// The original work was derived from Goji's middleware, source:
|
| 4 |
+
// https://github.com/zenazn/goji/tree/master/web/middleware
|
| 5 |
+
|
| 6 |
+
import (
|
| 7 |
+
"bytes"
|
| 8 |
+
"errors"
|
| 9 |
+
"fmt"
|
| 10 |
+
"io"
|
| 11 |
+
"net/http"
|
| 12 |
+
"os"
|
| 13 |
+
"runtime/debug"
|
| 14 |
+
"strings"
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
// Recoverer is a middleware that recovers from panics, logs the panic (and a
|
| 18 |
+
// backtrace), and returns a HTTP 500 (Internal Server Error) status if
|
| 19 |
+
// possible. Recoverer prints a request ID if one is provided.
|
| 20 |
+
//
|
| 21 |
+
// Alternatively, look at https://github.com/go-chi/httplog middleware pkgs.
|
| 22 |
+
func Recoverer(next http.Handler) http.Handler {
|
| 23 |
+
fn := func(w http.ResponseWriter, r *http.Request) {
|
| 24 |
+
defer func() {
|
| 25 |
+
if rvr := recover(); rvr != nil {
|
| 26 |
+
if rvr == http.ErrAbortHandler {
|
| 27 |
+
// we don't recover http.ErrAbortHandler so the response
|
| 28 |
+
// to the client is aborted, this should not be logged
|
| 29 |
+
panic(rvr)
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
logEntry := GetLogEntry(r)
|
| 33 |
+
if logEntry != nil {
|
| 34 |
+
logEntry.Panic(rvr, debug.Stack())
|
| 35 |
+
} else {
|
| 36 |
+
PrintPrettyStack(rvr)
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
if r.Header.Get("Connection") != "Upgrade" {
|
| 40 |
+
w.WriteHeader(http.StatusInternalServerError)
|
| 41 |
+
}
|
| 42 |
+
}
|
| 43 |
+
}()
|
| 44 |
+
|
| 45 |
+
next.ServeHTTP(w, r)
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
return http.HandlerFunc(fn)
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
// for ability to test the PrintPrettyStack function
|
| 52 |
+
var recovererErrorWriter io.Writer = os.Stderr
|
| 53 |
+
|
| 54 |
+
func PrintPrettyStack(rvr interface{}) {
|
| 55 |
+
debugStack := debug.Stack()
|
| 56 |
+
s := prettyStack{}
|
| 57 |
+
out, err := s.parse(debugStack, rvr)
|
| 58 |
+
if err == nil {
|
| 59 |
+
recovererErrorWriter.Write(out)
|
| 60 |
+
} else {
|
| 61 |
+
// print stdlib output as a fallback
|
| 62 |
+
os.Stderr.Write(debugStack)
|
| 63 |
+
}
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
type prettyStack struct {
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
func (s prettyStack) parse(debugStack []byte, rvr interface{}) ([]byte, error) {
|
| 70 |
+
var err error
|
| 71 |
+
useColor := true
|
| 72 |
+
buf := &bytes.Buffer{}
|
| 73 |
+
|
| 74 |
+
cW(buf, false, bRed, "\n")
|
| 75 |
+
cW(buf, useColor, bCyan, " panic: ")
|
| 76 |
+
cW(buf, useColor, bBlue, "%v", rvr)
|
| 77 |
+
cW(buf, false, bWhite, "\n \n")
|
| 78 |
+
|
| 79 |
+
// process debug stack info
|
| 80 |
+
stack := strings.Split(string(debugStack), "\n")
|
| 81 |
+
lines := []string{}
|
| 82 |
+
|
| 83 |
+
// locate panic line, as we may have nested panics
|
| 84 |
+
for i := len(stack) - 1; i > 0; i-- {
|
| 85 |
+
lines = append(lines, stack[i])
|
| 86 |
+
if strings.HasPrefix(stack[i], "panic(") {
|
| 87 |
+
lines = lines[0 : len(lines)-2] // remove boilerplate
|
| 88 |
+
break
|
| 89 |
+
}
|
| 90 |
+
}
|
| 91 |
+
|
| 92 |
+
// reverse
|
| 93 |
+
for i := len(lines)/2 - 1; i >= 0; i-- {
|
| 94 |
+
opp := len(lines) - 1 - i
|
| 95 |
+
lines[i], lines[opp] = lines[opp], lines[i]
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
+
// decorate
|
| 99 |
+
for i, line := range lines {
|
| 100 |
+
lines[i], err = s.decorateLine(line, useColor, i)
|
| 101 |
+
if err != nil {
|
| 102 |
+
return nil, err
|
| 103 |
+
}
|
| 104 |
+
}
|
| 105 |
+
|
| 106 |
+
for _, l := range lines {
|
| 107 |
+
fmt.Fprintf(buf, "%s", l)
|
| 108 |
+
}
|
| 109 |
+
return buf.Bytes(), nil
|
| 110 |
+
}
|
| 111 |
+
|
| 112 |
+
func (s prettyStack) decorateLine(line string, useColor bool, num int) (string, error) {
|
| 113 |
+
line = strings.TrimSpace(line)
|
| 114 |
+
if strings.HasPrefix(line, "\t") || strings.Contains(line, ".go:") {
|
| 115 |
+
return s.decorateSourceLine(line, useColor, num)
|
| 116 |
+
}
|
| 117 |
+
if strings.HasSuffix(line, ")") {
|
| 118 |
+
return s.decorateFuncCallLine(line, useColor, num)
|
| 119 |
+
}
|
| 120 |
+
if strings.HasPrefix(line, "\t") {
|
| 121 |
+
return strings.Replace(line, "\t", " ", 1), nil
|
| 122 |
+
}
|
| 123 |
+
return fmt.Sprintf(" %s\n", line), nil
|
| 124 |
+
}
|
| 125 |
+
|
| 126 |
+
func (s prettyStack) decorateFuncCallLine(line string, useColor bool, num int) (string, error) {
|
| 127 |
+
idx := strings.LastIndex(line, "(")
|
| 128 |
+
if idx < 0 {
|
| 129 |
+
return "", errors.New("not a func call line")
|
| 130 |
+
}
|
| 131 |
+
|
| 132 |
+
buf := &bytes.Buffer{}
|
| 133 |
+
pkg := line[0:idx]
|
| 134 |
+
// addr := line[idx:]
|
| 135 |
+
method := ""
|
| 136 |
+
|
| 137 |
+
if idx := strings.LastIndex(pkg, string(os.PathSeparator)); idx < 0 {
|
| 138 |
+
if idx := strings.Index(pkg, "."); idx > 0 {
|
| 139 |
+
method = pkg[idx:]
|
| 140 |
+
pkg = pkg[0:idx]
|
| 141 |
+
}
|
| 142 |
+
} else {
|
| 143 |
+
method = pkg[idx+1:]
|
| 144 |
+
pkg = pkg[0 : idx+1]
|
| 145 |
+
if idx := strings.Index(method, "."); idx > 0 {
|
| 146 |
+
pkg += method[0:idx]
|
| 147 |
+
method = method[idx:]
|
| 148 |
+
}
|
| 149 |
+
}
|
| 150 |
+
pkgColor := nYellow
|
| 151 |
+
methodColor := bGreen
|
| 152 |
+
|
| 153 |
+
if num == 0 {
|
| 154 |
+
cW(buf, useColor, bRed, " -> ")
|
| 155 |
+
pkgColor = bMagenta
|
| 156 |
+
methodColor = bRed
|
| 157 |
+
} else {
|
| 158 |
+
cW(buf, useColor, bWhite, " ")
|
| 159 |
+
}
|
| 160 |
+
cW(buf, useColor, pkgColor, "%s", pkg)
|
| 161 |
+
cW(buf, useColor, methodColor, "%s\n", method)
|
| 162 |
+
// cW(buf, useColor, nBlack, "%s", addr)
|
| 163 |
+
return buf.String(), nil
|
| 164 |
+
}
|
| 165 |
+
|
| 166 |
+
func (s prettyStack) decorateSourceLine(line string, useColor bool, num int) (string, error) {
|
| 167 |
+
idx := strings.LastIndex(line, ".go:")
|
| 168 |
+
if idx < 0 {
|
| 169 |
+
return "", errors.New("not a source line")
|
| 170 |
+
}
|
| 171 |
+
|
| 172 |
+
buf := &bytes.Buffer{}
|
| 173 |
+
path := line[0 : idx+3]
|
| 174 |
+
lineno := line[idx+3:]
|
| 175 |
+
|
| 176 |
+
idx = strings.LastIndex(path, string(os.PathSeparator))
|
| 177 |
+
dir := path[0 : idx+1]
|
| 178 |
+
file := path[idx+1:]
|
| 179 |
+
|
| 180 |
+
idx = strings.Index(lineno, " ")
|
| 181 |
+
if idx > 0 {
|
| 182 |
+
lineno = lineno[0:idx]
|
| 183 |
+
}
|
| 184 |
+
fileColor := bCyan
|
| 185 |
+
lineColor := bGreen
|
| 186 |
+
|
| 187 |
+
if num == 1 {
|
| 188 |
+
cW(buf, useColor, bRed, " -> ")
|
| 189 |
+
fileColor = bRed
|
| 190 |
+
lineColor = bMagenta
|
| 191 |
+
} else {
|
| 192 |
+
cW(buf, false, bWhite, " ")
|
| 193 |
+
}
|
| 194 |
+
cW(buf, useColor, bWhite, "%s", dir)
|
| 195 |
+
cW(buf, useColor, fileColor, "%s", file)
|
| 196 |
+
cW(buf, useColor, lineColor, "%s", lineno)
|
| 197 |
+
if num == 1 {
|
| 198 |
+
cW(buf, false, bWhite, "\n")
|
| 199 |
+
}
|
| 200 |
+
cW(buf, false, bWhite, "\n")
|
| 201 |
+
|
| 202 |
+
return buf.String(), nil
|
| 203 |
+
}
|
tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/environment/source/middleware/request_id.go
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
package middleware
|
| 2 |
+
|
| 3 |
+
// Ported from Goji's middleware, source:
|
| 4 |
+
// https://github.com/zenazn/goji/tree/master/web/middleware
|
| 5 |
+
|
| 6 |
+
import (
|
| 7 |
+
"context"
|
| 8 |
+
"crypto/rand"
|
| 9 |
+
"encoding/base64"
|
| 10 |
+
"fmt"
|
| 11 |
+
"net/http"
|
| 12 |
+
"os"
|
| 13 |
+
"strings"
|
| 14 |
+
"sync/atomic"
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
// Key to use when setting the request ID.
|
| 18 |
+
type ctxKeyRequestID int
|
| 19 |
+
|
| 20 |
+
// RequestIDKey is the key that holds the unique request ID in a request context.
|
| 21 |
+
const RequestIDKey ctxKeyRequestID = 0
|
| 22 |
+
|
| 23 |
+
// RequestIDHeader is the name of the HTTP Header which contains the request id.
|
| 24 |
+
// Exported so that it can be changed by developers
|
| 25 |
+
var RequestIDHeader = "X-Request-Id"
|
| 26 |
+
|
| 27 |
+
var prefix string
|
| 28 |
+
var reqid uint64
|
| 29 |
+
|
| 30 |
+
// A quick note on the statistics here: we're trying to calculate the chance that
|
| 31 |
+
// two randomly generated base62 prefixes will collide. We use the formula from
|
| 32 |
+
// http://en.wikipedia.org/wiki/Birthday_problem
|
| 33 |
+
//
|
| 34 |
+
// P[m, n] \approx 1 - e^{-m^2/2n}
|
| 35 |
+
//
|
| 36 |
+
// We ballpark an upper bound for $m$ by imagining (for whatever reason) a server
|
| 37 |
+
// that restarts every second over 10 years, for $m = 86400 * 365 * 10 = 315360000$
|
| 38 |
+
//
|
| 39 |
+
// For a $k$ character base-62 identifier, we have $n(k) = 62^k$
|
| 40 |
+
//
|
| 41 |
+
// Plugging this in, we find $P[m, n(10)] \approx 5.75%$, which is good enough for
|
| 42 |
+
// our purposes, and is surely more than anyone would ever need in practice -- a
|
| 43 |
+
// process that is rebooted a handful of times a day for a hundred years has less
|
| 44 |
+
// than a millionth of a percent chance of generating two colliding IDs.
|
| 45 |
+
|
| 46 |
+
func init() {
|
| 47 |
+
hostname, err := os.Hostname()
|
| 48 |
+
if hostname == "" || err != nil {
|
| 49 |
+
hostname = "localhost"
|
| 50 |
+
}
|
| 51 |
+
var buf [12]byte
|
| 52 |
+
var b64 string
|
| 53 |
+
for len(b64) < 10 {
|
| 54 |
+
rand.Read(buf[:])
|
| 55 |
+
b64 = base64.StdEncoding.EncodeToString(buf[:])
|
| 56 |
+
b64 = strings.NewReplacer("+", "", "/", "").Replace(b64)
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
prefix = fmt.Sprintf("%s/%s", hostname, b64[0:10])
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
// RequestID is a middleware that injects a request ID into the context of each
|
| 63 |
+
// request. A request ID is a string of the form "host.example.com/random-0001",
|
| 64 |
+
// where "random" is a base62 random string that uniquely identifies this go
|
| 65 |
+
// process, and where the last number is an atomically incremented request
|
| 66 |
+
// counter.
|
| 67 |
+
func RequestID(next http.Handler) http.Handler {
|
| 68 |
+
fn := func(w http.ResponseWriter, r *http.Request) {
|
| 69 |
+
ctx := r.Context()
|
| 70 |
+
requestID := r.Header.Get(RequestIDHeader)
|
| 71 |
+
if requestID == "" {
|
| 72 |
+
myid := atomic.AddUint64(&reqid, 1)
|
| 73 |
+
requestID = fmt.Sprintf("%s-%06d", prefix, myid)
|
| 74 |
+
}
|
| 75 |
+
ctx = context.WithValue(ctx, RequestIDKey, requestID)
|
| 76 |
+
next.ServeHTTP(w, r.WithContext(ctx))
|
| 77 |
+
}
|
| 78 |
+
return http.HandlerFunc(fn)
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
// GetReqID returns a request ID from the given context if one is present.
|
| 82 |
+
// Returns the empty string if a request ID cannot be found.
|
| 83 |
+
func GetReqID(ctx context.Context) string {
|
| 84 |
+
if ctx == nil {
|
| 85 |
+
return ""
|
| 86 |
+
}
|
| 87 |
+
if reqID, ok := ctx.Value(RequestIDKey).(string); ok {
|
| 88 |
+
return reqID
|
| 89 |
+
}
|
| 90 |
+
return ""
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
// NextRequestID generates the next request ID in the sequence.
|
| 94 |
+
func NextRequestID() uint64 {
|
| 95 |
+
return atomic.AddUint64(&reqid, 1)
|
| 96 |
+
}
|
tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/environment/source/middleware/request_size.go
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
package middleware
|
| 2 |
+
|
| 3 |
+
import (
|
| 4 |
+
"net/http"
|
| 5 |
+
)
|
| 6 |
+
|
| 7 |
+
// RequestSize is a middleware that will limit request sizes to a specified
|
| 8 |
+
// number of bytes. It uses MaxBytesReader to do so.
|
| 9 |
+
func RequestSize(bytes int64) func(http.Handler) http.Handler {
|
| 10 |
+
f := func(h http.Handler) http.Handler {
|
| 11 |
+
fn := func(w http.ResponseWriter, r *http.Request) {
|
| 12 |
+
r.Body = http.MaxBytesReader(w, r.Body, bytes)
|
| 13 |
+
h.ServeHTTP(w, r)
|
| 14 |
+
}
|
| 15 |
+
return http.HandlerFunc(fn)
|
| 16 |
+
}
|
| 17 |
+
return f
|
| 18 |
+
}
|
tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/environment/source/middleware/route_headers.go
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
package middleware
|
| 2 |
+
|
| 3 |
+
import (
|
| 4 |
+
"net/http"
|
| 5 |
+
"strings"
|
| 6 |
+
)
|
| 7 |
+
|
| 8 |
+
// RouteHeaders is a neat little header-based router that allows you to direct
|
| 9 |
+
// the flow of a request through a middleware stack based on a request header.
|
| 10 |
+
//
|
| 11 |
+
// For example, lets say you'd like to setup multiple routers depending on the
|
| 12 |
+
// request Host header, you could then do something as so:
|
| 13 |
+
//
|
| 14 |
+
// r := chi.NewRouter()
|
| 15 |
+
// rSubdomain := chi.NewRouter()
|
| 16 |
+
// r.Use(middleware.RouteHeaders().
|
| 17 |
+
// Route("Host", "example.com", middleware.New(r)).
|
| 18 |
+
// Route("Host", "*.example.com", middleware.New(rSubdomain)).
|
| 19 |
+
// Handler)
|
| 20 |
+
// r.Get("/", h)
|
| 21 |
+
// rSubdomain.Get("/", h2)
|
| 22 |
+
//
|
| 23 |
+
// Another example, imagine you want to setup multiple CORS handlers, where for
|
| 24 |
+
// your origin servers you allow authorized requests, but for third-party public
|
| 25 |
+
// requests, authorization is disabled.
|
| 26 |
+
//
|
| 27 |
+
// r := chi.NewRouter()
|
| 28 |
+
// r.Use(middleware.RouteHeaders().
|
| 29 |
+
// Route("Origin", "https://app.skyweaver.net", cors.Handler(cors.Options{
|
| 30 |
+
// AllowedOrigins: []string{"https://api.skyweaver.net"},
|
| 31 |
+
// AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
|
| 32 |
+
// AllowedHeaders: []string{"Accept", "Authorization", "Content-Type"},
|
| 33 |
+
// AllowCredentials: true, // <----------<<< allow credentials
|
| 34 |
+
// })).
|
| 35 |
+
// Route("Origin", "*", cors.Handler(cors.Options{
|
| 36 |
+
// AllowedOrigins: []string{"*"},
|
| 37 |
+
// AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
|
| 38 |
+
// AllowedHeaders: []string{"Accept", "Content-Type"},
|
| 39 |
+
// AllowCredentials: false, // <----------<<< do not allow credentials
|
| 40 |
+
// })).
|
| 41 |
+
// Handler)
|
| 42 |
+
func RouteHeaders() HeaderRouter {
|
| 43 |
+
return HeaderRouter{}
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
type HeaderRouter map[string][]HeaderRoute
|
| 47 |
+
|
| 48 |
+
func (hr HeaderRouter) Route(header, match string, middlewareHandler func(next http.Handler) http.Handler) HeaderRouter {
|
| 49 |
+
header = strings.ToLower(header)
|
| 50 |
+
k := hr[header]
|
| 51 |
+
if k == nil {
|
| 52 |
+
hr[header] = []HeaderRoute{}
|
| 53 |
+
}
|
| 54 |
+
hr[header] = append(hr[header], HeaderRoute{MatchOne: NewPattern(match), Middleware: middlewareHandler})
|
| 55 |
+
return hr
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
func (hr HeaderRouter) RouteAny(header string, match []string, middlewareHandler func(next http.Handler) http.Handler) HeaderRouter {
|
| 59 |
+
header = strings.ToLower(header)
|
| 60 |
+
k := hr[header]
|
| 61 |
+
if k == nil {
|
| 62 |
+
hr[header] = []HeaderRoute{}
|
| 63 |
+
}
|
| 64 |
+
patterns := []Pattern{}
|
| 65 |
+
for _, m := range match {
|
| 66 |
+
patterns = append(patterns, NewPattern(m))
|
| 67 |
+
}
|
| 68 |
+
hr[header] = append(hr[header], HeaderRoute{MatchAny: patterns, Middleware: middlewareHandler})
|
| 69 |
+
return hr
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
func (hr HeaderRouter) RouteDefault(handler func(next http.Handler) http.Handler) HeaderRouter {
|
| 73 |
+
hr["*"] = []HeaderRoute{{Middleware: handler}}
|
| 74 |
+
return hr
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
func (hr HeaderRouter) Handler(next http.Handler) http.Handler {
|
| 78 |
+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
| 79 |
+
if len(hr) == 0 {
|
| 80 |
+
// skip if no routes set
|
| 81 |
+
next.ServeHTTP(w, r)
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
+
// find first matching header route, and continue
|
| 85 |
+
for header, matchers := range hr {
|
| 86 |
+
headerValue := r.Header.Get(header)
|
| 87 |
+
if headerValue == "" {
|
| 88 |
+
continue
|
| 89 |
+
}
|
| 90 |
+
headerValue = strings.ToLower(headerValue)
|
| 91 |
+
for _, matcher := range matchers {
|
| 92 |
+
if matcher.IsMatch(headerValue) {
|
| 93 |
+
matcher.Middleware(next).ServeHTTP(w, r)
|
| 94 |
+
return
|
| 95 |
+
}
|
| 96 |
+
}
|
| 97 |
+
}
|
| 98 |
+
|
| 99 |
+
// if no match, check for "*" default route
|
| 100 |
+
matcher, ok := hr["*"]
|
| 101 |
+
if !ok || matcher[0].Middleware == nil {
|
| 102 |
+
next.ServeHTTP(w, r)
|
| 103 |
+
return
|
| 104 |
+
}
|
| 105 |
+
matcher[0].Middleware(next).ServeHTTP(w, r)
|
| 106 |
+
})
|
| 107 |
+
}
|
| 108 |
+
|
| 109 |
+
type HeaderRoute struct {
|
| 110 |
+
Middleware func(next http.Handler) http.Handler
|
| 111 |
+
MatchOne Pattern
|
| 112 |
+
MatchAny []Pattern
|
| 113 |
+
}
|
| 114 |
+
|
| 115 |
+
func (r HeaderRoute) IsMatch(value string) bool {
|
| 116 |
+
if len(r.MatchAny) > 0 {
|
| 117 |
+
for _, m := range r.MatchAny {
|
| 118 |
+
if m.Match(value) {
|
| 119 |
+
return true
|
| 120 |
+
}
|
| 121 |
+
}
|
| 122 |
+
} else if r.MatchOne.Match(value) {
|
| 123 |
+
return true
|
| 124 |
+
}
|
| 125 |
+
return false
|
| 126 |
+
}
|
| 127 |
+
|
| 128 |
+
type Pattern struct {
|
| 129 |
+
prefix string
|
| 130 |
+
suffix string
|
| 131 |
+
wildcard bool
|
| 132 |
+
}
|
| 133 |
+
|
| 134 |
+
func NewPattern(value string) Pattern {
|
| 135 |
+
p := Pattern{}
|
| 136 |
+
p.prefix, p.suffix, p.wildcard = strings.Cut(value, "*")
|
| 137 |
+
return p
|
| 138 |
+
}
|
| 139 |
+
|
| 140 |
+
func (p Pattern) Match(v string) bool {
|
| 141 |
+
if !p.wildcard {
|
| 142 |
+
return p.prefix == v
|
| 143 |
+
}
|
| 144 |
+
return len(v) >= len(p.prefix+p.suffix) && strings.HasPrefix(v, p.prefix) && strings.HasSuffix(v, p.suffix)
|
| 145 |
+
}
|
tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/environment/source/middleware/strip.go
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
package middleware
|
| 2 |
+
|
| 3 |
+
import (
|
| 4 |
+
"fmt"
|
| 5 |
+
"net/http"
|
| 6 |
+
"strings"
|
| 7 |
+
|
| 8 |
+
"github.com/go-chi/chi/v5"
|
| 9 |
+
)
|
| 10 |
+
|
| 11 |
+
// StripSlashes is a middleware that will match request paths with a trailing
|
| 12 |
+
// slash, strip it from the path and continue routing through the mux, if a route
|
| 13 |
+
// matches, then it will serve the handler.
|
| 14 |
+
func StripSlashes(next http.Handler) http.Handler {
|
| 15 |
+
fn := func(w http.ResponseWriter, r *http.Request) {
|
| 16 |
+
var path string
|
| 17 |
+
rctx := chi.RouteContext(r.Context())
|
| 18 |
+
if rctx != nil && rctx.RoutePath != "" {
|
| 19 |
+
path = rctx.RoutePath
|
| 20 |
+
} else {
|
| 21 |
+
path = r.URL.Path
|
| 22 |
+
}
|
| 23 |
+
if len(path) > 1 && path[len(path)-1] == '/' {
|
| 24 |
+
newPath := path[:len(path)-1]
|
| 25 |
+
if rctx == nil {
|
| 26 |
+
rctx.RoutePath = newPath
|
| 27 |
+
} else {
|
| 28 |
+
r.URL.Path = newPath
|
| 29 |
+
}
|
| 30 |
+
}
|
| 31 |
+
next.ServeHTTP(w, r)
|
| 32 |
+
}
|
| 33 |
+
return http.HandlerFunc(fn)
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
// RedirectSlashes is a middleware that will match request paths with a trailing
|
| 37 |
+
// slash and redirect to the same path, less the trailing slash.
|
| 38 |
+
//
|
| 39 |
+
// NOTE: RedirectSlashes middleware is *incompatible* with http.FileServer,
|
| 40 |
+
// see https://github.com/go-chi/chi/issues/343
|
| 41 |
+
func RedirectSlashes(next http.Handler) http.Handler {
|
| 42 |
+
fn := func(w http.ResponseWriter, r *http.Request) {
|
| 43 |
+
var path string
|
| 44 |
+
rctx := chi.RouteContext(r.Context())
|
| 45 |
+
if rctx != nil && rctx.RoutePath != "" {
|
| 46 |
+
path = rctx.RoutePath
|
| 47 |
+
} else {
|
| 48 |
+
path = r.URL.Path
|
| 49 |
+
}
|
| 50 |
+
if len(path) > 1 && path[len(path)-1] == '/' {
|
| 51 |
+
// Trim all leading and trailing slashes (e.g., "//evil.com", "/some/path//")
|
| 52 |
+
path = "/" + strings.Trim(path, "/")
|
| 53 |
+
if r.URL.RawQuery != "" {
|
| 54 |
+
path = fmt.Sprintf("%s?%s", path, r.URL.RawQuery)
|
| 55 |
+
}
|
| 56 |
+
http.Redirect(w, r, path, 301)
|
| 57 |
+
return
|
| 58 |
+
}
|
| 59 |
+
next.ServeHTTP(w, r)
|
| 60 |
+
}
|
| 61 |
+
return http.HandlerFunc(fn)
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
// StripPrefix is a middleware that will strip the provided prefix from the
|
| 65 |
+
// request path before handing the request over to the next handler.
|
| 66 |
+
func StripPrefix(prefix string) func(http.Handler) http.Handler {
|
| 67 |
+
return func(next http.Handler) http.Handler {
|
| 68 |
+
return http.StripPrefix(prefix, next)
|
| 69 |
+
}
|
| 70 |
+
}
|
tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/environment/source/middleware/sunset.go
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
package middleware
|
| 2 |
+
|
| 3 |
+
import (
|
| 4 |
+
"net/http"
|
| 5 |
+
"time"
|
| 6 |
+
)
|
| 7 |
+
|
| 8 |
+
// Sunset set Deprecation/Sunset header to response
|
| 9 |
+
// This can be used to enable Sunset in a route or a route group
|
| 10 |
+
// For more: https://www.rfc-editor.org/rfc/rfc8594.html
|
| 11 |
+
func Sunset(sunsetAt time.Time, links ...string) func(http.Handler) http.Handler {
|
| 12 |
+
return func(next http.Handler) http.Handler {
|
| 13 |
+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
| 14 |
+
if !sunsetAt.IsZero() {
|
| 15 |
+
w.Header().Set("Sunset", sunsetAt.Format(http.TimeFormat))
|
| 16 |
+
w.Header().Set("Deprecation", sunsetAt.Format(http.TimeFormat))
|
| 17 |
+
|
| 18 |
+
for _, link := range links {
|
| 19 |
+
w.Header().Add("Link", link)
|
| 20 |
+
}
|
| 21 |
+
}
|
| 22 |
+
next.ServeHTTP(w, r)
|
| 23 |
+
})
|
| 24 |
+
}
|
| 25 |
+
}
|
tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/environment/source/middleware/supress_notfound.go
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
package middleware
|
| 2 |
+
|
| 3 |
+
import (
|
| 4 |
+
"net/http"
|
| 5 |
+
|
| 6 |
+
"github.com/go-chi/chi/v5"
|
| 7 |
+
)
|
| 8 |
+
|
| 9 |
+
// SupressNotFound will quickly respond with a 404 if the route is not found
|
| 10 |
+
// and will not continue to the next middleware handler.
|
| 11 |
+
//
|
| 12 |
+
// This is handy to put at the top of your middleware stack to avoid unnecessary
|
| 13 |
+
// processing of requests that are not going to match any routes anyway. For
|
| 14 |
+
// example its super annoying to see a bunch of 404's in your logs from bots.
|
| 15 |
+
func SupressNotFound(router *chi.Mux) func(next http.Handler) http.Handler {
|
| 16 |
+
return func(next http.Handler) http.Handler {
|
| 17 |
+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
| 18 |
+
rctx := chi.RouteContext(r.Context())
|
| 19 |
+
match := rctx.Routes.Match(rctx, r.Method, r.URL.Path)
|
| 20 |
+
if !match {
|
| 21 |
+
router.NotFoundHandler().ServeHTTP(w, r)
|
| 22 |
+
return
|
| 23 |
+
}
|
| 24 |
+
next.ServeHTTP(w, r)
|
| 25 |
+
})
|
| 26 |
+
}
|
| 27 |
+
}
|
tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/environment/source/middleware/terminal.go
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
package middleware
|
| 2 |
+
|
| 3 |
+
// Ported from Goji's middleware, source:
|
| 4 |
+
// https://github.com/zenazn/goji/tree/master/web/middleware
|
| 5 |
+
|
| 6 |
+
import (
|
| 7 |
+
"fmt"
|
| 8 |
+
"io"
|
| 9 |
+
"os"
|
| 10 |
+
)
|
| 11 |
+
|
| 12 |
+
var (
|
| 13 |
+
// Normal colors
|
| 14 |
+
nBlack = []byte{'\033', '[', '3', '0', 'm'}
|
| 15 |
+
nRed = []byte{'\033', '[', '3', '1', 'm'}
|
| 16 |
+
nGreen = []byte{'\033', '[', '3', '2', 'm'}
|
| 17 |
+
nYellow = []byte{'\033', '[', '3', '3', 'm'}
|
| 18 |
+
nBlue = []byte{'\033', '[', '3', '4', 'm'}
|
| 19 |
+
nMagenta = []byte{'\033', '[', '3', '5', 'm'}
|
| 20 |
+
nCyan = []byte{'\033', '[', '3', '6', 'm'}
|
| 21 |
+
nWhite = []byte{'\033', '[', '3', '7', 'm'}
|
| 22 |
+
// Bright colors
|
| 23 |
+
bBlack = []byte{'\033', '[', '3', '0', ';', '1', 'm'}
|
| 24 |
+
bRed = []byte{'\033', '[', '3', '1', ';', '1', 'm'}
|
| 25 |
+
bGreen = []byte{'\033', '[', '3', '2', ';', '1', 'm'}
|
| 26 |
+
bYellow = []byte{'\033', '[', '3', '3', ';', '1', 'm'}
|
| 27 |
+
bBlue = []byte{'\033', '[', '3', '4', ';', '1', 'm'}
|
| 28 |
+
bMagenta = []byte{'\033', '[', '3', '5', ';', '1', 'm'}
|
| 29 |
+
bCyan = []byte{'\033', '[', '3', '6', ';', '1', 'm'}
|
| 30 |
+
bWhite = []byte{'\033', '[', '3', '7', ';', '1', 'm'}
|
| 31 |
+
|
| 32 |
+
reset = []byte{'\033', '[', '0', 'm'}
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
var IsTTY bool
|
| 36 |
+
|
| 37 |
+
func init() {
|
| 38 |
+
// This is sort of cheating: if stdout is a character device, we assume
|
| 39 |
+
// that means it's a TTY. Unfortunately, there are many non-TTY
|
| 40 |
+
// character devices, but fortunately stdout is rarely set to any of
|
| 41 |
+
// them.
|
| 42 |
+
//
|
| 43 |
+
// We could solve this properly by pulling in a dependency on
|
| 44 |
+
// code.google.com/p/go.crypto/ssh/terminal, for instance, but as a
|
| 45 |
+
// heuristic for whether to print in color or in black-and-white, I'd
|
| 46 |
+
// really rather not.
|
| 47 |
+
fi, err := os.Stdout.Stat()
|
| 48 |
+
if err == nil {
|
| 49 |
+
m := os.ModeDevice | os.ModeCharDevice
|
| 50 |
+
IsTTY = fi.Mode()&m == m
|
| 51 |
+
}
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
// colorWrite
|
| 55 |
+
func cW(w io.Writer, useColor bool, color []byte, s string, args ...interface{}) {
|
| 56 |
+
if IsTTY && useColor {
|
| 57 |
+
w.Write(color)
|
| 58 |
+
}
|
| 59 |
+
fmt.Fprintf(w, s, args...)
|
| 60 |
+
if IsTTY && useColor {
|
| 61 |
+
w.Write(reset)
|
| 62 |
+
}
|
| 63 |
+
}
|
tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/environment/source/middleware/throttle.go
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
package middleware
|
| 2 |
+
|
| 3 |
+
import (
|
| 4 |
+
"net/http"
|
| 5 |
+
"strconv"
|
| 6 |
+
"time"
|
| 7 |
+
)
|
| 8 |
+
|
| 9 |
+
const (
|
| 10 |
+
errCapacityExceeded = "Server capacity exceeded."
|
| 11 |
+
errTimedOut = "Timed out while waiting for a pending request to complete."
|
| 12 |
+
errContextCanceled = "Context was canceled."
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
var (
|
| 16 |
+
defaultBacklogTimeout = time.Second * 60
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
// ThrottleOpts represents a set of throttling options.
|
| 20 |
+
type ThrottleOpts struct {
|
| 21 |
+
RetryAfterFn func(ctxDone bool) time.Duration
|
| 22 |
+
Limit int
|
| 23 |
+
BacklogLimit int
|
| 24 |
+
BacklogTimeout time.Duration
|
| 25 |
+
StatusCode int
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
// Throttle is a middleware that limits number of currently processed requests
|
| 29 |
+
// at a time across all users. Note: Throttle is not a rate-limiter per user,
|
| 30 |
+
// instead it just puts a ceiling on the number of current in-flight requests
|
| 31 |
+
// being processed from the point from where the Throttle middleware is mounted.
|
| 32 |
+
func Throttle(limit int) func(http.Handler) http.Handler {
|
| 33 |
+
return ThrottleWithOpts(ThrottleOpts{Limit: limit, BacklogTimeout: defaultBacklogTimeout})
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
// ThrottleBacklog is a middleware that limits number of currently processed
|
| 37 |
+
// requests at a time and provides a backlog for holding a finite number of
|
| 38 |
+
// pending requests.
|
| 39 |
+
func ThrottleBacklog(limit, backlogLimit int, backlogTimeout time.Duration) func(http.Handler) http.Handler {
|
| 40 |
+
return ThrottleWithOpts(ThrottleOpts{Limit: limit, BacklogLimit: backlogLimit, BacklogTimeout: backlogTimeout})
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
// ThrottleWithOpts is a middleware that limits number of currently processed requests using passed ThrottleOpts.
|
| 44 |
+
func ThrottleWithOpts(opts ThrottleOpts) func(http.Handler) http.Handler {
|
| 45 |
+
if opts.Limit < 1 {
|
| 46 |
+
panic("chi/middleware: Throttle expects limit > 0")
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
if opts.BacklogLimit < 0 {
|
| 50 |
+
panic("chi/middleware: Throttle expects backlogLimit to be positive")
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
statusCode := opts.StatusCode
|
| 54 |
+
if statusCode == 0 {
|
| 55 |
+
statusCode = http.StatusTooManyRequests
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
t := throttler{
|
| 59 |
+
tokens: make(chan token, opts.Limit),
|
| 60 |
+
backlogTokens: make(chan token, opts.Limit+opts.BacklogLimit),
|
| 61 |
+
backlogTimeout: opts.BacklogTimeout,
|
| 62 |
+
statusCode: statusCode,
|
| 63 |
+
retryAfterFn: opts.RetryAfterFn,
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
// Filling tokens.
|
| 67 |
+
for i := 0; i < opts.Limit+opts.BacklogLimit; i++ {
|
| 68 |
+
if i < opts.Limit {
|
| 69 |
+
t.tokens <- token{}
|
| 70 |
+
}
|
| 71 |
+
t.backlogTokens <- token{}
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
return func(next http.Handler) http.Handler {
|
| 75 |
+
fn := func(w http.ResponseWriter, r *http.Request) {
|
| 76 |
+
ctx := r.Context()
|
| 77 |
+
|
| 78 |
+
select {
|
| 79 |
+
|
| 80 |
+
case <-ctx.Done():
|
| 81 |
+
t.setRetryAfterHeaderIfNeeded(w, true)
|
| 82 |
+
http.Error(w, errContextCanceled, t.statusCode)
|
| 83 |
+
return
|
| 84 |
+
|
| 85 |
+
case btok := <-t.backlogTokens:
|
| 86 |
+
timer := time.NewTimer(t.backlogTimeout)
|
| 87 |
+
|
| 88 |
+
defer func() {
|
| 89 |
+
t.backlogTokens <- btok
|
| 90 |
+
}()
|
| 91 |
+
|
| 92 |
+
select {
|
| 93 |
+
case <-timer.C:
|
| 94 |
+
t.setRetryAfterHeaderIfNeeded(w, false)
|
| 95 |
+
http.Error(w, errTimedOut, t.statusCode)
|
| 96 |
+
return
|
| 97 |
+
case <-ctx.Done():
|
| 98 |
+
timer.Stop()
|
| 99 |
+
t.setRetryAfterHeaderIfNeeded(w, true)
|
| 100 |
+
http.Error(w, errContextCanceled, t.statusCode)
|
| 101 |
+
return
|
| 102 |
+
case tok := <-t.tokens:
|
| 103 |
+
defer func() {
|
| 104 |
+
timer.Stop()
|
| 105 |
+
t.tokens <- tok
|
| 106 |
+
}()
|
| 107 |
+
next.ServeHTTP(w, r)
|
| 108 |
+
}
|
| 109 |
+
return
|
| 110 |
+
|
| 111 |
+
default:
|
| 112 |
+
t.setRetryAfterHeaderIfNeeded(w, false)
|
| 113 |
+
http.Error(w, errCapacityExceeded, t.statusCode)
|
| 114 |
+
return
|
| 115 |
+
}
|
| 116 |
+
}
|
| 117 |
+
|
| 118 |
+
return http.HandlerFunc(fn)
|
| 119 |
+
}
|
| 120 |
+
}
|
| 121 |
+
|
| 122 |
+
// token represents a request that is being processed.
|
| 123 |
+
type token struct{}
|
| 124 |
+
|
| 125 |
+
// throttler limits number of currently processed requests at a time.
|
| 126 |
+
type throttler struct {
|
| 127 |
+
tokens chan token
|
| 128 |
+
backlogTokens chan token
|
| 129 |
+
retryAfterFn func(ctxDone bool) time.Duration
|
| 130 |
+
backlogTimeout time.Duration
|
| 131 |
+
statusCode int
|
| 132 |
+
}
|
| 133 |
+
|
| 134 |
+
// setRetryAfterHeaderIfNeeded sets Retry-After HTTP header if corresponding retryAfterFn option of throttler is initialized.
|
| 135 |
+
func (t throttler) setRetryAfterHeaderIfNeeded(w http.ResponseWriter, ctxDone bool) {
|
| 136 |
+
if t.retryAfterFn == nil {
|
| 137 |
+
return
|
| 138 |
+
}
|
| 139 |
+
w.Header().Set("Retry-After", strconv.Itoa(int(t.retryAfterFn(ctxDone).Seconds())))
|
| 140 |
+
}
|
tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/environment/source/middleware/timeout.go
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
package middleware
|
| 2 |
+
|
| 3 |
+
import (
|
| 4 |
+
"context"
|
| 5 |
+
"net/http"
|
| 6 |
+
"time"
|
| 7 |
+
)
|
| 8 |
+
|
| 9 |
+
// Timeout is a middleware that cancels ctx after a given timeout and return
|
| 10 |
+
// a 504 Gateway Timeout error to the client.
|
| 11 |
+
//
|
| 12 |
+
// It's required that you select the ctx.Done() channel to check for the signal
|
| 13 |
+
// if the context has reached its deadline and return, otherwise the timeout
|
| 14 |
+
// signal will be just ignored.
|
| 15 |
+
//
|
| 16 |
+
// ie. a route/handler may look like:
|
| 17 |
+
//
|
| 18 |
+
// r.Get("/long", func(w http.ResponseWriter, r *http.Request) {
|
| 19 |
+
// ctx := r.Context()
|
| 20 |
+
// processTime := time.Duration(rand.Intn(4)+1) * time.Second
|
| 21 |
+
//
|
| 22 |
+
// select {
|
| 23 |
+
// case <-ctx.Done():
|
| 24 |
+
// return
|
| 25 |
+
//
|
| 26 |
+
// case <-time.After(processTime):
|
| 27 |
+
// // The above channel simulates some hard work.
|
| 28 |
+
// }
|
| 29 |
+
//
|
| 30 |
+
// w.Write([]byte("done"))
|
| 31 |
+
// })
|
| 32 |
+
func Timeout(timeout time.Duration) func(next http.Handler) http.Handler {
|
| 33 |
+
return func(next http.Handler) http.Handler {
|
| 34 |
+
fn := func(w http.ResponseWriter, r *http.Request) {
|
| 35 |
+
ctx, cancel := context.WithTimeout(r.Context(), timeout)
|
| 36 |
+
defer func() {
|
| 37 |
+
cancel()
|
| 38 |
+
if ctx.Err() == context.DeadlineExceeded {
|
| 39 |
+
w.WriteHeader(http.StatusGatewayTimeout)
|
| 40 |
+
}
|
| 41 |
+
}()
|
| 42 |
+
|
| 43 |
+
r = r.WithContext(ctx)
|
| 44 |
+
next.ServeHTTP(w, r)
|
| 45 |
+
}
|
| 46 |
+
return http.HandlerFunc(fn)
|
| 47 |
+
}
|
| 48 |
+
}
|
tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/environment/source/middleware/url_format.go
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
package middleware
|
| 2 |
+
|
| 3 |
+
import (
|
| 4 |
+
"context"
|
| 5 |
+
"net/http"
|
| 6 |
+
"strings"
|
| 7 |
+
|
| 8 |
+
"github.com/go-chi/chi/v5"
|
| 9 |
+
)
|
| 10 |
+
|
| 11 |
+
var (
|
| 12 |
+
// URLFormatCtxKey is the context.Context key to store the URL format data
|
| 13 |
+
// for a request.
|
| 14 |
+
URLFormatCtxKey = &contextKey{"URLFormat"}
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
// URLFormat is a middleware that parses the url extension from a request path and stores it
|
| 18 |
+
// on the context as a string under the key `middleware.URLFormatCtxKey`. The middleware will
|
| 19 |
+
// trim the suffix from the routing path and continue routing.
|
| 20 |
+
//
|
| 21 |
+
// Routers should not include a url parameter for the suffix when using this middleware.
|
| 22 |
+
//
|
| 23 |
+
// Sample usage for url paths `/articles/1`, `/articles/1.json` and `/articles/1.xml`:
|
| 24 |
+
//
|
| 25 |
+
// func routes() http.Handler {
|
| 26 |
+
// r := chi.NewRouter()
|
| 27 |
+
// r.Use(middleware.URLFormat)
|
| 28 |
+
//
|
| 29 |
+
// r.Get("/articles/{id}", ListArticles)
|
| 30 |
+
//
|
| 31 |
+
// return r
|
| 32 |
+
// }
|
| 33 |
+
//
|
| 34 |
+
// func ListArticles(w http.ResponseWriter, r *http.Request) {
|
| 35 |
+
// urlFormat, _ := r.Context().Value(middleware.URLFormatCtxKey).(string)
|
| 36 |
+
//
|
| 37 |
+
// switch urlFormat {
|
| 38 |
+
// case "json":
|
| 39 |
+
// render.JSON(w, r, articles)
|
| 40 |
+
// case "xml:"
|
| 41 |
+
// render.XML(w, r, articles)
|
| 42 |
+
// default:
|
| 43 |
+
// render.JSON(w, r, articles)
|
| 44 |
+
// }
|
| 45 |
+
// }
|
| 46 |
+
func URLFormat(next http.Handler) http.Handler {
|
| 47 |
+
fn := func(w http.ResponseWriter, r *http.Request) {
|
| 48 |
+
ctx := r.Context()
|
| 49 |
+
|
| 50 |
+
var format string
|
| 51 |
+
path := r.URL.Path
|
| 52 |
+
|
| 53 |
+
rctx := chi.RouteContext(r.Context())
|
| 54 |
+
if rctx != nil && rctx.RoutePath != "" {
|
| 55 |
+
path = rctx.RoutePath
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
if strings.Index(path, ".") > 0 {
|
| 59 |
+
base := strings.LastIndex(path, "/")
|
| 60 |
+
idx := strings.LastIndex(path[base:], ".")
|
| 61 |
+
|
| 62 |
+
if idx > 0 {
|
| 63 |
+
idx += base
|
| 64 |
+
format = path[idx+1:]
|
| 65 |
+
|
| 66 |
+
rctx.RoutePath = path[:idx]
|
| 67 |
+
}
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
r = r.WithContext(context.WithValue(ctx, URLFormatCtxKey, format))
|
| 71 |
+
|
| 72 |
+
next.ServeHTTP(w, r)
|
| 73 |
+
}
|
| 74 |
+
return http.HandlerFunc(fn)
|
| 75 |
+
}
|
tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/environment/source/middleware/value.go
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
package middleware
|
| 2 |
+
|
| 3 |
+
import (
|
| 4 |
+
"context"
|
| 5 |
+
"net/http"
|
| 6 |
+
)
|
| 7 |
+
|
| 8 |
+
// WithValue is a middleware that sets a given key/value in a context chain.
|
| 9 |
+
func WithValue(key, val interface{}) func(next http.Handler) http.Handler {
|
| 10 |
+
return func(next http.Handler) http.Handler {
|
| 11 |
+
fn := func(w http.ResponseWriter, r *http.Request) {
|
| 12 |
+
r = r.WithContext(context.WithValue(r.Context(), key, val))
|
| 13 |
+
next.ServeHTTP(w, r)
|
| 14 |
+
}
|
| 15 |
+
return http.HandlerFunc(fn)
|
| 16 |
+
}
|
| 17 |
+
}
|
tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/environment/source/middleware/wrap_writer.go
ADDED
|
@@ -0,0 +1,241 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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/web/middleware
|
| 5 |
+
|
| 6 |
+
import (
|
| 7 |
+
"bufio"
|
| 8 |
+
"io"
|
| 9 |
+
"net"
|
| 10 |
+
"net/http"
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
// NewWrapResponseWriter wraps an http.ResponseWriter, returning a proxy that allows you to
|
| 14 |
+
// hook into various parts of the response process.
|
| 15 |
+
func NewWrapResponseWriter(w http.ResponseWriter, protoMajor int) WrapResponseWriter {
|
| 16 |
+
_, fl := w.(http.Flusher)
|
| 17 |
+
|
| 18 |
+
bw := basicWriter{ResponseWriter: w}
|
| 19 |
+
|
| 20 |
+
if protoMajor == 2 {
|
| 21 |
+
_, ps := w.(http.Pusher)
|
| 22 |
+
if fl && ps {
|
| 23 |
+
return &http2FancyWriter{bw}
|
| 24 |
+
}
|
| 25 |
+
} else {
|
| 26 |
+
_, hj := w.(http.Hijacker)
|
| 27 |
+
_, rf := w.(io.ReaderFrom)
|
| 28 |
+
if fl && hj && rf {
|
| 29 |
+
return &httpFancyWriter{bw}
|
| 30 |
+
}
|
| 31 |
+
if fl && hj {
|
| 32 |
+
return &flushHijackWriter{bw}
|
| 33 |
+
}
|
| 34 |
+
if hj {
|
| 35 |
+
return &hijackWriter{bw}
|
| 36 |
+
}
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
if fl {
|
| 40 |
+
return &flushWriter{bw}
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
return &bw
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
// WrapResponseWriter is a proxy around an http.ResponseWriter that allows you to hook
|
| 47 |
+
// into various parts of the response process.
|
| 48 |
+
type WrapResponseWriter interface {
|
| 49 |
+
http.ResponseWriter
|
| 50 |
+
// Status returns the HTTP status of the request, or 0 if one has not
|
| 51 |
+
// yet been sent.
|
| 52 |
+
Status() int
|
| 53 |
+
// BytesWritten returns the total number of bytes sent to the client.
|
| 54 |
+
BytesWritten() int
|
| 55 |
+
// Tee causes the response body to be written to the given io.Writer in
|
| 56 |
+
// addition to proxying the writes through. Only one io.Writer can be
|
| 57 |
+
// tee'd to at once: setting a second one will overwrite the first.
|
| 58 |
+
// Writes will be sent to the proxy before being written to this
|
| 59 |
+
// io.Writer. It is illegal for the tee'd writer to be modified
|
| 60 |
+
// concurrently with writes.
|
| 61 |
+
Tee(io.Writer)
|
| 62 |
+
// Unwrap returns the original proxied target.
|
| 63 |
+
Unwrap() http.ResponseWriter
|
| 64 |
+
// Discard causes all writes to the original ResponseWriter be discarded,
|
| 65 |
+
// instead writing only to the tee'd writer if it's set.
|
| 66 |
+
// The caller is responsible for calling WriteHeader and Write on the
|
| 67 |
+
// original ResponseWriter once the processing is done.
|
| 68 |
+
Discard()
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
// basicWriter wraps a http.ResponseWriter that implements the minimal
|
| 72 |
+
// http.ResponseWriter interface.
|
| 73 |
+
type basicWriter struct {
|
| 74 |
+
http.ResponseWriter
|
| 75 |
+
tee io.Writer
|
| 76 |
+
code int
|
| 77 |
+
bytes int
|
| 78 |
+
wroteHeader bool
|
| 79 |
+
discard bool
|
| 80 |
+
}
|
| 81 |
+
|
| 82 |
+
func (b *basicWriter) WriteHeader(code int) {
|
| 83 |
+
if code >= 100 && code <= 199 && code != http.StatusSwitchingProtocols {
|
| 84 |
+
if !b.discard {
|
| 85 |
+
b.ResponseWriter.WriteHeader(code)
|
| 86 |
+
}
|
| 87 |
+
} else if !b.wroteHeader {
|
| 88 |
+
b.code = code
|
| 89 |
+
b.wroteHeader = true
|
| 90 |
+
if !b.discard {
|
| 91 |
+
b.ResponseWriter.WriteHeader(code)
|
| 92 |
+
}
|
| 93 |
+
}
|
| 94 |
+
}
|
| 95 |
+
|
| 96 |
+
func (b *basicWriter) Write(buf []byte) (n int, err error) {
|
| 97 |
+
b.maybeWriteHeader()
|
| 98 |
+
if !b.discard {
|
| 99 |
+
n, err = b.ResponseWriter.Write(buf)
|
| 100 |
+
if b.tee != nil {
|
| 101 |
+
_, err2 := b.tee.Write(buf[:n])
|
| 102 |
+
// Prefer errors generated by the proxied writer.
|
| 103 |
+
if err == nil {
|
| 104 |
+
err = err2
|
| 105 |
+
}
|
| 106 |
+
}
|
| 107 |
+
} else if b.tee != nil {
|
| 108 |
+
n, err = b.tee.Write(buf)
|
| 109 |
+
} else {
|
| 110 |
+
n, err = io.Discard.Write(buf)
|
| 111 |
+
}
|
| 112 |
+
b.bytes += n
|
| 113 |
+
return n, err
|
| 114 |
+
}
|
| 115 |
+
|
| 116 |
+
func (b *basicWriter) maybeWriteHeader() {
|
| 117 |
+
if !b.wroteHeader {
|
| 118 |
+
b.WriteHeader(http.StatusOK)
|
| 119 |
+
}
|
| 120 |
+
}
|
| 121 |
+
|
| 122 |
+
func (b *basicWriter) Status() int {
|
| 123 |
+
return b.code
|
| 124 |
+
}
|
| 125 |
+
|
| 126 |
+
func (b *basicWriter) BytesWritten() int {
|
| 127 |
+
return b.bytes
|
| 128 |
+
}
|
| 129 |
+
|
| 130 |
+
func (b *basicWriter) Tee(w io.Writer) {
|
| 131 |
+
b.tee = w
|
| 132 |
+
}
|
| 133 |
+
|
| 134 |
+
func (b *basicWriter) Unwrap() http.ResponseWriter {
|
| 135 |
+
return b.ResponseWriter
|
| 136 |
+
}
|
| 137 |
+
|
| 138 |
+
func (b *basicWriter) Discard() {
|
| 139 |
+
b.discard = true
|
| 140 |
+
}
|
| 141 |
+
|
| 142 |
+
// flushWriter ...
|
| 143 |
+
type flushWriter struct {
|
| 144 |
+
basicWriter
|
| 145 |
+
}
|
| 146 |
+
|
| 147 |
+
func (f *flushWriter) Flush() {
|
| 148 |
+
f.wroteHeader = true
|
| 149 |
+
fl := f.basicWriter.ResponseWriter.(http.Flusher)
|
| 150 |
+
fl.Flush()
|
| 151 |
+
}
|
| 152 |
+
|
| 153 |
+
var _ http.Flusher = &flushWriter{}
|
| 154 |
+
|
| 155 |
+
// hijackWriter ...
|
| 156 |
+
type hijackWriter struct {
|
| 157 |
+
basicWriter
|
| 158 |
+
}
|
| 159 |
+
|
| 160 |
+
func (f *hijackWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
|
| 161 |
+
hj := f.basicWriter.ResponseWriter.(http.Hijacker)
|
| 162 |
+
return hj.Hijack()
|
| 163 |
+
}
|
| 164 |
+
|
| 165 |
+
var _ http.Hijacker = &hijackWriter{}
|
| 166 |
+
|
| 167 |
+
// flushHijackWriter ...
|
| 168 |
+
type flushHijackWriter struct {
|
| 169 |
+
basicWriter
|
| 170 |
+
}
|
| 171 |
+
|
| 172 |
+
func (f *flushHijackWriter) Flush() {
|
| 173 |
+
f.wroteHeader = true
|
| 174 |
+
fl := f.basicWriter.ResponseWriter.(http.Flusher)
|
| 175 |
+
fl.Flush()
|
| 176 |
+
}
|
| 177 |
+
|
| 178 |
+
func (f *flushHijackWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
|
| 179 |
+
hj := f.basicWriter.ResponseWriter.(http.Hijacker)
|
| 180 |
+
return hj.Hijack()
|
| 181 |
+
}
|
| 182 |
+
|
| 183 |
+
var _ http.Flusher = &flushHijackWriter{}
|
| 184 |
+
var _ http.Hijacker = &flushHijackWriter{}
|
| 185 |
+
|
| 186 |
+
// httpFancyWriter is a HTTP writer that additionally satisfies
|
| 187 |
+
// http.Flusher, http.Hijacker, and io.ReaderFrom. It exists for the common case
|
| 188 |
+
// of wrapping the http.ResponseWriter that package http gives you, in order to
|
| 189 |
+
// make the proxied object support the full method set of the proxied object.
|
| 190 |
+
type httpFancyWriter struct {
|
| 191 |
+
basicWriter
|
| 192 |
+
}
|
| 193 |
+
|
| 194 |
+
func (f *httpFancyWriter) Flush() {
|
| 195 |
+
f.wroteHeader = true
|
| 196 |
+
fl := f.basicWriter.ResponseWriter.(http.Flusher)
|
| 197 |
+
fl.Flush()
|
| 198 |
+
}
|
| 199 |
+
|
| 200 |
+
func (f *httpFancyWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
|
| 201 |
+
hj := f.basicWriter.ResponseWriter.(http.Hijacker)
|
| 202 |
+
return hj.Hijack()
|
| 203 |
+
}
|
| 204 |
+
|
| 205 |
+
func (f *http2FancyWriter) Push(target string, opts *http.PushOptions) error {
|
| 206 |
+
return f.basicWriter.ResponseWriter.(http.Pusher).Push(target, opts)
|
| 207 |
+
}
|
| 208 |
+
|
| 209 |
+
func (f *httpFancyWriter) ReadFrom(r io.Reader) (int64, error) {
|
| 210 |
+
if f.basicWriter.tee != nil {
|
| 211 |
+
n, err := io.Copy(&f.basicWriter, r)
|
| 212 |
+
f.basicWriter.bytes += int(n)
|
| 213 |
+
return n, err
|
| 214 |
+
}
|
| 215 |
+
rf := f.basicWriter.ResponseWriter.(io.ReaderFrom)
|
| 216 |
+
f.basicWriter.maybeWriteHeader()
|
| 217 |
+
n, err := rf.ReadFrom(r)
|
| 218 |
+
f.basicWriter.bytes += int(n)
|
| 219 |
+
return n, err
|
| 220 |
+
}
|
| 221 |
+
|
| 222 |
+
var _ http.Flusher = &httpFancyWriter{}
|
| 223 |
+
var _ http.Hijacker = &httpFancyWriter{}
|
| 224 |
+
var _ http.Pusher = &http2FancyWriter{}
|
| 225 |
+
var _ io.ReaderFrom = &httpFancyWriter{}
|
| 226 |
+
|
| 227 |
+
// http2FancyWriter is a HTTP2 writer that additionally satisfies
|
| 228 |
+
// http.Flusher, and io.ReaderFrom. It exists for the common case
|
| 229 |
+
// of wrapping the http.ResponseWriter that package http gives you, in order to
|
| 230 |
+
// make the proxied object support the full method set of the proxied object.
|
| 231 |
+
type http2FancyWriter struct {
|
| 232 |
+
basicWriter
|
| 233 |
+
}
|
| 234 |
+
|
| 235 |
+
func (f *http2FancyWriter) Flush() {
|
| 236 |
+
f.wroteHeader = true
|
| 237 |
+
fl := f.basicWriter.ResponseWriter.(http.Flusher)
|
| 238 |
+
fl.Flush()
|
| 239 |
+
}
|
| 240 |
+
|
| 241 |
+
var _ http.Flusher = &http2FancyWriter{}
|
tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5d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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
package chi
|
| 2 |
+
|
| 3 |
+
import (
|
| 4 |
+
"context"
|
| 5 |
+
"fmt"
|
| 6 |
+
"net/http"
|
| 7 |
+
"strings"
|
| 8 |
+
"sync"
|
| 9 |
+
)
|
| 10 |
+
|
| 11 |
+
var _ Router = &Mux{}
|
| 12 |
+
|
| 13 |
+
// Mux is a simple HTTP route multiplexer that parses a request path,
|
| 14 |
+
// records any URL params, and executes an end handler. It implements
|
| 15 |
+
// the http.Handler interface and is friendly with the standard library.
|
| 16 |
+
//
|
| 17 |
+
// Mux is designed to be fast, minimal and offer a powerful API for building
|
| 18 |
+
// modular and composable HTTP services with a large set of handlers. It's
|
| 19 |
+
// particularly useful for writing large REST API services that break a handler
|
| 20 |
+
// into many smaller parts composed of middlewares and end handlers.
|
| 21 |
+
type Mux struct {
|
| 22 |
+
// The computed mux handler made of the chained middleware stack and
|
| 23 |
+
// the tree router
|
| 24 |
+
handler http.Handler
|
| 25 |
+
|
| 26 |
+
// The radix trie router
|
| 27 |
+
tree *node
|
| 28 |
+
|
| 29 |
+
// Custom method not allowed handler
|
| 30 |
+
methodNotAllowedHandler http.HandlerFunc
|
| 31 |
+
|
| 32 |
+
// A reference to the parent mux used by subrouters when mounting
|
| 33 |
+
// to a parent mux
|
| 34 |
+
parent *Mux
|
| 35 |
+
|
| 36 |
+
// Routing context pool
|
| 37 |
+
pool *sync.Pool
|
| 38 |
+
|
| 39 |
+
// Custom route not found handler
|
| 40 |
+
notFoundHandler http.HandlerFunc
|
| 41 |
+
|
| 42 |
+
// The middleware stack
|
| 43 |
+
middlewares []func(http.Handler) http.Handler
|
| 44 |
+
|
| 45 |
+
// Controls the behaviour of middleware chain generation when a mux
|
| 46 |
+
// is registered as an inline group inside another mux.
|
| 47 |
+
inline bool
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
// NewMux returns a newly initialized Mux object that implements the Router
|
| 51 |
+
// interface.
|
| 52 |
+
func NewMux() *Mux {
|
| 53 |
+
mux := &Mux{tree: &node{}, pool: &sync.Pool{}}
|
| 54 |
+
mux.pool.New = func() interface{} {
|
| 55 |
+
return NewRouteContext()
|
| 56 |
+
}
|
| 57 |
+
return mux
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
// ServeHTTP is the single method of the http.Handler interface that makes
|
| 61 |
+
// Mux interoperable with the standard library. It uses a sync.Pool to get and
|
| 62 |
+
// reuse routing contexts for each request.
|
| 63 |
+
func (mx *Mux) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
| 64 |
+
// Ensure the mux has some routes defined on the mux
|
| 65 |
+
if mx.handler == nil {
|
| 66 |
+
mx.NotFoundHandler().ServeHTTP(w, r)
|
| 67 |
+
return
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
// Check if a routing context already exists from a parent router.
|
| 71 |
+
rctx, _ := r.Context().Value(RouteCtxKey).(*Context)
|
| 72 |
+
if rctx != nil {
|
| 73 |
+
mx.handler.ServeHTTP(w, r)
|
| 74 |
+
return
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
// Fetch a RouteContext object from the sync pool, and call the computed
|
| 78 |
+
// mx.handler that is comprised of mx.middlewares + mx.routeHTTP.
|
| 79 |
+
// Once the request is finished, reset the routing context and put it back
|
| 80 |
+
// into the pool for reuse from another request.
|
| 81 |
+
rctx = mx.pool.Get().(*Context)
|
| 82 |
+
rctx.Reset()
|
| 83 |
+
rctx.Routes = mx
|
| 84 |
+
rctx.parentCtx = r.Context()
|
| 85 |
+
|
| 86 |
+
// NOTE: r.WithContext() causes 2 allocations and context.WithValue() causes 1 allocation
|
| 87 |
+
r = r.WithContext(context.WithValue(r.Context(), RouteCtxKey, rctx))
|
| 88 |
+
|
| 89 |
+
// Serve the request and once its done, put the request context back in the sync pool
|
| 90 |
+
mx.handler.ServeHTTP(w, r)
|
| 91 |
+
mx.pool.Put(rctx)
|
| 92 |
+
}
|
| 93 |
+
|
| 94 |
+
// Use appends a middleware handler to the Mux middleware stack.
|
| 95 |
+
//
|
| 96 |
+
// The middleware stack for any Mux will execute before searching for a matching
|
| 97 |
+
// route to a specific handler, which provides opportunity to respond early,
|
| 98 |
+
// change the course of the request execution, or set request-scoped values for
|
| 99 |
+
// the next http.Handler.
|
| 100 |
+
func (mx *Mux) Use(middlewares ...func(http.Handler) http.Handler) {
|
| 101 |
+
if mx.handler != nil {
|
| 102 |
+
panic("chi: all middlewares must be defined before routes on a mux")
|
| 103 |
+
}
|
| 104 |
+
mx.middlewares = append(mx.middlewares, middlewares...)
|
| 105 |
+
}
|
| 106 |
+
|
| 107 |
+
// Handle adds the route `pattern` that matches any http method to
|
| 108 |
+
// execute the `handler` http.Handler.
|
| 109 |
+
func (mx *Mux) Handle(pattern string, handler http.Handler) {
|
| 110 |
+
if method, rest, found := strings.Cut(pattern, " "); found {
|
| 111 |
+
mx.Method(method, rest, handler)
|
| 112 |
+
return
|
| 113 |
+
}
|
| 114 |
+
|
| 115 |
+
mx.handle(mALL, pattern, handler)
|
| 116 |
+
}
|
| 117 |
+
|
| 118 |
+
// HandleFunc adds the route `pattern` that matches any http method to
|
| 119 |
+
// execute the `handlerFn` http.HandlerFunc.
|
| 120 |
+
func (mx *Mux) HandleFunc(pattern string, handlerFn http.HandlerFunc) {
|
| 121 |
+
if method, rest, found := strings.Cut(pattern, " "); found {
|
| 122 |
+
mx.Method(method, rest, handlerFn)
|
| 123 |
+
return
|
| 124 |
+
}
|
| 125 |
+
|
| 126 |
+
mx.handle(mALL, pattern, handlerFn)
|
| 127 |
+
}
|
| 128 |
+
|
| 129 |
+
// Method adds the route `pattern` that matches `method` http method to
|
| 130 |
+
// execute the `handler` http.Handler.
|
| 131 |
+
func (mx *Mux) Method(method, pattern string, handler http.Handler) {
|
| 132 |
+
m, ok := methodMap[strings.ToUpper(method)]
|
| 133 |
+
if !ok {
|
| 134 |
+
panic(fmt.Sprintf("chi: '%s' http method is not supported.", method))
|
| 135 |
+
}
|
| 136 |
+
mx.handle(m, pattern, handler)
|
| 137 |
+
}
|
| 138 |
+
|
| 139 |
+
// MethodFunc adds the route `pattern` that matches `method` http method to
|
| 140 |
+
// execute the `handlerFn` http.HandlerFunc.
|
| 141 |
+
func (mx *Mux) MethodFunc(method, pattern string, handlerFn http.HandlerFunc) {
|
| 142 |
+
mx.Method(method, pattern, handlerFn)
|
| 143 |
+
}
|
| 144 |
+
|
| 145 |
+
// Connect adds the route `pattern` that matches a CONNECT http method to
|
| 146 |
+
// execute the `handlerFn` http.HandlerFunc.
|
| 147 |
+
func (mx *Mux) Connect(pattern string, handlerFn http.HandlerFunc) {
|
| 148 |
+
mx.handle(mCONNECT, pattern, handlerFn)
|
| 149 |
+
}
|
| 150 |
+
|
| 151 |
+
// Delete adds the route `pattern` that matches a DELETE http method to
|
| 152 |
+
// execute the `handlerFn` http.HandlerFunc.
|
| 153 |
+
func (mx *Mux) Delete(pattern string, handlerFn http.HandlerFunc) {
|
| 154 |
+
mx.handle(mDELETE, pattern, handlerFn)
|
| 155 |
+
}
|
| 156 |
+
|
| 157 |
+
// Get adds the route `pattern` that matches a GET http method to
|
| 158 |
+
// execute the `handlerFn` http.HandlerFunc.
|
| 159 |
+
func (mx *Mux) Get(pattern string, handlerFn http.HandlerFunc) {
|
| 160 |
+
mx.handle(mGET, pattern, handlerFn)
|
| 161 |
+
}
|
| 162 |
+
|
| 163 |
+
// Head adds the route `pattern` that matches a HEAD http method to
|
| 164 |
+
// execute the `handlerFn` http.HandlerFunc.
|
| 165 |
+
func (mx *Mux) Head(pattern string, handlerFn http.HandlerFunc) {
|
| 166 |
+
mx.handle(mHEAD, pattern, handlerFn)
|
| 167 |
+
}
|
| 168 |
+
|
| 169 |
+
// Options adds the route `pattern` that matches an OPTIONS http method to
|
| 170 |
+
// execute the `handlerFn` http.HandlerFunc.
|
| 171 |
+
func (mx *Mux) Options(pattern string, handlerFn http.HandlerFunc) {
|
| 172 |
+
mx.handle(mOPTIONS, pattern, handlerFn)
|
| 173 |
+
}
|
| 174 |
+
|
| 175 |
+
// Patch adds the route `pattern` that matches a PATCH http method to
|
| 176 |
+
// execute the `handlerFn` http.HandlerFunc.
|
| 177 |
+
func (mx *Mux) Patch(pattern string, handlerFn http.HandlerFunc) {
|
| 178 |
+
mx.handle(mPATCH, pattern, handlerFn)
|
| 179 |
+
}
|
| 180 |
+
|
| 181 |
+
// Post adds the route `pattern` that matches a POST http method to
|
| 182 |
+
// execute the `handlerFn` http.HandlerFunc.
|
| 183 |
+
func (mx *Mux) Post(pattern string, handlerFn http.HandlerFunc) {
|
| 184 |
+
mx.handle(mPOST, pattern, handlerFn)
|
| 185 |
+
}
|
| 186 |
+
|
| 187 |
+
// Put adds the route `pattern` that matches a PUT http method to
|
| 188 |
+
// execute the `handlerFn` http.HandlerFunc.
|
| 189 |
+
func (mx *Mux) Put(pattern string, handlerFn http.HandlerFunc) {
|
| 190 |
+
mx.handle(mPUT, pattern, handlerFn)
|
| 191 |
+
}
|
| 192 |
+
|
| 193 |
+
// Trace adds the route `pattern` that matches a TRACE http method to
|
| 194 |
+
// execute the `handlerFn` http.HandlerFunc.
|
| 195 |
+
func (mx *Mux) Trace(pattern string, handlerFn http.HandlerFunc) {
|
| 196 |
+
mx.handle(mTRACE, pattern, handlerFn)
|
| 197 |
+
}
|
| 198 |
+
|
| 199 |
+
// NotFound sets a custom http.HandlerFunc for routing paths that could
|
| 200 |
+
// not be found. The default 404 handler is `http.NotFound`.
|
| 201 |
+
func (mx *Mux) NotFound(handlerFn http.HandlerFunc) {
|
| 202 |
+
// Build NotFound handler chain
|
| 203 |
+
m := mx
|
| 204 |
+
hFn := handlerFn
|
| 205 |
+
if mx.inline && mx.parent != nil {
|
| 206 |
+
m = mx.parent
|
| 207 |
+
hFn = Chain(mx.middlewares...).HandlerFunc(hFn).ServeHTTP
|
| 208 |
+
}
|
| 209 |
+
|
| 210 |
+
// Update the notFoundHandler from this point forward
|
| 211 |
+
m.notFoundHandler = hFn
|
| 212 |
+
m.updateSubRoutes(func(subMux *Mux) {
|
| 213 |
+
if subMux.notFoundHandler == nil {
|
| 214 |
+
subMux.NotFound(hFn)
|
| 215 |
+
}
|
| 216 |
+
})
|
| 217 |
+
}
|
| 218 |
+
|
| 219 |
+
// MethodNotAllowed sets a custom http.HandlerFunc for routing paths where the
|
| 220 |
+
// method is unresolved. The default handler returns a 405 with an empty body.
|
| 221 |
+
func (mx *Mux) MethodNotAllowed(handlerFn http.HandlerFunc) {
|
| 222 |
+
// Build MethodNotAllowed handler chain
|
| 223 |
+
m := mx
|
| 224 |
+
hFn := handlerFn
|
| 225 |
+
if mx.inline && mx.parent != nil {
|
| 226 |
+
m = mx.parent
|
| 227 |
+
hFn = Chain(mx.middlewares...).HandlerFunc(hFn).ServeHTTP
|
| 228 |
+
}
|
| 229 |
+
|
| 230 |
+
// Update the methodNotAllowedHandler from this point forward
|
| 231 |
+
m.methodNotAllowedHandler = hFn
|
| 232 |
+
m.updateSubRoutes(func(subMux *Mux) {
|
| 233 |
+
if subMux.methodNotAllowedHandler == nil {
|
| 234 |
+
subMux.MethodNotAllowed(hFn)
|
| 235 |
+
}
|
| 236 |
+
})
|
| 237 |
+
}
|
| 238 |
+
|
| 239 |
+
// With adds inline middlewares for an endpoint handler.
|
| 240 |
+
func (mx *Mux) With(middlewares ...func(http.Handler) http.Handler) Router {
|
| 241 |
+
// Similarly as in handle(), we must build the mux handler once additional
|
| 242 |
+
// middleware registration isn't allowed for this stack, like now.
|
| 243 |
+
if !mx.inline && mx.handler == nil {
|
| 244 |
+
mx.updateRouteHandler()
|
| 245 |
+
}
|
| 246 |
+
|
| 247 |
+
// Copy middlewares from parent inline muxs
|
| 248 |
+
var mws Middlewares
|
| 249 |
+
if mx.inline {
|
| 250 |
+
mws = make(Middlewares, len(mx.middlewares))
|
| 251 |
+
copy(mws, mx.middlewares)
|
| 252 |
+
}
|
| 253 |
+
mws = append(mws, middlewares...)
|
| 254 |
+
|
| 255 |
+
im := &Mux{
|
| 256 |
+
pool: mx.pool, inline: true, parent: mx, tree: mx.tree, middlewares: mws,
|
| 257 |
+
notFoundHandler: mx.notFoundHandler, methodNotAllowedHandler: mx.methodNotAllowedHandler,
|
| 258 |
+
}
|
| 259 |
+
|
| 260 |
+
return im
|
| 261 |
+
}
|
| 262 |
+
|
| 263 |
+
// Group creates a new inline-Mux with a copy of middleware stack. It's useful
|
| 264 |
+
// for a group of handlers along the same routing path that use an additional
|
| 265 |
+
// set of middlewares. See _examples/.
|
| 266 |
+
func (mx *Mux) Group(fn func(r Router)) Router {
|
| 267 |
+
im := mx.With()
|
| 268 |
+
if fn != nil {
|
| 269 |
+
fn(im)
|
| 270 |
+
}
|
| 271 |
+
return im
|
| 272 |
+
}
|
| 273 |
+
|
| 274 |
+
// Route creates a new Mux and mounts it along the `pattern` as a subrouter.
|
| 275 |
+
// Effectively, this is a short-hand call to Mount. See _examples/.
|
| 276 |
+
func (mx *Mux) Route(pattern string, fn func(r Router)) Router {
|
| 277 |
+
if fn == nil {
|
| 278 |
+
panic(fmt.Sprintf("chi: attempting to Route() a nil subrouter on '%s'", pattern))
|
| 279 |
+
}
|
| 280 |
+
subRouter := NewRouter()
|
| 281 |
+
fn(subRouter)
|
| 282 |
+
mx.Mount(pattern, subRouter)
|
| 283 |
+
return subRouter
|
| 284 |
+
}
|
| 285 |
+
|
| 286 |
+
// Mount attaches another http.Handler or chi Router as a subrouter along a routing
|
| 287 |
+
// path. It's very useful to split up a large API as many independent routers and
|
| 288 |
+
// compose them as a single service using Mount. See _examples/.
|
| 289 |
+
//
|
| 290 |
+
// Note that Mount() simply sets a wildcard along the `pattern` that will continue
|
| 291 |
+
// routing at the `handler`, which in most cases is another chi.Router. As a result,
|
| 292 |
+
// if you define two Mount() routes on the exact same pattern the mount will panic.
|
| 293 |
+
func (mx *Mux) Mount(pattern string, handler http.Handler) {
|
| 294 |
+
if handler == nil {
|
| 295 |
+
panic(fmt.Sprintf("chi: attempting to Mount() a nil handler on '%s'", pattern))
|
| 296 |
+
}
|
| 297 |
+
|
| 298 |
+
// Provide runtime safety for ensuring a pattern isn't mounted on an existing
|
| 299 |
+
// routing pattern.
|
| 300 |
+
if mx.tree.findPattern(pattern+"*") || mx.tree.findPattern(pattern+"/*") {
|
| 301 |
+
panic(fmt.Sprintf("chi: attempting to Mount() a handler on an existing path, '%s'", pattern))
|
| 302 |
+
}
|
| 303 |
+
|
| 304 |
+
// Assign sub-Router's with the parent not found & method not allowed handler if not specified.
|
| 305 |
+
subr, ok := handler.(*Mux)
|
| 306 |
+
if ok && subr.notFoundHandler == nil && mx.notFoundHandler != nil {
|
| 307 |
+
subr.NotFound(mx.notFoundHandler)
|
| 308 |
+
}
|
| 309 |
+
if ok && subr.methodNotAllowedHandler == nil && mx.methodNotAllowedHandler != nil {
|
| 310 |
+
subr.MethodNotAllowed(mx.methodNotAllowedHandler)
|
| 311 |
+
}
|
| 312 |
+
|
| 313 |
+
mountHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
| 314 |
+
rctx := RouteContext(r.Context())
|
| 315 |
+
|
| 316 |
+
// shift the url path past the previous subrouter
|
| 317 |
+
rctx.RoutePath = mx.nextRoutePath(rctx)
|
| 318 |
+
|
| 319 |
+
// reset the wildcard URLParam which connects the subrouter
|
| 320 |
+
n := len(rctx.URLParams.Keys) - 1
|
| 321 |
+
if n >= 0 && rctx.URLParams.Keys[n] == "*" && len(rctx.URLParams.Values) > n {
|
| 322 |
+
rctx.URLParams.Values[n] = ""
|
| 323 |
+
}
|
| 324 |
+
|
| 325 |
+
handler.ServeHTTP(w, r)
|
| 326 |
+
})
|
| 327 |
+
|
| 328 |
+
if pattern == "" || pattern[len(pattern)-1] != '/' {
|
| 329 |
+
mx.handle(mALL|mSTUB, pattern, mountHandler)
|
| 330 |
+
mx.handle(mALL|mSTUB, pattern+"/", mountHandler)
|
| 331 |
+
pattern += "/"
|
| 332 |
+
}
|
| 333 |
+
|
| 334 |
+
method := mALL
|
| 335 |
+
subroutes, _ := handler.(Routes)
|
| 336 |
+
if subroutes != nil {
|
| 337 |
+
method |= mSTUB
|
| 338 |
+
}
|
| 339 |
+
n := mx.handle(method, pattern+"*", mountHandler)
|
| 340 |
+
|
| 341 |
+
if subroutes != nil {
|
| 342 |
+
n.subroutes = subroutes
|
| 343 |
+
}
|
| 344 |
+
}
|
| 345 |
+
|
| 346 |
+
// Routes returns a slice of routing information from the tree,
|
| 347 |
+
// useful for traversing available routes of a router.
|
| 348 |
+
func (mx *Mux) Routes() []Route {
|
| 349 |
+
return mx.tree.routes()
|
| 350 |
+
}
|
| 351 |
+
|
| 352 |
+
// Middlewares returns a slice of middleware handler functions.
|
| 353 |
+
func (mx *Mux) Middlewares() Middlewares {
|
| 354 |
+
return mx.middlewares
|
| 355 |
+
}
|
| 356 |
+
|
| 357 |
+
// Match searches the routing tree for a handler that matches the method/path.
|
| 358 |
+
// It's similar to routing a http request, but without executing the handler
|
| 359 |
+
// thereafter.
|
| 360 |
+
//
|
| 361 |
+
// Note: the *Context state is updated during execution, so manage
|
| 362 |
+
// the state carefully or make a NewRouteContext().
|
| 363 |
+
func (mx *Mux) Match(rctx *Context, method, path string) bool {
|
| 364 |
+
return mx.Find(rctx, method, path) != ""
|
| 365 |
+
}
|
| 366 |
+
|
| 367 |
+
// Find searches the routing tree for the pattern that matches
|
| 368 |
+
// the method/path.
|
| 369 |
+
//
|
| 370 |
+
// Note: the *Context state is updated during execution, so manage
|
| 371 |
+
// the state carefully or make a NewRouteContext().
|
| 372 |
+
func (mx *Mux) Find(rctx *Context, method, path string) string {
|
| 373 |
+
m, ok := methodMap[method]
|
| 374 |
+
if !ok {
|
| 375 |
+
return ""
|
| 376 |
+
}
|
| 377 |
+
|
| 378 |
+
node, _, _ := mx.tree.FindRoute(rctx, m, path)
|
| 379 |
+
pattern := rctx.routePattern
|
| 380 |
+
|
| 381 |
+
if node != nil {
|
| 382 |
+
if node.subroutes == nil {
|
| 383 |
+
e := node.endpoints[m]
|
| 384 |
+
return e.pattern
|
| 385 |
+
}
|
| 386 |
+
|
| 387 |
+
rctx.RoutePath = mx.nextRoutePath(rctx)
|
| 388 |
+
subPattern := node.subroutes.Find(rctx, method, rctx.RoutePath)
|
| 389 |
+
if subPattern == "" {
|
| 390 |
+
return ""
|
| 391 |
+
}
|
| 392 |
+
|
| 393 |
+
pattern = strings.TrimSuffix(pattern, "/*")
|
| 394 |
+
pattern += subPattern
|
| 395 |
+
}
|
| 396 |
+
|
| 397 |
+
return pattern
|
| 398 |
+
}
|
| 399 |
+
|
| 400 |
+
// NotFoundHandler returns the default Mux 404 responder whenever a route
|
| 401 |
+
// cannot be found.
|
| 402 |
+
func (mx *Mux) NotFoundHandler() http.HandlerFunc {
|
| 403 |
+
if mx.notFoundHandler != nil {
|
| 404 |
+
return mx.notFoundHandler
|
| 405 |
+
}
|
| 406 |
+
return http.NotFound
|
| 407 |
+
}
|
| 408 |
+
|
| 409 |
+
// MethodNotAllowedHandler returns the default Mux 405 responder whenever
|
| 410 |
+
// a method cannot be resolved for a route.
|
| 411 |
+
func (mx *Mux) MethodNotAllowedHandler(methodsAllowed ...methodTyp) http.HandlerFunc {
|
| 412 |
+
if mx.methodNotAllowedHandler != nil {
|
| 413 |
+
return mx.methodNotAllowedHandler
|
| 414 |
+
}
|
| 415 |
+
return methodNotAllowedHandler(methodsAllowed...)
|
| 416 |
+
}
|
| 417 |
+
|
| 418 |
+
// handle registers a http.Handler in the routing tree for a particular http method
|
| 419 |
+
// and routing pattern.
|
| 420 |
+
func (mx *Mux) handle(method methodTyp, pattern string, handler http.Handler) *node {
|
| 421 |
+
if len(pattern) == 0 || pattern[0] != '/' {
|
| 422 |
+
panic(fmt.Sprintf("chi: routing pattern must begin with '/' in '%s'", pattern))
|
| 423 |
+
}
|
| 424 |
+
|
| 425 |
+
// Build the computed routing handler for this routing pattern.
|
| 426 |
+
if !mx.inline && mx.handler == nil {
|
| 427 |
+
mx.updateRouteHandler()
|
| 428 |
+
}
|
| 429 |
+
|
| 430 |
+
// Build endpoint handler with inline middlewares for the route
|
| 431 |
+
var h http.Handler
|
| 432 |
+
if mx.inline {
|
| 433 |
+
mx.handler = http.HandlerFunc(mx.routeHTTP)
|
| 434 |
+
h = Chain(mx.middlewares...).Handler(handler)
|
| 435 |
+
} else {
|
| 436 |
+
h = handler
|
| 437 |
+
}
|
| 438 |
+
|
| 439 |
+
// Add the endpoint to the tree and return the node
|
| 440 |
+
return mx.tree.InsertRoute(method, pattern, h)
|
| 441 |
+
}
|
| 442 |
+
|
| 443 |
+
// routeHTTP routes a http.Request through the Mux routing tree to serve
|
| 444 |
+
// the matching handler for a particular http method.
|
| 445 |
+
func (mx *Mux) routeHTTP(w http.ResponseWriter, r *http.Request) {
|
| 446 |
+
// Grab the route context object
|
| 447 |
+
rctx := r.Context().Value(RouteCtxKey).(*Context)
|
| 448 |
+
|
| 449 |
+
// The request routing path
|
| 450 |
+
routePath := rctx.RoutePath
|
| 451 |
+
if routePath == "" {
|
| 452 |
+
if r.URL.RawPath != "" {
|
| 453 |
+
routePath = r.URL.RawPath
|
| 454 |
+
} else {
|
| 455 |
+
routePath = r.URL.Path
|
| 456 |
+
}
|
| 457 |
+
if routePath == "" {
|
| 458 |
+
routePath = "/"
|
| 459 |
+
}
|
| 460 |
+
}
|
| 461 |
+
|
| 462 |
+
// Check if method is supported by chi
|
| 463 |
+
if rctx.RouteMethod == "" {
|
| 464 |
+
rctx.RouteMethod = r.Method
|
| 465 |
+
}
|
| 466 |
+
method, ok := methodMap[rctx.RouteMethod]
|
| 467 |
+
if !ok {
|
| 468 |
+
mx.MethodNotAllowedHandler().ServeHTTP(w, r)
|
| 469 |
+
return
|
| 470 |
+
}
|
| 471 |
+
|
| 472 |
+
// Find the route
|
| 473 |
+
if _, _, h := mx.tree.FindRoute(rctx, method, routePath); h != nil {
|
| 474 |
+
if supportsPathValue {
|
| 475 |
+
setPathValue(rctx, r)
|
| 476 |
+
}
|
| 477 |
+
|
| 478 |
+
h.ServeHTTP(w, r)
|
| 479 |
+
return
|
| 480 |
+
}
|
| 481 |
+
if rctx.methodNotAllowed {
|
| 482 |
+
mx.MethodNotAllowedHandler(rctx.methodsAllowed...).ServeHTTP(w, r)
|
| 483 |
+
} else {
|
| 484 |
+
mx.NotFoundHandler().ServeHTTP(w, r)
|
| 485 |
+
}
|
| 486 |
+
}
|
| 487 |
+
|
| 488 |
+
func (mx *Mux) nextRoutePath(rctx *Context) string {
|
| 489 |
+
routePath := "/"
|
| 490 |
+
nx := len(rctx.routeParams.Keys) - 1 // index of last param in list
|
| 491 |
+
if nx >= 0 && rctx.routeParams.Keys[nx] == "*" && len(rctx.routeParams.Values) > nx {
|
| 492 |
+
routePath = "/" + rctx.routeParams.Values[nx]
|
| 493 |
+
}
|
| 494 |
+
return routePath
|
| 495 |
+
}
|
| 496 |
+
|
| 497 |
+
// Recursively update data on child routers.
|
| 498 |
+
func (mx *Mux) updateSubRoutes(fn func(subMux *Mux)) {
|
| 499 |
+
for _, r := range mx.tree.routes() {
|
| 500 |
+
subMux, ok := r.SubRoutes.(*Mux)
|
| 501 |
+
if !ok {
|
| 502 |
+
continue
|
| 503 |
+
}
|
| 504 |
+
fn(subMux)
|
| 505 |
+
}
|
| 506 |
+
}
|
| 507 |
+
|
| 508 |
+
// updateRouteHandler builds the single mux handler that is a chain of the middleware
|
| 509 |
+
// stack, as defined by calls to Use(), and the tree router (Mux) itself. After this
|
| 510 |
+
// point, no other middlewares can be registered on this Mux's stack. But you can still
|
| 511 |
+
// compose additional middlewares via Group()'s or using a chained middleware handler.
|
| 512 |
+
func (mx *Mux) updateRouteHandler() {
|
| 513 |
+
mx.handler = chain(mx.middlewares, http.HandlerFunc(mx.routeHTTP))
|
| 514 |
+
}
|
| 515 |
+
|
| 516 |
+
// methodNotAllowedHandler is a helper function to respond with a 405,
|
| 517 |
+
// method not allowed. It sets the Allow header with the list of allowed
|
| 518 |
+
// methods for the route.
|
| 519 |
+
func methodNotAllowedHandler(methodsAllowed ...methodTyp) func(w http.ResponseWriter, r *http.Request) {
|
| 520 |
+
return func(w http.ResponseWriter, r *http.Request) {
|
| 521 |
+
for _, m := range methodsAllowed {
|
| 522 |
+
w.Header().Add("Allow", reverseMethodMap[m])
|
| 523 |
+
}
|
| 524 |
+
w.WriteHeader(405)
|
| 525 |
+
w.Write(nil)
|
| 526 |
+
}
|
| 527 |
+
}
|
tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/environment/source/path_value.go
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
//go:build go1.22 && !tinygo
|
| 2 |
+
// +build go1.22,!tinygo
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
package chi
|
| 6 |
+
|
| 7 |
+
import "net/http"
|
| 8 |
+
|
| 9 |
+
// supportsPathValue is true if the Go version is 1.22 and above.
|
| 10 |
+
//
|
| 11 |
+
// If this is true, `net/http.Request` has methods `SetPathValue` and `PathValue`.
|
| 12 |
+
const supportsPathValue = true
|
| 13 |
+
|
| 14 |
+
// setPathValue sets the path values in the Request value
|
| 15 |
+
// based on the provided request context.
|
| 16 |
+
func setPathValue(rctx *Context, r *http.Request) {
|
| 17 |
+
for i, key := range rctx.URLParams.Keys {
|
| 18 |
+
value := rctx.URLParams.Values[i]
|
| 19 |
+
r.SetPathValue(key, value)
|
| 20 |
+
}
|
| 21 |
+
}
|
tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/environment/source/path_value_fallback.go
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
//go:build !go1.22 || tinygo
|
| 2 |
+
// +build !go1.22 tinygo
|
| 3 |
+
|
| 4 |
+
package chi
|
| 5 |
+
|
| 6 |
+
import "net/http"
|
| 7 |
+
|
| 8 |
+
// supportsPathValue is true if the Go version is 1.22 and above.
|
| 9 |
+
//
|
| 10 |
+
// If this is true, `net/http.Request` has methods `SetPathValue` and `PathValue`.
|
| 11 |
+
const supportsPathValue = false
|
| 12 |
+
|
| 13 |
+
// setPathValue sets the path values in the Request value
|
| 14 |
+
// based on the provided request context.
|
| 15 |
+
//
|
| 16 |
+
// setPathValue is only supported in Go 1.22 and above so
|
| 17 |
+
// this is just a blank function so that it compiles.
|
| 18 |
+
func setPathValue(rctx *Context, r *http.Request) {
|
| 19 |
+
}
|
tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/environment/source/tree.go
ADDED
|
@@ -0,0 +1,890 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
package chi
|
| 2 |
+
|
| 3 |
+
// Radix tree implementation below is a based on the original work by
|
| 4 |
+
// Armon Dadgar in https://github.com/armon/go-radix/blob/master/radix.go
|
| 5 |
+
// (MIT licensed). It's been heavily modified for use as a HTTP routing tree.
|
| 6 |
+
|
| 7 |
+
import (
|
| 8 |
+
"fmt"
|
| 9 |
+
"net/http"
|
| 10 |
+
"regexp"
|
| 11 |
+
"sort"
|
| 12 |
+
"strconv"
|
| 13 |
+
"strings"
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
type methodTyp uint
|
| 17 |
+
|
| 18 |
+
const (
|
| 19 |
+
mSTUB methodTyp = 1 << iota
|
| 20 |
+
mCONNECT
|
| 21 |
+
mDELETE
|
| 22 |
+
mGET
|
| 23 |
+
mHEAD
|
| 24 |
+
mOPTIONS
|
| 25 |
+
mPATCH
|
| 26 |
+
mPOST
|
| 27 |
+
mPUT
|
| 28 |
+
mTRACE
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
var mALL = mCONNECT | mDELETE | mGET | mHEAD |
|
| 32 |
+
mOPTIONS | mPATCH | mPOST | mPUT | mTRACE
|
| 33 |
+
|
| 34 |
+
var methodMap = map[string]methodTyp{
|
| 35 |
+
http.MethodConnect: mCONNECT,
|
| 36 |
+
http.MethodDelete: mDELETE,
|
| 37 |
+
http.MethodGet: mGET,
|
| 38 |
+
http.MethodHead: mHEAD,
|
| 39 |
+
http.MethodOptions: mOPTIONS,
|
| 40 |
+
http.MethodPatch: mPATCH,
|
| 41 |
+
http.MethodPost: mPOST,
|
| 42 |
+
http.MethodPut: mPUT,
|
| 43 |
+
http.MethodTrace: mTRACE,
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
var reverseMethodMap = map[methodTyp]string{
|
| 47 |
+
mCONNECT: http.MethodConnect,
|
| 48 |
+
mDELETE: http.MethodDelete,
|
| 49 |
+
mGET: http.MethodGet,
|
| 50 |
+
mHEAD: http.MethodHead,
|
| 51 |
+
mOPTIONS: http.MethodOptions,
|
| 52 |
+
mPATCH: http.MethodPatch,
|
| 53 |
+
mPOST: http.MethodPost,
|
| 54 |
+
mPUT: http.MethodPut,
|
| 55 |
+
mTRACE: http.MethodTrace,
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
// RegisterMethod adds support for custom HTTP method handlers, available
|
| 59 |
+
// via Router#Method and Router#MethodFunc
|
| 60 |
+
func RegisterMethod(method string) {
|
| 61 |
+
if method == "" {
|
| 62 |
+
return
|
| 63 |
+
}
|
| 64 |
+
method = strings.ToUpper(method)
|
| 65 |
+
if _, ok := methodMap[method]; ok {
|
| 66 |
+
return
|
| 67 |
+
}
|
| 68 |
+
n := len(methodMap)
|
| 69 |
+
if n > strconv.IntSize-2 {
|
| 70 |
+
panic(fmt.Sprintf("chi: max number of methods reached (%d)", strconv.IntSize))
|
| 71 |
+
}
|
| 72 |
+
mt := methodTyp(2 << n)
|
| 73 |
+
methodMap[method] = mt
|
| 74 |
+
mALL |= mt
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
type nodeTyp uint8
|
| 78 |
+
|
| 79 |
+
const (
|
| 80 |
+
ntStatic nodeTyp = iota // /home
|
| 81 |
+
ntRegexp // /{id:[0-9]+}
|
| 82 |
+
ntParam // /{user}
|
| 83 |
+
ntCatchAll // /api/v1/*
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
type node struct {
|
| 87 |
+
// subroutes on the leaf node
|
| 88 |
+
subroutes Routes
|
| 89 |
+
|
| 90 |
+
// regexp matcher for regexp nodes
|
| 91 |
+
rex *regexp.Regexp
|
| 92 |
+
|
| 93 |
+
// HTTP handler endpoints on the leaf node
|
| 94 |
+
endpoints endpoints
|
| 95 |
+
|
| 96 |
+
// prefix is the common prefix we ignore
|
| 97 |
+
prefix string
|
| 98 |
+
|
| 99 |
+
// child nodes should be stored in-order for iteration,
|
| 100 |
+
// in groups of the node type.
|
| 101 |
+
children [ntCatchAll + 1]nodes
|
| 102 |
+
|
| 103 |
+
// first byte of the child prefix
|
| 104 |
+
tail byte
|
| 105 |
+
|
| 106 |
+
// node type: static, regexp, param, catchAll
|
| 107 |
+
typ nodeTyp
|
| 108 |
+
|
| 109 |
+
// first byte of the prefix
|
| 110 |
+
label byte
|
| 111 |
+
}
|
| 112 |
+
|
| 113 |
+
// endpoints is a mapping of http method constants to handlers
|
| 114 |
+
// for a given route.
|
| 115 |
+
type endpoints map[methodTyp]*endpoint
|
| 116 |
+
|
| 117 |
+
type endpoint struct {
|
| 118 |
+
// endpoint handler
|
| 119 |
+
handler http.Handler
|
| 120 |
+
|
| 121 |
+
// pattern is the routing pattern for handler nodes
|
| 122 |
+
pattern string
|
| 123 |
+
|
| 124 |
+
// parameter keys recorded on handler nodes
|
| 125 |
+
paramKeys []string
|
| 126 |
+
}
|
| 127 |
+
|
| 128 |
+
func (s endpoints) Value(method methodTyp) *endpoint {
|
| 129 |
+
mh, ok := s[method]
|
| 130 |
+
if !ok {
|
| 131 |
+
mh = &endpoint{}
|
| 132 |
+
s[method] = mh
|
| 133 |
+
}
|
| 134 |
+
return mh
|
| 135 |
+
}
|
| 136 |
+
|
| 137 |
+
func (n *node) InsertRoute(method methodTyp, pattern string, handler http.Handler) *node {
|
| 138 |
+
var parent *node
|
| 139 |
+
search := pattern
|
| 140 |
+
|
| 141 |
+
for {
|
| 142 |
+
// Handle key exhaustion
|
| 143 |
+
if len(search) == 0 {
|
| 144 |
+
// Insert or update the node's leaf handler
|
| 145 |
+
n.setEndpoint(method, handler, pattern)
|
| 146 |
+
return n
|
| 147 |
+
}
|
| 148 |
+
|
| 149 |
+
// We're going to be searching for a wild node next,
|
| 150 |
+
// in this case, we need to get the tail
|
| 151 |
+
var label = search[0]
|
| 152 |
+
var segTail byte
|
| 153 |
+
var segEndIdx int
|
| 154 |
+
var segTyp nodeTyp
|
| 155 |
+
var segRexpat string
|
| 156 |
+
if label == '{' || label == '*' {
|
| 157 |
+
segTyp, _, segRexpat, segTail, _, segEndIdx = patNextSegment(search)
|
| 158 |
+
}
|
| 159 |
+
|
| 160 |
+
var prefix string
|
| 161 |
+
if segTyp == ntRegexp {
|
| 162 |
+
prefix = segRexpat
|
| 163 |
+
}
|
| 164 |
+
|
| 165 |
+
// Look for the edge to attach to
|
| 166 |
+
parent = n
|
| 167 |
+
n = n.getEdge(segTyp, label, segTail, prefix)
|
| 168 |
+
|
| 169 |
+
// No edge, create one
|
| 170 |
+
if n == nil {
|
| 171 |
+
child := &node{label: label, tail: segTail, prefix: search}
|
| 172 |
+
hn := parent.addChild(child, search)
|
| 173 |
+
hn.setEndpoint(method, handler, pattern)
|
| 174 |
+
|
| 175 |
+
return hn
|
| 176 |
+
}
|
| 177 |
+
|
| 178 |
+
// Found an edge to match the pattern
|
| 179 |
+
|
| 180 |
+
if n.typ > ntStatic {
|
| 181 |
+
// We found a param node, trim the param from the search path and continue.
|
| 182 |
+
// This param/wild pattern segment would already be on the tree from a previous
|
| 183 |
+
// call to addChild when creating a new node.
|
| 184 |
+
search = search[segEndIdx:]
|
| 185 |
+
continue
|
| 186 |
+
}
|
| 187 |
+
|
| 188 |
+
// Static nodes fall below here.
|
| 189 |
+
// Determine longest prefix of the search key on match.
|
| 190 |
+
commonPrefix := longestPrefix(search, n.prefix)
|
| 191 |
+
if commonPrefix == len(n.prefix) {
|
| 192 |
+
// the common prefix is as long as the current node's prefix we're attempting to insert.
|
| 193 |
+
// keep the search going.
|
| 194 |
+
search = search[commonPrefix:]
|
| 195 |
+
continue
|
| 196 |
+
}
|
| 197 |
+
|
| 198 |
+
// Split the node
|
| 199 |
+
child := &node{
|
| 200 |
+
typ: ntStatic,
|
| 201 |
+
prefix: search[:commonPrefix],
|
| 202 |
+
}
|
| 203 |
+
parent.replaceChild(search[0], segTail, child)
|
| 204 |
+
|
| 205 |
+
// Restore the existing node
|
| 206 |
+
n.label = n.prefix[commonPrefix]
|
| 207 |
+
n.prefix = n.prefix[commonPrefix:]
|
| 208 |
+
child.addChild(n, n.prefix)
|
| 209 |
+
|
| 210 |
+
// If the new key is a subset, set the method/handler on this node and finish.
|
| 211 |
+
search = search[commonPrefix:]
|
| 212 |
+
if len(search) == 0 {
|
| 213 |
+
child.setEndpoint(method, handler, pattern)
|
| 214 |
+
return child
|
| 215 |
+
}
|
| 216 |
+
|
| 217 |
+
// Create a new edge for the node
|
| 218 |
+
subchild := &node{
|
| 219 |
+
typ: ntStatic,
|
| 220 |
+
label: search[0],
|
| 221 |
+
prefix: search,
|
| 222 |
+
}
|
| 223 |
+
hn := child.addChild(subchild, search)
|
| 224 |
+
hn.setEndpoint(method, handler, pattern)
|
| 225 |
+
return hn
|
| 226 |
+
}
|
| 227 |
+
}
|
| 228 |
+
|
| 229 |
+
// addChild appends the new `child` node to the tree using the `pattern` as the trie key.
|
| 230 |
+
// For a URL router like chi's, we split the static, param, regexp and wildcard segments
|
| 231 |
+
// into different nodes. In addition, addChild will recursively call itself until every
|
| 232 |
+
// pattern segment is added to the url pattern tree as individual nodes, depending on type.
|
| 233 |
+
func (n *node) addChild(child *node, prefix string) *node {
|
| 234 |
+
search := prefix
|
| 235 |
+
|
| 236 |
+
// handler leaf node added to the tree is the child.
|
| 237 |
+
// this may be overridden later down the flow
|
| 238 |
+
hn := child
|
| 239 |
+
|
| 240 |
+
// Parse next segment
|
| 241 |
+
segTyp, _, segRexpat, segTail, segStartIdx, segEndIdx := patNextSegment(search)
|
| 242 |
+
|
| 243 |
+
// Add child depending on next up segment
|
| 244 |
+
switch segTyp {
|
| 245 |
+
|
| 246 |
+
case ntStatic:
|
| 247 |
+
// Search prefix is all static (that is, has no params in path)
|
| 248 |
+
// noop
|
| 249 |
+
|
| 250 |
+
default:
|
| 251 |
+
// Search prefix contains a param, regexp or wildcard
|
| 252 |
+
|
| 253 |
+
if segTyp == ntRegexp {
|
| 254 |
+
rex, err := regexp.Compile(segRexpat)
|
| 255 |
+
if err != nil {
|
| 256 |
+
panic(fmt.Sprintf("chi: invalid regexp pattern '%s' in route param", segRexpat))
|
| 257 |
+
}
|
| 258 |
+
child.prefix = segRexpat
|
| 259 |
+
child.rex = rex
|
| 260 |
+
}
|
| 261 |
+
|
| 262 |
+
if segStartIdx == 0 {
|
| 263 |
+
// Route starts with a param
|
| 264 |
+
child.typ = segTyp
|
| 265 |
+
|
| 266 |
+
if segTyp == ntCatchAll {
|
| 267 |
+
segStartIdx = -1
|
| 268 |
+
} else {
|
| 269 |
+
segStartIdx = segEndIdx
|
| 270 |
+
}
|
| 271 |
+
if segStartIdx < 0 {
|
| 272 |
+
segStartIdx = len(search)
|
| 273 |
+
}
|
| 274 |
+
child.tail = segTail // for params, we set the tail
|
| 275 |
+
|
| 276 |
+
if segStartIdx != len(search) {
|
| 277 |
+
// add static edge for the remaining part, split the end.
|
| 278 |
+
// its not possible to have adjacent param nodes, so its certainly
|
| 279 |
+
// going to be a static node next.
|
| 280 |
+
|
| 281 |
+
search = search[segStartIdx:] // advance search position
|
| 282 |
+
|
| 283 |
+
nn := &node{
|
| 284 |
+
typ: ntStatic,
|
| 285 |
+
label: search[0],
|
| 286 |
+
prefix: search,
|
| 287 |
+
}
|
| 288 |
+
hn = child.addChild(nn, search)
|
| 289 |
+
}
|
| 290 |
+
|
| 291 |
+
} else if segStartIdx > 0 {
|
| 292 |
+
// Route has some param
|
| 293 |
+
|
| 294 |
+
// starts with a static segment
|
| 295 |
+
child.typ = ntStatic
|
| 296 |
+
child.prefix = search[:segStartIdx]
|
| 297 |
+
child.rex = nil
|
| 298 |
+
|
| 299 |
+
// add the param edge node
|
| 300 |
+
search = search[segStartIdx:]
|
| 301 |
+
|
| 302 |
+
nn := &node{
|
| 303 |
+
typ: segTyp,
|
| 304 |
+
label: search[0],
|
| 305 |
+
tail: segTail,
|
| 306 |
+
}
|
| 307 |
+
hn = child.addChild(nn, search)
|
| 308 |
+
|
| 309 |
+
}
|
| 310 |
+
}
|
| 311 |
+
|
| 312 |
+
n.children[child.typ] = append(n.children[child.typ], child)
|
| 313 |
+
n.children[child.typ].Sort()
|
| 314 |
+
return hn
|
| 315 |
+
}
|
| 316 |
+
|
| 317 |
+
func (n *node) replaceChild(label, tail byte, child *node) {
|
| 318 |
+
for i := 0; i < len(n.children[child.typ]); i++ {
|
| 319 |
+
if n.children[child.typ][i].label == label && n.children[child.typ][i].tail == tail {
|
| 320 |
+
n.children[child.typ][i] = child
|
| 321 |
+
n.children[child.typ][i].label = label
|
| 322 |
+
n.children[child.typ][i].tail = tail
|
| 323 |
+
return
|
| 324 |
+
}
|
| 325 |
+
}
|
| 326 |
+
panic("chi: replacing missing child")
|
| 327 |
+
}
|
| 328 |
+
|
| 329 |
+
func (n *node) getEdge(ntyp nodeTyp, label, tail byte, prefix string) *node {
|
| 330 |
+
nds := n.children[ntyp]
|
| 331 |
+
for i := 0; i < len(nds); i++ {
|
| 332 |
+
if nds[i].label == label && nds[i].tail == tail {
|
| 333 |
+
if ntyp == ntRegexp && nds[i].prefix != prefix {
|
| 334 |
+
continue
|
| 335 |
+
}
|
| 336 |
+
return nds[i]
|
| 337 |
+
}
|
| 338 |
+
}
|
| 339 |
+
return nil
|
| 340 |
+
}
|
| 341 |
+
|
| 342 |
+
func (n *node) setEndpoint(method methodTyp, handler http.Handler, pattern string) {
|
| 343 |
+
// Set the handler for the method type on the node
|
| 344 |
+
if n.endpoints == nil {
|
| 345 |
+
n.endpoints = make(endpoints)
|
| 346 |
+
}
|
| 347 |
+
|
| 348 |
+
paramKeys := patParamKeys(pattern)
|
| 349 |
+
|
| 350 |
+
if method&mSTUB == mSTUB {
|
| 351 |
+
n.endpoints.Value(mSTUB).handler = handler
|
| 352 |
+
}
|
| 353 |
+
if method&mALL == mALL {
|
| 354 |
+
h := n.endpoints.Value(mALL)
|
| 355 |
+
h.handler = handler
|
| 356 |
+
h.pattern = pattern
|
| 357 |
+
h.paramKeys = paramKeys
|
| 358 |
+
for _, m := range methodMap {
|
| 359 |
+
h := n.endpoints.Value(m)
|
| 360 |
+
h.handler = handler
|
| 361 |
+
h.pattern = pattern
|
| 362 |
+
h.paramKeys = paramKeys
|
| 363 |
+
}
|
| 364 |
+
} else {
|
| 365 |
+
h := n.endpoints.Value(method)
|
| 366 |
+
h.handler = handler
|
| 367 |
+
h.pattern = pattern
|
| 368 |
+
h.paramKeys = paramKeys
|
| 369 |
+
}
|
| 370 |
+
}
|
| 371 |
+
|
| 372 |
+
func (n *node) FindRoute(rctx *Context, method methodTyp, path string) (*node, endpoints, http.Handler) {
|
| 373 |
+
// Reset the context routing pattern and params
|
| 374 |
+
rctx.routePattern = ""
|
| 375 |
+
rctx.routeParams.Keys = rctx.routeParams.Keys[:0]
|
| 376 |
+
rctx.routeParams.Values = rctx.routeParams.Values[:0]
|
| 377 |
+
|
| 378 |
+
// Find the routing handlers for the path
|
| 379 |
+
rn := n.findRoute(rctx, method, path)
|
| 380 |
+
if rn == nil {
|
| 381 |
+
return nil, nil, nil
|
| 382 |
+
}
|
| 383 |
+
|
| 384 |
+
// Record the routing params in the request lifecycle
|
| 385 |
+
rctx.URLParams.Keys = append(rctx.URLParams.Keys, rctx.routeParams.Keys...)
|
| 386 |
+
rctx.URLParams.Values = append(rctx.URLParams.Values, rctx.routeParams.Values...)
|
| 387 |
+
|
| 388 |
+
// Record the routing pattern in the request lifecycle
|
| 389 |
+
if rn.endpoints[method].pattern != "" {
|
| 390 |
+
rctx.routePattern = rn.endpoints[method].pattern
|
| 391 |
+
rctx.RoutePatterns = append(rctx.RoutePatterns, rctx.routePattern)
|
| 392 |
+
}
|
| 393 |
+
|
| 394 |
+
return rn, rn.endpoints, rn.endpoints[method].handler
|
| 395 |
+
}
|
| 396 |
+
|
| 397 |
+
// Recursive edge traversal by checking all nodeTyp groups along the way.
|
| 398 |
+
// It's like searching through a multi-dimensional radix trie.
|
| 399 |
+
func (n *node) findRoute(rctx *Context, method methodTyp, path string) *node {
|
| 400 |
+
nn := n
|
| 401 |
+
search := path
|
| 402 |
+
|
| 403 |
+
for t, nds := range nn.children {
|
| 404 |
+
ntyp := nodeTyp(t)
|
| 405 |
+
if len(nds) == 0 {
|
| 406 |
+
continue
|
| 407 |
+
}
|
| 408 |
+
|
| 409 |
+
var xn *node
|
| 410 |
+
xsearch := search
|
| 411 |
+
|
| 412 |
+
var label byte
|
| 413 |
+
if search != "" {
|
| 414 |
+
label = search[0]
|
| 415 |
+
}
|
| 416 |
+
|
| 417 |
+
switch ntyp {
|
| 418 |
+
case ntStatic:
|
| 419 |
+
xn = nds.findEdge(label)
|
| 420 |
+
if xn == nil || !strings.HasPrefix(xsearch, xn.prefix) {
|
| 421 |
+
continue
|
| 422 |
+
}
|
| 423 |
+
xsearch = xsearch[len(xn.prefix):]
|
| 424 |
+
|
| 425 |
+
case ntParam, ntRegexp:
|
| 426 |
+
// short-circuit and return no matching route for empty param values
|
| 427 |
+
if xsearch == "" {
|
| 428 |
+
continue
|
| 429 |
+
}
|
| 430 |
+
|
| 431 |
+
// serially loop through each node grouped by the tail delimiter
|
| 432 |
+
for idx := 0; idx < len(nds); idx++ {
|
| 433 |
+
xn = nds[idx]
|
| 434 |
+
|
| 435 |
+
// label for param nodes is the delimiter byte
|
| 436 |
+
p := strings.IndexByte(xsearch, xn.tail)
|
| 437 |
+
|
| 438 |
+
if p < 0 {
|
| 439 |
+
if xn.tail == '/' {
|
| 440 |
+
p = len(xsearch)
|
| 441 |
+
} else {
|
| 442 |
+
continue
|
| 443 |
+
}
|
| 444 |
+
} else if ntyp == ntRegexp && p == 0 {
|
| 445 |
+
continue
|
| 446 |
+
}
|
| 447 |
+
|
| 448 |
+
if ntyp == ntRegexp && xn.rex != nil {
|
| 449 |
+
if !xn.rex.MatchString(xsearch[:p]) {
|
| 450 |
+
continue
|
| 451 |
+
}
|
| 452 |
+
} else if strings.IndexByte(xsearch[:p], '/') != -1 {
|
| 453 |
+
// avoid a match across path segments
|
| 454 |
+
continue
|
| 455 |
+
}
|
| 456 |
+
|
| 457 |
+
prevlen := len(rctx.routeParams.Values)
|
| 458 |
+
rctx.routeParams.Values = append(rctx.routeParams.Values, xsearch[:p])
|
| 459 |
+
xsearch = xsearch[p:]
|
| 460 |
+
|
| 461 |
+
if len(xsearch) == 0 {
|
| 462 |
+
if xn.isLeaf() {
|
| 463 |
+
h := xn.endpoints[method]
|
| 464 |
+
if h != nil && h.handler != nil {
|
| 465 |
+
rctx.routeParams.Keys = append(rctx.routeParams.Keys, h.paramKeys...)
|
| 466 |
+
return xn
|
| 467 |
+
}
|
| 468 |
+
|
| 469 |
+
for endpoints := range xn.endpoints {
|
| 470 |
+
if endpoints == mALL || endpoints == mSTUB {
|
| 471 |
+
continue
|
| 472 |
+
}
|
| 473 |
+
rctx.methodsAllowed = append(rctx.methodsAllowed, endpoints)
|
| 474 |
+
}
|
| 475 |
+
|
| 476 |
+
// flag that the routing context found a route, but not a corresponding
|
| 477 |
+
// supported method
|
| 478 |
+
rctx.methodNotAllowed = true
|
| 479 |
+
}
|
| 480 |
+
}
|
| 481 |
+
|
| 482 |
+
// recursively find the next node on this branch
|
| 483 |
+
fin := xn.findRoute(rctx, method, xsearch)
|
| 484 |
+
if fin != nil {
|
| 485 |
+
return fin
|
| 486 |
+
}
|
| 487 |
+
|
| 488 |
+
// not found on this branch, reset vars
|
| 489 |
+
rctx.routeParams.Values = rctx.routeParams.Values[:prevlen]
|
| 490 |
+
xsearch = search
|
| 491 |
+
}
|
| 492 |
+
|
| 493 |
+
rctx.routeParams.Values = append(rctx.routeParams.Values, "")
|
| 494 |
+
|
| 495 |
+
default:
|
| 496 |
+
// catch-all nodes
|
| 497 |
+
rctx.routeParams.Values = append(rctx.routeParams.Values, search)
|
| 498 |
+
xn = nds[0]
|
| 499 |
+
xsearch = ""
|
| 500 |
+
}
|
| 501 |
+
|
| 502 |
+
if xn == nil {
|
| 503 |
+
continue
|
| 504 |
+
}
|
| 505 |
+
|
| 506 |
+
// did we find it yet?
|
| 507 |
+
if len(xsearch) == 0 {
|
| 508 |
+
if xn.isLeaf() {
|
| 509 |
+
h := xn.endpoints[method]
|
| 510 |
+
if h != nil && h.handler != nil {
|
| 511 |
+
rctx.routeParams.Keys = append(rctx.routeParams.Keys, h.paramKeys...)
|
| 512 |
+
return xn
|
| 513 |
+
}
|
| 514 |
+
|
| 515 |
+
for endpoints := range xn.endpoints {
|
| 516 |
+
if endpoints == mALL || endpoints == mSTUB {
|
| 517 |
+
continue
|
| 518 |
+
}
|
| 519 |
+
rctx.methodsAllowed = append(rctx.methodsAllowed, endpoints)
|
| 520 |
+
}
|
| 521 |
+
|
| 522 |
+
// flag that the routing context found a route, but not a corresponding
|
| 523 |
+
// supported method
|
| 524 |
+
rctx.methodNotAllowed = true
|
| 525 |
+
}
|
| 526 |
+
}
|
| 527 |
+
|
| 528 |
+
// recursively find the next node..
|
| 529 |
+
fin := xn.findRoute(rctx, method, xsearch)
|
| 530 |
+
if fin != nil {
|
| 531 |
+
return fin
|
| 532 |
+
}
|
| 533 |
+
|
| 534 |
+
// Did not find final handler, let's remove the param here if it was set
|
| 535 |
+
if xn.typ > ntStatic {
|
| 536 |
+
if len(rctx.routeParams.Values) > 0 {
|
| 537 |
+
rctx.routeParams.Values = rctx.routeParams.Values[:len(rctx.routeParams.Values)-1]
|
| 538 |
+
}
|
| 539 |
+
}
|
| 540 |
+
|
| 541 |
+
}
|
| 542 |
+
|
| 543 |
+
return nil
|
| 544 |
+
}
|
| 545 |
+
|
| 546 |
+
func (n *node) findEdge(ntyp nodeTyp, label byte) *node {
|
| 547 |
+
nds := n.children[ntyp]
|
| 548 |
+
num := len(nds)
|
| 549 |
+
idx := 0
|
| 550 |
+
|
| 551 |
+
switch ntyp {
|
| 552 |
+
case ntStatic, ntParam, ntRegexp:
|
| 553 |
+
i, j := 0, num-1
|
| 554 |
+
for i <= j {
|
| 555 |
+
idx = i + (j-i)/2
|
| 556 |
+
if label > nds[idx].label {
|
| 557 |
+
i = idx + 1
|
| 558 |
+
} else if label < nds[idx].label {
|
| 559 |
+
j = idx - 1
|
| 560 |
+
} else {
|
| 561 |
+
i = num // breaks cond
|
| 562 |
+
}
|
| 563 |
+
}
|
| 564 |
+
if nds[idx].label != label {
|
| 565 |
+
return nil
|
| 566 |
+
}
|
| 567 |
+
return nds[idx]
|
| 568 |
+
|
| 569 |
+
default: // catch all
|
| 570 |
+
return nds[idx]
|
| 571 |
+
}
|
| 572 |
+
}
|
| 573 |
+
|
| 574 |
+
func (n *node) isLeaf() bool {
|
| 575 |
+
return n.endpoints != nil
|
| 576 |
+
}
|
| 577 |
+
|
| 578 |
+
func (n *node) findPattern(pattern string) bool {
|
| 579 |
+
nn := n
|
| 580 |
+
for _, nds := range nn.children {
|
| 581 |
+
if len(nds) == 0 {
|
| 582 |
+
continue
|
| 583 |
+
}
|
| 584 |
+
|
| 585 |
+
n = nn.findEdge(nds[0].typ, pattern[0])
|
| 586 |
+
if n == nil {
|
| 587 |
+
continue
|
| 588 |
+
}
|
| 589 |
+
|
| 590 |
+
var idx int
|
| 591 |
+
var xpattern string
|
| 592 |
+
|
| 593 |
+
switch n.typ {
|
| 594 |
+
case ntStatic:
|
| 595 |
+
idx = longestPrefix(pattern, n.prefix)
|
| 596 |
+
if idx < len(n.prefix) {
|
| 597 |
+
continue
|
| 598 |
+
}
|
| 599 |
+
|
| 600 |
+
case ntParam, ntRegexp:
|
| 601 |
+
idx = strings.IndexByte(pattern, '}') + 1
|
| 602 |
+
|
| 603 |
+
case ntCatchAll:
|
| 604 |
+
idx = longestPrefix(pattern, "*")
|
| 605 |
+
|
| 606 |
+
default:
|
| 607 |
+
panic("chi: unknown node type")
|
| 608 |
+
}
|
| 609 |
+
|
| 610 |
+
xpattern = pattern[idx:]
|
| 611 |
+
if len(xpattern) == 0 {
|
| 612 |
+
return true
|
| 613 |
+
}
|
| 614 |
+
|
| 615 |
+
return n.findPattern(xpattern)
|
| 616 |
+
}
|
| 617 |
+
return false
|
| 618 |
+
}
|
| 619 |
+
|
| 620 |
+
func (n *node) routes() []Route {
|
| 621 |
+
rts := []Route{}
|
| 622 |
+
|
| 623 |
+
n.walk(func(eps endpoints, subroutes Routes) bool {
|
| 624 |
+
if eps[mSTUB] != nil && eps[mSTUB].handler != nil && subroutes == nil {
|
| 625 |
+
return false
|
| 626 |
+
}
|
| 627 |
+
|
| 628 |
+
// Group methodHandlers by unique patterns
|
| 629 |
+
pats := make(map[string]endpoints)
|
| 630 |
+
|
| 631 |
+
for mt, h := range eps {
|
| 632 |
+
if h.pattern == "" {
|
| 633 |
+
continue
|
| 634 |
+
}
|
| 635 |
+
p, ok := pats[h.pattern]
|
| 636 |
+
if !ok {
|
| 637 |
+
p = endpoints{}
|
| 638 |
+
pats[h.pattern] = p
|
| 639 |
+
}
|
| 640 |
+
p[mt] = h
|
| 641 |
+
}
|
| 642 |
+
|
| 643 |
+
for p, mh := range pats {
|
| 644 |
+
hs := make(map[string]http.Handler)
|
| 645 |
+
if mh[mALL] != nil && mh[mALL].handler != nil {
|
| 646 |
+
hs["*"] = mh[mALL].handler
|
| 647 |
+
}
|
| 648 |
+
|
| 649 |
+
for mt, h := range mh {
|
| 650 |
+
if h.handler == nil {
|
| 651 |
+
continue
|
| 652 |
+
}
|
| 653 |
+
m := methodTypString(mt)
|
| 654 |
+
if m == "" {
|
| 655 |
+
continue
|
| 656 |
+
}
|
| 657 |
+
hs[m] = h.handler
|
| 658 |
+
}
|
| 659 |
+
|
| 660 |
+
rt := Route{subroutes, hs, p}
|
| 661 |
+
rts = append(rts, rt)
|
| 662 |
+
}
|
| 663 |
+
|
| 664 |
+
return false
|
| 665 |
+
})
|
| 666 |
+
|
| 667 |
+
return rts
|
| 668 |
+
}
|
| 669 |
+
|
| 670 |
+
func (n *node) walk(fn func(eps endpoints, subroutes Routes) bool) bool {
|
| 671 |
+
// Visit the leaf values if any
|
| 672 |
+
if (n.endpoints != nil || n.subroutes != nil) && fn(n.endpoints, n.subroutes) {
|
| 673 |
+
return true
|
| 674 |
+
}
|
| 675 |
+
|
| 676 |
+
// Recurse on the children
|
| 677 |
+
for _, ns := range n.children {
|
| 678 |
+
for _, cn := range ns {
|
| 679 |
+
if cn.walk(fn) {
|
| 680 |
+
return true
|
| 681 |
+
}
|
| 682 |
+
}
|
| 683 |
+
}
|
| 684 |
+
return false
|
| 685 |
+
}
|
| 686 |
+
|
| 687 |
+
// patNextSegment returns the next segment details from a pattern:
|
| 688 |
+
// node type, param key, regexp string, param tail byte, param starting index, param ending index
|
| 689 |
+
func patNextSegment(pattern string) (nodeTyp, string, string, byte, int, int) {
|
| 690 |
+
ps := strings.Index(pattern, "{")
|
| 691 |
+
ws := strings.Index(pattern, "*")
|
| 692 |
+
|
| 693 |
+
if ps < 0 && ws < 0 {
|
| 694 |
+
return ntStatic, "", "", 0, 0, len(pattern) // we return the entire thing
|
| 695 |
+
}
|
| 696 |
+
|
| 697 |
+
// Sanity check
|
| 698 |
+
if ps >= 0 && ws >= 0 && ws < ps {
|
| 699 |
+
panic("chi: wildcard '*' must be the last pattern in a route, otherwise use a '{param}'")
|
| 700 |
+
}
|
| 701 |
+
|
| 702 |
+
var tail byte = '/' // Default endpoint tail to / byte
|
| 703 |
+
|
| 704 |
+
if ps >= 0 {
|
| 705 |
+
// Param/Regexp pattern is next
|
| 706 |
+
nt := ntParam
|
| 707 |
+
|
| 708 |
+
// Read to closing } taking into account opens and closes in curl count (cc)
|
| 709 |
+
cc := 0
|
| 710 |
+
pe := ps
|
| 711 |
+
for i, c := range pattern[ps:] {
|
| 712 |
+
if c == '{' {
|
| 713 |
+
cc++
|
| 714 |
+
} else if c == '}' {
|
| 715 |
+
cc--
|
| 716 |
+
if cc == 0 {
|
| 717 |
+
pe = ps + i
|
| 718 |
+
break
|
| 719 |
+
}
|
| 720 |
+
}
|
| 721 |
+
}
|
| 722 |
+
if pe == ps {
|
| 723 |
+
panic("chi: route param closing delimiter '}' is missing")
|
| 724 |
+
}
|
| 725 |
+
|
| 726 |
+
key := pattern[ps+1 : pe]
|
| 727 |
+
pe++ // set end to next position
|
| 728 |
+
|
| 729 |
+
if pe < len(pattern) {
|
| 730 |
+
tail = pattern[pe]
|
| 731 |
+
}
|
| 732 |
+
|
| 733 |
+
key, rexpat, isRegexp := strings.Cut(key, ":")
|
| 734 |
+
if isRegexp {
|
| 735 |
+
nt = ntRegexp
|
| 736 |
+
}
|
| 737 |
+
|
| 738 |
+
if len(rexpat) > 0 {
|
| 739 |
+
if rexpat[0] != '^' {
|
| 740 |
+
rexpat = "^" + rexpat
|
| 741 |
+
}
|
| 742 |
+
if rexpat[len(rexpat)-1] != '$' {
|
| 743 |
+
rexpat += "$"
|
| 744 |
+
}
|
| 745 |
+
}
|
| 746 |
+
|
| 747 |
+
return nt, key, rexpat, tail, ps, pe
|
| 748 |
+
}
|
| 749 |
+
|
| 750 |
+
// Wildcard pattern as finale
|
| 751 |
+
if ws < len(pattern)-1 {
|
| 752 |
+
panic("chi: wildcard '*' must be the last value in a route. trim trailing text or use a '{param}' instead")
|
| 753 |
+
}
|
| 754 |
+
return ntCatchAll, "*", "", 0, ws, len(pattern)
|
| 755 |
+
}
|
| 756 |
+
|
| 757 |
+
func patParamKeys(pattern string) []string {
|
| 758 |
+
pat := pattern
|
| 759 |
+
paramKeys := []string{}
|
| 760 |
+
for {
|
| 761 |
+
ptyp, paramKey, _, _, _, e := patNextSegment(pat)
|
| 762 |
+
if ptyp == ntStatic {
|
| 763 |
+
return paramKeys
|
| 764 |
+
}
|
| 765 |
+
for i := 0; i < len(paramKeys); i++ {
|
| 766 |
+
if paramKeys[i] == paramKey {
|
| 767 |
+
panic(fmt.Sprintf("chi: routing pattern '%s' contains duplicate param key, '%s'", pattern, paramKey))
|
| 768 |
+
}
|
| 769 |
+
}
|
| 770 |
+
paramKeys = append(paramKeys, paramKey)
|
| 771 |
+
pat = pat[e:]
|
| 772 |
+
}
|
| 773 |
+
}
|
| 774 |
+
|
| 775 |
+
// longestPrefix finds the length of the shared prefix
|
| 776 |
+
// of two strings
|
| 777 |
+
func longestPrefix(k1, k2 string) int {
|
| 778 |
+
max := len(k1)
|
| 779 |
+
if l := len(k2); l < max {
|
| 780 |
+
max = l
|
| 781 |
+
}
|
| 782 |
+
var i int
|
| 783 |
+
for i = 0; i < max; i++ {
|
| 784 |
+
if k1[i] != k2[i] {
|
| 785 |
+
break
|
| 786 |
+
}
|
| 787 |
+
}
|
| 788 |
+
return i
|
| 789 |
+
}
|
| 790 |
+
|
| 791 |
+
func methodTypString(method methodTyp) string {
|
| 792 |
+
for s, t := range methodMap {
|
| 793 |
+
if method == t {
|
| 794 |
+
return s
|
| 795 |
+
}
|
| 796 |
+
}
|
| 797 |
+
return ""
|
| 798 |
+
}
|
| 799 |
+
|
| 800 |
+
type nodes []*node
|
| 801 |
+
|
| 802 |
+
// Sort the list of nodes by label
|
| 803 |
+
func (ns nodes) Sort() { sort.Sort(ns); ns.tailSort() }
|
| 804 |
+
func (ns nodes) Len() int { return len(ns) }
|
| 805 |
+
func (ns nodes) Swap(i, j int) { ns[i], ns[j] = ns[j], ns[i] }
|
| 806 |
+
func (ns nodes) Less(i, j int) bool { return ns[i].label < ns[j].label }
|
| 807 |
+
|
| 808 |
+
// tailSort pushes nodes with '/' as the tail to the end of the list for param nodes.
|
| 809 |
+
// The list order determines the traversal order.
|
| 810 |
+
func (ns nodes) tailSort() {
|
| 811 |
+
for i := len(ns) - 1; i >= 0; i-- {
|
| 812 |
+
if ns[i].typ > ntStatic && ns[i].tail == '/' {
|
| 813 |
+
ns.Swap(i, len(ns)-1)
|
| 814 |
+
return
|
| 815 |
+
}
|
| 816 |
+
}
|
| 817 |
+
}
|
| 818 |
+
|
| 819 |
+
func (ns nodes) findEdge(label byte) *node {
|
| 820 |
+
num := len(ns)
|
| 821 |
+
idx := 0
|
| 822 |
+
i, j := 0, num-1
|
| 823 |
+
for i <= j {
|
| 824 |
+
idx = i + (j-i)/2
|
| 825 |
+
if label > ns[idx].label {
|
| 826 |
+
i = idx + 1
|
| 827 |
+
} else if label < ns[idx].label {
|
| 828 |
+
j = idx - 1
|
| 829 |
+
} else {
|
| 830 |
+
i = num // breaks cond
|
| 831 |
+
}
|
| 832 |
+
}
|
| 833 |
+
if ns[idx].label != label {
|
| 834 |
+
return nil
|
| 835 |
+
}
|
| 836 |
+
return ns[idx]
|
| 837 |
+
}
|
| 838 |
+
|
| 839 |
+
// Route describes the details of a routing handler.
|
| 840 |
+
// Handlers map key is an HTTP method
|
| 841 |
+
type Route struct {
|
| 842 |
+
SubRoutes Routes
|
| 843 |
+
Handlers map[string]http.Handler
|
| 844 |
+
Pattern string
|
| 845 |
+
}
|
| 846 |
+
|
| 847 |
+
// WalkFunc is the type of the function called for each method and route visited by Walk.
|
| 848 |
+
type WalkFunc func(method string, route string, handler http.Handler, middlewares ...func(http.Handler) http.Handler) error
|
| 849 |
+
|
| 850 |
+
// Walk walks any router tree that implements Routes interface.
|
| 851 |
+
func Walk(r Routes, walkFn WalkFunc) error {
|
| 852 |
+
return walk(r, walkFn, "")
|
| 853 |
+
}
|
| 854 |
+
|
| 855 |
+
func walk(r Routes, walkFn WalkFunc, parentRoute string, parentMw ...func(http.Handler) http.Handler) error {
|
| 856 |
+
for _, route := range r.Routes() {
|
| 857 |
+
mws := make([]func(http.Handler) http.Handler, len(parentMw))
|
| 858 |
+
copy(mws, parentMw)
|
| 859 |
+
mws = append(mws, r.Middlewares()...)
|
| 860 |
+
|
| 861 |
+
if route.SubRoutes != nil {
|
| 862 |
+
if err := walk(route.SubRoutes, walkFn, parentRoute+route.Pattern, mws...); err != nil {
|
| 863 |
+
return err
|
| 864 |
+
}
|
| 865 |
+
continue
|
| 866 |
+
}
|
| 867 |
+
|
| 868 |
+
for method, handler := range route.Handlers {
|
| 869 |
+
if method == "*" {
|
| 870 |
+
// Ignore a "catchAll" method, since we pass down all the specific methods for each route.
|
| 871 |
+
continue
|
| 872 |
+
}
|
| 873 |
+
|
| 874 |
+
fullRoute := parentRoute + route.Pattern
|
| 875 |
+
fullRoute = strings.Replace(fullRoute, "/*/", "/", -1)
|
| 876 |
+
|
| 877 |
+
if chain, ok := handler.(*ChainHandler); ok {
|
| 878 |
+
if err := walkFn(method, fullRoute, chain.Endpoint, append(mws, chain.Middlewares...)...); err != nil {
|
| 879 |
+
return err
|
| 880 |
+
}
|
| 881 |
+
} else {
|
| 882 |
+
if err := walkFn(method, fullRoute, handler, mws...); err != nil {
|
| 883 |
+
return err
|
| 884 |
+
}
|
| 885 |
+
}
|
| 886 |
+
}
|
| 887 |
+
}
|
| 888 |
+
|
| 889 |
+
return nil
|
| 890 |
+
}
|
tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/instruction.md
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
StripSlashes middleware panics when route context is nil
|
| 2 |
+
|
| 3 |
+
#### Description
|
| 4 |
+
|
| 5 |
+
The StripSlashes middleware causes a panic when the route context is nil. This happens when the middleware is used outside of the chi router context or in certain edge cases where the context hasn't been properly initialized.
|
| 6 |
+
|
| 7 |
+
#### Steps/Code to Reproduce
|
| 8 |
+
|
| 9 |
+
```go
|
| 10 |
+
package main
|
| 11 |
+
|
| 12 |
+
import (
|
| 13 |
+
"net/http"
|
| 14 |
+
"net/http/httptest"
|
| 15 |
+
|
| 16 |
+
"github.com/go-chi/chi/v5/middleware"
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
func main() {
|
| 20 |
+
handler := middleware.StripSlashes(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
| 21 |
+
w.Write([]byte("OK"))
|
| 22 |
+
}))
|
| 23 |
+
|
| 24 |
+
req := httptest.NewRequest("GET", "/test/", nil)
|
| 25 |
+
w := httptest.NewRecorder()
|
| 26 |
+
|
| 27 |
+
// This will panic with "runtime error: invalid memory address or nil pointer dereference"
|
| 28 |
+
handler.ServeHTTP(w, req)
|
| 29 |
+
}
|
| 30 |
+
```
|
| 31 |
+
|
| 32 |
+
The panic occurs because the middleware tries to access `rctx.RoutePath` when `rctx` is nil, but the condition check is inverted - it tries to set `rctx.RoutePath` when `rctx` is nil instead of when it's not nil.
|
| 33 |
+
Work in /testbed. This is a broken source-only export without Git history or tests. Grading restores frozen private tests independently. Repair existing implementation files; new arbitrary files are outside the current transfer contract. Offline Go dependencies are preinstalled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 |
+
"-json",
|
| 7 |
+
"-count=1",
|
| 8 |
+
"./..."
|
| 9 |
+
],
|
| 10 |
+
"execution_profile": "harbor-semantic-jobs-v1",
|
| 11 |
+
"full_rfc008": false,
|
| 12 |
+
"generated_reference": false,
|
| 13 |
+
"native_validation_performed": false,
|
| 14 |
+
"negative_classes": {
|
| 15 |
+
"delete-tests": "same wrong implementation with verifier deletion/stdout spoof attempt",
|
| 16 |
+
"original-bug": "task-specific unchanged semantic bug",
|
| 17 |
+
"stale-reward": "same wrong implementation with stale reward spoof attempt"
|
| 18 |
+
},
|
| 19 |
+
"new_arbitrary_files_supported": false,
|
| 20 |
+
"partial_hunk_control": {
|
| 21 |
+
"basis": "single-hunk original-bug canary is NOP-equivalent; other canaries test abuse classes only",
|
| 22 |
+
"extra_semantic_repair_coverage": false,
|
| 23 |
+
"original_patch_hunks": 1
|
| 24 |
+
},
|
| 25 |
+
"private_frozen_files": {
|
| 26 |
+
"context_test.go": {
|
| 27 |
+
"mode": "0o644",
|
| 28 |
+
"sha256": "ca0a71488dadcf8fcecfed5d33d6d93de9b15ea34f2d89003076eb041dc14cae",
|
| 29 |
+
"size": 2408
|
| 30 |
+
},
|
| 31 |
+
"middleware/compress_test.go": {
|
| 32 |
+
"mode": "0o644",
|
| 33 |
+
"sha256": "9278c028717563fb87223044c10efc6115ea706c0771cab7170d90aef89be2f7",
|
| 34 |
+
"size": 5356
|
| 35 |
+
},
|
| 36 |
+
"middleware/content_charset_test.go": {
|
| 37 |
+
"mode": "0o644",
|
| 38 |
+
"sha256": "39f7d5333c1f47a11796952ed1ec13aa6dd880c940069a2c49552c2936e2c1a1",
|
| 39 |
+
"size": 2960
|
| 40 |
+
},
|
| 41 |
+
"middleware/content_encoding_test.go": {
|
| 42 |
+
"mode": "0o644",
|
| 43 |
+
"sha256": "98bf93fb59e1613843f8f838ddb17dc160cf88dd95135b1b2637e188c69c4644",
|
| 44 |
+
"size": 1929
|
| 45 |
+
},
|
| 46 |
+
"middleware/content_type_test.go": {
|
| 47 |
+
"mode": "0o644",
|
| 48 |
+
"sha256": "233f0c2f27f3d37719c6abc393c86804424a47d1ccebe17b018629b42f05a050",
|
| 49 |
+
"size": 2095
|
| 50 |
+
},
|
| 51 |
+
"middleware/get_head_test.go": {
|
| 52 |
+
"mode": "0o644",
|
| 53 |
+
"sha256": "a660972a6e389bbea07db1414dd72c0593809c644a941b431ce43c902ec65099",
|
| 54 |
+
"size": 1941
|
| 55 |
+
},
|
| 56 |
+
"middleware/logger_test.go": {
|
| 57 |
+
"mode": "0o644",
|
| 58 |
+
"sha256": "f15a9900122e075c3b2d146aeae6128dfe17d57a45ef40b06f7a9e74cc4fb35f",
|
| 59 |
+
"size": 1116
|
| 60 |
+
},
|
| 61 |
+
"middleware/middleware_test.go": {
|
| 62 |
+
"mode": "0o644",
|
| 63 |
+
"sha256": "81744b82c9ea90b57a384c4a44cd889f7f5ab446b35eeb3d34983d0419a860bc",
|
| 64 |
+
"size": 3359
|
| 65 |
+
},
|
| 66 |
+
"middleware/realip_test.go": {
|
| 67 |
+
"mode": "0o644",
|
| 68 |
+
"sha256": "6cab78b3ebc7fd581261afae6740423edc2199d9ba7ae3e0ffda99354f90d534",
|
| 69 |
+
"size": 2311
|
| 70 |
+
},
|
| 71 |
+
"middleware/recoverer_test.go": {
|
| 72 |
+
"mode": "0o644",
|
| 73 |
+
"sha256": "55df30e19bcde6f044bf775e082edc445cc288594aa5b77150d99cd4c5a884fc",
|
| 74 |
+
"size": 1456
|
| 75 |
+
},
|
| 76 |
+
"middleware/request_id_test.go": {
|
| 77 |
+
"mode": "0o644",
|
| 78 |
+
"sha256": "519e920890f2a33b77d0140a4a8ffcc933752bc04b3efb0af0793e11722b639e",
|
| 79 |
+
"size": 1402
|
| 80 |
+
},
|
| 81 |
+
"middleware/strip_test.go": {
|
| 82 |
+
"mode": "0o644",
|
| 83 |
+
"sha256": "25f4ed91d5dbe150e54694f6b032e156fc241690fe9d9bd575f4799e0f39d9aa",
|
| 84 |
+
"size": 8033
|
| 85 |
+
},
|
| 86 |
+
"middleware/sunset_test.go": {
|
| 87 |
+
"mode": "0o644",
|
| 88 |
+
"sha256": "ea640c109f86cc5d1fbceea558e3ab983d8f0de642c16fd2b4ad750efada0101",
|
| 89 |
+
"size": 2459
|
| 90 |
+
},
|
| 91 |
+
"middleware/throttle_test.go": {
|
| 92 |
+
"mode": "0o644",
|
| 93 |
+
"sha256": "81e253f3039743db388ab9832fbc962271f529569440650e9c06a8270c948d2a",
|
| 94 |
+
"size": 6704
|
| 95 |
+
},
|
| 96 |
+
"middleware/url_format_test.go": {
|
| 97 |
+
"mode": "0o644",
|
| 98 |
+
"sha256": "04efa8cfbda922ff671ab3bdbc00f6fa06bf776c300dc8605815de1def33aaa1",
|
| 99 |
+
"size": 1653
|
| 100 |
+
},
|
| 101 |
+
"middleware/wrap_writer_test.go": {
|
| 102 |
+
"mode": "0o644",
|
| 103 |
+
"sha256": "bb07fb38cf0896a7c6bae8916bcad85002b29a71fdd5bb3058ddccf12a3dc0f8",
|
| 104 |
+
"size": 2454
|
| 105 |
+
},
|
| 106 |
+
"mux_test.go": {
|
| 107 |
+
"mode": "0o644",
|
| 108 |
+
"sha256": "bfcf63336579f68160e44af3cef60949ebd506632950d6b584eace8be26d0037",
|
| 109 |
+
"size": 56448
|
| 110 |
+
},
|
| 111 |
+
"path_value_test.go": {
|
| 112 |
+
"mode": "0o644",
|
| 113 |
+
"sha256": "3df65ffdb1dfc529cb9f61ed17bbd189d8c14f581473cd00d5a4949df277ad71",
|
| 114 |
+
"size": 1801
|
| 115 |
+
},
|
| 116 |
+
"testdata/cert.pem": {
|
| 117 |
+
"mode": "0o644",
|
| 118 |
+
"sha256": "8fb47f5e036bcb4daaeed180fa7164504a8ca43bdc010fd418618da48fdd340c",
|
| 119 |
+
"size": 1098
|
| 120 |
+
},
|
| 121 |
+
"testdata/key.pem": {
|
| 122 |
+
"mode": "0o644",
|
| 123 |
+
"sha256": "fecae623b5a0ae0a1110285c66e3e0e7f6019f49fd540ed3d9b85f65b9054f6c",
|
| 124 |
+
"size": 1678
|
| 125 |
+
},
|
| 126 |
+
"tree_test.go": {
|
| 127 |
+
"mode": "0o644",
|
| 128 |
+
"sha256": "6d1b6f2b721c61b03dd8bbdfeccf5431ccc1f1d6080afed6fe67e677fc790ce2",
|
| 129 |
+
"size": 22673
|
| 130 |
+
}
|
| 131 |
+
},
|
| 132 |
+
"public_files": {
|
| 133 |
+
".github/FUNDING.yml": {
|
| 134 |
+
"mode": "0o644",
|
| 135 |
+
"sha256": "0e236eacfd30c201e85ff368730f13e6dfb24d21a72c57c8bed0a099bebb3c7b",
|
| 136 |
+
"size": 723
|
| 137 |
+
},
|
| 138 |
+
".gitignore": {
|
| 139 |
+
"mode": "0o644",
|
| 140 |
+
"sha256": "785f18e2ac99c66b81d8d185aa1a65cfe4f39b12bef24c1f8037d85840490929",
|
| 141 |
+
"size": 20
|
| 142 |
+
},
|
| 143 |
+
"CHANGELOG.md": {
|
| 144 |
+
"mode": "0o644",
|
| 145 |
+
"sha256": "65c419049d2e6efc04b110426e4b5efc628a4ee6b8f080092f25a0dbbb35f071",
|
| 146 |
+
"size": 15854
|
| 147 |
+
},
|
| 148 |
+
"CONTRIBUTING.md": {
|
| 149 |
+
"mode": "0o644",
|
| 150 |
+
"sha256": "961da16c4fcec58d600fd49cc6ab0dd632d143646d4fcb2f5b1818fb0197fbe7",
|
| 151 |
+
"size": 1187
|
| 152 |
+
},
|
| 153 |
+
"LICENSE": {
|
| 154 |
+
"mode": "0o644",
|
| 155 |
+
"sha256": "a2d51b7515acfaff2f7a88688650f2fc4fd99561383e72bba2305e3db59a1647",
|
| 156 |
+
"size": 1123
|
| 157 |
+
},
|
| 158 |
+
"Makefile": {
|
| 159 |
+
"mode": "0o644",
|
| 160 |
+
"sha256": "b343163c9d40108d37c7e5fe73e3c1d8f39c2acf26de5d1d227a1be2770205ad",
|
| 161 |
+
"size": 490
|
| 162 |
+
},
|
| 163 |
+
"README.md": {
|
| 164 |
+
"mode": "0o644",
|
| 165 |
+
"sha256": "ef6ed94a615b29c4161b9898955e602404655049a68ddf46066bda30e3f297c9",
|
| 166 |
+
"size": 23313
|
| 167 |
+
},
|
| 168 |
+
"SECURITY.md": {
|
| 169 |
+
"mode": "0o644",
|
| 170 |
+
"sha256": "c5f181d947996aebd888182fb8dccf6e7026690e701b5e5ca50b288b4a2cf5c3",
|
| 171 |
+
"size": 313
|
| 172 |
+
},
|
| 173 |
+
"_examples/README.md": {
|
| 174 |
+
"mode": "0o644",
|
| 175 |
+
"sha256": "ca21040bf599dc318f33aa9839c347c9f368f09d97a420e8911b4eac7fdc82fe",
|
| 176 |
+
"size": 1751
|
| 177 |
+
},
|
| 178 |
+
"_examples/chi.svg": {
|
| 179 |
+
"mode": "0o644",
|
| 180 |
+
"sha256": "1a7e8131f1ed48303dc42d93bc50ea330f08f0875cba64d0d03eb96754984127",
|
| 181 |
+
"size": 2650
|
| 182 |
+
},
|
| 183 |
+
"_examples/custom-handler/main.go": {
|
| 184 |
+
"mode": "0o644",
|
| 185 |
+
"sha256": "e5a1f419488c233784f1f7951089305915b377af7462f137cd9bb5fd2a792924",
|
| 186 |
+
"size": 625
|
| 187 |
+
},
|
| 188 |
+
"_examples/custom-method/main.go": {
|
| 189 |
+
"mode": "0o644",
|
| 190 |
+
"sha256": "de61c6c6cad7f28a9b8b506d32bba52485f6e902ae25954913e8480fa5cfd8a5",
|
| 191 |
+
"size": 853
|
| 192 |
+
},
|
| 193 |
+
"_examples/fileserver/data/notes.txt": {
|
| 194 |
+
"mode": "0o644",
|
| 195 |
+
"sha256": "9e343fdfdcb2892c5eb6906d405f9ac1f7cb1d73cd8b7f31cf6329719af96576",
|
| 196 |
+
"size": 11
|
| 197 |
+
},
|
| 198 |
+
"_examples/fileserver/main.go": {
|
| 199 |
+
"mode": "0o644",
|
| 200 |
+
"sha256": "0c0b96e8104689bc86312805d70a97aea7c870801f09ca03dd47c8edde31f620",
|
| 201 |
+
"size": 1521
|
| 202 |
+
},
|
| 203 |
+
"_examples/graceful/main.go": {
|
| 204 |
+
"mode": "0o644",
|
| 205 |
+
"sha256": "b642f761971c48ecaf181a0e7fbbe313f893cb5e2a595b3cfe001f916e3bc4b6",
|
| 206 |
+
"size": 1967
|
| 207 |
+
},
|
| 208 |
+
"_examples/hello-world/main.go": {
|
| 209 |
+
"mode": "0o644",
|
| 210 |
+
"sha256": "e8104f868c9a673d864c489347970200808acc0652035a0376694bf5b7b07c86",
|
| 211 |
+
"size": 359
|
| 212 |
+
},
|
| 213 |
+
"_examples/limits/main.go": {
|
| 214 |
+
"mode": "0o644",
|
| 215 |
+
"sha256": "590c6d2f322481fed8abe1c2ca34eb0868f0d0a07d1c6aaccc0d6589b59a5d0d",
|
| 216 |
+
"size": 2181
|
| 217 |
+
},
|
| 218 |
+
"_examples/logging/main.go": {
|
| 219 |
+
"mode": "0o644",
|
| 220 |
+
"sha256": "7b3cf7fb608cc030569d4bf75d30f0807ba0849543ee469ad5adadd13a3b8667",
|
| 221 |
+
"size": 272
|
| 222 |
+
},
|
| 223 |
+
"_examples/rest/go.mod": {
|
| 224 |
+
"mode": "0o644",
|
| 225 |
+
"sha256": "18ba038ee1930a7817c888711e6562aa26281658ef1515789b41fc3b47599c61",
|
| 226 |
+
"size": 141
|
| 227 |
+
},
|
| 228 |
+
"_examples/rest/go.sum": {
|
| 229 |
+
"mode": "0o644",
|
| 230 |
+
"sha256": "3c5053c9c97cfde3e040fd39ff6858ec65288042a624075ea13784b58cfc1037",
|
| 231 |
+
"size": 688
|
| 232 |
+
},
|
| 233 |
+
"_examples/rest/main.go": {
|
| 234 |
+
"mode": "0o644",
|
| 235 |
+
"sha256": "0b9e1e73d7c649601508b3bb6c46b1587da6346f9cf33d6070f06f8e5c3a9298",
|
| 236 |
+
"size": 15501
|
| 237 |
+
},
|
| 238 |
+
"_examples/rest/routes.json": {
|
| 239 |
+
"mode": "0o644",
|
| 240 |
+
"sha256": "a16c95138e7f52046f617d5a98ebb1c83217d0672ed06dfece652f98ff4e2da2",
|
| 241 |
+
"size": 11317
|
| 242 |
+
},
|
| 243 |
+
"_examples/rest/routes.md": {
|
| 244 |
+
"mode": "0o644",
|
| 245 |
+
"sha256": "26951bf691542d7ca05d990dc9bddaa90bfb01497bb4f55c4cfb97581319e6f2",
|
| 246 |
+
"size": 4665
|
| 247 |
+
},
|
| 248 |
+
"_examples/router-walk/main.go": {
|
| 249 |
+
"mode": "0o644",
|
| 250 |
+
"sha256": "5e8548ddda78f646d448602e6922252d079803c555086b3f00280b00d10efed6",
|
| 251 |
+
"size": 914
|
| 252 |
+
},
|
| 253 |
+
"_examples/todos-resource/main.go": {
|
| 254 |
+
"mode": "0o644",
|
| 255 |
+
"sha256": "a0a551b6d6ca81210b129cd2a70d76e38370b2461be78813e6f117cfef6da020",
|
| 256 |
+
"size": 748
|
| 257 |
+
},
|
| 258 |
+
"_examples/todos-resource/todos.go": {
|
| 259 |
+
"mode": "0o644",
|
| 260 |
+
"sha256": "5979a0124dbb9febcc1c6846ed843590d40f5ae440d2694e57c0860bf7bc395c",
|
| 261 |
+
"size": 1453
|
| 262 |
+
},
|
| 263 |
+
"_examples/todos-resource/users.go": {
|
| 264 |
+
"mode": "0o644",
|
| 265 |
+
"sha256": "212c60277ace9afb95ef02729be959c2c39472a14854d2af95c711c265e3778f",
|
| 266 |
+
"size": 1323
|
| 267 |
+
},
|
| 268 |
+
"_examples/versions/data/article.go": {
|
| 269 |
+
"mode": "0o644",
|
| 270 |
+
"sha256": "90aa75226b4514e40033fb406de1319f419c4af5291a82e21a615ba7b5a121f8",
|
| 271 |
+
"size": 388
|
| 272 |
+
},
|
| 273 |
+
"_examples/versions/data/errors.go": {
|
| 274 |
+
"mode": "0o644",
|
| 275 |
+
"sha256": "e2803582a823e474441392b40e958bf1e1545613d61d1b7911cf6556854635bf",
|
| 276 |
+
"size": 542
|
| 277 |
+
},
|
| 278 |
+
"_examples/versions/go.mod": {
|
| 279 |
+
"mode": "0o644",
|
| 280 |
+
"sha256": "fd833daea12d311fd9c7182d433095eb224be3a4c80b26311e848c7e452affb5",
|
| 281 |
+
"size": 152
|
| 282 |
+
},
|
| 283 |
+
"_examples/versions/go.sum": {
|
| 284 |
+
"mode": "0o644",
|
| 285 |
+
"sha256": "0712aab90567f5fa0c11762c05494f8f6b42993b18573679ce165b805ad4e733",
|
| 286 |
+
"size": 491
|
| 287 |
+
},
|
| 288 |
+
"_examples/versions/main.go": {
|
| 289 |
+
"mode": "0o644",
|
| 290 |
+
"sha256": "16f5c10e86dadb44039863545041cfbc60de40271b71ea1b1ea62ca46ca2a207",
|
| 291 |
+
"size": 4072
|
| 292 |
+
},
|
| 293 |
+
"_examples/versions/presenter/v1/article.go": {
|
| 294 |
+
"mode": "0o644",
|
| 295 |
+
"sha256": "6183e68da9fde3998f49f528775d302a2462427d24d7d72a1f3511501b8bd817",
|
| 296 |
+
"size": 399
|
| 297 |
+
},
|
| 298 |
+
"_examples/versions/presenter/v2/article.go": {
|
| 299 |
+
"mode": "0o644",
|
| 300 |
+
"sha256": "8b1ab4c9e381ac1116f2a0572e173b9ddd6b7e889fe1cd6aeb1e75a7d9c10209",
|
| 301 |
+
"size": 625
|
| 302 |
+
},
|
| 303 |
+
"_examples/versions/presenter/v3/article.go": {
|
| 304 |
+
"mode": "0o644",
|
| 305 |
+
"sha256": "19a384a64f6009a8565d3b8bae774ed26d1d730ba6f4e5b510ab23f594833173",
|
| 306 |
+
"size": 1002
|
| 307 |
+
},
|
| 308 |
+
"chain.go": {
|
| 309 |
+
"mode": "0o644",
|
| 310 |
+
"sha256": "8c22d7bbc23f4b4d46ded5ef721a9c3a173031fa2c2c9a7b48c46c2b07e8bd80",
|
| 311 |
+
"size": 1517
|
| 312 |
+
},
|
| 313 |
+
"chi.go": {
|
| 314 |
+
"mode": "0o644",
|
| 315 |
+
"sha256": "3eaf980c6907f98497be9ec759ae601510f799656fd0663c2c3e202ba4fc5ec9",
|
| 316 |
+
"size": 4688
|
| 317 |
+
},
|
| 318 |
+
"context.go": {
|
| 319 |
+
"mode": "0o644",
|
| 320 |
+
"sha256": "b453c9c10998064775d2d9886a30d37f1d42a39555ef7b52847af4952fa90f56",
|
| 321 |
+
"size": 4831
|
| 322 |
+
},
|
| 323 |
+
"go.mod": {
|
| 324 |
+
"mode": "0o644",
|
| 325 |
+
"sha256": "a8921a2ecd771d342ee6e7f45065941a943f17c39f1cee4633dd264efa7e97ae",
|
| 326 |
+
"size": 149
|
| 327 |
+
},
|
| 328 |
+
"middleware/basic_auth.go": {
|
| 329 |
+
"mode": "0o644",
|
| 330 |
+
"sha256": "d1cfa0a32e9871cfba478590d1c1010297df20e53bbf83f34b09a4ec76e8b278",
|
| 331 |
+
"size": 852
|
| 332 |
+
},
|
| 333 |
+
"middleware/clean_path.go": {
|
| 334 |
+
"mode": "0o644",
|
| 335 |
+
"sha256": "00cf47dd47cf4b6a1fa29dd3d5cdff4f7de8adbea410801128afe8aeaed35f0d",
|
| 336 |
+
"size": 658
|
| 337 |
+
},
|
| 338 |
+
"middleware/compress.go": {
|
| 339 |
+
"mode": "0o644",
|
| 340 |
+
"sha256": "3965e92c59ea9a1fb48fa47705870ad379a7ef2c39a41dfb211b5975f86c2773",
|
| 341 |
+
"size": 11491
|
| 342 |
+
},
|
| 343 |
+
"middleware/content_charset.go": {
|
| 344 |
+
"mode": "0o644",
|
| 345 |
+
"sha256": "54e0df171b3d78cabeb8af3878e861e264f1e95c993352c4edabc94cb3532295",
|
| 346 |
+
"size": 1239
|
| 347 |
+
},
|
| 348 |
+
"middleware/content_encoding.go": {
|
| 349 |
+
"mode": "0o644",
|
| 350 |
+
"sha256": "a1b35d124400a423d385eca9d066593c790e867db165af75e25b31b649d37fd8",
|
| 351 |
+
"size": 1087
|
| 352 |
+
},
|
| 353 |
+
"middleware/content_type.go": {
|
| 354 |
+
"mode": "0o644",
|
| 355 |
+
"sha256": "5cedd359e3390edc613557681dda3cbc60faa2e4efb2a8974464876377b3a797",
|
| 356 |
+
"size": 1265
|
| 357 |
+
},
|
| 358 |
+
"middleware/get_head.go": {
|
| 359 |
+
"mode": "0o644",
|
| 360 |
+
"sha256": "76af2014d2080f597a0205f51265badcad7af64d09652a79ed1a214074f5c582",
|
| 361 |
+
"size": 977
|
| 362 |
+
},
|
| 363 |
+
"middleware/heartbeat.go": {
|
| 364 |
+
"mode": "0o644",
|
| 365 |
+
"sha256": "fc242f8c645cfc7640a36bccccb490cad9136ed07a751b0998f554412c29bca4",
|
| 366 |
+
"size": 755
|
| 367 |
+
},
|
| 368 |
+
"middleware/logger.go": {
|
| 369 |
+
"mode": "0o644",
|
| 370 |
+
"sha256": "3b47fe124f65cb60406896cafb7e1b3e179039d8ff852ddf31994875fdef3e64",
|
| 371 |
+
"size": 4978
|
| 372 |
+
},
|
| 373 |
+
"middleware/maybe.go": {
|
| 374 |
+
"mode": "0o644",
|
| 375 |
+
"sha256": "d600d7ea5b3184ea8ea952a145b1fca21e458b94601eae4a6cb215b3a04a3dee",
|
| 376 |
+
"size": 636
|
| 377 |
+
},
|
| 378 |
+
"middleware/middleware.go": {
|
| 379 |
+
"mode": "0o644",
|
| 380 |
+
"sha256": "8789f84814d815185cb5a7bc6c247969b67d0ff9b8045ee1c95f4c9ad59ff4b1",
|
| 381 |
+
"size": 684
|
| 382 |
+
},
|
| 383 |
+
"middleware/nocache.go": {
|
| 384 |
+
"mode": "0o644",
|
| 385 |
+
"sha256": "389eb1e308194561aea139b8d56d49f46b9fb30d84f8da146f5deaea66c5f5cc",
|
| 386 |
+
"size": 1430
|
| 387 |
+
},
|
| 388 |
+
"middleware/page_route.go": {
|
| 389 |
+
"mode": "0o644",
|
| 390 |
+
"sha256": "03f3b50c4a55c345b52ce8f4b6d5da4b977647dd66e2e59b5f3635bda93e779c",
|
| 391 |
+
"size": 519
|
| 392 |
+
},
|
| 393 |
+
"middleware/path_rewrite.go": {
|
| 394 |
+
"mode": "0o644",
|
| 395 |
+
"sha256": "0f3c01a40e320494a093d804376f3be8519b86285b5e24e8937bcee2c13c9279",
|
| 396 |
+
"size": 422
|
| 397 |
+
},
|
| 398 |
+
"middleware/profiler.go": {
|
| 399 |
+
"mode": "0o644",
|
| 400 |
+
"sha256": "16b08d9316eb363cd21aa07a5bddc371b2539333124f9468e5cce9253c4cf655",
|
| 401 |
+
"size": 1325
|
| 402 |
+
},
|
| 403 |
+
"middleware/realip.go": {
|
| 404 |
+
"mode": "0o644",
|
| 405 |
+
"sha256": "8bf96831b9d8da8aab450e5036b92319a7e9480562e48c143b9e5a38d7adf040",
|
| 406 |
+
"size": 1826
|
| 407 |
+
},
|
| 408 |
+
"middleware/recoverer.go": {
|
| 409 |
+
"mode": "0o644",
|
| 410 |
+
"sha256": "5bb87fe7d8d78862758c2c566db65937d00148855e2375baf1ad50d91cb635dd",
|
| 411 |
+
"size": 4858
|
| 412 |
+
},
|
| 413 |
+
"middleware/request_id.go": {
|
| 414 |
+
"mode": "0o644",
|
| 415 |
+
"sha256": "287ac51224b728167000cb8efa060e7b3ff8172445c919e204fb21f858b6490f",
|
| 416 |
+
"size": 2966
|
| 417 |
+
},
|
| 418 |
+
"middleware/request_size.go": {
|
| 419 |
+
"mode": "0o644",
|
| 420 |
+
"sha256": "81a98f72a81442a01a4353be9833fda6803f585bf768cebde5a34ebc1c44c390",
|
| 421 |
+
"size": 454
|
| 422 |
+
},
|
| 423 |
+
"middleware/route_headers.go": {
|
| 424 |
+
"mode": "0o644",
|
| 425 |
+
"sha256": "19dce12b9e5ad910782b6fe553737e86e91a311a04aac8029bbf168b1afc6e6a",
|
| 426 |
+
"size": 4157
|
| 427 |
+
},
|
| 428 |
+
"middleware/strip.go": {
|
| 429 |
+
"mode": "0o644",
|
| 430 |
+
"sha256": "84d89295fbf662d65e43d567618569e962fa851893b51e4db5b940a8ed314d7b",
|
| 431 |
+
"size": 2009
|
| 432 |
+
},
|
| 433 |
+
"middleware/sunset.go": {
|
| 434 |
+
"mode": "0o644",
|
| 435 |
+
"sha256": "16d99c4da576c4b7c024a12da139421d5659290250bed8f3d3ff6e0ed5d93b0e",
|
| 436 |
+
"size": 700
|
| 437 |
+
},
|
| 438 |
+
"middleware/supress_notfound.go": {
|
| 439 |
+
"mode": "0o644",
|
| 440 |
+
"sha256": "b3c6cb935d27aaddef0dcb5d165447babf5e9bb42cf28b4ee4e5f4693810888f",
|
| 441 |
+
"size": 852
|
| 442 |
+
},
|
| 443 |
+
"middleware/terminal.go": {
|
| 444 |
+
"mode": "0o644",
|
| 445 |
+
"sha256": "da7433dde376ab7deef82ff5acad6bbbc64beca4dad95372468a5747e23dfaa3",
|
| 446 |
+
"size": 1885
|
| 447 |
+
},
|
| 448 |
+
"middleware/throttle.go": {
|
| 449 |
+
"mode": "0o644",
|
| 450 |
+
"sha256": "0b1d8983f9344502164a9d6534614efd7a0a45e3cea0571c7075b455ffbdf2c9",
|
| 451 |
+
"size": 3933
|
| 452 |
+
},
|
| 453 |
+
"middleware/timeout.go": {
|
| 454 |
+
"mode": "0o644",
|
| 455 |
+
"sha256": "3e83d007ec300b4ea605d97a336351e562ecd84b32c2b26206791d11ce7501c0",
|
| 456 |
+
"size": 1222
|
| 457 |
+
},
|
| 458 |
+
"middleware/url_format.go": {
|
| 459 |
+
"mode": "0o644",
|
| 460 |
+
"sha256": "3b2045310974325041cee84b235c92f3716d582aac04a3617df43d2eca00cb3d",
|
| 461 |
+
"size": 1786
|
| 462 |
+
},
|
| 463 |
+
"middleware/value.go": {
|
| 464 |
+
"mode": "0o644",
|
| 465 |
+
"sha256": "2ccb524ee5ccd68885ff0d1de052068026f5c3298705fec930175e2ec06a1f91",
|
| 466 |
+
"size": 436
|
| 467 |
+
},
|
| 468 |
+
"middleware/wrap_writer.go": {
|
| 469 |
+
"mode": "0o644",
|
| 470 |
+
"sha256": "6910a3750fb03f79a0eabda47f2b826482df1219a78b9d057248ca0e50bdd486",
|
| 471 |
+
"size": 6080
|
| 472 |
+
},
|
| 473 |
+
"mux.go": {
|
| 474 |
+
"mode": "0o644",
|
| 475 |
+
"sha256": "56cf12330e1fcfc11ebb5861b714b2bb3178fdcbf5fa5ae2317eb5c8c332160e",
|
| 476 |
+
"size": 16661
|
| 477 |
+
},
|
| 478 |
+
"path_value.go": {
|
| 479 |
+
"mode": "0o644",
|
| 480 |
+
"sha256": "05827f643e0153ca95763474a707f2e656c4d4a3d016cefb52a8ee92fa483a0c",
|
| 481 |
+
"size": 537
|
| 482 |
+
},
|
| 483 |
+
"path_value_fallback.go": {
|
| 484 |
+
"mode": "0o644",
|
| 485 |
+
"sha256": "cb2f0c7c6c29de7566124accf9dfaf6bd863c85e925db6cc12c56f213e1e7d3c",
|
| 486 |
+
"size": 541
|
| 487 |
+
},
|
| 488 |
+
"tree.go": {
|
| 489 |
+
"mode": "0o644",
|
| 490 |
+
"sha256": "7e7f0feabb0f0955613390e3ea0ba14dba6fa42fc6f173402dd2a8ce9aa676f2",
|
| 491 |
+
"size": 20698
|
| 492 |
+
}
|
| 493 |
+
},
|
| 494 |
+
"reference_kind": "provided_healthy_parent_changed_implementation",
|
| 495 |
+
"runtime": {
|
| 496 |
+
"clean_runtime_qualified": false,
|
| 497 |
+
"config": {
|
| 498 |
+
"architecture": "amd64",
|
| 499 |
+
"digest": "sha256:d7b1de3fa043311bb204c5bfb2be4c7f93929ddad99500c0168cd3118dbcf76e",
|
| 500 |
+
"os": "linux",
|
| 501 |
+
"sha256": "d7b1de3fa043311bb204c5bfb2be4c7f93929ddad99500c0168cd3118dbcf76e"
|
| 502 |
+
},
|
| 503 |
+
"context_inventory_sha256": "23b0f2ba866e198fbca4a22b95acccd6a186f4a24a1dfa909fccf97c470b7450",
|
| 504 |
+
"image": "registry.hf.space/harness-hub-scale800-4c935f9a1651fcba-starter-v1@sha256:bbed00cf0d9a5a2069d8473257b0e669cdf51b8fee4b724ec869acdfcada8fe1",
|
| 505 |
+
"image_pull_qualified": false,
|
| 506 |
+
"provider_image_source_attestation": false,
|
| 507 |
+
"publication_receipt_sha256": "327a53dad2f1e62f63373df395bdd26df1b879cdc55d9c03d07c4b75145cae3c",
|
| 508 |
+
"receipt_path": "resolutions/4c935f9a1651fcba/resolution.json",
|
| 509 |
+
"receipt_sha256": "5db25ab194ef9d226607eea555f97dfeea90449379718e1cc63126a618525afb",
|
| 510 |
+
"registry_digest": "sha256:bbed00cf0d9a5a2069d8473257b0e669cdf51b8fee4b724ec869acdfcada8fe1",
|
| 511 |
+
"source_image": "docker.io/swebench/swesmith.x86_64.go-chi_1776_chi.23c395f8@sha256:95b4aa88b074049389ac8260e984b6296b15db672dbc1d9518cd213bc3f3d945",
|
| 512 |
+
"source_receipt_sha256": "cf3b8d2fb430451ded7c33221f05e6b92c0883cda0f690e9ed9ca94be1bb0038",
|
| 513 |
+
"space_commit": "ef44afa5f4452f8db605b5ebe752ce52a97b753e",
|
| 514 |
+
"space_id": "harness-hub/scale800-4c935f9a1651fcba-starter-v1",
|
| 515 |
+
"status": "registry_resolved_paused_unqualified"
|
| 516 |
+
},
|
| 517 |
+
"schema": "scale800-go-jobs-transform-v1",
|
| 518 |
+
"source_completion_sha256": "fed08b6cbb432453cc05d1492416824ef61c2964bc2dc17212b2bbed431ebde1",
|
| 519 |
+
"source_index_sha256": "c6a579667a10d7d11f14285e2bc349086351d67c49a8f378c797c819dea8a47f",
|
| 520 |
+
"source_inventory_verified": true,
|
| 521 |
+
"source_manifest_sha256": null,
|
| 522 |
+
"source_proof_sha256": "010aba5ec320d548fb3bd8779b415ccdede620aecd0a87822a7dd3335ec73685",
|
| 523 |
+
"source_record": {
|
| 524 |
+
"anti_training_canary": false,
|
| 525 |
+
"anti_training_reviewed": true,
|
| 526 |
+
"candidate_id": "swesmith-go:go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg",
|
| 527 |
+
"cloud_launch_ready": false,
|
| 528 |
+
"healthy_mirror_commit": "7c70394a5a10a618122c2907dfe384a5cc995a3c",
|
| 529 |
+
"instruction_sha256": "3b695aff4ce31803303cdf2f56e28fbae3083720a81fdfa9f7b52876c2f973dc",
|
| 530 |
+
"is_fork": true,
|
| 531 |
+
"learner_path": "tasks/840bc95239383129f5b22459/learner",
|
| 532 |
+
"license_evidence": {
|
| 533 |
+
"source_code": {
|
| 534 |
+
"reviewed": true,
|
| 535 |
+
"sha256": "a2d51b7515acfaff2f7a88688650f2fc4fd99561383e72bba2305e3db59a1647",
|
| 536 |
+
"spdx": "MIT",
|
| 537 |
+
"training_allowed": true,
|
| 538 |
+
"url": "https://raw.githubusercontent.com/swesmith/go-chi__chi.23c395f8/b006964850fd10824c7bce34d64a6fccfb9eef08/LICENSE"
|
| 539 |
+
},
|
| 540 |
+
"task_bundle": {
|
| 541 |
+
"basis": "Captured publisher MIT/train declaration; exact software permission separately checked per source",
|
| 542 |
+
"reviewed": true,
|
| 543 |
+
"sha256": "2e917153213a0f11a6d0f314e87d8092eb8289ae4dc767cdc020d024c7d5339f",
|
| 544 |
+
"spdx": "MIT",
|
| 545 |
+
"training_allowed": true,
|
| 546 |
+
"url": "https://huggingface.co/api/datasets/SWE-bench/SWE-smith-go/revision/d2f6ecc2cf8ae1af00fc790801bbacfa2913e015"
|
| 547 |
+
}
|
| 548 |
+
},
|
| 549 |
+
"original_source_commit": "23c395f8524a30334126ca16fb4d37b88745b9b9",
|
| 550 |
+
"patch_sha256": "89086cccb936e73906c6ae762bc71c92bfa56c30bfa425dc897540d2ea739341",
|
| 551 |
+
"provenance_path": "tasks/840bc95239383129f5b22459/source-proof.json",
|
| 552 |
+
"repository_lineage": [
|
| 553 |
+
"swesmith/go-chi__chi.23c395f8",
|
| 554 |
+
"go-chi/chi"
|
| 555 |
+
],
|
| 556 |
+
"rfc008_complete": false,
|
| 557 |
+
"runtime_qualified": false,
|
| 558 |
+
"runtime_test_identity_status": "pending_runtime_test_identity_resolution",
|
| 559 |
+
"semantic_nonoverlap_established": false,
|
| 560 |
+
"source_commit": "b006964850fd10824c7bce34d64a6fccfb9eef08",
|
| 561 |
+
"source_dataset": "SWE-bench/SWE-smith-go",
|
| 562 |
+
"source_head_commit": "e90b717dacb12b1e156782d764fac4352cf8e03f",
|
| 563 |
+
"source_kind": "mutation",
|
| 564 |
+
"source_repo": "swesmith/go-chi__chi.23c395f8",
|
| 565 |
+
"source_revision": "d2f6ecc2cf8ae1af00fc790801bbacfa2913e015",
|
| 566 |
+
"source_split": "train",
|
| 567 |
+
"target_model_calibrated": false,
|
| 568 |
+
"task_id": "go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg",
|
| 569 |
+
"task_sha256": "c1354bb558a7920007079b9275944c582c4e87897894e1d36c3d5a505b433b99",
|
| 570 |
+
"training_admissible": false,
|
| 571 |
+
"upstream_identity_reviewed": true,
|
| 572 |
+
"upstream_repo": "go-chi/chi",
|
| 573 |
+
"usage": "quarantine_only"
|
| 574 |
+
},
|
| 575 |
+
"source_row_sha256": "42a1ca13d7c426fba9bba13bb90e4be142d0cc97ab3a0acf2ac9b59889611a5c",
|
| 576 |
+
"source_test_command": "go test -v ./...",
|
| 577 |
+
"target_model_calibrated": false,
|
| 578 |
+
"training_admissible": false,
|
| 579 |
+
"transfer_allowlist": [
|
| 580 |
+
"/testbed/.github/FUNDING.yml",
|
| 581 |
+
"/testbed/.gitignore",
|
| 582 |
+
"/testbed/CHANGELOG.md",
|
| 583 |
+
"/testbed/CONTRIBUTING.md",
|
| 584 |
+
"/testbed/LICENSE",
|
| 585 |
+
"/testbed/Makefile",
|
| 586 |
+
"/testbed/README.md",
|
| 587 |
+
"/testbed/SECURITY.md",
|
| 588 |
+
"/testbed/_examples/README.md",
|
| 589 |
+
"/testbed/_examples/chi.svg",
|
| 590 |
+
"/testbed/_examples/custom-handler/main.go",
|
| 591 |
+
"/testbed/_examples/custom-method/main.go",
|
| 592 |
+
"/testbed/_examples/fileserver/data/notes.txt",
|
| 593 |
+
"/testbed/_examples/fileserver/main.go",
|
| 594 |
+
"/testbed/_examples/graceful/main.go",
|
| 595 |
+
"/testbed/_examples/hello-world/main.go",
|
| 596 |
+
"/testbed/_examples/limits/main.go",
|
| 597 |
+
"/testbed/_examples/logging/main.go",
|
| 598 |
+
"/testbed/_examples/rest/go.mod",
|
| 599 |
+
"/testbed/_examples/rest/go.sum",
|
| 600 |
+
"/testbed/_examples/rest/main.go",
|
| 601 |
+
"/testbed/_examples/rest/routes.json",
|
| 602 |
+
"/testbed/_examples/rest/routes.md",
|
| 603 |
+
"/testbed/_examples/router-walk/main.go",
|
| 604 |
+
"/testbed/_examples/todos-resource/main.go",
|
| 605 |
+
"/testbed/_examples/todos-resource/todos.go",
|
| 606 |
+
"/testbed/_examples/todos-resource/users.go",
|
| 607 |
+
"/testbed/_examples/versions/data/article.go",
|
| 608 |
+
"/testbed/_examples/versions/data/errors.go",
|
| 609 |
+
"/testbed/_examples/versions/go.mod",
|
| 610 |
+
"/testbed/_examples/versions/go.sum",
|
| 611 |
+
"/testbed/_examples/versions/main.go",
|
| 612 |
+
"/testbed/_examples/versions/presenter/v1/article.go",
|
| 613 |
+
"/testbed/_examples/versions/presenter/v2/article.go",
|
| 614 |
+
"/testbed/_examples/versions/presenter/v3/article.go",
|
| 615 |
+
"/testbed/chain.go",
|
| 616 |
+
"/testbed/chi.go",
|
| 617 |
+
"/testbed/context.go",
|
| 618 |
+
"/testbed/go.mod",
|
| 619 |
+
"/testbed/middleware/basic_auth.go",
|
| 620 |
+
"/testbed/middleware/clean_path.go",
|
| 621 |
+
"/testbed/middleware/compress.go",
|
| 622 |
+
"/testbed/middleware/content_charset.go",
|
| 623 |
+
"/testbed/middleware/content_encoding.go",
|
| 624 |
+
"/testbed/middleware/content_type.go",
|
| 625 |
+
"/testbed/middleware/get_head.go",
|
| 626 |
+
"/testbed/middleware/heartbeat.go",
|
| 627 |
+
"/testbed/middleware/logger.go",
|
| 628 |
+
"/testbed/middleware/maybe.go",
|
| 629 |
+
"/testbed/middleware/middleware.go",
|
| 630 |
+
"/testbed/middleware/nocache.go",
|
| 631 |
+
"/testbed/middleware/page_route.go",
|
| 632 |
+
"/testbed/middleware/path_rewrite.go",
|
| 633 |
+
"/testbed/middleware/profiler.go",
|
| 634 |
+
"/testbed/middleware/realip.go",
|
| 635 |
+
"/testbed/middleware/recoverer.go",
|
| 636 |
+
"/testbed/middleware/request_id.go",
|
| 637 |
+
"/testbed/middleware/request_size.go",
|
| 638 |
+
"/testbed/middleware/route_headers.go",
|
| 639 |
+
"/testbed/middleware/strip.go",
|
| 640 |
+
"/testbed/middleware/sunset.go",
|
| 641 |
+
"/testbed/middleware/supress_notfound.go",
|
| 642 |
+
"/testbed/middleware/terminal.go",
|
| 643 |
+
"/testbed/middleware/throttle.go",
|
| 644 |
+
"/testbed/middleware/timeout.go",
|
| 645 |
+
"/testbed/middleware/url_format.go",
|
| 646 |
+
"/testbed/middleware/value.go",
|
| 647 |
+
"/testbed/middleware/wrap_writer.go",
|
| 648 |
+
"/testbed/mux.go",
|
| 649 |
+
"/testbed/path_value.go",
|
| 650 |
+
"/testbed/path_value_fallback.go",
|
| 651 |
+
"/testbed/tree.go"
|
| 652 |
+
]
|
| 653 |
+
}
|
tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/solution/solve.sh
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/bin/bash
|
| 2 |
+
set -eu
|
| 3 |
+
/usr/local/bin/python -I - <<'OPENENV_REFERENCE'
|
| 4 |
+
import base64,json
|
| 5 |
+
from pathlib import Path
|
| 6 |
+
files=json.loads('{"middleware/strip.go": {"data": "cGFja2FnZSBtaWRkbGV3YXJlCgppbXBvcnQgKAoJImZtdCIKCSJuZXQvaHR0cCIKCSJzdHJpbmdzIgoKCSJnaXRodWIuY29tL2dvLWNoaS9jaGkvdjUiCikKCi8vIFN0cmlwU2xhc2hlcyBpcyBhIG1pZGRsZXdhcmUgdGhhdCB3aWxsIG1hdGNoIHJlcXVlc3QgcGF0aHMgd2l0aCBhIHRyY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m92aWRlZCBwcmVmaXggZnJvbSB0aGUKLy8gcmVxdWVzdCBwYXRoIGJlZm9yZSBoYW5kaW5nIHRoZSByZXF1ZXN0IG92ZXIgdG8gdGhlIG5leHQgaGFuZGxlci4KZnVuYyBTdHJpcFByZWZpeChwcmVmaXggc3RyaW5nKSBmdW5jKGh0dHAuSGFuZGxlcikgaHR0cC5IYW5kbGVyIHsKCXJldHVybiBmdW5jKG5leHQgaHR0cC5IYW5kbGVyKSBodHRwLkhhbmRsZXIgewoJCXJldHVybiBodHRwLlN0cmlwUHJlZml4KHByZWZpeCwgbmV4dCkKCX0KfQo=", "mode": 420}}')
|
| 7 |
+
for name,item in files.items():
|
| 8 |
+
p=Path('/testbed')/name
|
| 9 |
+
p.parent.mkdir(parents=True,exist_ok=True)
|
| 10 |
+
p.write_bytes(base64.b64decode(item['data']))
|
| 11 |
+
p.chmod(item['mode'])
|
| 12 |
+
OPENENV_REFERENCE
|
tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/task.toml
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version = "1.0"
|
| 2 |
+
[agent]
|
| 3 |
+
timeout_sec = 300.0
|
| 4 |
+
[verifier]
|
| 5 |
+
timeout_sec = 300.0
|
| 6 |
+
environment_mode = "shared"
|
| 7 |
+
[environment]
|
| 8 |
+
docker_image = "ghcr.io/younesbelkada/scale800@sha256:d600c5c315a94f187482d203bd39239d207b4913589e08e14fea2500c0a98f1c"
|
| 9 |
+
cpus = 1
|
| 10 |
+
memory_mb = 2048
|
| 11 |
+
storage_mb = 4096
|
tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/tests/frozen/context_test.go
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
package chi
|
| 2 |
+
|
| 3 |
+
import "testing"
|
| 4 |
+
|
| 5 |
+
// TestRoutePattern tests correct in-the-middle wildcard removals.
|
| 6 |
+
// If user organizes a router like this:
|
| 7 |
+
//
|
| 8 |
+
// (router.go)
|
| 9 |
+
//
|
| 10 |
+
// r.Route("/v1", func(r chi.Router) {
|
| 11 |
+
// r.Mount("/resources", resourcesController{}.Router())
|
| 12 |
+
// }
|
| 13 |
+
//
|
| 14 |
+
// (resources_controller.go)
|
| 15 |
+
//
|
| 16 |
+
// r.Route("/", func(r chi.Router) {
|
| 17 |
+
// r.Get("/{resource_id}", getResource())
|
| 18 |
+
// // other routes...
|
| 19 |
+
// }
|
| 20 |
+
//
|
| 21 |
+
// This test checks how the route pattern is calculated
|
| 22 |
+
// "/v1/resources/{resource_id}" (right)
|
| 23 |
+
// "/v1/resources/*/{resource_id}" (wrong)
|
| 24 |
+
func TestRoutePattern(t *testing.T) {
|
| 25 |
+
routePatterns := []string{
|
| 26 |
+
"/v1/*",
|
| 27 |
+
"/resources/*",
|
| 28 |
+
"/{resource_id}",
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
x := &Context{
|
| 32 |
+
RoutePatterns: routePatterns,
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
if p := x.RoutePattern(); p != "/v1/resources/{resource_id}" {
|
| 36 |
+
t.Fatal("unexpected route pattern: " + p)
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
x.RoutePatterns = []string{
|
| 40 |
+
"/v1/*",
|
| 41 |
+
"/resources/*",
|
| 42 |
+
// Additional wildcard, depending on the router structure of the user
|
| 43 |
+
"/*",
|
| 44 |
+
"/{resource_id}",
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
// Correctly removes in-the-middle wildcards instead of "/v1/resources/*/{resource_id}"
|
| 48 |
+
if p := x.RoutePattern(); p != "/v1/resources/{resource_id}" {
|
| 49 |
+
t.Fatal("unexpected route pattern: " + p)
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
x.RoutePatterns = []string{
|
| 53 |
+
"/v1/*",
|
| 54 |
+
"/resources/*",
|
| 55 |
+
// Even with many wildcards
|
| 56 |
+
"/*",
|
| 57 |
+
"/*",
|
| 58 |
+
"/*",
|
| 59 |
+
"/{resource_id}/*", // Keeping trailing wildcard
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
// Correctly removes in-the-middle wildcards instead of "/v1/resources/*/*/{resource_id}/*"
|
| 63 |
+
if p := x.RoutePattern(); p != "/v1/resources/{resource_id}/*" {
|
| 64 |
+
t.Fatal("unexpected route pattern: " + p)
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
x.RoutePatterns = []string{
|
| 68 |
+
"/v1/*",
|
| 69 |
+
"/resources/*",
|
| 70 |
+
// And respects asterisks as part of the paths
|
| 71 |
+
"/*special_path/*",
|
| 72 |
+
"/with_asterisks*/*",
|
| 73 |
+
"/{resource_id}",
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
// Correctly removes in-the-middle wildcards instead of "/v1/resourcesspecial_path/with_asterisks{resource_id}"
|
| 77 |
+
if p := x.RoutePattern(); p != "/v1/resources/*special_path/with_asterisks*/{resource_id}" {
|
| 78 |
+
t.Fatal("unexpected route pattern: " + p)
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
// Testing for the root route pattern
|
| 82 |
+
x.RoutePatterns = []string{"/"}
|
| 83 |
+
// It should just return "/" as the pattern
|
| 84 |
+
if p := x.RoutePattern(); p != "/" {
|
| 85 |
+
t.Fatal("unexpected route pattern for root: " + p)
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
// Testing empty route pattern for nil context
|
| 89 |
+
var nilContext *Context
|
| 90 |
+
if p := nilContext.RoutePattern(); p != "" {
|
| 91 |
+
t.Fatalf("unexpected non-empty route pattern for nil context: %q", p)
|
| 92 |
+
}
|
| 93 |
+
}
|
tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/tests/frozen/middleware/compress_test.go
ADDED
|
@@ -0,0 +1,219 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 12 |
+
"testing"
|
| 13 |
+
|
| 14 |
+
"github.com/go-chi/chi/v5"
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
func TestCompressor(t *testing.T) {
|
| 18 |
+
r := chi.NewRouter()
|
| 19 |
+
|
| 20 |
+
compressor := NewCompressor(5, "text/html", "text/css")
|
| 21 |
+
if len(compressor.encoders) != 0 || len(compressor.pooledEncoders) != 2 {
|
| 22 |
+
t.Errorf("gzip and deflate should be pooled")
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
compressor.SetEncoder("nop", func(w io.Writer, _ int) io.Writer {
|
| 26 |
+
return w
|
| 27 |
+
})
|
| 28 |
+
|
| 29 |
+
if len(compressor.encoders) != 1 {
|
| 30 |
+
t.Errorf("nop encoder should be stored in the encoders map")
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
r.Use(compressor.Handler)
|
| 34 |
+
|
| 35 |
+
r.Get("/gethtml", func(w http.ResponseWriter, r *http.Request) {
|
| 36 |
+
w.Header().Set("Content-Type", "text/html")
|
| 37 |
+
w.Write([]byte("textstring"))
|
| 38 |
+
})
|
| 39 |
+
|
| 40 |
+
r.Get("/getcss", func(w http.ResponseWriter, r *http.Request) {
|
| 41 |
+
w.Header().Set("Content-Type", "text/html")
|
| 42 |
+
w.Write([]byte("textstring"))
|
| 43 |
+
})
|
| 44 |
+
|
| 45 |
+
r.Get("/getplain", func(w http.ResponseWriter, r *http.Request) {
|
| 46 |
+
w.Header().Set("Content-Type", "text/html")
|
| 47 |
+
w.Write([]byte("textstring"))
|
| 48 |
+
})
|
| 49 |
+
|
| 50 |
+
ts := httptest.NewServer(r)
|
| 51 |
+
defer ts.Close()
|
| 52 |
+
|
| 53 |
+
tests := []struct {
|
| 54 |
+
name string
|
| 55 |
+
path string
|
| 56 |
+
expectedEncoding string
|
| 57 |
+
acceptedEncodings []string
|
| 58 |
+
}{
|
| 59 |
+
{
|
| 60 |
+
name: "no expected encodings due to no accepted encodings",
|
| 61 |
+
path: "/gethtml",
|
| 62 |
+
acceptedEncodings: nil,
|
| 63 |
+
expectedEncoding: "",
|
| 64 |
+
},
|
| 65 |
+
{
|
| 66 |
+
name: "no expected encodings due to content type",
|
| 67 |
+
path: "/getplain",
|
| 68 |
+
acceptedEncodings: nil,
|
| 69 |
+
expectedEncoding: "",
|
| 70 |
+
},
|
| 71 |
+
{
|
| 72 |
+
name: "gzip is only encoding",
|
| 73 |
+
path: "/gethtml",
|
| 74 |
+
acceptedEncodings: []string{"gzip"},
|
| 75 |
+
expectedEncoding: "gzip",
|
| 76 |
+
},
|
| 77 |
+
{
|
| 78 |
+
name: "gzip is preferred over deflate",
|
| 79 |
+
path: "/getcss",
|
| 80 |
+
acceptedEncodings: []string{"gzip", "deflate"},
|
| 81 |
+
expectedEncoding: "gzip",
|
| 82 |
+
},
|
| 83 |
+
{
|
| 84 |
+
name: "deflate is used",
|
| 85 |
+
path: "/getcss",
|
| 86 |
+
acceptedEncodings: []string{"deflate"},
|
| 87 |
+
expectedEncoding: "deflate",
|
| 88 |
+
},
|
| 89 |
+
{
|
| 90 |
+
|
| 91 |
+
name: "nop is preferred",
|
| 92 |
+
path: "/getcss",
|
| 93 |
+
acceptedEncodings: []string{"nop, gzip, deflate"},
|
| 94 |
+
expectedEncoding: "nop",
|
| 95 |
+
},
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
+
for _, tc := range tests {
|
| 99 |
+
tc := tc
|
| 100 |
+
t.Run(tc.name, func(t *testing.T) {
|
| 101 |
+
resp, respString := testRequestWithAcceptedEncodings(t, ts, "GET", tc.path, tc.acceptedEncodings...)
|
| 102 |
+
if respString != "textstring" {
|
| 103 |
+
t.Errorf("response text doesn't match; expected:%q, got:%q", "textstring", respString)
|
| 104 |
+
}
|
| 105 |
+
if got := resp.Header.Get("Content-Encoding"); got != tc.expectedEncoding {
|
| 106 |
+
t.Errorf("expected encoding %q but got %q", tc.expectedEncoding, got)
|
| 107 |
+
}
|
| 108 |
+
|
| 109 |
+
})
|
| 110 |
+
|
| 111 |
+
}
|
| 112 |
+
}
|
| 113 |
+
|
| 114 |
+
func TestCompressorWildcards(t *testing.T) {
|
| 115 |
+
tests := []struct {
|
| 116 |
+
name string
|
| 117 |
+
recover string
|
| 118 |
+
types []string
|
| 119 |
+
typesCount int
|
| 120 |
+
wcCount int
|
| 121 |
+
}{
|
| 122 |
+
{
|
| 123 |
+
name: "defaults",
|
| 124 |
+
typesCount: 10,
|
| 125 |
+
},
|
| 126 |
+
{
|
| 127 |
+
name: "no wildcard",
|
| 128 |
+
types: []string{"text/plain", "text/html"},
|
| 129 |
+
typesCount: 2,
|
| 130 |
+
},
|
| 131 |
+
{
|
| 132 |
+
name: "invalid wildcard #1",
|
| 133 |
+
types: []string{"audio/*wav"},
|
| 134 |
+
recover: "middleware/compress: Unsupported content-type wildcard pattern 'audio/*wav'. Only '/*' supported",
|
| 135 |
+
},
|
| 136 |
+
{
|
| 137 |
+
name: "invalid wildcard #2",
|
| 138 |
+
types: []string{"application*/*"},
|
| 139 |
+
recover: "middleware/compress: Unsupported content-type wildcard pattern 'application*/*'. Only '/*' supported",
|
| 140 |
+
},
|
| 141 |
+
{
|
| 142 |
+
name: "valid wildcard",
|
| 143 |
+
types: []string{"text/*"},
|
| 144 |
+
wcCount: 1,
|
| 145 |
+
},
|
| 146 |
+
{
|
| 147 |
+
name: "mixed",
|
| 148 |
+
types: []string{"audio/wav", "text/*"},
|
| 149 |
+
typesCount: 1,
|
| 150 |
+
wcCount: 1,
|
| 151 |
+
},
|
| 152 |
+
}
|
| 153 |
+
for _, tt := range tests {
|
| 154 |
+
t.Run(tt.name, func(t *testing.T) {
|
| 155 |
+
defer func() {
|
| 156 |
+
if tt.recover == "" {
|
| 157 |
+
tt.recover = "<nil>"
|
| 158 |
+
}
|
| 159 |
+
if r := recover(); tt.recover != fmt.Sprintf("%v", r) {
|
| 160 |
+
t.Errorf("Unexpected value recovered: %v", r)
|
| 161 |
+
}
|
| 162 |
+
}()
|
| 163 |
+
compressor := NewCompressor(5, tt.types...)
|
| 164 |
+
if len(compressor.allowedTypes) != tt.typesCount {
|
| 165 |
+
t.Errorf("expected %d allowedTypes, got %d", tt.typesCount, len(compressor.allowedTypes))
|
| 166 |
+
}
|
| 167 |
+
if len(compressor.allowedWildcards) != tt.wcCount {
|
| 168 |
+
t.Errorf("expected %d allowedWildcards, got %d", tt.wcCount, len(compressor.allowedWildcards))
|
| 169 |
+
}
|
| 170 |
+
})
|
| 171 |
+
}
|
| 172 |
+
}
|
| 173 |
+
|
| 174 |
+
func testRequestWithAcceptedEncodings(t *testing.T, ts *httptest.Server, method, path string, encodings ...string) (*http.Response, string) {
|
| 175 |
+
req, err := http.NewRequest(method, ts.URL+path, nil)
|
| 176 |
+
if err != nil {
|
| 177 |
+
t.Fatal(err)
|
| 178 |
+
return nil, ""
|
| 179 |
+
}
|
| 180 |
+
if len(encodings) > 0 {
|
| 181 |
+
encodingsString := strings.Join(encodings, ",")
|
| 182 |
+
req.Header.Set("Accept-Encoding", encodingsString)
|
| 183 |
+
}
|
| 184 |
+
|
| 185 |
+
resp, err := http.DefaultClient.Do(req)
|
| 186 |
+
if err != nil {
|
| 187 |
+
t.Fatal(err)
|
| 188 |
+
return nil, ""
|
| 189 |
+
}
|
| 190 |
+
|
| 191 |
+
respBody := decodeResponseBody(t, resp)
|
| 192 |
+
defer resp.Body.Close()
|
| 193 |
+
|
| 194 |
+
return resp, respBody
|
| 195 |
+
}
|
| 196 |
+
|
| 197 |
+
func decodeResponseBody(t *testing.T, resp *http.Response) string {
|
| 198 |
+
var reader io.ReadCloser
|
| 199 |
+
switch resp.Header.Get("Content-Encoding") {
|
| 200 |
+
case "gzip":
|
| 201 |
+
var err error
|
| 202 |
+
reader, err = gzip.NewReader(resp.Body)
|
| 203 |
+
if err != nil {
|
| 204 |
+
t.Fatal(err)
|
| 205 |
+
}
|
| 206 |
+
case "deflate":
|
| 207 |
+
reader = flate.NewReader(resp.Body)
|
| 208 |
+
default:
|
| 209 |
+
reader = resp.Body
|
| 210 |
+
}
|
| 211 |
+
respBody, err := ioutil.ReadAll(reader)
|
| 212 |
+
if err != nil {
|
| 213 |
+
t.Fatal(err)
|
| 214 |
+
return ""
|
| 215 |
+
}
|
| 216 |
+
reader.Close()
|
| 217 |
+
|
| 218 |
+
return string(respBody)
|
| 219 |
+
}
|
tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/tests/frozen/middleware/content_charset_test.go
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
package middleware
|
| 2 |
+
|
| 3 |
+
import (
|
| 4 |
+
"net/http"
|
| 5 |
+
"net/http/httptest"
|
| 6 |
+
"testing"
|
| 7 |
+
|
| 8 |
+
"github.com/go-chi/chi/v5"
|
| 9 |
+
)
|
| 10 |
+
|
| 11 |
+
func TestContentCharset(t *testing.T) {
|
| 12 |
+
t.Parallel()
|
| 13 |
+
|
| 14 |
+
var tests = []struct {
|
| 15 |
+
name string
|
| 16 |
+
inputValue string
|
| 17 |
+
inputContentCharset []string
|
| 18 |
+
want int
|
| 19 |
+
}{
|
| 20 |
+
{
|
| 21 |
+
"should accept requests with a matching charset",
|
| 22 |
+
"application/json; charset=UTF-8",
|
| 23 |
+
[]string{"UTF-8"},
|
| 24 |
+
http.StatusOK,
|
| 25 |
+
},
|
| 26 |
+
{
|
| 27 |
+
"should be case-insensitive",
|
| 28 |
+
"application/json; charset=utf-8",
|
| 29 |
+
[]string{"UTF-8"},
|
| 30 |
+
http.StatusOK,
|
| 31 |
+
},
|
| 32 |
+
{
|
| 33 |
+
"should accept requests with a matching charset with extra values",
|
| 34 |
+
"application/json; foo=bar; charset=UTF-8; spam=eggs",
|
| 35 |
+
[]string{"UTF-8"},
|
| 36 |
+
http.StatusOK,
|
| 37 |
+
},
|
| 38 |
+
{
|
| 39 |
+
"should accept requests with a matching charset when multiple charsets are supported",
|
| 40 |
+
"text/xml; charset=UTF-8",
|
| 41 |
+
[]string{"UTF-8", "Latin-1"},
|
| 42 |
+
http.StatusOK,
|
| 43 |
+
},
|
| 44 |
+
{
|
| 45 |
+
"should accept requests with no charset if empty charset headers are allowed",
|
| 46 |
+
"text/xml",
|
| 47 |
+
[]string{"UTF-8", ""},
|
| 48 |
+
http.StatusOK,
|
| 49 |
+
},
|
| 50 |
+
{
|
| 51 |
+
"should not accept requests with no charset if empty charset headers are not allowed",
|
| 52 |
+
"text/xml",
|
| 53 |
+
[]string{"UTF-8"},
|
| 54 |
+
http.StatusUnsupportedMediaType,
|
| 55 |
+
},
|
| 56 |
+
{
|
| 57 |
+
"should not accept requests with a mismatching charset",
|
| 58 |
+
"text/plain; charset=Latin-1",
|
| 59 |
+
[]string{"UTF-8"},
|
| 60 |
+
http.StatusUnsupportedMediaType,
|
| 61 |
+
},
|
| 62 |
+
{
|
| 63 |
+
"should not accept requests with a mismatching charset even if empty charsets are allowed",
|
| 64 |
+
"text/plain; charset=Latin-1",
|
| 65 |
+
[]string{"UTF-8", ""},
|
| 66 |
+
http.StatusUnsupportedMediaType,
|
| 67 |
+
},
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
for _, tt := range tests {
|
| 71 |
+
var tt = tt
|
| 72 |
+
t.Run(tt.name, func(t *testing.T) {
|
| 73 |
+
t.Parallel()
|
| 74 |
+
|
| 75 |
+
var recorder = httptest.NewRecorder()
|
| 76 |
+
|
| 77 |
+
var r = chi.NewRouter()
|
| 78 |
+
r.Use(ContentCharset(tt.inputContentCharset...))
|
| 79 |
+
r.Get("/", func(w http.ResponseWriter, r *http.Request) {})
|
| 80 |
+
|
| 81 |
+
var req, _ = http.NewRequest("GET", "/", nil)
|
| 82 |
+
req.Header.Set("Content-Type", tt.inputValue)
|
| 83 |
+
|
| 84 |
+
r.ServeHTTP(recorder, req)
|
| 85 |
+
var res = recorder.Result()
|
| 86 |
+
|
| 87 |
+
if res.StatusCode != tt.want {
|
| 88 |
+
t.Errorf("response is incorrect, got %d, want %d", recorder.Code, tt.want)
|
| 89 |
+
}
|
| 90 |
+
})
|
| 91 |
+
}
|
| 92 |
+
}
|
| 93 |
+
|
| 94 |
+
func TestSplit(t *testing.T) {
|
| 95 |
+
t.Parallel()
|
| 96 |
+
|
| 97 |
+
var s1, s2 = split(" type1;type2 ", ";")
|
| 98 |
+
|
| 99 |
+
if s1 != "type1" || s2 != "type2" {
|
| 100 |
+
t.Errorf("Want type1, type2 got %s, %s", s1, s2)
|
| 101 |
+
}
|
| 102 |
+
|
| 103 |
+
s1, s2 = split("type1 ", ";")
|
| 104 |
+
|
| 105 |
+
if s1 != "type1" {
|
| 106 |
+
t.Errorf("Want \"type1\" got \"%s\"", s1)
|
| 107 |
+
}
|
| 108 |
+
if s2 != "" {
|
| 109 |
+
t.Errorf("Want empty string got \"%s\"", s2)
|
| 110 |
+
}
|
| 111 |
+
}
|
| 112 |
+
|
| 113 |
+
func TestContentEncoding(t *testing.T) {
|
| 114 |
+
t.Parallel()
|
| 115 |
+
|
| 116 |
+
if !contentEncoding("application/json; foo=bar; charset=utf-8; spam=eggs", []string{"utf-8"}...) {
|
| 117 |
+
t.Error("Want true, got false")
|
| 118 |
+
}
|
| 119 |
+
|
| 120 |
+
if contentEncoding("text/plain; charset=latin-1", []string{"utf-8"}...) {
|
| 121 |
+
t.Error("Want false, got true")
|
| 122 |
+
}
|
| 123 |
+
|
| 124 |
+
if !contentEncoding("text/xml; charset=UTF-8", []string{"latin-1", "utf-8"}...) {
|
| 125 |
+
t.Error("Want true, got false")
|
| 126 |
+
}
|
| 127 |
+
}
|
tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/tests/frozen/middleware/content_encoding_test.go
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
package middleware
|
| 2 |
+
|
| 3 |
+
import (
|
| 4 |
+
"bytes"
|
| 5 |
+
"net/http"
|
| 6 |
+
"net/http/httptest"
|
| 7 |
+
"testing"
|
| 8 |
+
|
| 9 |
+
"github.com/go-chi/chi/v5"
|
| 10 |
+
)
|
| 11 |
+
|
| 12 |
+
func TestContentEncodingMiddleware(t *testing.T) {
|
| 13 |
+
t.Parallel()
|
| 14 |
+
|
| 15 |
+
// support for:
|
| 16 |
+
// Content-Encoding: gzip
|
| 17 |
+
// Content-Encoding: deflate
|
| 18 |
+
// Content-Encoding: gzip, deflate
|
| 19 |
+
// Content-Encoding: deflate, gzip
|
| 20 |
+
middleware := AllowContentEncoding("deflate", "gzip")
|
| 21 |
+
|
| 22 |
+
tests := []struct {
|
| 23 |
+
name string
|
| 24 |
+
encodings []string
|
| 25 |
+
expectedStatus int
|
| 26 |
+
}{
|
| 27 |
+
{
|
| 28 |
+
name: "Support no encoding",
|
| 29 |
+
encodings: []string{},
|
| 30 |
+
expectedStatus: 200,
|
| 31 |
+
},
|
| 32 |
+
{
|
| 33 |
+
name: "Support gzip encoding",
|
| 34 |
+
encodings: []string{"gzip"},
|
| 35 |
+
expectedStatus: 200,
|
| 36 |
+
},
|
| 37 |
+
{
|
| 38 |
+
name: "No support for br encoding",
|
| 39 |
+
encodings: []string{"br"},
|
| 40 |
+
expectedStatus: 415,
|
| 41 |
+
},
|
| 42 |
+
{
|
| 43 |
+
name: "Support for gzip and deflate encoding",
|
| 44 |
+
encodings: []string{"gzip", "deflate"},
|
| 45 |
+
expectedStatus: 200,
|
| 46 |
+
},
|
| 47 |
+
{
|
| 48 |
+
name: "Support for deflate and gzip encoding",
|
| 49 |
+
encodings: []string{"deflate", "gzip"},
|
| 50 |
+
expectedStatus: 200,
|
| 51 |
+
},
|
| 52 |
+
{
|
| 53 |
+
name: "No support for deflate and br encoding",
|
| 54 |
+
encodings: []string{"deflate", "br"},
|
| 55 |
+
expectedStatus: 415,
|
| 56 |
+
},
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
for _, tt := range tests {
|
| 60 |
+
var tt = tt
|
| 61 |
+
t.Run(tt.name, func(t *testing.T) {
|
| 62 |
+
t.Parallel()
|
| 63 |
+
|
| 64 |
+
body := []byte("This is my content. There are many like this but this one is mine")
|
| 65 |
+
r := httptest.NewRequest("POST", "/", bytes.NewReader(body))
|
| 66 |
+
for _, encoding := range tt.encodings {
|
| 67 |
+
r.Header.Set("Content-Encoding", encoding)
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
w := httptest.NewRecorder()
|
| 71 |
+
router := chi.NewRouter()
|
| 72 |
+
router.Use(middleware)
|
| 73 |
+
router.Post("/", func(w http.ResponseWriter, r *http.Request) {})
|
| 74 |
+
|
| 75 |
+
router.ServeHTTP(w, r)
|
| 76 |
+
res := w.Result()
|
| 77 |
+
if res.StatusCode != tt.expectedStatus {
|
| 78 |
+
t.Errorf("response is incorrect, got %d, want %d", w.Code, tt.expectedStatus)
|
| 79 |
+
}
|
| 80 |
+
})
|
| 81 |
+
}
|
| 82 |
+
}
|
tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/tests/frozen/middleware/content_type_test.go
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
package middleware
|
| 2 |
+
|
| 3 |
+
import (
|
| 4 |
+
"bytes"
|
| 5 |
+
"net/http"
|
| 6 |
+
"net/http/httptest"
|
| 7 |
+
"testing"
|
| 8 |
+
|
| 9 |
+
"github.com/go-chi/chi/v5"
|
| 10 |
+
)
|
| 11 |
+
|
| 12 |
+
func TestContentType(t *testing.T) {
|
| 13 |
+
t.Parallel()
|
| 14 |
+
|
| 15 |
+
var tests = []struct {
|
| 16 |
+
name string
|
| 17 |
+
inputValue string
|
| 18 |
+
allowedContentTypes []string
|
| 19 |
+
want int
|
| 20 |
+
}{
|
| 21 |
+
{
|
| 22 |
+
"should accept requests with a matching content type",
|
| 23 |
+
"application/json; charset=UTF-8",
|
| 24 |
+
[]string{"application/json"},
|
| 25 |
+
http.StatusOK,
|
| 26 |
+
},
|
| 27 |
+
{
|
| 28 |
+
"should accept requests with a matching content type no charset",
|
| 29 |
+
"application/json",
|
| 30 |
+
[]string{"application/json"},
|
| 31 |
+
http.StatusOK,
|
| 32 |
+
},
|
| 33 |
+
{
|
| 34 |
+
"should accept requests with a matching content-type with extra values",
|
| 35 |
+
"application/json; foo=bar; charset=UTF-8; spam=eggs",
|
| 36 |
+
[]string{"application/json"},
|
| 37 |
+
http.StatusOK,
|
| 38 |
+
},
|
| 39 |
+
{
|
| 40 |
+
"should accept requests with a matching content type when multiple content types are supported",
|
| 41 |
+
"text/xml; charset=UTF-8",
|
| 42 |
+
[]string{"application/json", "text/xml"},
|
| 43 |
+
http.StatusOK,
|
| 44 |
+
},
|
| 45 |
+
{
|
| 46 |
+
"should not accept requests with a mismatching content type",
|
| 47 |
+
"text/plain; charset=latin-1",
|
| 48 |
+
[]string{"application/json"},
|
| 49 |
+
http.StatusUnsupportedMediaType,
|
| 50 |
+
},
|
| 51 |
+
{
|
| 52 |
+
"should not accept requests with a mismatching content type even if multiple content types are allowed",
|
| 53 |
+
"text/plain; charset=Latin-1",
|
| 54 |
+
[]string{"application/json", "text/xml"},
|
| 55 |
+
http.StatusUnsupportedMediaType,
|
| 56 |
+
},
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
for _, tt := range tests {
|
| 60 |
+
var tt = tt
|
| 61 |
+
t.Run(tt.name, func(t *testing.T) {
|
| 62 |
+
t.Parallel()
|
| 63 |
+
|
| 64 |
+
recorder := httptest.NewRecorder()
|
| 65 |
+
|
| 66 |
+
r := chi.NewRouter()
|
| 67 |
+
r.Use(AllowContentType(tt.allowedContentTypes...))
|
| 68 |
+
r.Post("/", func(w http.ResponseWriter, r *http.Request) {})
|
| 69 |
+
|
| 70 |
+
body := []byte("This is my content. There are many like this but this one is mine")
|
| 71 |
+
req := httptest.NewRequest("POST", "/", bytes.NewReader(body))
|
| 72 |
+
req.Header.Set("Content-Type", tt.inputValue)
|
| 73 |
+
|
| 74 |
+
r.ServeHTTP(recorder, req)
|
| 75 |
+
res := recorder.Result()
|
| 76 |
+
|
| 77 |
+
if res.StatusCode != tt.want {
|
| 78 |
+
t.Errorf("response is incorrect, got %d, want %d", recorder.Code, tt.want)
|
| 79 |
+
}
|
| 80 |
+
})
|
| 81 |
+
}
|
| 82 |
+
}
|
tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/tests/frozen/middleware/get_head_test.go
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
package middleware
|
| 2 |
+
|
| 3 |
+
import (
|
| 4 |
+
"net/http"
|
| 5 |
+
"net/http/httptest"
|
| 6 |
+
"testing"
|
| 7 |
+
|
| 8 |
+
"github.com/go-chi/chi/v5"
|
| 9 |
+
)
|
| 10 |
+
|
| 11 |
+
func TestGetHead(t *testing.T) {
|
| 12 |
+
r := chi.NewRouter()
|
| 13 |
+
r.Use(GetHead)
|
| 14 |
+
r.Get("/hi", func(w http.ResponseWriter, r *http.Request) {
|
| 15 |
+
w.Header().Set("X-Test", "yes")
|
| 16 |
+
w.Write([]byte("bye"))
|
| 17 |
+
})
|
| 18 |
+
r.Route("/articles", func(r chi.Router) {
|
| 19 |
+
r.Get("/{id}", func(w http.ResponseWriter, r *http.Request) {
|
| 20 |
+
id := chi.URLParam(r, "id")
|
| 21 |
+
w.Header().Set("X-Article", id)
|
| 22 |
+
w.Write([]byte("article:" + id))
|
| 23 |
+
})
|
| 24 |
+
})
|
| 25 |
+
r.Route("/users", func(r chi.Router) {
|
| 26 |
+
r.Head("/{id}", func(w http.ResponseWriter, r *http.Request) {
|
| 27 |
+
w.Header().Set("X-User", "-")
|
| 28 |
+
w.Write([]byte("user"))
|
| 29 |
+
})
|
| 30 |
+
r.Get("/{id}", func(w http.ResponseWriter, r *http.Request) {
|
| 31 |
+
id := chi.URLParam(r, "id")
|
| 32 |
+
w.Header().Set("X-User", id)
|
| 33 |
+
w.Write([]byte("user:" + id))
|
| 34 |
+
})
|
| 35 |
+
})
|
| 36 |
+
|
| 37 |
+
ts := httptest.NewServer(r)
|
| 38 |
+
defer ts.Close()
|
| 39 |
+
|
| 40 |
+
if _, body := testRequest(t, ts, "GET", "/hi", nil); body != "bye" {
|
| 41 |
+
t.Fatal(body)
|
| 42 |
+
}
|
| 43 |
+
if req, body := testRequest(t, ts, "HEAD", "/hi", nil); body != "" || req.Header.Get("X-Test") != "yes" {
|
| 44 |
+
t.Fatal(body)
|
| 45 |
+
}
|
| 46 |
+
if _, body := testRequest(t, ts, "GET", "/", nil); body != "404 page not found\n" {
|
| 47 |
+
t.Fatal(body)
|
| 48 |
+
}
|
| 49 |
+
if req, body := testRequest(t, ts, "HEAD", "/", nil); body != "" || req.StatusCode != 404 {
|
| 50 |
+
t.Fatal(body)
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
if _, body := testRequest(t, ts, "GET", "/articles/5", nil); body != "article:5" {
|
| 54 |
+
t.Fatal(body)
|
| 55 |
+
}
|
| 56 |
+
if req, body := testRequest(t, ts, "HEAD", "/articles/5", nil); body != "" || req.Header.Get("X-Article") != "5" {
|
| 57 |
+
t.Fatalf("expecting X-Article header '5' but got '%s'", req.Header.Get("X-Article"))
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
if _, body := testRequest(t, ts, "GET", "/users/1", nil); body != "user:1" {
|
| 61 |
+
t.Fatal(body)
|
| 62 |
+
}
|
| 63 |
+
if req, body := testRequest(t, ts, "HEAD", "/users/1", nil); body != "" || req.Header.Get("X-User") != "-" {
|
| 64 |
+
t.Fatalf("expecting X-User header '-' but got '%s'", req.Header.Get("X-User"))
|
| 65 |
+
}
|
| 66 |
+
}
|
tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/tests/frozen/middleware/logger_test.go
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
package middleware
|
| 2 |
+
|
| 3 |
+
import (
|
| 4 |
+
"bufio"
|
| 5 |
+
"bytes"
|
| 6 |
+
"net"
|
| 7 |
+
"net/http"
|
| 8 |
+
"net/http/httptest"
|
| 9 |
+
"testing"
|
| 10 |
+
"time"
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
type testLoggerWriter struct {
|
| 14 |
+
*httptest.ResponseRecorder
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
func (cw testLoggerWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
|
| 18 |
+
return nil, nil, nil
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
func TestRequestLogger(t *testing.T) {
|
| 22 |
+
testHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
| 23 |
+
_, ok := w.(http.Hijacker)
|
| 24 |
+
if !ok {
|
| 25 |
+
t.Errorf("http.Hijacker is unavailable on the writer. add the interface methods.")
|
| 26 |
+
}
|
| 27 |
+
})
|
| 28 |
+
|
| 29 |
+
r := httptest.NewRequest("GET", "/", nil)
|
| 30 |
+
w := testLoggerWriter{
|
| 31 |
+
ResponseRecorder: httptest.NewRecorder(),
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
handler := DefaultLogger(testHandler)
|
| 35 |
+
handler.ServeHTTP(w, r)
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
func TestRequestLoggerReadFrom(t *testing.T) {
|
| 39 |
+
data := []byte("file data")
|
| 40 |
+
testHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
| 41 |
+
http.ServeContent(w, r, "file", time.Time{}, bytes.NewReader(data))
|
| 42 |
+
})
|
| 43 |
+
|
| 44 |
+
r := httptest.NewRequest("GET", "/", nil)
|
| 45 |
+
w := httptest.NewRecorder()
|
| 46 |
+
|
| 47 |
+
handler := DefaultLogger(testHandler)
|
| 48 |
+
handler.ServeHTTP(w, r)
|
| 49 |
+
|
| 50 |
+
assertEqual(t, data, w.Body.Bytes())
|
| 51 |
+
}
|
tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/tests/frozen/middleware/middleware_test.go
ADDED
|
@@ -0,0 +1,154 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
package middleware
|
| 2 |
+
|
| 3 |
+
import (
|
| 4 |
+
"crypto/tls"
|
| 5 |
+
"io"
|
| 6 |
+
"net/http"
|
| 7 |
+
"net/http/httptest"
|
| 8 |
+
"path"
|
| 9 |
+
"reflect"
|
| 10 |
+
"runtime"
|
| 11 |
+
"testing"
|
| 12 |
+
"time"
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
var testdataDir string
|
| 16 |
+
|
| 17 |
+
func init() {
|
| 18 |
+
_, filename, _, _ := runtime.Caller(0)
|
| 19 |
+
testdataDir = path.Join(path.Dir(filename), "/../testdata")
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
func TestWrapWriterHTTP2(t *testing.T) {
|
| 23 |
+
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
| 24 |
+
if r.Proto != "HTTP/2.0" {
|
| 25 |
+
t.Fatalf("request proto should be HTTP/2.0 but was %s", r.Proto)
|
| 26 |
+
}
|
| 27 |
+
_, fl := w.(http.Flusher)
|
| 28 |
+
if !fl {
|
| 29 |
+
t.Fatal("request should have been a http.Flusher")
|
| 30 |
+
}
|
| 31 |
+
_, hj := w.(http.Hijacker)
|
| 32 |
+
if hj {
|
| 33 |
+
t.Fatal("request should not have been a http.Hijacker")
|
| 34 |
+
}
|
| 35 |
+
_, rf := w.(io.ReaderFrom)
|
| 36 |
+
if rf {
|
| 37 |
+
t.Fatal("request should not have been an io.ReaderFrom")
|
| 38 |
+
}
|
| 39 |
+
_, ps := w.(http.Pusher)
|
| 40 |
+
if !ps {
|
| 41 |
+
t.Fatal("request should have been a http.Pusher")
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
w.Write([]byte("OK"))
|
| 45 |
+
})
|
| 46 |
+
|
| 47 |
+
wmw := func(next http.Handler) http.Handler {
|
| 48 |
+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
| 49 |
+
next.ServeHTTP(NewWrapResponseWriter(w, r.ProtoMajor), r)
|
| 50 |
+
})
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
server := http.Server{
|
| 54 |
+
Addr: ":7072",
|
| 55 |
+
Handler: wmw(handler),
|
| 56 |
+
}
|
| 57 |
+
// By serving over TLS, we get HTTP2 requests
|
| 58 |
+
go server.ListenAndServeTLS(testdataDir+"/cert.pem", testdataDir+"/key.pem")
|
| 59 |
+
defer server.Close()
|
| 60 |
+
// We need the server to start before making the request
|
| 61 |
+
time.Sleep(100 * time.Millisecond)
|
| 62 |
+
|
| 63 |
+
client := &http.Client{
|
| 64 |
+
Transport: &http.Transport{
|
| 65 |
+
TLSClientConfig: &tls.Config{
|
| 66 |
+
// The certificates we are using are self signed
|
| 67 |
+
InsecureSkipVerify: true,
|
| 68 |
+
},
|
| 69 |
+
ForceAttemptHTTP2: true,
|
| 70 |
+
},
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
resp, err := client.Get("https://localhost:7072")
|
| 74 |
+
if err != nil {
|
| 75 |
+
t.Fatalf("could not get server: %v", err)
|
| 76 |
+
}
|
| 77 |
+
if resp.StatusCode != 200 {
|
| 78 |
+
t.Fatalf("non 200 response: %v", resp.StatusCode)
|
| 79 |
+
}
|
| 80 |
+
}
|
| 81 |
+
|
| 82 |
+
func testRequest(t *testing.T, ts *httptest.Server, method, path string, body io.Reader) (*http.Response, string) {
|
| 83 |
+
req, err := http.NewRequest(method, ts.URL+path, body)
|
| 84 |
+
if err != nil {
|
| 85 |
+
t.Fatal(err)
|
| 86 |
+
return nil, ""
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
resp, err := http.DefaultClient.Do(req)
|
| 90 |
+
if err != nil {
|
| 91 |
+
t.Fatal(err)
|
| 92 |
+
return nil, ""
|
| 93 |
+
}
|
| 94 |
+
|
| 95 |
+
respBody, err := io.ReadAll(resp.Body)
|
| 96 |
+
if err != nil {
|
| 97 |
+
t.Fatal(err)
|
| 98 |
+
return nil, ""
|
| 99 |
+
}
|
| 100 |
+
defer resp.Body.Close()
|
| 101 |
+
|
| 102 |
+
return resp, string(respBody)
|
| 103 |
+
}
|
| 104 |
+
|
| 105 |
+
func testRequestNoRedirect(t *testing.T, ts *httptest.Server, method, path string, body io.Reader) (*http.Response, string) {
|
| 106 |
+
req, err := http.NewRequest(method, ts.URL+path, body)
|
| 107 |
+
if err != nil {
|
| 108 |
+
t.Fatal(err)
|
| 109 |
+
return nil, ""
|
| 110 |
+
}
|
| 111 |
+
|
| 112 |
+
// http client that doesn't redirect
|
| 113 |
+
httpClient := &http.Client{
|
| 114 |
+
CheckRedirect: func(req *http.Request, via []*http.Request) error {
|
| 115 |
+
return http.ErrUseLastResponse
|
| 116 |
+
},
|
| 117 |
+
}
|
| 118 |
+
|
| 119 |
+
resp, err := httpClient.Do(req)
|
| 120 |
+
if err != nil {
|
| 121 |
+
t.Fatal(err)
|
| 122 |
+
return nil, ""
|
| 123 |
+
}
|
| 124 |
+
|
| 125 |
+
respBody, err := io.ReadAll(resp.Body)
|
| 126 |
+
if err != nil {
|
| 127 |
+
t.Fatal(err)
|
| 128 |
+
return nil, ""
|
| 129 |
+
}
|
| 130 |
+
defer resp.Body.Close()
|
| 131 |
+
|
| 132 |
+
return resp, string(respBody)
|
| 133 |
+
}
|
| 134 |
+
|
| 135 |
+
func assertNoError(t *testing.T, err error) {
|
| 136 |
+
t.Helper()
|
| 137 |
+
if err != nil {
|
| 138 |
+
t.Fatalf("expecting no error")
|
| 139 |
+
}
|
| 140 |
+
}
|
| 141 |
+
|
| 142 |
+
func assertError(t *testing.T, err error) {
|
| 143 |
+
t.Helper()
|
| 144 |
+
if err == nil {
|
| 145 |
+
t.Fatalf("expecting error")
|
| 146 |
+
}
|
| 147 |
+
}
|
| 148 |
+
|
| 149 |
+
func assertEqual(t *testing.T, a, b interface{}) {
|
| 150 |
+
t.Helper()
|
| 151 |
+
if !reflect.DeepEqual(a, b) {
|
| 152 |
+
t.Fatalf("expecting values to be equal but got: '%v' and '%v'", a, b)
|
| 153 |
+
}
|
| 154 |
+
}
|
tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/tests/frozen/middleware/realip_test.go
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
package middleware
|
| 2 |
+
|
| 3 |
+
import (
|
| 4 |
+
"net/http"
|
| 5 |
+
"net/http/httptest"
|
| 6 |
+
"testing"
|
| 7 |
+
|
| 8 |
+
"github.com/go-chi/chi/v5"
|
| 9 |
+
)
|
| 10 |
+
|
| 11 |
+
func TestXRealIP(t *testing.T) {
|
| 12 |
+
req, _ := http.NewRequest("GET", "/", nil)
|
| 13 |
+
req.Header.Add("X-Real-IP", "100.100.100.100")
|
| 14 |
+
w := httptest.NewRecorder()
|
| 15 |
+
|
| 16 |
+
r := chi.NewRouter()
|
| 17 |
+
r.Use(RealIP)
|
| 18 |
+
|
| 19 |
+
realIP := ""
|
| 20 |
+
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
| 21 |
+
realIP = r.RemoteAddr
|
| 22 |
+
w.Write([]byte("Hello World"))
|
| 23 |
+
})
|
| 24 |
+
r.ServeHTTP(w, req)
|
| 25 |
+
|
| 26 |
+
if w.Code != 200 {
|
| 27 |
+
t.Fatal("Response Code should be 200")
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
if realIP != "100.100.100.100" {
|
| 31 |
+
t.Fatal("Test get real IP error.")
|
| 32 |
+
}
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
func TestXForwardForIP(t *testing.T) {
|
| 36 |
+
xForwardedForIPs := []string{
|
| 37 |
+
"100.100.100.100",
|
| 38 |
+
"100.100.100.100, 200.200.200.200",
|
| 39 |
+
"100.100.100.100,200.200.200.200",
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
r := chi.NewRouter()
|
| 43 |
+
r.Use(RealIP)
|
| 44 |
+
|
| 45 |
+
for _, v := range xForwardedForIPs {
|
| 46 |
+
req, _ := http.NewRequest("GET", "/", nil)
|
| 47 |
+
req.Header.Add("X-Forwarded-For", v)
|
| 48 |
+
|
| 49 |
+
w := httptest.NewRecorder()
|
| 50 |
+
|
| 51 |
+
realIP := ""
|
| 52 |
+
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
| 53 |
+
realIP = r.RemoteAddr
|
| 54 |
+
w.Write([]byte("Hello World"))
|
| 55 |
+
})
|
| 56 |
+
r.ServeHTTP(w, req)
|
| 57 |
+
|
| 58 |
+
if w.Code != 200 {
|
| 59 |
+
t.Fatal("Response Code should be 200")
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
if realIP != "100.100.100.100" {
|
| 63 |
+
t.Fatal("Test get real IP error.")
|
| 64 |
+
}
|
| 65 |
+
}
|
| 66 |
+
}
|
| 67 |
+
|
| 68 |
+
func TestXForwardForXRealIPPrecedence(t *testing.T) {
|
| 69 |
+
req, _ := http.NewRequest("GET", "/", nil)
|
| 70 |
+
req.Header.Add("X-Forwarded-For", "0.0.0.0")
|
| 71 |
+
req.Header.Add("X-Real-IP", "100.100.100.100")
|
| 72 |
+
w := httptest.NewRecorder()
|
| 73 |
+
|
| 74 |
+
r := chi.NewRouter()
|
| 75 |
+
r.Use(RealIP)
|
| 76 |
+
|
| 77 |
+
realIP := ""
|
| 78 |
+
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
| 79 |
+
realIP = r.RemoteAddr
|
| 80 |
+
w.Write([]byte("Hello World"))
|
| 81 |
+
})
|
| 82 |
+
r.ServeHTTP(w, req)
|
| 83 |
+
|
| 84 |
+
if w.Code != 200 {
|
| 85 |
+
t.Fatal("Response Code should be 200")
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
if realIP != "100.100.100.100" {
|
| 89 |
+
t.Fatal("Test get real IP precedence error.")
|
| 90 |
+
}
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
func TestInvalidIP(t *testing.T) {
|
| 94 |
+
req, _ := http.NewRequest("GET", "/", nil)
|
| 95 |
+
req.Header.Add("X-Real-IP", "100.100.100.1000")
|
| 96 |
+
w := httptest.NewRecorder()
|
| 97 |
+
|
| 98 |
+
r := chi.NewRouter()
|
| 99 |
+
r.Use(RealIP)
|
| 100 |
+
|
| 101 |
+
realIP := ""
|
| 102 |
+
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
| 103 |
+
realIP = r.RemoteAddr
|
| 104 |
+
w.Write([]byte("Hello World"))
|
| 105 |
+
})
|
| 106 |
+
r.ServeHTTP(w, req)
|
| 107 |
+
|
| 108 |
+
if w.Code != 200 {
|
| 109 |
+
t.Fatal("Response Code should be 200")
|
| 110 |
+
}
|
| 111 |
+
|
| 112 |
+
if realIP != "" {
|
| 113 |
+
t.Fatal("Invalid IP used.")
|
| 114 |
+
}
|
| 115 |
+
}
|
tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/tests/frozen/middleware/recoverer_test.go
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
package middleware
|
| 2 |
+
|
| 3 |
+
import (
|
| 4 |
+
"bytes"
|
| 5 |
+
"net/http"
|
| 6 |
+
"net/http/httptest"
|
| 7 |
+
"strings"
|
| 8 |
+
"testing"
|
| 9 |
+
|
| 10 |
+
"github.com/go-chi/chi/v5"
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
func panickingHandler(http.ResponseWriter, *http.Request) { panic("foo") }
|
| 14 |
+
|
| 15 |
+
func TestRecoverer(t *testing.T) {
|
| 16 |
+
r := chi.NewRouter()
|
| 17 |
+
|
| 18 |
+
oldRecovererErrorWriter := recovererErrorWriter
|
| 19 |
+
defer func() { recovererErrorWriter = oldRecovererErrorWriter }()
|
| 20 |
+
buf := &bytes.Buffer{}
|
| 21 |
+
recovererErrorWriter = buf
|
| 22 |
+
|
| 23 |
+
r.Use(Recoverer)
|
| 24 |
+
r.Get("/", panickingHandler)
|
| 25 |
+
|
| 26 |
+
ts := httptest.NewServer(r)
|
| 27 |
+
defer ts.Close()
|
| 28 |
+
|
| 29 |
+
res, _ := testRequest(t, ts, "GET", "/", nil)
|
| 30 |
+
assertEqual(t, res.StatusCode, http.StatusInternalServerError)
|
| 31 |
+
|
| 32 |
+
lines := strings.Split(buf.String(), "\n")
|
| 33 |
+
for _, line := range lines {
|
| 34 |
+
if strings.HasPrefix(strings.TrimSpace(line), "->") {
|
| 35 |
+
if !strings.Contains(line, "panickingHandler") {
|
| 36 |
+
t.Fatalf("First func call line should refer to panickingHandler, but actual line:\n%v\n", line)
|
| 37 |
+
}
|
| 38 |
+
return
|
| 39 |
+
}
|
| 40 |
+
}
|
| 41 |
+
t.Fatal("First func call line should start with ->.")
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
func TestRecovererAbortHandler(t *testing.T) {
|
| 45 |
+
defer func() {
|
| 46 |
+
rcv := recover()
|
| 47 |
+
if rcv != http.ErrAbortHandler {
|
| 48 |
+
t.Fatalf("http.ErrAbortHandler should not be recovered")
|
| 49 |
+
}
|
| 50 |
+
}()
|
| 51 |
+
|
| 52 |
+
w := httptest.NewRecorder()
|
| 53 |
+
|
| 54 |
+
r := chi.NewRouter()
|
| 55 |
+
r.Use(Recoverer)
|
| 56 |
+
|
| 57 |
+
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
| 58 |
+
panic(http.ErrAbortHandler)
|
| 59 |
+
})
|
| 60 |
+
|
| 61 |
+
req, err := http.NewRequest("GET", "/", nil)
|
| 62 |
+
if err != nil {
|
| 63 |
+
t.Fatal(err)
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
r.ServeHTTP(w, req)
|
| 67 |
+
}
|
tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/tests/frozen/middleware/request_id_test.go
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
package middleware
|
| 2 |
+
|
| 3 |
+
import (
|
| 4 |
+
"fmt"
|
| 5 |
+
"net/http"
|
| 6 |
+
"net/http/httptest"
|
| 7 |
+
"testing"
|
| 8 |
+
|
| 9 |
+
"github.com/go-chi/chi/v5"
|
| 10 |
+
)
|
| 11 |
+
|
| 12 |
+
func maintainDefaultRequestID() func() {
|
| 13 |
+
original := RequestIDHeader
|
| 14 |
+
|
| 15 |
+
return func() {
|
| 16 |
+
RequestIDHeader = original
|
| 17 |
+
}
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
func TestRequestID(t *testing.T) {
|
| 21 |
+
tests := map[string]struct {
|
| 22 |
+
requestIDHeader string
|
| 23 |
+
request func() *http.Request
|
| 24 |
+
expectedResponse string
|
| 25 |
+
}{
|
| 26 |
+
"Retrieves Request Id from default header": {
|
| 27 |
+
"X-Request-Id",
|
| 28 |
+
func() *http.Request {
|
| 29 |
+
req, _ := http.NewRequest("GET", "/", nil)
|
| 30 |
+
req.Header.Add("X-Request-Id", "req-123456")
|
| 31 |
+
|
| 32 |
+
return req
|
| 33 |
+
},
|
| 34 |
+
"RequestID: req-123456",
|
| 35 |
+
},
|
| 36 |
+
"Retrieves Request Id from custom header": {
|
| 37 |
+
"X-Trace-Id",
|
| 38 |
+
func() *http.Request {
|
| 39 |
+
req, _ := http.NewRequest("GET", "/", nil)
|
| 40 |
+
req.Header.Add("X-Trace-Id", "trace:abc123")
|
| 41 |
+
|
| 42 |
+
return req
|
| 43 |
+
},
|
| 44 |
+
"RequestID: trace:abc123",
|
| 45 |
+
},
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
defer maintainDefaultRequestID()()
|
| 49 |
+
|
| 50 |
+
for _, test := range tests {
|
| 51 |
+
w := httptest.NewRecorder()
|
| 52 |
+
|
| 53 |
+
r := chi.NewRouter()
|
| 54 |
+
|
| 55 |
+
RequestIDHeader = test.requestIDHeader
|
| 56 |
+
|
| 57 |
+
r.Use(RequestID)
|
| 58 |
+
|
| 59 |
+
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
| 60 |
+
requestID := GetReqID(r.Context())
|
| 61 |
+
response := fmt.Sprintf("RequestID: %s", requestID)
|
| 62 |
+
|
| 63 |
+
w.Write([]byte(response))
|
| 64 |
+
})
|
| 65 |
+
r.ServeHTTP(w, test.request())
|
| 66 |
+
|
| 67 |
+
if w.Body.String() != test.expectedResponse {
|
| 68 |
+
t.Fatalf("RequestID was not the expected value")
|
| 69 |
+
}
|
| 70 |
+
}
|
| 71 |
+
}
|
tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/tests/frozen/middleware/strip_test.go
ADDED
|
@@ -0,0 +1,273 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
package middleware
|
| 2 |
+
|
| 3 |
+
import (
|
| 4 |
+
"net/http"
|
| 5 |
+
"net/http/httptest"
|
| 6 |
+
"net/url"
|
| 7 |
+
"testing"
|
| 8 |
+
|
| 9 |
+
"github.com/go-chi/chi/v5"
|
| 10 |
+
)
|
| 11 |
+
|
| 12 |
+
func TestStripSlashes(t *testing.T) {
|
| 13 |
+
r := chi.NewRouter()
|
| 14 |
+
|
| 15 |
+
// This middleware must be mounted at the top level of the router, not at the end-handler
|
| 16 |
+
// because then it'll be too late and will end up in a 404
|
| 17 |
+
r.Use(StripSlashes)
|
| 18 |
+
|
| 19 |
+
r.NotFound(func(w http.ResponseWriter, r *http.Request) {
|
| 20 |
+
w.WriteHeader(404)
|
| 21 |
+
w.Write([]byte("nothing here"))
|
| 22 |
+
})
|
| 23 |
+
|
| 24 |
+
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
| 25 |
+
w.Write([]byte("root"))
|
| 26 |
+
})
|
| 27 |
+
|
| 28 |
+
r.Route("/accounts/{accountID}", func(r chi.Router) {
|
| 29 |
+
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
| 30 |
+
accountID := chi.URLParam(r, "accountID")
|
| 31 |
+
w.Write([]byte(accountID))
|
| 32 |
+
})
|
| 33 |
+
})
|
| 34 |
+
|
| 35 |
+
ts := httptest.NewServer(r)
|
| 36 |
+
defer ts.Close()
|
| 37 |
+
|
| 38 |
+
if _, resp := testRequest(t, ts, "GET", "/", nil); resp != "root" {
|
| 39 |
+
t.Fatal(resp)
|
| 40 |
+
}
|
| 41 |
+
if _, resp := testRequest(t, ts, "GET", "//", nil); resp != "root" {
|
| 42 |
+
t.Fatal(resp)
|
| 43 |
+
}
|
| 44 |
+
if _, resp := testRequest(t, ts, "GET", "/accounts/admin", nil); resp != "admin" {
|
| 45 |
+
t.Fatal(resp)
|
| 46 |
+
}
|
| 47 |
+
if _, resp := testRequest(t, ts, "GET", "/accounts/admin/", nil); resp != "admin" {
|
| 48 |
+
t.Fatal(resp)
|
| 49 |
+
}
|
| 50 |
+
if _, resp := testRequest(t, ts, "GET", "/nothing-here", nil); resp != "nothing here" {
|
| 51 |
+
t.Fatal(resp)
|
| 52 |
+
}
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
func TestStripSlashesInRoute(t *testing.T) {
|
| 56 |
+
r := chi.NewRouter()
|
| 57 |
+
|
| 58 |
+
r.NotFound(func(w http.ResponseWriter, r *http.Request) {
|
| 59 |
+
w.WriteHeader(404)
|
| 60 |
+
w.Write([]byte("nothing here"))
|
| 61 |
+
})
|
| 62 |
+
|
| 63 |
+
r.Get("/hi", func(w http.ResponseWriter, r *http.Request) {
|
| 64 |
+
w.Write([]byte("hi"))
|
| 65 |
+
})
|
| 66 |
+
|
| 67 |
+
r.Route("/accounts/{accountID}", func(r chi.Router) {
|
| 68 |
+
r.Use(StripSlashes)
|
| 69 |
+
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
| 70 |
+
w.Write([]byte("accounts index"))
|
| 71 |
+
})
|
| 72 |
+
r.Get("/query", func(w http.ResponseWriter, r *http.Request) {
|
| 73 |
+
accountID := chi.URLParam(r, "accountID")
|
| 74 |
+
w.Write([]byte(accountID))
|
| 75 |
+
})
|
| 76 |
+
})
|
| 77 |
+
|
| 78 |
+
ts := httptest.NewServer(r)
|
| 79 |
+
defer ts.Close()
|
| 80 |
+
|
| 81 |
+
if _, resp := testRequest(t, ts, "GET", "/hi", nil); resp != "hi" {
|
| 82 |
+
t.Fatal(resp)
|
| 83 |
+
}
|
| 84 |
+
if _, resp := testRequest(t, ts, "GET", "/hi/", nil); resp != "nothing here" {
|
| 85 |
+
t.Fatal(resp)
|
| 86 |
+
}
|
| 87 |
+
if _, resp := testRequest(t, ts, "GET", "/accounts/admin", nil); resp != "accounts index" {
|
| 88 |
+
t.Fatal(resp)
|
| 89 |
+
}
|
| 90 |
+
if _, resp := testRequest(t, ts, "GET", "/accounts/admin/", nil); resp != "accounts index" {
|
| 91 |
+
t.Fatal(resp)
|
| 92 |
+
}
|
| 93 |
+
if _, resp := testRequest(t, ts, "GET", "/accounts/admin/query", nil); resp != "admin" {
|
| 94 |
+
t.Fatal(resp)
|
| 95 |
+
}
|
| 96 |
+
if _, resp := testRequest(t, ts, "GET", "/accounts/admin/query/", nil); resp != "admin" {
|
| 97 |
+
t.Fatal(resp)
|
| 98 |
+
}
|
| 99 |
+
}
|
| 100 |
+
|
| 101 |
+
func TestRedirectSlashes(t *testing.T) {
|
| 102 |
+
r := chi.NewRouter()
|
| 103 |
+
|
| 104 |
+
// This middleware must be mounted at the top level of the router, not at the end-handler
|
| 105 |
+
// because then it'll be too late and will end up in a 404
|
| 106 |
+
r.Use(RedirectSlashes)
|
| 107 |
+
|
| 108 |
+
r.NotFound(func(w http.ResponseWriter, r *http.Request) {
|
| 109 |
+
w.WriteHeader(404)
|
| 110 |
+
w.Write([]byte("nothing here"))
|
| 111 |
+
})
|
| 112 |
+
|
| 113 |
+
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
| 114 |
+
w.Write([]byte("root"))
|
| 115 |
+
})
|
| 116 |
+
|
| 117 |
+
r.Route("/accounts/{accountID}", func(r chi.Router) {
|
| 118 |
+
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
| 119 |
+
accountID := chi.URLParam(r, "accountID")
|
| 120 |
+
w.Write([]byte(accountID))
|
| 121 |
+
})
|
| 122 |
+
})
|
| 123 |
+
|
| 124 |
+
ts := httptest.NewServer(r)
|
| 125 |
+
defer ts.Close()
|
| 126 |
+
|
| 127 |
+
if resp, body := testRequest(t, ts, "GET", "/", nil); body != "root" || resp.StatusCode != 200 {
|
| 128 |
+
t.Fatal(body, resp.StatusCode)
|
| 129 |
+
}
|
| 130 |
+
|
| 131 |
+
// NOTE: the testRequest client will follow the redirection..
|
| 132 |
+
if resp, body := testRequest(t, ts, "GET", "//", nil); body != "root" || resp.StatusCode != 200 {
|
| 133 |
+
t.Fatal(body, resp.StatusCode)
|
| 134 |
+
}
|
| 135 |
+
|
| 136 |
+
if resp, body := testRequest(t, ts, "GET", "/accounts/admin", nil); body != "admin" || resp.StatusCode != 200 {
|
| 137 |
+
t.Fatal(body, resp.StatusCode)
|
| 138 |
+
}
|
| 139 |
+
|
| 140 |
+
// NOTE: the testRequest client will follow the redirection..
|
| 141 |
+
if resp, body := testRequest(t, ts, "GET", "/accounts/admin/", nil); body != "admin" || resp.StatusCode != 200 {
|
| 142 |
+
t.Fatal(body, resp.StatusCode)
|
| 143 |
+
}
|
| 144 |
+
|
| 145 |
+
if resp, body := testRequest(t, ts, "GET", "/nothing-here", nil); body != "nothing here" || resp.StatusCode != 404 {
|
| 146 |
+
t.Fatal(body, resp.StatusCode)
|
| 147 |
+
}
|
| 148 |
+
|
| 149 |
+
// Ensure redirect Location url is correct
|
| 150 |
+
{
|
| 151 |
+
resp, body := testRequestNoRedirect(t, ts, "GET", "/accounts/someuser/", nil)
|
| 152 |
+
if resp.StatusCode != 301 {
|
| 153 |
+
t.Fatal(body, resp.StatusCode)
|
| 154 |
+
}
|
| 155 |
+
location := resp.Header.Get("Location")
|
| 156 |
+
if location != "/accounts/someuser" {
|
| 157 |
+
t.Fatalf("invalid redirection, should be /accounts/someuser")
|
| 158 |
+
}
|
| 159 |
+
}
|
| 160 |
+
|
| 161 |
+
// Ensure query params are kept in tact upon redirecting a slash
|
| 162 |
+
{
|
| 163 |
+
resp, body := testRequestNoRedirect(t, ts, "GET", "/accounts/someuser/?a=1&b=2", nil)
|
| 164 |
+
if resp.StatusCode != 301 {
|
| 165 |
+
t.Fatal(body, resp.StatusCode)
|
| 166 |
+
}
|
| 167 |
+
location := resp.Header.Get("Location")
|
| 168 |
+
if location != "/accounts/someuser?a=1&b=2" {
|
| 169 |
+
t.Fatalf("invalid redirection, should be /accounts/someuser?a=1&b=2")
|
| 170 |
+
}
|
| 171 |
+
}
|
| 172 |
+
|
| 173 |
+
// Ensure that we don't redirect to 'evil.com', but rather to 'server.url/evil.com/'
|
| 174 |
+
{
|
| 175 |
+
paths := []string{"//evil.com/", "///evil.com/"}
|
| 176 |
+
|
| 177 |
+
for _, p := range paths {
|
| 178 |
+
resp, body := testRequest(t, ts, "GET", p, nil)
|
| 179 |
+
if u, err := url.Parse(ts.URL); err != nil && resp.Request.URL.Host != u.Host {
|
| 180 |
+
t.Fatalf("host should remain the same. got: %q, want: %q", resp.Request.URL.Host, ts.URL)
|
| 181 |
+
}
|
| 182 |
+
if body != "nothing here" || resp.StatusCode != 404 {
|
| 183 |
+
t.Fatal(body, resp.StatusCode)
|
| 184 |
+
}
|
| 185 |
+
}
|
| 186 |
+
}
|
| 187 |
+
|
| 188 |
+
// Ensure that we don't redirect to 'evil.com', but rather to 'server.url/evil.com/'
|
| 189 |
+
{
|
| 190 |
+
resp, body := testRequest(t, ts, "GET", "//evil.com/", nil)
|
| 191 |
+
if u, err := url.Parse(ts.URL); err != nil && resp.Request.URL.Host != u.Host {
|
| 192 |
+
t.Fatalf("host should remain the same. got: %q, want: %q", resp.Request.URL.Host, ts.URL)
|
| 193 |
+
}
|
| 194 |
+
if body != "nothing here" || resp.StatusCode != 404 {
|
| 195 |
+
t.Fatal(body, resp.StatusCode)
|
| 196 |
+
}
|
| 197 |
+
}
|
| 198 |
+
}
|
| 199 |
+
|
| 200 |
+
// This tests a http.Handler that is not chi.Router
|
| 201 |
+
// In these cases, the routeContext is nil
|
| 202 |
+
func TestStripSlashesWithNilContext(t *testing.T) {
|
| 203 |
+
r := http.NewServeMux()
|
| 204 |
+
|
| 205 |
+
r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
| 206 |
+
w.Write([]byte("root"))
|
| 207 |
+
})
|
| 208 |
+
|
| 209 |
+
r.HandleFunc("/accounts", func(w http.ResponseWriter, r *http.Request) {
|
| 210 |
+
w.Write([]byte("accounts"))
|
| 211 |
+
})
|
| 212 |
+
|
| 213 |
+
r.HandleFunc("/accounts/admin", func(w http.ResponseWriter, r *http.Request) {
|
| 214 |
+
w.Write([]byte("admin"))
|
| 215 |
+
})
|
| 216 |
+
|
| 217 |
+
ts := httptest.NewServer(StripSlashes(r))
|
| 218 |
+
defer ts.Close()
|
| 219 |
+
|
| 220 |
+
if _, resp := testRequest(t, ts, "GET", "/", nil); resp != "root" {
|
| 221 |
+
t.Fatal(resp)
|
| 222 |
+
}
|
| 223 |
+
if _, resp := testRequest(t, ts, "GET", "//", nil); resp != "root" {
|
| 224 |
+
t.Fatal(resp)
|
| 225 |
+
}
|
| 226 |
+
if _, resp := testRequest(t, ts, "GET", "/accounts", nil); resp != "accounts" {
|
| 227 |
+
t.Fatal(resp)
|
| 228 |
+
}
|
| 229 |
+
if _, resp := testRequest(t, ts, "GET", "/accounts/", nil); resp != "accounts" {
|
| 230 |
+
t.Fatal(resp)
|
| 231 |
+
}
|
| 232 |
+
if _, resp := testRequest(t, ts, "GET", "/accounts/admin", nil); resp != "admin" {
|
| 233 |
+
t.Fatal(resp)
|
| 234 |
+
}
|
| 235 |
+
if _, resp := testRequest(t, ts, "GET", "/accounts/admin/", nil); resp != "admin" {
|
| 236 |
+
t.Fatal(resp)
|
| 237 |
+
}
|
| 238 |
+
}
|
| 239 |
+
|
| 240 |
+
func TestStripPrefix(t *testing.T) {
|
| 241 |
+
r := chi.NewRouter()
|
| 242 |
+
|
| 243 |
+
r.Use(StripPrefix("/api"))
|
| 244 |
+
|
| 245 |
+
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
| 246 |
+
w.Write([]byte("api root"))
|
| 247 |
+
})
|
| 248 |
+
|
| 249 |
+
r.Get("/accounts", func(w http.ResponseWriter, r *http.Request) {
|
| 250 |
+
w.Write([]byte("api accounts"))
|
| 251 |
+
})
|
| 252 |
+
|
| 253 |
+
r.Get("/accounts/{accountID}", func(w http.ResponseWriter, r *http.Request) {
|
| 254 |
+
accountID := chi.URLParam(r, "accountID")
|
| 255 |
+
w.Write([]byte(accountID))
|
| 256 |
+
})
|
| 257 |
+
|
| 258 |
+
ts := httptest.NewServer(r)
|
| 259 |
+
defer ts.Close()
|
| 260 |
+
|
| 261 |
+
if _, resp := testRequest(t, ts, "GET", "/api/", nil); resp != "api root" {
|
| 262 |
+
t.Fatalf("got: %q, want: %q", resp, "api root")
|
| 263 |
+
}
|
| 264 |
+
if _, resp := testRequest(t, ts, "GET", "/api/accounts", nil); resp != "api accounts" {
|
| 265 |
+
t.Fatalf("got: %q, want: %q", resp, "api accounts")
|
| 266 |
+
}
|
| 267 |
+
if _, resp := testRequest(t, ts, "GET", "/api/accounts/admin", nil); resp != "admin" {
|
| 268 |
+
t.Fatalf("got: %q, want: %q", resp, "admin")
|
| 269 |
+
}
|
| 270 |
+
if _, resp := testRequest(t, ts, "GET", "/api-nope/", nil); resp != "404 page not found\n" {
|
| 271 |
+
t.Fatalf("got: %q, want: %q", resp, "404 page not found\n")
|
| 272 |
+
}
|
| 273 |
+
}
|
tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/tests/frozen/middleware/sunset_test.go
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
package middleware
|
| 2 |
+
|
| 3 |
+
import (
|
| 4 |
+
"net/http"
|
| 5 |
+
"net/http/httptest"
|
| 6 |
+
"testing"
|
| 7 |
+
"time"
|
| 8 |
+
|
| 9 |
+
"github.com/go-chi/chi/v5"
|
| 10 |
+
)
|
| 11 |
+
|
| 12 |
+
func TestSunset(t *testing.T) {
|
| 13 |
+
|
| 14 |
+
t.Run("Sunset without link", func(t *testing.T) {
|
| 15 |
+
req, _ := http.NewRequest("GET", "/", nil)
|
| 16 |
+
w := httptest.NewRecorder()
|
| 17 |
+
|
| 18 |
+
r := chi.NewRouter()
|
| 19 |
+
|
| 20 |
+
sunsetAt := time.Date(2025, 12, 24, 10, 20, 0, 0, time.UTC)
|
| 21 |
+
r.Use(Sunset(sunsetAt))
|
| 22 |
+
|
| 23 |
+
var sunset, deprecation string
|
| 24 |
+
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
| 25 |
+
clonedHeader := w.Header().Clone()
|
| 26 |
+
sunset = clonedHeader.Get("Sunset")
|
| 27 |
+
deprecation = clonedHeader.Get("Deprecation")
|
| 28 |
+
w.Write([]byte("I'll be unavailable soon"))
|
| 29 |
+
})
|
| 30 |
+
r.ServeHTTP(w, req)
|
| 31 |
+
|
| 32 |
+
if w.Code != 200 {
|
| 33 |
+
t.Fatal("Response Code should be 200")
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
if sunset != "Wed, 24 Dec 2025 10:20:00 GMT" {
|
| 37 |
+
t.Fatal("Test get sunset error.", sunset)
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
if deprecation != "Wed, 24 Dec 2025 10:20:00 GMT" {
|
| 41 |
+
t.Fatal("Test get deprecation error.")
|
| 42 |
+
}
|
| 43 |
+
})
|
| 44 |
+
|
| 45 |
+
t.Run("Sunset with link", func(t *testing.T) {
|
| 46 |
+
req, _ := http.NewRequest("GET", "/", nil)
|
| 47 |
+
w := httptest.NewRecorder()
|
| 48 |
+
|
| 49 |
+
r := chi.NewRouter()
|
| 50 |
+
|
| 51 |
+
sunsetAt := time.Date(2025, 12, 24, 10, 20, 0, 0, time.UTC)
|
| 52 |
+
deprecationLink := "https://example.com/v1/deprecation-details"
|
| 53 |
+
r.Use(Sunset(sunsetAt, deprecationLink))
|
| 54 |
+
|
| 55 |
+
var sunset, deprecation, link string
|
| 56 |
+
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
| 57 |
+
clonedHeader := w.Header().Clone()
|
| 58 |
+
sunset = clonedHeader.Get("Sunset")
|
| 59 |
+
deprecation = clonedHeader.Get("Deprecation")
|
| 60 |
+
link = clonedHeader.Get("Link")
|
| 61 |
+
|
| 62 |
+
w.Write([]byte("I'll be unavailable soon"))
|
| 63 |
+
})
|
| 64 |
+
|
| 65 |
+
r.ServeHTTP(w, req)
|
| 66 |
+
|
| 67 |
+
if w.Code != 200 {
|
| 68 |
+
t.Fatal("Response Code should be 200")
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
if sunset != "Wed, 24 Dec 2025 10:20:00 GMT" {
|
| 72 |
+
t.Fatal("Test get sunset error.", sunset)
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
if deprecation != "Wed, 24 Dec 2025 10:20:00 GMT" {
|
| 76 |
+
t.Fatal("Test get deprecation error.")
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
if link != deprecationLink {
|
| 80 |
+
t.Fatal("Test get deprecation link error.")
|
| 81 |
+
}
|
| 82 |
+
})
|
| 83 |
+
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
/**
|
| 87 |
+
EXAMPLE USAGES
|
| 88 |
+
func main() {
|
| 89 |
+
r := chi.NewRouter()
|
| 90 |
+
|
| 91 |
+
sunsetAt := time.Date(2025, 12, 24, 10, 20, 0, 0, time.UTC)
|
| 92 |
+
r.Use(middleware.Sunset(sunsetAt))
|
| 93 |
+
|
| 94 |
+
// can provide additional link for updated resource
|
| 95 |
+
// r.Use(middleware.Sunset(sunsetAt, "https://example.com/v1/deprecation-details"))
|
| 96 |
+
|
| 97 |
+
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
| 98 |
+
w.Write([]byte("This endpoint will be removed soon"))
|
| 99 |
+
})
|
| 100 |
+
|
| 101 |
+
log.Println("Listening on port: 3000")
|
| 102 |
+
http.ListenAndServe(":3000", r)
|
| 103 |
+
}
|
| 104 |
+
**/
|
tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/tests/frozen/middleware/throttle_test.go
ADDED
|
@@ -0,0 +1,301 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
package middleware
|
| 2 |
+
|
| 3 |
+
import (
|
| 4 |
+
"io/ioutil"
|
| 5 |
+
"net/http"
|
| 6 |
+
"net/http/httptest"
|
| 7 |
+
"strings"
|
| 8 |
+
"sync"
|
| 9 |
+
"testing"
|
| 10 |
+
"time"
|
| 11 |
+
|
| 12 |
+
"github.com/go-chi/chi/v5"
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
var testContent = []byte("Hello world!")
|
| 16 |
+
|
| 17 |
+
func TestThrottleBacklog(t *testing.T) {
|
| 18 |
+
r := chi.NewRouter()
|
| 19 |
+
|
| 20 |
+
r.Use(ThrottleBacklog(10, 50, time.Second*10))
|
| 21 |
+
|
| 22 |
+
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
| 23 |
+
w.WriteHeader(http.StatusOK)
|
| 24 |
+
time.Sleep(time.Second * 1) // Expensive operation.
|
| 25 |
+
w.Write(testContent)
|
| 26 |
+
})
|
| 27 |
+
|
| 28 |
+
server := httptest.NewServer(r)
|
| 29 |
+
defer server.Close()
|
| 30 |
+
|
| 31 |
+
client := http.Client{
|
| 32 |
+
Timeout: time.Second * 5, // Maximum waiting time.
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
var wg sync.WaitGroup
|
| 36 |
+
|
| 37 |
+
// The throttler processes 10 consecutive requests, each one of those
|
| 38 |
+
// requests lasts 1s. The maximum number of requests this can possible serve
|
| 39 |
+
// before the clients time out (5s) is 40.
|
| 40 |
+
for i := 0; i < 40; i++ {
|
| 41 |
+
wg.Add(1)
|
| 42 |
+
go func(i int) {
|
| 43 |
+
defer wg.Done()
|
| 44 |
+
|
| 45 |
+
res, err := client.Get(server.URL)
|
| 46 |
+
assertNoError(t, err)
|
| 47 |
+
|
| 48 |
+
assertEqual(t, http.StatusOK, res.StatusCode)
|
| 49 |
+
buf, err := ioutil.ReadAll(res.Body)
|
| 50 |
+
assertNoError(t, err)
|
| 51 |
+
assertEqual(t, testContent, buf)
|
| 52 |
+
}(i)
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
wg.Wait()
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
func TestThrottleClientTimeout(t *testing.T) {
|
| 59 |
+
r := chi.NewRouter()
|
| 60 |
+
|
| 61 |
+
r.Use(ThrottleBacklog(10, 50, time.Second*10))
|
| 62 |
+
|
| 63 |
+
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
| 64 |
+
w.WriteHeader(http.StatusOK)
|
| 65 |
+
time.Sleep(time.Second * 5) // Expensive operation.
|
| 66 |
+
w.Write(testContent)
|
| 67 |
+
})
|
| 68 |
+
|
| 69 |
+
server := httptest.NewServer(r)
|
| 70 |
+
defer server.Close()
|
| 71 |
+
|
| 72 |
+
client := http.Client{
|
| 73 |
+
Timeout: time.Second * 3, // Maximum waiting time.
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
var wg sync.WaitGroup
|
| 77 |
+
|
| 78 |
+
for i := 0; i < 10; i++ {
|
| 79 |
+
wg.Add(1)
|
| 80 |
+
go func(i int) {
|
| 81 |
+
defer wg.Done()
|
| 82 |
+
_, err := client.Get(server.URL)
|
| 83 |
+
assertError(t, err)
|
| 84 |
+
}(i)
|
| 85 |
+
}
|
| 86 |
+
|
| 87 |
+
wg.Wait()
|
| 88 |
+
}
|
| 89 |
+
|
| 90 |
+
func TestThrottleTriggerGatewayTimeout(t *testing.T) {
|
| 91 |
+
r := chi.NewRouter()
|
| 92 |
+
|
| 93 |
+
r.Use(ThrottleBacklog(50, 100, time.Second*5))
|
| 94 |
+
|
| 95 |
+
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
| 96 |
+
w.WriteHeader(http.StatusOK)
|
| 97 |
+
time.Sleep(time.Second * 10) // Expensive operation.
|
| 98 |
+
w.Write(testContent)
|
| 99 |
+
})
|
| 100 |
+
|
| 101 |
+
server := httptest.NewServer(r)
|
| 102 |
+
defer server.Close()
|
| 103 |
+
|
| 104 |
+
client := http.Client{
|
| 105 |
+
Timeout: time.Second * 60, // Maximum waiting time.
|
| 106 |
+
}
|
| 107 |
+
|
| 108 |
+
var wg sync.WaitGroup
|
| 109 |
+
|
| 110 |
+
// These requests will be processed normally until they finish.
|
| 111 |
+
for i := 0; i < 50; i++ {
|
| 112 |
+
wg.Add(1)
|
| 113 |
+
go func(i int) {
|
| 114 |
+
defer wg.Done()
|
| 115 |
+
|
| 116 |
+
res, err := client.Get(server.URL)
|
| 117 |
+
assertNoError(t, err)
|
| 118 |
+
assertEqual(t, http.StatusOK, res.StatusCode)
|
| 119 |
+
}(i)
|
| 120 |
+
}
|
| 121 |
+
|
| 122 |
+
time.Sleep(time.Second * 1)
|
| 123 |
+
|
| 124 |
+
// These requests will wait for the first batch to complete but it will take
|
| 125 |
+
// too much time, so they will eventually receive a timeout error.
|
| 126 |
+
for i := 0; i < 50; i++ {
|
| 127 |
+
wg.Add(1)
|
| 128 |
+
go func(i int) {
|
| 129 |
+
defer wg.Done()
|
| 130 |
+
|
| 131 |
+
res, err := client.Get(server.URL)
|
| 132 |
+
assertNoError(t, err)
|
| 133 |
+
|
| 134 |
+
buf, err := ioutil.ReadAll(res.Body)
|
| 135 |
+
assertNoError(t, err)
|
| 136 |
+
assertEqual(t, http.StatusTooManyRequests, res.StatusCode)
|
| 137 |
+
assertEqual(t, errTimedOut, strings.TrimSpace(string(buf)))
|
| 138 |
+
}(i)
|
| 139 |
+
}
|
| 140 |
+
|
| 141 |
+
wg.Wait()
|
| 142 |
+
}
|
| 143 |
+
|
| 144 |
+
func TestThrottleMaximum(t *testing.T) {
|
| 145 |
+
r := chi.NewRouter()
|
| 146 |
+
|
| 147 |
+
r.Use(ThrottleBacklog(10, 10, time.Second*5))
|
| 148 |
+
|
| 149 |
+
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
| 150 |
+
w.WriteHeader(http.StatusOK)
|
| 151 |
+
time.Sleep(time.Second * 3) // Expensive operation.
|
| 152 |
+
w.Write(testContent)
|
| 153 |
+
})
|
| 154 |
+
|
| 155 |
+
server := httptest.NewServer(r)
|
| 156 |
+
defer server.Close()
|
| 157 |
+
|
| 158 |
+
client := http.Client{
|
| 159 |
+
Timeout: time.Second * 60, // Maximum waiting time.
|
| 160 |
+
}
|
| 161 |
+
|
| 162 |
+
var wg sync.WaitGroup
|
| 163 |
+
|
| 164 |
+
for i := 0; i < 20; i++ {
|
| 165 |
+
wg.Add(1)
|
| 166 |
+
go func(i int) {
|
| 167 |
+
defer wg.Done()
|
| 168 |
+
|
| 169 |
+
res, err := client.Get(server.URL)
|
| 170 |
+
assertNoError(t, err)
|
| 171 |
+
assertEqual(t, http.StatusOK, res.StatusCode)
|
| 172 |
+
|
| 173 |
+
buf, err := ioutil.ReadAll(res.Body)
|
| 174 |
+
assertNoError(t, err)
|
| 175 |
+
assertEqual(t, testContent, buf)
|
| 176 |
+
}(i)
|
| 177 |
+
}
|
| 178 |
+
|
| 179 |
+
// Wait less time than what the server takes to reply.
|
| 180 |
+
time.Sleep(time.Second * 2)
|
| 181 |
+
|
| 182 |
+
// At this point the server is still processing, all the following request
|
| 183 |
+
// will be beyond the server capacity.
|
| 184 |
+
for i := 0; i < 20; i++ {
|
| 185 |
+
wg.Add(1)
|
| 186 |
+
go func(i int) {
|
| 187 |
+
defer wg.Done()
|
| 188 |
+
|
| 189 |
+
res, err := client.Get(server.URL)
|
| 190 |
+
assertNoError(t, err)
|
| 191 |
+
|
| 192 |
+
buf, err := ioutil.ReadAll(res.Body)
|
| 193 |
+
assertNoError(t, err)
|
| 194 |
+
assertEqual(t, http.StatusTooManyRequests, res.StatusCode)
|
| 195 |
+
assertEqual(t, errCapacityExceeded, strings.TrimSpace(string(buf)))
|
| 196 |
+
}(i)
|
| 197 |
+
}
|
| 198 |
+
|
| 199 |
+
wg.Wait()
|
| 200 |
+
}
|
| 201 |
+
|
| 202 |
+
// NOTE: test is disabled as it requires some refactoring. It is prone to intermittent failure.
|
| 203 |
+
/*func TestThrottleRetryAfter(t *testing.T) {
|
| 204 |
+
r := chi.NewRouter()
|
| 205 |
+
|
| 206 |
+
retryAfterFn := func(ctxDone bool) time.Duration { return time.Hour * 1 }
|
| 207 |
+
r.Use(ThrottleWithOpts(ThrottleOpts{Limit: 10, RetryAfterFn: retryAfterFn}))
|
| 208 |
+
|
| 209 |
+
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
| 210 |
+
w.WriteHeader(http.StatusOK)
|
| 211 |
+
time.Sleep(time.Second * 4) // Expensive operation.
|
| 212 |
+
w.Write(testContent)
|
| 213 |
+
})
|
| 214 |
+
|
| 215 |
+
server := httptest.NewServer(r)
|
| 216 |
+
defer server.Close()
|
| 217 |
+
|
| 218 |
+
client := http.Client{
|
| 219 |
+
Timeout: time.Second * 60, // Maximum waiting time.
|
| 220 |
+
}
|
| 221 |
+
|
| 222 |
+
var wg sync.WaitGroup
|
| 223 |
+
|
| 224 |
+
for i := 0; i < 10; i++ {
|
| 225 |
+
wg.Add(1)
|
| 226 |
+
go func(i int) {
|
| 227 |
+
defer wg.Done()
|
| 228 |
+
|
| 229 |
+
res, err := client.Get(server.URL)
|
| 230 |
+
assertNoError(t, err)
|
| 231 |
+
assertEqual(t, http.StatusOK, res.StatusCode)
|
| 232 |
+
}(i)
|
| 233 |
+
}
|
| 234 |
+
|
| 235 |
+
time.Sleep(time.Second * 1)
|
| 236 |
+
|
| 237 |
+
for i := 0; i < 10; i++ {
|
| 238 |
+
wg.Add(1)
|
| 239 |
+
go func(i int) {
|
| 240 |
+
defer wg.Done()
|
| 241 |
+
|
| 242 |
+
res, err := client.Get(server.URL)
|
| 243 |
+
assertNoError(t, err)
|
| 244 |
+
assertEqual(t, http.StatusTooManyRequests, res.StatusCode)
|
| 245 |
+
assertEqual(t, res.Header.Get("Retry-After"), "3600")
|
| 246 |
+
}(i)
|
| 247 |
+
}
|
| 248 |
+
|
| 249 |
+
wg.Wait()
|
| 250 |
+
}*/
|
| 251 |
+
|
| 252 |
+
func TestThrottleCustomStatusCode(t *testing.T) {
|
| 253 |
+
const timeout = time.Second * 3
|
| 254 |
+
|
| 255 |
+
wait := make(chan struct{})
|
| 256 |
+
|
| 257 |
+
r := chi.NewRouter()
|
| 258 |
+
r.Use(ThrottleWithOpts(ThrottleOpts{Limit: 1, StatusCode: http.StatusServiceUnavailable}))
|
| 259 |
+
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
| 260 |
+
select {
|
| 261 |
+
case <-wait:
|
| 262 |
+
case <-time.After(timeout):
|
| 263 |
+
}
|
| 264 |
+
w.WriteHeader(http.StatusOK)
|
| 265 |
+
})
|
| 266 |
+
server := httptest.NewServer(r)
|
| 267 |
+
defer server.Close()
|
| 268 |
+
|
| 269 |
+
const totalRequestCount = 5
|
| 270 |
+
|
| 271 |
+
codes := make(chan int, totalRequestCount)
|
| 272 |
+
errs := make(chan error, totalRequestCount)
|
| 273 |
+
client := &http.Client{Timeout: timeout}
|
| 274 |
+
for i := 0; i < totalRequestCount; i++ {
|
| 275 |
+
go func() {
|
| 276 |
+
resp, err := client.Get(server.URL)
|
| 277 |
+
if err != nil {
|
| 278 |
+
errs <- err
|
| 279 |
+
return
|
| 280 |
+
}
|
| 281 |
+
codes <- resp.StatusCode
|
| 282 |
+
}()
|
| 283 |
+
}
|
| 284 |
+
|
| 285 |
+
waitResponse := func(wantCode int) {
|
| 286 |
+
select {
|
| 287 |
+
case err := <-errs:
|
| 288 |
+
t.Fatal(err)
|
| 289 |
+
case code := <-codes:
|
| 290 |
+
assertEqual(t, wantCode, code)
|
| 291 |
+
case <-time.After(timeout):
|
| 292 |
+
t.Fatalf("waiting %d code, timeout exceeded", wantCode)
|
| 293 |
+
}
|
| 294 |
+
}
|
| 295 |
+
|
| 296 |
+
for i := 0; i < totalRequestCount-1; i++ {
|
| 297 |
+
waitResponse(http.StatusServiceUnavailable)
|
| 298 |
+
}
|
| 299 |
+
close(wait) // Allow the last request to proceed.
|
| 300 |
+
waitResponse(http.StatusOK)
|
| 301 |
+
}
|
tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/tests/frozen/middleware/url_format_test.go
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
package middleware
|
| 2 |
+
|
| 3 |
+
import (
|
| 4 |
+
"net/http"
|
| 5 |
+
"net/http/httptest"
|
| 6 |
+
"testing"
|
| 7 |
+
|
| 8 |
+
"github.com/go-chi/chi/v5"
|
| 9 |
+
)
|
| 10 |
+
|
| 11 |
+
func TestURLFormat(t *testing.T) {
|
| 12 |
+
r := chi.NewRouter()
|
| 13 |
+
|
| 14 |
+
r.Use(URLFormat)
|
| 15 |
+
|
| 16 |
+
r.NotFound(func(w http.ResponseWriter, r *http.Request) {
|
| 17 |
+
w.WriteHeader(404)
|
| 18 |
+
w.Write([]byte("nothing here"))
|
| 19 |
+
})
|
| 20 |
+
|
| 21 |
+
r.Route("/samples/articles/samples.{articleID}", func(r chi.Router) {
|
| 22 |
+
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
| 23 |
+
articleID := chi.URLParam(r, "articleID")
|
| 24 |
+
w.Write([]byte(articleID))
|
| 25 |
+
})
|
| 26 |
+
})
|
| 27 |
+
|
| 28 |
+
r.Route("/articles/{articleID}", func(r chi.Router) {
|
| 29 |
+
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
| 30 |
+
articleID := chi.URLParam(r, "articleID")
|
| 31 |
+
w.Write([]byte(articleID))
|
| 32 |
+
})
|
| 33 |
+
})
|
| 34 |
+
|
| 35 |
+
ts := httptest.NewServer(r)
|
| 36 |
+
defer ts.Close()
|
| 37 |
+
|
| 38 |
+
if _, resp := testRequest(t, ts, "GET", "/articles/1.json", nil); resp != "1" {
|
| 39 |
+
t.Fatal(resp)
|
| 40 |
+
}
|
| 41 |
+
if _, resp := testRequest(t, ts, "GET", "/articles/1.xml", nil); resp != "1" {
|
| 42 |
+
t.Fatal(resp)
|
| 43 |
+
}
|
| 44 |
+
if _, resp := testRequest(t, ts, "GET", "/samples/articles/samples.1.json", nil); resp != "1" {
|
| 45 |
+
t.Fatal(resp)
|
| 46 |
+
}
|
| 47 |
+
if _, resp := testRequest(t, ts, "GET", "/samples/articles/samples.1.xml", nil); resp != "1" {
|
| 48 |
+
t.Fatal(resp)
|
| 49 |
+
}
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
func TestURLFormatInSubRouter(t *testing.T) {
|
| 53 |
+
r := chi.NewRouter()
|
| 54 |
+
|
| 55 |
+
r.Route("/articles/{articleID}", func(r chi.Router) {
|
| 56 |
+
r.Use(URLFormat)
|
| 57 |
+
r.Get("/subroute", func(w http.ResponseWriter, r *http.Request) {
|
| 58 |
+
articleID := chi.URLParam(r, "articleID")
|
| 59 |
+
w.Write([]byte(articleID))
|
| 60 |
+
})
|
| 61 |
+
})
|
| 62 |
+
|
| 63 |
+
ts := httptest.NewServer(r)
|
| 64 |
+
defer ts.Close()
|
| 65 |
+
|
| 66 |
+
if _, resp := testRequest(t, ts, "GET", "/articles/1/subroute.json", nil); resp != "1" {
|
| 67 |
+
t.Fatal(resp)
|
| 68 |
+
}
|
| 69 |
+
}
|
tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/tests/frozen/middleware/wrap_writer_test.go
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
package middleware
|
| 2 |
+
|
| 3 |
+
import (
|
| 4 |
+
"bytes"
|
| 5 |
+
"net/http"
|
| 6 |
+
"net/http/httptest"
|
| 7 |
+
"testing"
|
| 8 |
+
)
|
| 9 |
+
|
| 10 |
+
func TestHttpFancyWriterRemembersWroteHeaderWhenFlushed(t *testing.T) {
|
| 11 |
+
f := &httpFancyWriter{basicWriter: basicWriter{ResponseWriter: httptest.NewRecorder()}}
|
| 12 |
+
f.Flush()
|
| 13 |
+
|
| 14 |
+
if !f.wroteHeader {
|
| 15 |
+
t.Fatal("want Flush to have set wroteHeader=true")
|
| 16 |
+
}
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
func TestHttp2FancyWriterRemembersWroteHeaderWhenFlushed(t *testing.T) {
|
| 20 |
+
f := &http2FancyWriter{basicWriter{ResponseWriter: httptest.NewRecorder()}}
|
| 21 |
+
f.Flush()
|
| 22 |
+
|
| 23 |
+
if !f.wroteHeader {
|
| 24 |
+
t.Fatal("want Flush to have set wroteHeader=true")
|
| 25 |
+
}
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
func TestBasicWritesTeesWritesWithoutDiscard(t *testing.T) {
|
| 29 |
+
// explicitly create the struct instead of NewRecorder to control the value of Code
|
| 30 |
+
original := &httptest.ResponseRecorder{
|
| 31 |
+
HeaderMap: make(http.Header),
|
| 32 |
+
Body: new(bytes.Buffer),
|
| 33 |
+
}
|
| 34 |
+
wrap := &basicWriter{ResponseWriter: original}
|
| 35 |
+
|
| 36 |
+
var buf bytes.Buffer
|
| 37 |
+
wrap.Tee(&buf)
|
| 38 |
+
|
| 39 |
+
_, err := wrap.Write([]byte("hello world"))
|
| 40 |
+
assertNoError(t, err)
|
| 41 |
+
|
| 42 |
+
assertEqual(t, 200, original.Code)
|
| 43 |
+
assertEqual(t, []byte("hello world"), original.Body.Bytes())
|
| 44 |
+
assertEqual(t, []byte("hello world"), buf.Bytes())
|
| 45 |
+
assertEqual(t, 11, wrap.BytesWritten())
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
func TestBasicWriterDiscardsWritesToOriginalResponseWriter(t *testing.T) {
|
| 49 |
+
t.Run("With Tee", func(t *testing.T) {
|
| 50 |
+
// explicitly create the struct instead of NewRecorder to control the value of Code
|
| 51 |
+
original := &httptest.ResponseRecorder{
|
| 52 |
+
HeaderMap: make(http.Header),
|
| 53 |
+
Body: new(bytes.Buffer),
|
| 54 |
+
}
|
| 55 |
+
wrap := &basicWriter{ResponseWriter: original}
|
| 56 |
+
|
| 57 |
+
var buf bytes.Buffer
|
| 58 |
+
wrap.Tee(&buf)
|
| 59 |
+
wrap.Discard()
|
| 60 |
+
|
| 61 |
+
_, err := wrap.Write([]byte("hello world"))
|
| 62 |
+
assertNoError(t, err)
|
| 63 |
+
|
| 64 |
+
assertEqual(t, 0, original.Code) // wrapper shouldn't call WriteHeader implicitly
|
| 65 |
+
assertEqual(t, 0, original.Body.Len())
|
| 66 |
+
assertEqual(t, []byte("hello world"), buf.Bytes())
|
| 67 |
+
assertEqual(t, 11, wrap.BytesWritten())
|
| 68 |
+
})
|
| 69 |
+
|
| 70 |
+
t.Run("Without Tee", func(t *testing.T) {
|
| 71 |
+
// explicitly create the struct instead of NewRecorder to control the value of Code
|
| 72 |
+
original := &httptest.ResponseRecorder{
|
| 73 |
+
HeaderMap: make(http.Header),
|
| 74 |
+
Body: new(bytes.Buffer),
|
| 75 |
+
}
|
| 76 |
+
wrap := &basicWriter{ResponseWriter: original}
|
| 77 |
+
wrap.Discard()
|
| 78 |
+
|
| 79 |
+
_, err := wrap.Write([]byte("hello world"))
|
| 80 |
+
assertNoError(t, err)
|
| 81 |
+
|
| 82 |
+
assertEqual(t, 0, original.Code) // wrapper shouldn't call WriteHeader implicitly
|
| 83 |
+
assertEqual(t, 0, original.Body.Len())
|
| 84 |
+
assertEqual(t, 11, wrap.BytesWritten())
|
| 85 |
+
})
|
| 86 |
+
}
|
tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/tests/frozen/mux_test.go
ADDED
|
@@ -0,0 +1,2042 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
package chi
|
| 2 |
+
|
| 3 |
+
import (
|
| 4 |
+
"bytes"
|
| 5 |
+
"context"
|
| 6 |
+
"fmt"
|
| 7 |
+
"io"
|
| 8 |
+
"net"
|
| 9 |
+
"net/http"
|
| 10 |
+
"net/http/httptest"
|
| 11 |
+
"sync"
|
| 12 |
+
"testing"
|
| 13 |
+
"time"
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
func TestMuxBasic(t *testing.T) {
|
| 17 |
+
var count uint64
|
| 18 |
+
countermw := func(next http.Handler) http.Handler {
|
| 19 |
+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
| 20 |
+
count++
|
| 21 |
+
next.ServeHTTP(w, r)
|
| 22 |
+
})
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
usermw := func(next http.Handler) http.Handler {
|
| 26 |
+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
| 27 |
+
ctx := r.Context()
|
| 28 |
+
ctx = context.WithValue(ctx, ctxKey{"user"}, "peter")
|
| 29 |
+
r = r.WithContext(ctx)
|
| 30 |
+
next.ServeHTTP(w, r)
|
| 31 |
+
})
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
exmw := func(next http.Handler) http.Handler {
|
| 35 |
+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
| 36 |
+
ctx := context.WithValue(r.Context(), ctxKey{"ex"}, "a")
|
| 37 |
+
r = r.WithContext(ctx)
|
| 38 |
+
next.ServeHTTP(w, r)
|
| 39 |
+
})
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
logbuf := bytes.NewBufferString("")
|
| 43 |
+
logmsg := "logmw test"
|
| 44 |
+
logmw := func(next http.Handler) http.Handler {
|
| 45 |
+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
| 46 |
+
logbuf.WriteString(logmsg)
|
| 47 |
+
next.ServeHTTP(w, r)
|
| 48 |
+
})
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
cxindex := func(w http.ResponseWriter, r *http.Request) {
|
| 52 |
+
ctx := r.Context()
|
| 53 |
+
user := ctx.Value(ctxKey{"user"}).(string)
|
| 54 |
+
w.WriteHeader(200)
|
| 55 |
+
w.Write([]byte(fmt.Sprintf("hi %s", user)))
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
ping := func(w http.ResponseWriter, r *http.Request) {
|
| 59 |
+
w.WriteHeader(200)
|
| 60 |
+
w.Write([]byte("."))
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
headPing := func(w http.ResponseWriter, r *http.Request) {
|
| 64 |
+
w.Header().Set("X-Ping", "1")
|
| 65 |
+
w.WriteHeader(200)
|
| 66 |
+
}
|
| 67 |
+
|
| 68 |
+
createPing := func(w http.ResponseWriter, r *http.Request) {
|
| 69 |
+
// create ....
|
| 70 |
+
w.WriteHeader(201)
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
pingAll := func(w http.ResponseWriter, r *http.Request) {
|
| 74 |
+
w.WriteHeader(200)
|
| 75 |
+
w.Write([]byte("ping all"))
|
| 76 |
+
}
|
| 77 |
+
|
| 78 |
+
pingAll2 := func(w http.ResponseWriter, r *http.Request) {
|
| 79 |
+
w.WriteHeader(200)
|
| 80 |
+
w.Write([]byte("ping all2"))
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
pingOne := func(w http.ResponseWriter, r *http.Request) {
|
| 84 |
+
idParam := URLParam(r, "id")
|
| 85 |
+
w.WriteHeader(200)
|
| 86 |
+
w.Write([]byte(fmt.Sprintf("ping one id: %s", idParam)))
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
pingWoop := func(w http.ResponseWriter, r *http.Request) {
|
| 90 |
+
w.WriteHeader(200)
|
| 91 |
+
w.Write([]byte("woop." + URLParam(r, "iidd")))
|
| 92 |
+
}
|
| 93 |
+
|
| 94 |
+
catchAll := func(w http.ResponseWriter, r *http.Request) {
|
| 95 |
+
w.WriteHeader(200)
|
| 96 |
+
w.Write([]byte("catchall"))
|
| 97 |
+
}
|
| 98 |
+
|
| 99 |
+
m := NewRouter()
|
| 100 |
+
m.Use(countermw)
|
| 101 |
+
m.Use(usermw)
|
| 102 |
+
m.Use(exmw)
|
| 103 |
+
m.Use(logmw)
|
| 104 |
+
m.Get("/", cxindex)
|
| 105 |
+
m.Method("GET", "/ping", http.HandlerFunc(ping))
|
| 106 |
+
m.MethodFunc("GET", "/pingall", pingAll)
|
| 107 |
+
m.MethodFunc("get", "/ping/all", pingAll)
|
| 108 |
+
m.Get("/ping/all2", pingAll2)
|
| 109 |
+
|
| 110 |
+
m.Head("/ping", headPing)
|
| 111 |
+
m.Post("/ping", createPing)
|
| 112 |
+
m.Get("/ping/{id}", pingWoop)
|
| 113 |
+
m.Get("/ping/{id}", pingOne) // expected to overwrite to pingOne handler
|
| 114 |
+
m.Get("/ping/{iidd}/woop", pingWoop)
|
| 115 |
+
m.HandleFunc("/admin/*", catchAll)
|
| 116 |
+
// m.Post("/admin/*", catchAll)
|
| 117 |
+
|
| 118 |
+
ts := httptest.NewServer(m)
|
| 119 |
+
defer ts.Close()
|
| 120 |
+
|
| 121 |
+
// GET /
|
| 122 |
+
if _, body := testRequest(t, ts, "GET", "/", nil); body != "hi peter" {
|
| 123 |
+
t.Fatal(body)
|
| 124 |
+
}
|
| 125 |
+
tlogmsg, _ := logbuf.ReadString(0)
|
| 126 |
+
if tlogmsg != logmsg {
|
| 127 |
+
t.Error("expecting log message from middleware:", logmsg)
|
| 128 |
+
}
|
| 129 |
+
|
| 130 |
+
// GET /ping
|
| 131 |
+
if _, body := testRequest(t, ts, "GET", "/ping", nil); body != "." {
|
| 132 |
+
t.Fatal(body)
|
| 133 |
+
}
|
| 134 |
+
|
| 135 |
+
// GET /pingall
|
| 136 |
+
if _, body := testRequest(t, ts, "GET", "/pingall", nil); body != "ping all" {
|
| 137 |
+
t.Fatal(body)
|
| 138 |
+
}
|
| 139 |
+
|
| 140 |
+
// GET /ping/all
|
| 141 |
+
if _, body := testRequest(t, ts, "GET", "/ping/all", nil); body != "ping all" {
|
| 142 |
+
t.Fatal(body)
|
| 143 |
+
}
|
| 144 |
+
|
| 145 |
+
// GET /ping/all2
|
| 146 |
+
if _, body := testRequest(t, ts, "GET", "/ping/all2", nil); body != "ping all2" {
|
| 147 |
+
t.Fatal(body)
|
| 148 |
+
}
|
| 149 |
+
|
| 150 |
+
// GET /ping/123
|
| 151 |
+
if _, body := testRequest(t, ts, "GET", "/ping/123", nil); body != "ping one id: 123" {
|
| 152 |
+
t.Fatal(body)
|
| 153 |
+
}
|
| 154 |
+
|
| 155 |
+
// GET /ping/allan
|
| 156 |
+
if _, body := testRequest(t, ts, "GET", "/ping/allan", nil); body != "ping one id: allan" {
|
| 157 |
+
t.Fatal(body)
|
| 158 |
+
}
|
| 159 |
+
|
| 160 |
+
// GET /ping/1/woop
|
| 161 |
+
if _, body := testRequest(t, ts, "GET", "/ping/1/woop", nil); body != "woop.1" {
|
| 162 |
+
t.Fatal(body)
|
| 163 |
+
}
|
| 164 |
+
|
| 165 |
+
// HEAD /ping
|
| 166 |
+
resp, err := http.Head(ts.URL + "/ping")
|
| 167 |
+
if err != nil {
|
| 168 |
+
t.Fatal(err)
|
| 169 |
+
}
|
| 170 |
+
if resp.StatusCode != 200 {
|
| 171 |
+
t.Error("head failed, should be 200")
|
| 172 |
+
}
|
| 173 |
+
if resp.Header.Get("X-Ping") == "" {
|
| 174 |
+
t.Error("expecting X-Ping header")
|
| 175 |
+
}
|
| 176 |
+
|
| 177 |
+
// GET /admin/catch-this
|
| 178 |
+
if _, body := testRequest(t, ts, "GET", "/admin/catch-thazzzzz", nil); body != "catchall" {
|
| 179 |
+
t.Fatal(body)
|
| 180 |
+
}
|
| 181 |
+
|
| 182 |
+
// POST /admin/catch-this
|
| 183 |
+
resp, err = http.Post(ts.URL+"/admin/casdfsadfs", "text/plain", bytes.NewReader([]byte{}))
|
| 184 |
+
if err != nil {
|
| 185 |
+
t.Fatal(err)
|
| 186 |
+
}
|
| 187 |
+
|
| 188 |
+
body, err := io.ReadAll(resp.Body)
|
| 189 |
+
if err != nil {
|
| 190 |
+
t.Fatal(err)
|
| 191 |
+
}
|
| 192 |
+
defer resp.Body.Close()
|
| 193 |
+
|
| 194 |
+
if resp.StatusCode != 200 {
|
| 195 |
+
t.Error("POST failed, should be 200")
|
| 196 |
+
}
|
| 197 |
+
|
| 198 |
+
if string(body) != "catchall" {
|
| 199 |
+
t.Error("expecting response body: 'catchall'")
|
| 200 |
+
}
|
| 201 |
+
|
| 202 |
+
// Custom http method DIE /ping/1/woop
|
| 203 |
+
if resp, body := testRequest(t, ts, "DIE", "/ping/1/woop", nil); body != "" || resp.StatusCode != 405 {
|
| 204 |
+
t.Fatalf("expecting 405 status and empty body, got %d '%s'", resp.StatusCode, body)
|
| 205 |
+
}
|
| 206 |
+
}
|
| 207 |
+
|
| 208 |
+
func TestMuxMounts(t *testing.T) {
|
| 209 |
+
r := NewRouter()
|
| 210 |
+
|
| 211 |
+
r.Get("/{hash}", func(w http.ResponseWriter, r *http.Request) {
|
| 212 |
+
v := URLParam(r, "hash")
|
| 213 |
+
w.Write([]byte(fmt.Sprintf("/%s", v)))
|
| 214 |
+
})
|
| 215 |
+
|
| 216 |
+
r.Route("/{hash}/share", func(r Router) {
|
| 217 |
+
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
| 218 |
+
v := URLParam(r, "hash")
|
| 219 |
+
w.Write([]byte(fmt.Sprintf("/%s/share", v)))
|
| 220 |
+
})
|
| 221 |
+
r.Get("/{network}", func(w http.ResponseWriter, r *http.Request) {
|
| 222 |
+
v := URLParam(r, "hash")
|
| 223 |
+
n := URLParam(r, "network")
|
| 224 |
+
w.Write([]byte(fmt.Sprintf("/%s/share/%s", v, n)))
|
| 225 |
+
})
|
| 226 |
+
})
|
| 227 |
+
|
| 228 |
+
m := NewRouter()
|
| 229 |
+
m.Mount("/sharing", r)
|
| 230 |
+
|
| 231 |
+
ts := httptest.NewServer(m)
|
| 232 |
+
defer ts.Close()
|
| 233 |
+
|
| 234 |
+
if _, body := testRequest(t, ts, "GET", "/sharing/aBc", nil); body != "/aBc" {
|
| 235 |
+
t.Fatal(body)
|
| 236 |
+
}
|
| 237 |
+
if _, body := testRequest(t, ts, "GET", "/sharing/aBc/share", nil); body != "/aBc/share" {
|
| 238 |
+
t.Fatal(body)
|
| 239 |
+
}
|
| 240 |
+
if _, body := testRequest(t, ts, "GET", "/sharing/aBc/share/twitter", nil); body != "/aBc/share/twitter" {
|
| 241 |
+
t.Fatal(body)
|
| 242 |
+
}
|
| 243 |
+
}
|
| 244 |
+
|
| 245 |
+
func TestMuxPlain(t *testing.T) {
|
| 246 |
+
r := NewRouter()
|
| 247 |
+
r.Get("/hi", func(w http.ResponseWriter, r *http.Request) {
|
| 248 |
+
w.Write([]byte("bye"))
|
| 249 |
+
})
|
| 250 |
+
r.NotFound(func(w http.ResponseWriter, r *http.Request) {
|
| 251 |
+
w.WriteHeader(404)
|
| 252 |
+
w.Write([]byte("nothing here"))
|
| 253 |
+
})
|
| 254 |
+
|
| 255 |
+
ts := httptest.NewServer(r)
|
| 256 |
+
defer ts.Close()
|
| 257 |
+
|
| 258 |
+
if _, body := testRequest(t, ts, "GET", "/hi", nil); body != "bye" {
|
| 259 |
+
t.Fatal(body)
|
| 260 |
+
}
|
| 261 |
+
if _, body := testRequest(t, ts, "GET", "/nothing-here", nil); body != "nothing here" {
|
| 262 |
+
t.Fatal(body)
|
| 263 |
+
}
|
| 264 |
+
}
|
| 265 |
+
|
| 266 |
+
func TestMuxEmptyRoutes(t *testing.T) {
|
| 267 |
+
mux := NewRouter()
|
| 268 |
+
|
| 269 |
+
apiRouter := NewRouter()
|
| 270 |
+
// oops, we forgot to declare any route handlers
|
| 271 |
+
|
| 272 |
+
mux.Handle("/api*", apiRouter)
|
| 273 |
+
|
| 274 |
+
if _, body := testHandler(t, mux, "GET", "/", nil); body != "404 page not found\n" {
|
| 275 |
+
t.Fatal(body)
|
| 276 |
+
}
|
| 277 |
+
|
| 278 |
+
if _, body := testHandler(t, apiRouter, "GET", "/", nil); body != "404 page not found\n" {
|
| 279 |
+
t.Fatal(body)
|
| 280 |
+
}
|
| 281 |
+
}
|
| 282 |
+
|
| 283 |
+
// Test a mux that routes a trailing slash, see also middleware/strip_test.go
|
| 284 |
+
// for an example of using a middleware to handle trailing slashes.
|
| 285 |
+
func TestMuxTrailingSlash(t *testing.T) {
|
| 286 |
+
r := NewRouter()
|
| 287 |
+
r.NotFound(func(w http.ResponseWriter, r *http.Request) {
|
| 288 |
+
w.WriteHeader(404)
|
| 289 |
+
w.Write([]byte("nothing here"))
|
| 290 |
+
})
|
| 291 |
+
|
| 292 |
+
subRoutes := NewRouter()
|
| 293 |
+
indexHandler := func(w http.ResponseWriter, r *http.Request) {
|
| 294 |
+
accountID := URLParam(r, "accountID")
|
| 295 |
+
w.Write([]byte(accountID))
|
| 296 |
+
}
|
| 297 |
+
subRoutes.Get("/", indexHandler)
|
| 298 |
+
|
| 299 |
+
r.Mount("/accounts/{accountID}", subRoutes)
|
| 300 |
+
r.Get("/accounts/{accountID}/", indexHandler)
|
| 301 |
+
|
| 302 |
+
ts := httptest.NewServer(r)
|
| 303 |
+
defer ts.Close()
|
| 304 |
+
|
| 305 |
+
if _, body := testRequest(t, ts, "GET", "/accounts/admin", nil); body != "admin" {
|
| 306 |
+
t.Fatal(body)
|
| 307 |
+
}
|
| 308 |
+
if _, body := testRequest(t, ts, "GET", "/accounts/admin/", nil); body != "admin" {
|
| 309 |
+
t.Fatal(body)
|
| 310 |
+
}
|
| 311 |
+
if _, body := testRequest(t, ts, "GET", "/nothing-here", nil); body != "nothing here" {
|
| 312 |
+
t.Fatal(body)
|
| 313 |
+
}
|
| 314 |
+
}
|
| 315 |
+
|
| 316 |
+
func TestMuxNestedNotFound(t *testing.T) {
|
| 317 |
+
r := NewRouter()
|
| 318 |
+
|
| 319 |
+
r.Use(func(next http.Handler) http.Handler {
|
| 320 |
+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
| 321 |
+
r = r.WithContext(context.WithValue(r.Context(), ctxKey{"mw"}, "mw"))
|
| 322 |
+
next.ServeHTTP(w, r)
|
| 323 |
+
})
|
| 324 |
+
})
|
| 325 |
+
|
| 326 |
+
r.Get("/hi", func(w http.ResponseWriter, r *http.Request) {
|
| 327 |
+
w.Write([]byte("bye"))
|
| 328 |
+
})
|
| 329 |
+
|
| 330 |
+
r.With(func(next http.Handler) http.Handler {
|
| 331 |
+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
| 332 |
+
r = r.WithContext(context.WithValue(r.Context(), ctxKey{"with"}, "with"))
|
| 333 |
+
next.ServeHTTP(w, r)
|
| 334 |
+
})
|
| 335 |
+
}).NotFound(func(w http.ResponseWriter, r *http.Request) {
|
| 336 |
+
chkMw := r.Context().Value(ctxKey{"mw"}).(string)
|
| 337 |
+
chkWith := r.Context().Value(ctxKey{"with"}).(string)
|
| 338 |
+
w.WriteHeader(404)
|
| 339 |
+
w.Write([]byte(fmt.Sprintf("root 404 %s %s", chkMw, chkWith)))
|
| 340 |
+
})
|
| 341 |
+
|
| 342 |
+
sr1 := NewRouter()
|
| 343 |
+
|
| 344 |
+
sr1.Get("/sub", func(w http.ResponseWriter, r *http.Request) {
|
| 345 |
+
w.Write([]byte("sub"))
|
| 346 |
+
})
|
| 347 |
+
sr1.Group(func(sr1 Router) {
|
| 348 |
+
sr1.Use(func(next http.Handler) http.Handler {
|
| 349 |
+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
| 350 |
+
r = r.WithContext(context.WithValue(r.Context(), ctxKey{"mw2"}, "mw2"))
|
| 351 |
+
next.ServeHTTP(w, r)
|
| 352 |
+
})
|
| 353 |
+
})
|
| 354 |
+
sr1.NotFound(func(w http.ResponseWriter, r *http.Request) {
|
| 355 |
+
chkMw2 := r.Context().Value(ctxKey{"mw2"}).(string)
|
| 356 |
+
w.WriteHeader(404)
|
| 357 |
+
w.Write([]byte(fmt.Sprintf("sub 404 %s", chkMw2)))
|
| 358 |
+
})
|
| 359 |
+
})
|
| 360 |
+
|
| 361 |
+
sr2 := NewRouter()
|
| 362 |
+
sr2.Get("/sub", func(w http.ResponseWriter, r *http.Request) {
|
| 363 |
+
w.Write([]byte("sub2"))
|
| 364 |
+
})
|
| 365 |
+
|
| 366 |
+
r.Mount("/admin1", sr1)
|
| 367 |
+
r.Mount("/admin2", sr2)
|
| 368 |
+
|
| 369 |
+
ts := httptest.NewServer(r)
|
| 370 |
+
defer ts.Close()
|
| 371 |
+
|
| 372 |
+
if _, body := testRequest(t, ts, "GET", "/hi", nil); body != "bye" {
|
| 373 |
+
t.Fatal(body)
|
| 374 |
+
}
|
| 375 |
+
if _, body := testRequest(t, ts, "GET", "/nothing-here", nil); body != "root 404 mw with" {
|
| 376 |
+
t.Fatal(body)
|
| 377 |
+
}
|
| 378 |
+
if _, body := testRequest(t, ts, "GET", "/admin1/sub", nil); body != "sub" {
|
| 379 |
+
t.Fatal(body)
|
| 380 |
+
}
|
| 381 |
+
if _, body := testRequest(t, ts, "GET", "/admin1/nope", nil); body != "sub 404 mw2" {
|
| 382 |
+
t.Fatal(body)
|
| 383 |
+
}
|
| 384 |
+
if _, body := testRequest(t, ts, "GET", "/admin2/sub", nil); body != "sub2" {
|
| 385 |
+
t.Fatal(body)
|
| 386 |
+
}
|
| 387 |
+
|
| 388 |
+
// Not found pages should bubble up to the root.
|
| 389 |
+
if _, body := testRequest(t, ts, "GET", "/admin2/nope", nil); body != "root 404 mw with" {
|
| 390 |
+
t.Fatal(body)
|
| 391 |
+
}
|
| 392 |
+
}
|
| 393 |
+
|
| 394 |
+
func TestMethodNotAllowed(t *testing.T) {
|
| 395 |
+
r := NewRouter()
|
| 396 |
+
|
| 397 |
+
r.Get("/hi", func(w http.ResponseWriter, r *http.Request) {
|
| 398 |
+
w.Write([]byte("hi, get"))
|
| 399 |
+
})
|
| 400 |
+
|
| 401 |
+
r.Head("/hi", func(w http.ResponseWriter, r *http.Request) {
|
| 402 |
+
w.Write([]byte("hi, head"))
|
| 403 |
+
})
|
| 404 |
+
|
| 405 |
+
ts := httptest.NewServer(r)
|
| 406 |
+
defer ts.Close()
|
| 407 |
+
|
| 408 |
+
t.Run("Registered Method", func(t *testing.T) {
|
| 409 |
+
resp, _ := testRequest(t, ts, "GET", "/hi", nil)
|
| 410 |
+
if resp.StatusCode != 200 {
|
| 411 |
+
t.Fatal(resp.Status)
|
| 412 |
+
}
|
| 413 |
+
if resp.Header.Values("Allow") != nil {
|
| 414 |
+
t.Fatal("allow should be empty when method is registered")
|
| 415 |
+
}
|
| 416 |
+
})
|
| 417 |
+
|
| 418 |
+
t.Run("Unregistered Method", func(t *testing.T) {
|
| 419 |
+
resp, _ := testRequest(t, ts, "POST", "/hi", nil)
|
| 420 |
+
if resp.StatusCode != 405 {
|
| 421 |
+
t.Fatal(resp.Status)
|
| 422 |
+
}
|
| 423 |
+
allowedMethods := resp.Header.Values("Allow")
|
| 424 |
+
if len(allowedMethods) != 2 || ((allowedMethods[0] != "GET" || allowedMethods[1] != "HEAD") &&
|
| 425 |
+
(allowedMethods[1] != "GET" || allowedMethods[0] != "HEAD")) {
|
| 426 |
+
t.Fatal("Allow header should contain 2 headers: GET, HEAD. Received: ", allowedMethods)
|
| 427 |
+
}
|
| 428 |
+
})
|
| 429 |
+
}
|
| 430 |
+
|
| 431 |
+
func TestMuxNestedMethodNotAllowed(t *testing.T) {
|
| 432 |
+
r := NewRouter()
|
| 433 |
+
r.Get("/root", func(w http.ResponseWriter, r *http.Request) {
|
| 434 |
+
w.Write([]byte("root"))
|
| 435 |
+
})
|
| 436 |
+
r.MethodNotAllowed(func(w http.ResponseWriter, r *http.Request) {
|
| 437 |
+
w.WriteHeader(405)
|
| 438 |
+
w.Write([]byte("root 405"))
|
| 439 |
+
})
|
| 440 |
+
|
| 441 |
+
sr1 := NewRouter()
|
| 442 |
+
sr1.Get("/sub1", func(w http.ResponseWriter, r *http.Request) {
|
| 443 |
+
w.Write([]byte("sub1"))
|
| 444 |
+
})
|
| 445 |
+
sr1.MethodNotAllowed(func(w http.ResponseWriter, r *http.Request) {
|
| 446 |
+
w.WriteHeader(405)
|
| 447 |
+
w.Write([]byte("sub1 405"))
|
| 448 |
+
})
|
| 449 |
+
|
| 450 |
+
sr2 := NewRouter()
|
| 451 |
+
sr2.Get("/sub2", func(w http.ResponseWriter, r *http.Request) {
|
| 452 |
+
w.Write([]byte("sub2"))
|
| 453 |
+
})
|
| 454 |
+
|
| 455 |
+
pathVar := NewRouter()
|
| 456 |
+
pathVar.Get("/{var}", func(w http.ResponseWriter, r *http.Request) {
|
| 457 |
+
w.Write([]byte("pv"))
|
| 458 |
+
})
|
| 459 |
+
pathVar.MethodNotAllowed(func(w http.ResponseWriter, r *http.Request) {
|
| 460 |
+
w.WriteHeader(405)
|
| 461 |
+
w.Write([]byte("pv 405"))
|
| 462 |
+
})
|
| 463 |
+
|
| 464 |
+
r.Mount("/prefix1", sr1)
|
| 465 |
+
r.Mount("/prefix2", sr2)
|
| 466 |
+
r.Mount("/pathVar", pathVar)
|
| 467 |
+
|
| 468 |
+
ts := httptest.NewServer(r)
|
| 469 |
+
defer ts.Close()
|
| 470 |
+
|
| 471 |
+
if _, body := testRequest(t, ts, "GET", "/root", nil); body != "root" {
|
| 472 |
+
t.Fatal(body)
|
| 473 |
+
}
|
| 474 |
+
if _, body := testRequest(t, ts, "PUT", "/root", nil); body != "root 405" {
|
| 475 |
+
t.Fatal(body)
|
| 476 |
+
}
|
| 477 |
+
if _, body := testRequest(t, ts, "GET", "/prefix1/sub1", nil); body != "sub1" {
|
| 478 |
+
t.Fatal(body)
|
| 479 |
+
}
|
| 480 |
+
if _, body := testRequest(t, ts, "PUT", "/prefix1/sub1", nil); body != "sub1 405" {
|
| 481 |
+
t.Fatal(body)
|
| 482 |
+
}
|
| 483 |
+
if _, body := testRequest(t, ts, "GET", "/prefix2/sub2", nil); body != "sub2" {
|
| 484 |
+
t.Fatal(body)
|
| 485 |
+
}
|
| 486 |
+
if _, body := testRequest(t, ts, "PUT", "/prefix2/sub2", nil); body != "root 405" {
|
| 487 |
+
t.Fatal(body)
|
| 488 |
+
}
|
| 489 |
+
if _, body := testRequest(t, ts, "GET", "/pathVar/myvar", nil); body != "pv" {
|
| 490 |
+
t.Fatal(body)
|
| 491 |
+
}
|
| 492 |
+
if _, body := testRequest(t, ts, "DELETE", "/pathVar/myvar", nil); body != "pv 405" {
|
| 493 |
+
t.Fatal(body)
|
| 494 |
+
}
|
| 495 |
+
}
|
| 496 |
+
|
| 497 |
+
func TestMuxComplicatedNotFound(t *testing.T) {
|
| 498 |
+
decorateRouter := func(r *Mux) {
|
| 499 |
+
// Root router with groups
|
| 500 |
+
r.Get("/auth", func(w http.ResponseWriter, r *http.Request) {
|
| 501 |
+
w.Write([]byte("auth get"))
|
| 502 |
+
})
|
| 503 |
+
r.Route("/public", func(r Router) {
|
| 504 |
+
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
| 505 |
+
w.Write([]byte("public get"))
|
| 506 |
+
})
|
| 507 |
+
})
|
| 508 |
+
|
| 509 |
+
// sub router with groups
|
| 510 |
+
sub0 := NewRouter()
|
| 511 |
+
sub0.Route("/resource", func(r Router) {
|
| 512 |
+
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
| 513 |
+
w.Write([]byte("private get"))
|
| 514 |
+
})
|
| 515 |
+
})
|
| 516 |
+
r.Mount("/private", sub0)
|
| 517 |
+
|
| 518 |
+
// sub router with groups
|
| 519 |
+
sub1 := NewRouter()
|
| 520 |
+
sub1.Route("/resource", func(r Router) {
|
| 521 |
+
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
| 522 |
+
w.Write([]byte("private get"))
|
| 523 |
+
})
|
| 524 |
+
})
|
| 525 |
+
r.With(func(next http.Handler) http.Handler { return next }).Mount("/private_mw", sub1)
|
| 526 |
+
}
|
| 527 |
+
|
| 528 |
+
testNotFound := func(t *testing.T, r *Mux) {
|
| 529 |
+
ts := httptest.NewServer(r)
|
| 530 |
+
defer ts.Close()
|
| 531 |
+
|
| 532 |
+
// check that we didn't break correct routes
|
| 533 |
+
if _, body := testRequest(t, ts, "GET", "/auth", nil); body != "auth get" {
|
| 534 |
+
t.Fatal(body)
|
| 535 |
+
}
|
| 536 |
+
if _, body := testRequest(t, ts, "GET", "/public", nil); body != "public get" {
|
| 537 |
+
t.Fatal(body)
|
| 538 |
+
}
|
| 539 |
+
if _, body := testRequest(t, ts, "GET", "/public/", nil); body != "public get" {
|
| 540 |
+
t.Fatal(body)
|
| 541 |
+
}
|
| 542 |
+
if _, body := testRequest(t, ts, "GET", "/private/resource", nil); body != "private get" {
|
| 543 |
+
t.Fatal(body)
|
| 544 |
+
}
|
| 545 |
+
// check custom not-found on all levels
|
| 546 |
+
if _, body := testRequest(t, ts, "GET", "/nope", nil); body != "custom not-found" {
|
| 547 |
+
t.Fatal(body)
|
| 548 |
+
}
|
| 549 |
+
if _, body := testRequest(t, ts, "GET", "/public/nope", nil); body != "custom not-found" {
|
| 550 |
+
t.Fatal(body)
|
| 551 |
+
}
|
| 552 |
+
if _, body := testRequest(t, ts, "GET", "/private/nope", nil); body != "custom not-found" {
|
| 553 |
+
t.Fatal(body)
|
| 554 |
+
}
|
| 555 |
+
if _, body := testRequest(t, ts, "GET", "/private/resource/nope", nil); body != "custom not-found" {
|
| 556 |
+
t.Fatal(body)
|
| 557 |
+
}
|
| 558 |
+
if _, body := testRequest(t, ts, "GET", "/private_mw/nope", nil); body != "custom not-found" {
|
| 559 |
+
t.Fatal(body)
|
| 560 |
+
}
|
| 561 |
+
if _, body := testRequest(t, ts, "GET", "/private_mw/resource/nope", nil); body != "custom not-found" {
|
| 562 |
+
t.Fatal(body)
|
| 563 |
+
}
|
| 564 |
+
// check custom not-found on trailing slash routes
|
| 565 |
+
if _, body := testRequest(t, ts, "GET", "/auth/", nil); body != "custom not-found" {
|
| 566 |
+
t.Fatal(body)
|
| 567 |
+
}
|
| 568 |
+
}
|
| 569 |
+
|
| 570 |
+
t.Run("pre", func(t *testing.T) {
|
| 571 |
+
r := NewRouter()
|
| 572 |
+
r.NotFound(func(w http.ResponseWriter, r *http.Request) {
|
| 573 |
+
w.Write([]byte("custom not-found"))
|
| 574 |
+
})
|
| 575 |
+
decorateRouter(r)
|
| 576 |
+
testNotFound(t, r)
|
| 577 |
+
})
|
| 578 |
+
|
| 579 |
+
t.Run("post", func(t *testing.T) {
|
| 580 |
+
r := NewRouter()
|
| 581 |
+
decorateRouter(r)
|
| 582 |
+
r.NotFound(func(w http.ResponseWriter, r *http.Request) {
|
| 583 |
+
w.Write([]byte("custom not-found"))
|
| 584 |
+
})
|
| 585 |
+
testNotFound(t, r)
|
| 586 |
+
})
|
| 587 |
+
}
|
| 588 |
+
|
| 589 |
+
func TestMuxWith(t *testing.T) {
|
| 590 |
+
var cmwInit1, cmwHandler1 uint64
|
| 591 |
+
var cmwInit2, cmwHandler2 uint64
|
| 592 |
+
mw1 := func(next http.Handler) http.Handler {
|
| 593 |
+
cmwInit1++
|
| 594 |
+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
| 595 |
+
cmwHandler1++
|
| 596 |
+
r = r.WithContext(context.WithValue(r.Context(), ctxKey{"inline1"}, "yes"))
|
| 597 |
+
next.ServeHTTP(w, r)
|
| 598 |
+
})
|
| 599 |
+
}
|
| 600 |
+
mw2 := func(next http.Handler) http.Handler {
|
| 601 |
+
cmwInit2++
|
| 602 |
+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
| 603 |
+
cmwHandler2++
|
| 604 |
+
r = r.WithContext(context.WithValue(r.Context(), ctxKey{"inline2"}, "yes"))
|
| 605 |
+
next.ServeHTTP(w, r)
|
| 606 |
+
})
|
| 607 |
+
}
|
| 608 |
+
|
| 609 |
+
r := NewRouter()
|
| 610 |
+
r.Get("/hi", func(w http.ResponseWriter, r *http.Request) {
|
| 611 |
+
w.Write([]byte("bye"))
|
| 612 |
+
})
|
| 613 |
+
r.With(mw1).With(mw2).Get("/inline", func(w http.ResponseWriter, r *http.Request) {
|
| 614 |
+
v1 := r.Context().Value(ctxKey{"inline1"}).(string)
|
| 615 |
+
v2 := r.Context().Value(ctxKey{"inline2"}).(string)
|
| 616 |
+
w.Write([]byte(fmt.Sprintf("inline %s %s", v1, v2)))
|
| 617 |
+
})
|
| 618 |
+
|
| 619 |
+
ts := httptest.NewServer(r)
|
| 620 |
+
defer ts.Close()
|
| 621 |
+
|
| 622 |
+
if _, body := testRequest(t, ts, "GET", "/hi", nil); body != "bye" {
|
| 623 |
+
t.Fatal(body)
|
| 624 |
+
}
|
| 625 |
+
if _, body := testRequest(t, ts, "GET", "/inline", nil); body != "inline yes yes" {
|
| 626 |
+
t.Fatal(body)
|
| 627 |
+
}
|
| 628 |
+
if cmwInit1 != 1 {
|
| 629 |
+
t.Fatalf("expecting cmwInit1 to be 1, got %d", cmwInit1)
|
| 630 |
+
}
|
| 631 |
+
if cmwHandler1 != 1 {
|
| 632 |
+
t.Fatalf("expecting cmwHandler1 to be 1, got %d", cmwHandler1)
|
| 633 |
+
}
|
| 634 |
+
if cmwInit2 != 1 {
|
| 635 |
+
t.Fatalf("expecting cmwInit2 to be 1, got %d", cmwInit2)
|
| 636 |
+
}
|
| 637 |
+
if cmwHandler2 != 1 {
|
| 638 |
+
t.Fatalf("expecting cmwHandler2 to be 1, got %d", cmwHandler2)
|
| 639 |
+
}
|
| 640 |
+
}
|
| 641 |
+
|
| 642 |
+
func TestMuxHandlePatternValidation(t *testing.T) {
|
| 643 |
+
testCases := []struct {
|
| 644 |
+
name string
|
| 645 |
+
pattern string
|
| 646 |
+
method string
|
| 647 |
+
path string
|
| 648 |
+
expectedBody string
|
| 649 |
+
expectedStatus int
|
| 650 |
+
shouldPanic bool
|
| 651 |
+
}{
|
| 652 |
+
// Valid patterns
|
| 653 |
+
{
|
| 654 |
+
name: "Valid pattern without HTTP GET",
|
| 655 |
+
pattern: "/user/{id}",
|
| 656 |
+
shouldPanic: false,
|
| 657 |
+
method: "GET",
|
| 658 |
+
path: "/user/123",
|
| 659 |
+
expectedBody: "without-prefix GET",
|
| 660 |
+
expectedStatus: http.StatusOK,
|
| 661 |
+
},
|
| 662 |
+
{
|
| 663 |
+
name: "Valid pattern with HTTP POST",
|
| 664 |
+
pattern: "POST /products/{id}",
|
| 665 |
+
shouldPanic: false,
|
| 666 |
+
method: "POST",
|
| 667 |
+
path: "/products/456",
|
| 668 |
+
expectedBody: "with-prefix POST",
|
| 669 |
+
expectedStatus: http.StatusOK,
|
| 670 |
+
},
|
| 671 |
+
// Invalid patterns
|
| 672 |
+
{
|
| 673 |
+
name: "Invalid pattern with no method",
|
| 674 |
+
pattern: "INVALID/user/{id}",
|
| 675 |
+
shouldPanic: true,
|
| 676 |
+
},
|
| 677 |
+
{
|
| 678 |
+
name: "Invalid pattern with supported method",
|
| 679 |
+
pattern: "GET/user/{id}",
|
| 680 |
+
shouldPanic: true,
|
| 681 |
+
},
|
| 682 |
+
{
|
| 683 |
+
name: "Invalid pattern with unsupported method",
|
| 684 |
+
pattern: "UNSUPPORTED /unsupported-method",
|
| 685 |
+
shouldPanic: true,
|
| 686 |
+
},
|
| 687 |
+
}
|
| 688 |
+
|
| 689 |
+
for _, tc := range testCases {
|
| 690 |
+
t.Run(tc.name, func(t *testing.T) {
|
| 691 |
+
defer func() {
|
| 692 |
+
if r := recover(); r != nil && !tc.shouldPanic {
|
| 693 |
+
t.Errorf("Unexpected panic for pattern %s:\n%v", tc.pattern, r)
|
| 694 |
+
}
|
| 695 |
+
}()
|
| 696 |
+
|
| 697 |
+
r1 := NewRouter()
|
| 698 |
+
r1.Handle(tc.pattern, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
| 699 |
+
w.Write([]byte(tc.expectedBody))
|
| 700 |
+
}))
|
| 701 |
+
|
| 702 |
+
// Test that HandleFunc also handles method patterns
|
| 703 |
+
r2 := NewRouter()
|
| 704 |
+
r2.HandleFunc(tc.pattern, func(w http.ResponseWriter, r *http.Request) {
|
| 705 |
+
w.Write([]byte(tc.expectedBody))
|
| 706 |
+
})
|
| 707 |
+
|
| 708 |
+
if !tc.shouldPanic {
|
| 709 |
+
for _, r := range []Router{r1, r2} {
|
| 710 |
+
// Use testRequest for valid patterns
|
| 711 |
+
ts := httptest.NewServer(r)
|
| 712 |
+
defer ts.Close()
|
| 713 |
+
|
| 714 |
+
resp, body := testRequest(t, ts, tc.method, tc.path, nil)
|
| 715 |
+
if body != tc.expectedBody || resp.StatusCode != tc.expectedStatus {
|
| 716 |
+
t.Errorf("Expected status %d and body %s; got status %d and body %s for pattern %s",
|
| 717 |
+
tc.expectedStatus, tc.expectedBody, resp.StatusCode, body, tc.pattern)
|
| 718 |
+
}
|
| 719 |
+
}
|
| 720 |
+
}
|
| 721 |
+
})
|
| 722 |
+
}
|
| 723 |
+
}
|
| 724 |
+
|
| 725 |
+
func TestRouterFromMuxWith(t *testing.T) {
|
| 726 |
+
t.Parallel()
|
| 727 |
+
|
| 728 |
+
r := NewRouter()
|
| 729 |
+
|
| 730 |
+
with := r.With(func(next http.Handler) http.Handler {
|
| 731 |
+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
| 732 |
+
next.ServeHTTP(w, r)
|
| 733 |
+
})
|
| 734 |
+
})
|
| 735 |
+
|
| 736 |
+
with.Get("/with_middleware", func(w http.ResponseWriter, r *http.Request) {})
|
| 737 |
+
|
| 738 |
+
ts := httptest.NewServer(with)
|
| 739 |
+
defer ts.Close()
|
| 740 |
+
|
| 741 |
+
// Without the fix this test was committed with, this causes a panic.
|
| 742 |
+
testRequest(t, ts, http.MethodGet, "/with_middleware", nil)
|
| 743 |
+
}
|
| 744 |
+
|
| 745 |
+
func TestMuxMiddlewareStack(t *testing.T) {
|
| 746 |
+
var stdmwInit, stdmwHandler uint64
|
| 747 |
+
stdmw := func(next http.Handler) http.Handler {
|
| 748 |
+
stdmwInit++
|
| 749 |
+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
| 750 |
+
stdmwHandler++
|
| 751 |
+
next.ServeHTTP(w, r)
|
| 752 |
+
})
|
| 753 |
+
}
|
| 754 |
+
_ = stdmw
|
| 755 |
+
|
| 756 |
+
var ctxmwInit, ctxmwHandler uint64
|
| 757 |
+
ctxmw := func(next http.Handler) http.Handler {
|
| 758 |
+
ctxmwInit++
|
| 759 |
+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
| 760 |
+
ctxmwHandler++
|
| 761 |
+
ctx := r.Context()
|
| 762 |
+
ctx = context.WithValue(ctx, ctxKey{"count.ctxmwHandler"}, ctxmwHandler)
|
| 763 |
+
r = r.WithContext(ctx)
|
| 764 |
+
next.ServeHTTP(w, r)
|
| 765 |
+
})
|
| 766 |
+
}
|
| 767 |
+
|
| 768 |
+
var inCtxmwInit, inCtxmwHandler uint64
|
| 769 |
+
inCtxmw := func(next http.Handler) http.Handler {
|
| 770 |
+
inCtxmwInit++
|
| 771 |
+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
| 772 |
+
inCtxmwHandler++
|
| 773 |
+
next.ServeHTTP(w, r)
|
| 774 |
+
})
|
| 775 |
+
}
|
| 776 |
+
|
| 777 |
+
r := NewRouter()
|
| 778 |
+
r.Use(stdmw)
|
| 779 |
+
r.Use(ctxmw)
|
| 780 |
+
r.Use(func(next http.Handler) http.Handler {
|
| 781 |
+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
| 782 |
+
if r.URL.Path == "/ping" {
|
| 783 |
+
w.Write([]byte("pong"))
|
| 784 |
+
return
|
| 785 |
+
}
|
| 786 |
+
next.ServeHTTP(w, r)
|
| 787 |
+
})
|
| 788 |
+
})
|
| 789 |
+
|
| 790 |
+
var handlerCount uint64
|
| 791 |
+
|
| 792 |
+
r.With(inCtxmw).Get("/", func(w http.ResponseWriter, r *http.Request) {
|
| 793 |
+
handlerCount++
|
| 794 |
+
ctx := r.Context()
|
| 795 |
+
ctxmwHandlerCount := ctx.Value(ctxKey{"count.ctxmwHandler"}).(uint64)
|
| 796 |
+
w.Write([]byte(fmt.Sprintf("inits:%d reqs:%d ctxValue:%d", ctxmwInit, handlerCount, ctxmwHandlerCount)))
|
| 797 |
+
})
|
| 798 |
+
|
| 799 |
+
r.Get("/hi", func(w http.ResponseWriter, r *http.Request) {
|
| 800 |
+
w.Write([]byte("wooot"))
|
| 801 |
+
})
|
| 802 |
+
|
| 803 |
+
ts := httptest.NewServer(r)
|
| 804 |
+
defer ts.Close()
|
| 805 |
+
|
| 806 |
+
testRequest(t, ts, "GET", "/", nil)
|
| 807 |
+
testRequest(t, ts, "GET", "/", nil)
|
| 808 |
+
var body string
|
| 809 |
+
_, body = testRequest(t, ts, "GET", "/", nil)
|
| 810 |
+
if body != "inits:1 reqs:3 ctxValue:3" {
|
| 811 |
+
t.Fatalf("got: '%s'", body)
|
| 812 |
+
}
|
| 813 |
+
|
| 814 |
+
_, body = testRequest(t, ts, "GET", "/ping", nil)
|
| 815 |
+
if body != "pong" {
|
| 816 |
+
t.Fatalf("got: '%s'", body)
|
| 817 |
+
}
|
| 818 |
+
}
|
| 819 |
+
|
| 820 |
+
func TestMuxRouteGroups(t *testing.T) {
|
| 821 |
+
var stdmwInit, stdmwHandler uint64
|
| 822 |
+
|
| 823 |
+
stdmw := func(next http.Handler) http.Handler {
|
| 824 |
+
stdmwInit++
|
| 825 |
+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
| 826 |
+
stdmwHandler++
|
| 827 |
+
next.ServeHTTP(w, r)
|
| 828 |
+
})
|
| 829 |
+
}
|
| 830 |
+
|
| 831 |
+
var stdmwInit2, stdmwHandler2 uint64
|
| 832 |
+
stdmw2 := func(next http.Handler) http.Handler {
|
| 833 |
+
stdmwInit2++
|
| 834 |
+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
| 835 |
+
stdmwHandler2++
|
| 836 |
+
next.ServeHTTP(w, r)
|
| 837 |
+
})
|
| 838 |
+
}
|
| 839 |
+
|
| 840 |
+
r := NewRouter()
|
| 841 |
+
r.Group(func(r Router) {
|
| 842 |
+
r.Use(stdmw)
|
| 843 |
+
r.Get("/group", func(w http.ResponseWriter, r *http.Request) {
|
| 844 |
+
w.Write([]byte("root group"))
|
| 845 |
+
})
|
| 846 |
+
})
|
| 847 |
+
r.Group(func(r Router) {
|
| 848 |
+
r.Use(stdmw2)
|
| 849 |
+
r.Get("/group2", func(w http.ResponseWriter, r *http.Request) {
|
| 850 |
+
w.Write([]byte("root group2"))
|
| 851 |
+
})
|
| 852 |
+
})
|
| 853 |
+
|
| 854 |
+
ts := httptest.NewServer(r)
|
| 855 |
+
defer ts.Close()
|
| 856 |
+
|
| 857 |
+
// GET /group
|
| 858 |
+
_, body := testRequest(t, ts, "GET", "/group", nil)
|
| 859 |
+
if body != "root group" {
|
| 860 |
+
t.Fatalf("got: '%s'", body)
|
| 861 |
+
}
|
| 862 |
+
if stdmwInit != 1 || stdmwHandler != 1 {
|
| 863 |
+
t.Logf("stdmw counters failed, should be 1:1, got %d:%d", stdmwInit, stdmwHandler)
|
| 864 |
+
}
|
| 865 |
+
|
| 866 |
+
// GET /group2
|
| 867 |
+
_, body = testRequest(t, ts, "GET", "/group2", nil)
|
| 868 |
+
if body != "root group2" {
|
| 869 |
+
t.Fatalf("got: '%s'", body)
|
| 870 |
+
}
|
| 871 |
+
if stdmwInit2 != 1 || stdmwHandler2 != 1 {
|
| 872 |
+
t.Fatalf("stdmw2 counters failed, should be 1:1, got %d:%d", stdmwInit2, stdmwHandler2)
|
| 873 |
+
}
|
| 874 |
+
}
|
| 875 |
+
|
| 876 |
+
func TestMuxBig(t *testing.T) {
|
| 877 |
+
r := bigMux()
|
| 878 |
+
|
| 879 |
+
ts := httptest.NewServer(r)
|
| 880 |
+
defer ts.Close()
|
| 881 |
+
|
| 882 |
+
var body, expected string
|
| 883 |
+
|
| 884 |
+
_, body = testRequest(t, ts, "GET", "/favicon.ico", nil)
|
| 885 |
+
if body != "fav" {
|
| 886 |
+
t.Fatalf("got '%s'", body)
|
| 887 |
+
}
|
| 888 |
+
_, body = testRequest(t, ts, "GET", "/hubs/4/view", nil)
|
| 889 |
+
if body != "/hubs/4/view reqid:1 session:anonymous" {
|
| 890 |
+
t.Fatalf("got '%v'", body)
|
| 891 |
+
}
|
| 892 |
+
_, body = testRequest(t, ts, "GET", "/hubs/4/view/index.html", nil)
|
| 893 |
+
if body != "/hubs/4/view/index.html reqid:1 session:anonymous" {
|
| 894 |
+
t.Fatalf("got '%s'", body)
|
| 895 |
+
}
|
| 896 |
+
_, body = testRequest(t, ts, "POST", "/hubs/ethereumhub/view/index.html", nil)
|
| 897 |
+
if body != "/hubs/ethereumhub/view/index.html reqid:1 session:anonymous" {
|
| 898 |
+
t.Fatalf("got '%s'", body)
|
| 899 |
+
}
|
| 900 |
+
_, body = testRequest(t, ts, "GET", "/", nil)
|
| 901 |
+
if body != "/ reqid:1 session:elvis" {
|
| 902 |
+
t.Fatalf("got '%s'", body)
|
| 903 |
+
}
|
| 904 |
+
_, body = testRequest(t, ts, "GET", "/suggestions", nil)
|
| 905 |
+
if body != "/suggestions reqid:1 session:elvis" {
|
| 906 |
+
t.Fatalf("got '%s'", body)
|
| 907 |
+
}
|
| 908 |
+
_, body = testRequest(t, ts, "GET", "/woot/444/hiiii", nil)
|
| 909 |
+
if body != "/woot/444/hiiii" {
|
| 910 |
+
t.Fatalf("got '%s'", body)
|
| 911 |
+
}
|
| 912 |
+
_, body = testRequest(t, ts, "GET", "/hubs/123", nil)
|
| 913 |
+
expected = "/hubs/123 reqid:1 session:elvis"
|
| 914 |
+
if body != expected {
|
| 915 |
+
t.Fatalf("expected:%s got:%s", expected, body)
|
| 916 |
+
}
|
| 917 |
+
_, body = testRequest(t, ts, "GET", "/hubs/123/touch", nil)
|
| 918 |
+
if body != "/hubs/123/touch reqid:1 session:elvis" {
|
| 919 |
+
t.Fatalf("got '%s'", body)
|
| 920 |
+
}
|
| 921 |
+
_, body = testRequest(t, ts, "GET", "/hubs/123/webhooks", nil)
|
| 922 |
+
if body != "/hubs/123/webhooks reqid:1 session:elvis" {
|
| 923 |
+
t.Fatalf("got '%s'", body)
|
| 924 |
+
}
|
| 925 |
+
_, body = testRequest(t, ts, "GET", "/hubs/123/posts", nil)
|
| 926 |
+
if body != "/hubs/123/posts reqid:1 session:elvis" {
|
| 927 |
+
t.Fatalf("got '%s'", body)
|
| 928 |
+
}
|
| 929 |
+
_, body = testRequest(t, ts, "GET", "/folders", nil)
|
| 930 |
+
if body != "404 page not found\n" {
|
| 931 |
+
t.Fatalf("got '%s'", body)
|
| 932 |
+
}
|
| 933 |
+
_, body = testRequest(t, ts, "GET", "/folders/", nil)
|
| 934 |
+
if body != "/folders/ reqid:1 session:elvis" {
|
| 935 |
+
t.Fatalf("got '%s'", body)
|
| 936 |
+
}
|
| 937 |
+
_, body = testRequest(t, ts, "GET", "/folders/public", nil)
|
| 938 |
+
if body != "/folders/public reqid:1 session:elvis" {
|
| 939 |
+
t.Fatalf("got '%s'", body)
|
| 940 |
+
}
|
| 941 |
+
_, body = testRequest(t, ts, "GET", "/folders/nothing", nil)
|
| 942 |
+
if body != "404 page not found\n" {
|
| 943 |
+
t.Fatalf("got '%s'", body)
|
| 944 |
+
}
|
| 945 |
+
}
|
| 946 |
+
|
| 947 |
+
func bigMux() Router {
|
| 948 |
+
var r *Mux
|
| 949 |
+
var sr3 *Mux
|
| 950 |
+
// var sr1, sr2, sr3, sr4, sr5, sr6 *Mux
|
| 951 |
+
r = NewRouter()
|
| 952 |
+
r.Use(func(next http.Handler) http.Handler {
|
| 953 |
+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
| 954 |
+
ctx := context.WithValue(r.Context(), ctxKey{"requestID"}, "1")
|
| 955 |
+
next.ServeHTTP(w, r.WithContext(ctx))
|
| 956 |
+
})
|
| 957 |
+
})
|
| 958 |
+
r.Use(func(next http.Handler) http.Handler {
|
| 959 |
+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
| 960 |
+
next.ServeHTTP(w, r)
|
| 961 |
+
})
|
| 962 |
+
})
|
| 963 |
+
r.Group(func(r Router) {
|
| 964 |
+
r.Use(func(next http.Handler) http.Handler {
|
| 965 |
+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
| 966 |
+
ctx := context.WithValue(r.Context(), ctxKey{"session.user"}, "anonymous")
|
| 967 |
+
next.ServeHTTP(w, r.WithContext(ctx))
|
| 968 |
+
})
|
| 969 |
+
})
|
| 970 |
+
r.Get("/favicon.ico", func(w http.ResponseWriter, r *http.Request) {
|
| 971 |
+
w.Write([]byte("fav"))
|
| 972 |
+
})
|
| 973 |
+
r.Get("/hubs/{hubID}/view", func(w http.ResponseWriter, r *http.Request) {
|
| 974 |
+
ctx := r.Context()
|
| 975 |
+
s := fmt.Sprintf("/hubs/%s/view reqid:%s session:%s", URLParam(r, "hubID"),
|
| 976 |
+
ctx.Value(ctxKey{"requestID"}), ctx.Value(ctxKey{"session.user"}))
|
| 977 |
+
w.Write([]byte(s))
|
| 978 |
+
})
|
| 979 |
+
r.Get("/hubs/{hubID}/view/*", func(w http.ResponseWriter, r *http.Request) {
|
| 980 |
+
ctx := r.Context()
|
| 981 |
+
s := fmt.Sprintf("/hubs/%s/view/%s reqid:%s session:%s", URLParamFromCtx(ctx, "hubID"),
|
| 982 |
+
URLParam(r, "*"), ctx.Value(ctxKey{"requestID"}), ctx.Value(ctxKey{"session.user"}))
|
| 983 |
+
w.Write([]byte(s))
|
| 984 |
+
})
|
| 985 |
+
r.Post("/hubs/{hubSlug}/view/*", func(w http.ResponseWriter, r *http.Request) {
|
| 986 |
+
ctx := r.Context()
|
| 987 |
+
s := fmt.Sprintf("/hubs/%s/view/%s reqid:%s session:%s", URLParamFromCtx(ctx, "hubSlug"),
|
| 988 |
+
URLParam(r, "*"), ctx.Value(ctxKey{"requestID"}), ctx.Value(ctxKey{"session.user"}))
|
| 989 |
+
w.Write([]byte(s))
|
| 990 |
+
})
|
| 991 |
+
})
|
| 992 |
+
r.Group(func(r Router) {
|
| 993 |
+
r.Use(func(next http.Handler) http.Handler {
|
| 994 |
+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
| 995 |
+
ctx := context.WithValue(r.Context(), ctxKey{"session.user"}, "elvis")
|
| 996 |
+
next.ServeHTTP(w, r.WithContext(ctx))
|
| 997 |
+
})
|
| 998 |
+
})
|
| 999 |
+
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
| 1000 |
+
ctx := r.Context()
|
| 1001 |
+
s := fmt.Sprintf("/ reqid:%s session:%s", ctx.Value(ctxKey{"requestID"}), ctx.Value(ctxKey{"session.user"}))
|
| 1002 |
+
w.Write([]byte(s))
|
| 1003 |
+
})
|
| 1004 |
+
r.Get("/suggestions", func(w http.ResponseWriter, r *http.Request) {
|
| 1005 |
+
ctx := r.Context()
|
| 1006 |
+
s := fmt.Sprintf("/suggestions reqid:%s session:%s", ctx.Value(ctxKey{"requestID"}), ctx.Value(ctxKey{"session.user"}))
|
| 1007 |
+
w.Write([]byte(s))
|
| 1008 |
+
})
|
| 1009 |
+
|
| 1010 |
+
r.Get("/woot/{wootID}/*", func(w http.ResponseWriter, r *http.Request) {
|
| 1011 |
+
s := fmt.Sprintf("/woot/%s/%s", URLParam(r, "wootID"), URLParam(r, "*"))
|
| 1012 |
+
w.Write([]byte(s))
|
| 1013 |
+
})
|
| 1014 |
+
|
| 1015 |
+
r.Route("/hubs", func(r Router) {
|
| 1016 |
+
_ = r.(*Mux) // sr1
|
| 1017 |
+
r.Route("/{hubID}", func(r Router) {
|
| 1018 |
+
_ = r.(*Mux) // sr2
|
| 1019 |
+
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
| 1020 |
+
ctx := r.Context()
|
| 1021 |
+
s := fmt.Sprintf("/hubs/%s reqid:%s session:%s",
|
| 1022 |
+
URLParam(r, "hubID"), ctx.Value(ctxKey{"requestID"}), ctx.Value(ctxKey{"session.user"}))
|
| 1023 |
+
w.Write([]byte(s))
|
| 1024 |
+
})
|
| 1025 |
+
r.Get("/touch", func(w http.ResponseWriter, r *http.Request) {
|
| 1026 |
+
ctx := r.Context()
|
| 1027 |
+
s := fmt.Sprintf("/hubs/%s/touch reqid:%s session:%s", URLParam(r, "hubID"),
|
| 1028 |
+
ctx.Value(ctxKey{"requestID"}), ctx.Value(ctxKey{"session.user"}))
|
| 1029 |
+
w.Write([]byte(s))
|
| 1030 |
+
})
|
| 1031 |
+
|
| 1032 |
+
sr3 = NewRouter()
|
| 1033 |
+
sr3.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
| 1034 |
+
ctx := r.Context()
|
| 1035 |
+
s := fmt.Sprintf("/hubs/%s/webhooks reqid:%s session:%s", URLParam(r, "hubID"),
|
| 1036 |
+
ctx.Value(ctxKey{"requestID"}), ctx.Value(ctxKey{"session.user"}))
|
| 1037 |
+
w.Write([]byte(s))
|
| 1038 |
+
})
|
| 1039 |
+
sr3.Route("/{webhookID}", func(r Router) {
|
| 1040 |
+
_ = r.(*Mux) // sr4
|
| 1041 |
+
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
| 1042 |
+
ctx := r.Context()
|
| 1043 |
+
s := fmt.Sprintf("/hubs/%s/webhooks/%s reqid:%s session:%s", URLParam(r, "hubID"),
|
| 1044 |
+
URLParam(r, "webhookID"), ctx.Value(ctxKey{"requestID"}), ctx.Value(ctxKey{"session.user"}))
|
| 1045 |
+
w.Write([]byte(s))
|
| 1046 |
+
})
|
| 1047 |
+
})
|
| 1048 |
+
|
| 1049 |
+
r.Mount("/webhooks", Chain(func(next http.Handler) http.Handler {
|
| 1050 |
+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
| 1051 |
+
next.ServeHTTP(w, r.WithContext(context.WithValue(r.Context(), ctxKey{"hook"}, true)))
|
| 1052 |
+
})
|
| 1053 |
+
}).Handler(sr3))
|
| 1054 |
+
|
| 1055 |
+
r.Route("/posts", func(r Router) {
|
| 1056 |
+
_ = r.(*Mux) // sr5
|
| 1057 |
+
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
| 1058 |
+
ctx := r.Context()
|
| 1059 |
+
s := fmt.Sprintf("/hubs/%s/posts reqid:%s session:%s", URLParam(r, "hubID"),
|
| 1060 |
+
ctx.Value(ctxKey{"requestID"}), ctx.Value(ctxKey{"session.user"}))
|
| 1061 |
+
w.Write([]byte(s))
|
| 1062 |
+
})
|
| 1063 |
+
})
|
| 1064 |
+
})
|
| 1065 |
+
})
|
| 1066 |
+
|
| 1067 |
+
r.Route("/folders/", func(r Router) {
|
| 1068 |
+
_ = r.(*Mux) // sr6
|
| 1069 |
+
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
| 1070 |
+
ctx := r.Context()
|
| 1071 |
+
s := fmt.Sprintf("/folders/ reqid:%s session:%s",
|
| 1072 |
+
ctx.Value(ctxKey{"requestID"}), ctx.Value(ctxKey{"session.user"}))
|
| 1073 |
+
w.Write([]byte(s))
|
| 1074 |
+
})
|
| 1075 |
+
r.Get("/public", func(w http.ResponseWriter, r *http.Request) {
|
| 1076 |
+
ctx := r.Context()
|
| 1077 |
+
s := fmt.Sprintf("/folders/public reqid:%s session:%s",
|
| 1078 |
+
ctx.Value(ctxKey{"requestID"}), ctx.Value(ctxKey{"session.user"}))
|
| 1079 |
+
w.Write([]byte(s))
|
| 1080 |
+
})
|
| 1081 |
+
})
|
| 1082 |
+
})
|
| 1083 |
+
|
| 1084 |
+
return r
|
| 1085 |
+
}
|
| 1086 |
+
|
| 1087 |
+
func TestMuxSubroutesBasic(t *testing.T) {
|
| 1088 |
+
hIndex := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
| 1089 |
+
w.Write([]byte("index"))
|
| 1090 |
+
})
|
| 1091 |
+
hArticlesList := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
| 1092 |
+
w.Write([]byte("articles-list"))
|
| 1093 |
+
})
|
| 1094 |
+
hSearchArticles := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
| 1095 |
+
w.Write([]byte("search-articles"))
|
| 1096 |
+
})
|
| 1097 |
+
hGetArticle := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
| 1098 |
+
w.Write([]byte(fmt.Sprintf("get-article:%s", URLParam(r, "id"))))
|
| 1099 |
+
})
|
| 1100 |
+
hSyncArticle := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
| 1101 |
+
w.Write([]byte(fmt.Sprintf("sync-article:%s", URLParam(r, "id"))))
|
| 1102 |
+
})
|
| 1103 |
+
|
| 1104 |
+
r := NewRouter()
|
| 1105 |
+
// var rr1, rr2 *Mux
|
| 1106 |
+
r.Get("/", hIndex)
|
| 1107 |
+
r.Route("/articles", func(r Router) {
|
| 1108 |
+
// rr1 = r.(*Mux)
|
| 1109 |
+
r.Get("/", hArticlesList)
|
| 1110 |
+
r.Get("/search", hSearchArticles)
|
| 1111 |
+
r.Route("/{id}", func(r Router) {
|
| 1112 |
+
// rr2 = r.(*Mux)
|
| 1113 |
+
r.Get("/", hGetArticle)
|
| 1114 |
+
r.Get("/sync", hSyncArticle)
|
| 1115 |
+
})
|
| 1116 |
+
})
|
| 1117 |
+
|
| 1118 |
+
// log.Println("~~~~~~~~~")
|
| 1119 |
+
// log.Println("~~~~~~~~~")
|
| 1120 |
+
// debugPrintTree(0, 0, r.tree, 0)
|
| 1121 |
+
// log.Println("~~~~~~~~~")
|
| 1122 |
+
// log.Println("~~~~~~~~~")
|
| 1123 |
+
|
| 1124 |
+
// log.Println("~~~~~~~~~")
|
| 1125 |
+
// log.Println("~~~~~~~~~")
|
| 1126 |
+
// debugPrintTree(0, 0, rr1.tree, 0)
|
| 1127 |
+
// log.Println("~~~~~~~~~")
|
| 1128 |
+
// log.Println("~~~~~~~~~")
|
| 1129 |
+
|
| 1130 |
+
// log.Println("~~~~~~~~~")
|
| 1131 |
+
// log.Println("~~~~~~~~~")
|
| 1132 |
+
// debugPrintTree(0, 0, rr2.tree, 0)
|
| 1133 |
+
// log.Println("~~~~~~~~~")
|
| 1134 |
+
// log.Println("~~~~~~~~~")
|
| 1135 |
+
|
| 1136 |
+
ts := httptest.NewServer(r)
|
| 1137 |
+
defer ts.Close()
|
| 1138 |
+
|
| 1139 |
+
var body, expected string
|
| 1140 |
+
|
| 1141 |
+
_, body = testRequest(t, ts, "GET", "/", nil)
|
| 1142 |
+
expected = "index"
|
| 1143 |
+
if body != expected {
|
| 1144 |
+
t.Fatalf("expected:%s got:%s", expected, body)
|
| 1145 |
+
}
|
| 1146 |
+
_, body = testRequest(t, ts, "GET", "/articles", nil)
|
| 1147 |
+
expected = "articles-list"
|
| 1148 |
+
if body != expected {
|
| 1149 |
+
t.Fatalf("expected:%s got:%s", expected, body)
|
| 1150 |
+
}
|
| 1151 |
+
_, body = testRequest(t, ts, "GET", "/articles/search", nil)
|
| 1152 |
+
expected = "search-articles"
|
| 1153 |
+
if body != expected {
|
| 1154 |
+
t.Fatalf("expected:%s got:%s", expected, body)
|
| 1155 |
+
}
|
| 1156 |
+
_, body = testRequest(t, ts, "GET", "/articles/123", nil)
|
| 1157 |
+
expected = "get-article:123"
|
| 1158 |
+
if body != expected {
|
| 1159 |
+
t.Fatalf("expected:%s got:%s", expected, body)
|
| 1160 |
+
}
|
| 1161 |
+
_, body = testRequest(t, ts, "GET", "/articles/123/sync", nil)
|
| 1162 |
+
expected = "sync-article:123"
|
| 1163 |
+
if body != expected {
|
| 1164 |
+
t.Fatalf("expected:%s got:%s", expected, body)
|
| 1165 |
+
}
|
| 1166 |
+
}
|
| 1167 |
+
|
| 1168 |
+
func TestMuxSubroutes(t *testing.T) {
|
| 1169 |
+
hHubView1 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
| 1170 |
+
w.Write([]byte("hub1"))
|
| 1171 |
+
})
|
| 1172 |
+
hHubView2 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
| 1173 |
+
w.Write([]byte("hub2"))
|
| 1174 |
+
})
|
| 1175 |
+
hHubView3 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
| 1176 |
+
w.Write([]byte("hub3"))
|
| 1177 |
+
})
|
| 1178 |
+
hAccountView1 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
| 1179 |
+
w.Write([]byte("account1"))
|
| 1180 |
+
})
|
| 1181 |
+
hAccountView2 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
| 1182 |
+
w.Write([]byte("account2"))
|
| 1183 |
+
})
|
| 1184 |
+
|
| 1185 |
+
r := NewRouter()
|
| 1186 |
+
r.Get("/hubs/{hubID}/view", hHubView1)
|
| 1187 |
+
r.Get("/hubs/{hubID}/view/*", hHubView2)
|
| 1188 |
+
|
| 1189 |
+
sr := NewRouter()
|
| 1190 |
+
sr.Get("/", hHubView3)
|
| 1191 |
+
r.Mount("/hubs/{hubID}/users", sr)
|
| 1192 |
+
r.Get("/hubs/{hubID}/users/", func(w http.ResponseWriter, r *http.Request) {
|
| 1193 |
+
w.Write([]byte("hub3 override"))
|
| 1194 |
+
})
|
| 1195 |
+
|
| 1196 |
+
sr3 := NewRouter()
|
| 1197 |
+
sr3.Get("/", hAccountView1)
|
| 1198 |
+
sr3.Get("/hi", hAccountView2)
|
| 1199 |
+
|
| 1200 |
+
// var sr2 *Mux
|
| 1201 |
+
r.Route("/accounts/{accountID}", func(r Router) {
|
| 1202 |
+
_ = r.(*Mux) // sr2
|
| 1203 |
+
// r.Get("/", hAccountView1)
|
| 1204 |
+
r.Mount("/", sr3)
|
| 1205 |
+
})
|
| 1206 |
+
|
| 1207 |
+
// This is the same as the r.Route() call mounted on sr2
|
| 1208 |
+
// sr2 := NewRouter()
|
| 1209 |
+
// sr2.Mount("/", sr3)
|
| 1210 |
+
// r.Mount("/accounts/{accountID}", sr2)
|
| 1211 |
+
|
| 1212 |
+
ts := httptest.NewServer(r)
|
| 1213 |
+
defer ts.Close()
|
| 1214 |
+
|
| 1215 |
+
var body, expected string
|
| 1216 |
+
|
| 1217 |
+
_, body = testRequest(t, ts, "GET", "/hubs/123/view", nil)
|
| 1218 |
+
expected = "hub1"
|
| 1219 |
+
if body != expected {
|
| 1220 |
+
t.Fatalf("expected:%s got:%s", expected, body)
|
| 1221 |
+
}
|
| 1222 |
+
_, body = testRequest(t, ts, "GET", "/hubs/123/view/index.html", nil)
|
| 1223 |
+
expected = "hub2"
|
| 1224 |
+
if body != expected {
|
| 1225 |
+
t.Fatalf("expected:%s got:%s", expected, body)
|
| 1226 |
+
}
|
| 1227 |
+
_, body = testRequest(t, ts, "GET", "/hubs/123/users", nil)
|
| 1228 |
+
expected = "hub3"
|
| 1229 |
+
if body != expected {
|
| 1230 |
+
t.Fatalf("expected:%s got:%s", expected, body)
|
| 1231 |
+
}
|
| 1232 |
+
_, body = testRequest(t, ts, "GET", "/hubs/123/users/", nil)
|
| 1233 |
+
expected = "hub3 override"
|
| 1234 |
+
if body != expected {
|
| 1235 |
+
t.Fatalf("expected:%s got:%s", expected, body)
|
| 1236 |
+
}
|
| 1237 |
+
_, body = testRequest(t, ts, "GET", "/accounts/44", nil)
|
| 1238 |
+
expected = "account1"
|
| 1239 |
+
if body != expected {
|
| 1240 |
+
t.Fatalf("request:%s expected:%s got:%s", "GET /accounts/44", expected, body)
|
| 1241 |
+
}
|
| 1242 |
+
_, body = testRequest(t, ts, "GET", "/accounts/44/hi", nil)
|
| 1243 |
+
expected = "account2"
|
| 1244 |
+
if body != expected {
|
| 1245 |
+
t.Fatalf("expected:%s got:%s", expected, body)
|
| 1246 |
+
}
|
| 1247 |
+
|
| 1248 |
+
// Test that we're building the routingPatterns properly
|
| 1249 |
+
router := r
|
| 1250 |
+
req, _ := http.NewRequest("GET", "/accounts/44/hi", nil)
|
| 1251 |
+
|
| 1252 |
+
rctx := NewRouteContext()
|
| 1253 |
+
req = req.WithContext(context.WithValue(req.Context(), RouteCtxKey, rctx))
|
| 1254 |
+
|
| 1255 |
+
w := httptest.NewRecorder()
|
| 1256 |
+
router.ServeHTTP(w, req)
|
| 1257 |
+
|
| 1258 |
+
body = w.Body.String()
|
| 1259 |
+
expected = "account2"
|
| 1260 |
+
if body != expected {
|
| 1261 |
+
t.Fatalf("expected:%s got:%s", expected, body)
|
| 1262 |
+
}
|
| 1263 |
+
|
| 1264 |
+
routePatterns := rctx.RoutePatterns
|
| 1265 |
+
if len(rctx.RoutePatterns) != 3 {
|
| 1266 |
+
t.Fatalf("expected 3 routing patterns, got:%d", len(rctx.RoutePatterns))
|
| 1267 |
+
}
|
| 1268 |
+
expected = "/accounts/{accountID}/*"
|
| 1269 |
+
if routePatterns[0] != expected {
|
| 1270 |
+
t.Fatalf("routePattern, expected:%s got:%s", expected, routePatterns[0])
|
| 1271 |
+
}
|
| 1272 |
+
expected = "/*"
|
| 1273 |
+
if routePatterns[1] != expected {
|
| 1274 |
+
t.Fatalf("routePattern, expected:%s got:%s", expected, routePatterns[1])
|
| 1275 |
+
}
|
| 1276 |
+
expected = "/hi"
|
| 1277 |
+
if routePatterns[2] != expected {
|
| 1278 |
+
t.Fatalf("routePattern, expected:%s got:%s", expected, routePatterns[2])
|
| 1279 |
+
}
|
| 1280 |
+
|
| 1281 |
+
}
|
| 1282 |
+
|
| 1283 |
+
func TestSingleHandler(t *testing.T) {
|
| 1284 |
+
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
| 1285 |
+
name := URLParam(r, "name")
|
| 1286 |
+
w.Write([]byte("hi " + name))
|
| 1287 |
+
})
|
| 1288 |
+
|
| 1289 |
+
r, _ := http.NewRequest("GET", "/", nil)
|
| 1290 |
+
rctx := NewRouteContext()
|
| 1291 |
+
r = r.WithContext(context.WithValue(r.Context(), RouteCtxKey, rctx))
|
| 1292 |
+
rctx.URLParams.Add("name", "joe")
|
| 1293 |
+
|
| 1294 |
+
w := httptest.NewRecorder()
|
| 1295 |
+
h.ServeHTTP(w, r)
|
| 1296 |
+
|
| 1297 |
+
body := w.Body.String()
|
| 1298 |
+
expected := "hi joe"
|
| 1299 |
+
if body != expected {
|
| 1300 |
+
t.Fatalf("expected:%s got:%s", expected, body)
|
| 1301 |
+
}
|
| 1302 |
+
}
|
| 1303 |
+
|
| 1304 |
+
// TODO: a Router wrapper test..
|
| 1305 |
+
//
|
| 1306 |
+
// type ACLMux struct {
|
| 1307 |
+
// *Mux
|
| 1308 |
+
// XX string
|
| 1309 |
+
// }
|
| 1310 |
+
//
|
| 1311 |
+
// func NewACLMux() *ACLMux {
|
| 1312 |
+
// return &ACLMux{Mux: NewRouter(), XX: "hihi"}
|
| 1313 |
+
// }
|
| 1314 |
+
//
|
| 1315 |
+
// // TODO: this should be supported...
|
| 1316 |
+
// func TestWoot(t *testing.T) {
|
| 1317 |
+
// var r Router = NewRouter()
|
| 1318 |
+
//
|
| 1319 |
+
// var r2 Router = NewACLMux() //NewRouter()
|
| 1320 |
+
// r2.Get("/hi", func(w http.ResponseWriter, r *http.Request) {
|
| 1321 |
+
// w.Write([]byte("hi"))
|
| 1322 |
+
// })
|
| 1323 |
+
//
|
| 1324 |
+
// r.Mount("/", r2)
|
| 1325 |
+
// }
|
| 1326 |
+
|
| 1327 |
+
func TestServeHTTPExistingContext(t *testing.T) {
|
| 1328 |
+
r := NewRouter()
|
| 1329 |
+
r.Get("/hi", func(w http.ResponseWriter, r *http.Request) {
|
| 1330 |
+
s, _ := r.Context().Value(ctxKey{"testCtx"}).(string)
|
| 1331 |
+
w.Write([]byte(s))
|
| 1332 |
+
})
|
| 1333 |
+
r.NotFound(func(w http.ResponseWriter, r *http.Request) {
|
| 1334 |
+
s, _ := r.Context().Value(ctxKey{"testCtx"}).(string)
|
| 1335 |
+
w.WriteHeader(404)
|
| 1336 |
+
w.Write([]byte(s))
|
| 1337 |
+
})
|
| 1338 |
+
|
| 1339 |
+
testcases := []struct {
|
| 1340 |
+
Ctx context.Context
|
| 1341 |
+
Method string
|
| 1342 |
+
Path string
|
| 1343 |
+
ExpectedBody string
|
| 1344 |
+
ExpectedStatus int
|
| 1345 |
+
}{
|
| 1346 |
+
{
|
| 1347 |
+
Method: "GET",
|
| 1348 |
+
Path: "/hi",
|
| 1349 |
+
Ctx: context.WithValue(context.Background(), ctxKey{"testCtx"}, "hi ctx"),
|
| 1350 |
+
ExpectedStatus: 200,
|
| 1351 |
+
ExpectedBody: "hi ctx",
|
| 1352 |
+
},
|
| 1353 |
+
{
|
| 1354 |
+
Method: "GET",
|
| 1355 |
+
Path: "/hello",
|
| 1356 |
+
Ctx: context.WithValue(context.Background(), ctxKey{"testCtx"}, "nothing here ctx"),
|
| 1357 |
+
ExpectedStatus: 404,
|
| 1358 |
+
ExpectedBody: "nothing here ctx",
|
| 1359 |
+
},
|
| 1360 |
+
}
|
| 1361 |
+
|
| 1362 |
+
for _, tc := range testcases {
|
| 1363 |
+
resp := httptest.NewRecorder()
|
| 1364 |
+
req, err := http.NewRequest(tc.Method, tc.Path, nil)
|
| 1365 |
+
if err != nil {
|
| 1366 |
+
t.Fatalf("%v", err)
|
| 1367 |
+
}
|
| 1368 |
+
req = req.WithContext(tc.Ctx)
|
| 1369 |
+
r.ServeHTTP(resp, req)
|
| 1370 |
+
b, err := io.ReadAll(resp.Body)
|
| 1371 |
+
if err != nil {
|
| 1372 |
+
t.Fatalf("%v", err)
|
| 1373 |
+
}
|
| 1374 |
+
if resp.Code != tc.ExpectedStatus {
|
| 1375 |
+
t.Fatalf("%v != %v", tc.ExpectedStatus, resp.Code)
|
| 1376 |
+
}
|
| 1377 |
+
if string(b) != tc.ExpectedBody {
|
| 1378 |
+
t.Fatalf("%s != %s", tc.ExpectedBody, b)
|
| 1379 |
+
}
|
| 1380 |
+
}
|
| 1381 |
+
}
|
| 1382 |
+
|
| 1383 |
+
func TestNestedGroups(t *testing.T) {
|
| 1384 |
+
handlerPrintCounter := func(w http.ResponseWriter, r *http.Request) {
|
| 1385 |
+
counter, _ := r.Context().Value(ctxKey{"counter"}).(int)
|
| 1386 |
+
w.Write([]byte(fmt.Sprintf("%v", counter)))
|
| 1387 |
+
}
|
| 1388 |
+
|
| 1389 |
+
mwIncreaseCounter := func(next http.Handler) http.Handler {
|
| 1390 |
+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
| 1391 |
+
ctx := r.Context()
|
| 1392 |
+
counter, _ := ctx.Value(ctxKey{"counter"}).(int)
|
| 1393 |
+
counter++
|
| 1394 |
+
ctx = context.WithValue(ctx, ctxKey{"counter"}, counter)
|
| 1395 |
+
next.ServeHTTP(w, r.WithContext(ctx))
|
| 1396 |
+
})
|
| 1397 |
+
}
|
| 1398 |
+
|
| 1399 |
+
// Each route represents value of its counter (number of applied middlewares).
|
| 1400 |
+
r := NewRouter() // counter == 0
|
| 1401 |
+
r.Get("/0", handlerPrintCounter)
|
| 1402 |
+
r.Group(func(r Router) {
|
| 1403 |
+
r.Use(mwIncreaseCounter) // counter == 1
|
| 1404 |
+
r.Get("/1", handlerPrintCounter)
|
| 1405 |
+
|
| 1406 |
+
// r.Handle(GET, "/2", Chain(mwIncreaseCounter).HandlerFunc(handlerPrintCounter))
|
| 1407 |
+
r.With(mwIncreaseCounter).Get("/2", handlerPrintCounter)
|
| 1408 |
+
|
| 1409 |
+
r.Group(func(r Router) {
|
| 1410 |
+
r.Use(mwIncreaseCounter, mwIncreaseCounter) // counter == 3
|
| 1411 |
+
r.Get("/3", handlerPrintCounter)
|
| 1412 |
+
})
|
| 1413 |
+
r.Route("/", func(r Router) {
|
| 1414 |
+
r.Use(mwIncreaseCounter, mwIncreaseCounter) // counter == 3
|
| 1415 |
+
|
| 1416 |
+
// r.Handle(GET, "/4", Chain(mwIncreaseCounter).HandlerFunc(handlerPrintCounter))
|
| 1417 |
+
r.With(mwIncreaseCounter).Get("/4", handlerPrintCounter)
|
| 1418 |
+
|
| 1419 |
+
r.Group(func(r Router) {
|
| 1420 |
+
r.Use(mwIncreaseCounter, mwIncreaseCounter) // counter == 5
|
| 1421 |
+
r.Get("/5", handlerPrintCounter)
|
| 1422 |
+
// r.Handle(GET, "/6", Chain(mwIncreaseCounter).HandlerFunc(handlerPrintCounter))
|
| 1423 |
+
r.With(mwIncreaseCounter).Get("/6", handlerPrintCounter)
|
| 1424 |
+
|
| 1425 |
+
})
|
| 1426 |
+
})
|
| 1427 |
+
})
|
| 1428 |
+
|
| 1429 |
+
ts := httptest.NewServer(r)
|
| 1430 |
+
defer ts.Close()
|
| 1431 |
+
|
| 1432 |
+
for _, route := range []string{"0", "1", "2", "3", "4", "5", "6"} {
|
| 1433 |
+
if _, body := testRequest(t, ts, "GET", "/"+route, nil); body != route {
|
| 1434 |
+
t.Errorf("expected %v, got %v", route, body)
|
| 1435 |
+
}
|
| 1436 |
+
}
|
| 1437 |
+
}
|
| 1438 |
+
|
| 1439 |
+
func TestMiddlewarePanicOnLateUse(t *testing.T) {
|
| 1440 |
+
handler := func(w http.ResponseWriter, r *http.Request) {
|
| 1441 |
+
w.Write([]byte("hello\n"))
|
| 1442 |
+
}
|
| 1443 |
+
|
| 1444 |
+
mw := func(next http.Handler) http.Handler {
|
| 1445 |
+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
| 1446 |
+
next.ServeHTTP(w, r)
|
| 1447 |
+
})
|
| 1448 |
+
}
|
| 1449 |
+
|
| 1450 |
+
defer func() {
|
| 1451 |
+
if recover() == nil {
|
| 1452 |
+
t.Error("expected panic()")
|
| 1453 |
+
}
|
| 1454 |
+
}()
|
| 1455 |
+
|
| 1456 |
+
r := NewRouter()
|
| 1457 |
+
r.Get("/", handler)
|
| 1458 |
+
r.Use(mw) // Too late to apply middleware, we're expecting panic().
|
| 1459 |
+
}
|
| 1460 |
+
|
| 1461 |
+
func TestMountingExistingPath(t *testing.T) {
|
| 1462 |
+
handler := func(w http.ResponseWriter, r *http.Request) {}
|
| 1463 |
+
|
| 1464 |
+
defer func() {
|
| 1465 |
+
if recover() == nil {
|
| 1466 |
+
t.Error("expected panic()")
|
| 1467 |
+
}
|
| 1468 |
+
}()
|
| 1469 |
+
|
| 1470 |
+
r := NewRouter()
|
| 1471 |
+
r.Get("/", handler)
|
| 1472 |
+
r.Mount("/hi", http.HandlerFunc(handler))
|
| 1473 |
+
r.Mount("/hi", http.HandlerFunc(handler))
|
| 1474 |
+
}
|
| 1475 |
+
|
| 1476 |
+
func TestMountingSimilarPattern(t *testing.T) {
|
| 1477 |
+
r := NewRouter()
|
| 1478 |
+
r.Get("/hi", func(w http.ResponseWriter, r *http.Request) {
|
| 1479 |
+
w.Write([]byte("bye"))
|
| 1480 |
+
})
|
| 1481 |
+
|
| 1482 |
+
r2 := NewRouter()
|
| 1483 |
+
r2.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
| 1484 |
+
w.Write([]byte("foobar"))
|
| 1485 |
+
})
|
| 1486 |
+
|
| 1487 |
+
r3 := NewRouter()
|
| 1488 |
+
r3.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
| 1489 |
+
w.Write([]byte("foo"))
|
| 1490 |
+
})
|
| 1491 |
+
|
| 1492 |
+
r.Mount("/foobar", r2)
|
| 1493 |
+
r.Mount("/foo", r3)
|
| 1494 |
+
|
| 1495 |
+
ts := httptest.NewServer(r)
|
| 1496 |
+
defer ts.Close()
|
| 1497 |
+
|
| 1498 |
+
if _, body := testRequest(t, ts, "GET", "/hi", nil); body != "bye" {
|
| 1499 |
+
t.Fatal(body)
|
| 1500 |
+
}
|
| 1501 |
+
}
|
| 1502 |
+
|
| 1503 |
+
func TestMuxEmptyParams(t *testing.T) {
|
| 1504 |
+
r := NewRouter()
|
| 1505 |
+
r.Get(`/users/{x}/{y}/{z}`, func(w http.ResponseWriter, r *http.Request) {
|
| 1506 |
+
x := URLParam(r, "x")
|
| 1507 |
+
y := URLParam(r, "y")
|
| 1508 |
+
z := URLParam(r, "z")
|
| 1509 |
+
w.Write([]byte(fmt.Sprintf("%s-%s-%s", x, y, z)))
|
| 1510 |
+
})
|
| 1511 |
+
|
| 1512 |
+
ts := httptest.NewServer(r)
|
| 1513 |
+
defer ts.Close()
|
| 1514 |
+
|
| 1515 |
+
if _, body := testRequest(t, ts, "GET", "/users/a/b/c", nil); body != "a-b-c" {
|
| 1516 |
+
t.Fatal(body)
|
| 1517 |
+
}
|
| 1518 |
+
if _, body := testRequest(t, ts, "GET", "/users///c", nil); body != "--c" {
|
| 1519 |
+
t.Fatal(body)
|
| 1520 |
+
}
|
| 1521 |
+
}
|
| 1522 |
+
|
| 1523 |
+
func TestMuxMissingParams(t *testing.T) {
|
| 1524 |
+
r := NewRouter()
|
| 1525 |
+
r.Get(`/user/{userId:\d+}`, func(w http.ResponseWriter, r *http.Request) {
|
| 1526 |
+
userID := URLParam(r, "userId")
|
| 1527 |
+
w.Write([]byte(fmt.Sprintf("userId = '%s'", userID)))
|
| 1528 |
+
})
|
| 1529 |
+
r.NotFound(func(w http.ResponseWriter, r *http.Request) {
|
| 1530 |
+
w.WriteHeader(404)
|
| 1531 |
+
w.Write([]byte("nothing here"))
|
| 1532 |
+
})
|
| 1533 |
+
|
| 1534 |
+
ts := httptest.NewServer(r)
|
| 1535 |
+
defer ts.Close()
|
| 1536 |
+
|
| 1537 |
+
if _, body := testRequest(t, ts, "GET", "/user/123", nil); body != "userId = '123'" {
|
| 1538 |
+
t.Fatal(body)
|
| 1539 |
+
}
|
| 1540 |
+
if _, body := testRequest(t, ts, "GET", "/user/", nil); body != "nothing here" {
|
| 1541 |
+
t.Fatal(body)
|
| 1542 |
+
}
|
| 1543 |
+
}
|
| 1544 |
+
|
| 1545 |
+
func TestMuxWildcardRoute(t *testing.T) {
|
| 1546 |
+
handler := func(w http.ResponseWriter, r *http.Request) {}
|
| 1547 |
+
|
| 1548 |
+
defer func() {
|
| 1549 |
+
if recover() == nil {
|
| 1550 |
+
t.Error("expected panic()")
|
| 1551 |
+
}
|
| 1552 |
+
}()
|
| 1553 |
+
|
| 1554 |
+
r := NewRouter()
|
| 1555 |
+
r.Get("/*/wildcard/must/be/at/end", handler)
|
| 1556 |
+
}
|
| 1557 |
+
|
| 1558 |
+
func TestMuxWildcardRouteCheckTwo(t *testing.T) {
|
| 1559 |
+
handler := func(w http.ResponseWriter, r *http.Request) {}
|
| 1560 |
+
|
| 1561 |
+
defer func() {
|
| 1562 |
+
if recover() == nil {
|
| 1563 |
+
t.Error("expected panic()")
|
| 1564 |
+
}
|
| 1565 |
+
}()
|
| 1566 |
+
|
| 1567 |
+
r := NewRouter()
|
| 1568 |
+
r.Get("/*/wildcard/{must}/be/at/end", handler)
|
| 1569 |
+
}
|
| 1570 |
+
|
| 1571 |
+
func TestMuxRegexp(t *testing.T) {
|
| 1572 |
+
r := NewRouter()
|
| 1573 |
+
r.Route("/{param:[0-9]*}/test", func(r Router) {
|
| 1574 |
+
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
| 1575 |
+
w.Write([]byte(fmt.Sprintf("Hi: %s", URLParam(r, "param"))))
|
| 1576 |
+
})
|
| 1577 |
+
})
|
| 1578 |
+
|
| 1579 |
+
ts := httptest.NewServer(r)
|
| 1580 |
+
defer ts.Close()
|
| 1581 |
+
|
| 1582 |
+
if _, body := testRequest(t, ts, "GET", "//test", nil); body != "Hi: " {
|
| 1583 |
+
t.Fatal(body)
|
| 1584 |
+
}
|
| 1585 |
+
}
|
| 1586 |
+
|
| 1587 |
+
func TestMuxRegexp2(t *testing.T) {
|
| 1588 |
+
r := NewRouter()
|
| 1589 |
+
r.Get("/foo-{suffix:[a-z]{2,3}}.json", func(w http.ResponseWriter, r *http.Request) {
|
| 1590 |
+
w.Write([]byte(URLParam(r, "suffix")))
|
| 1591 |
+
})
|
| 1592 |
+
ts := httptest.NewServer(r)
|
| 1593 |
+
defer ts.Close()
|
| 1594 |
+
|
| 1595 |
+
if _, body := testRequest(t, ts, "GET", "/foo-.json", nil); body != "" {
|
| 1596 |
+
t.Fatal(body)
|
| 1597 |
+
}
|
| 1598 |
+
if _, body := testRequest(t, ts, "GET", "/foo-abc.json", nil); body != "abc" {
|
| 1599 |
+
t.Fatal(body)
|
| 1600 |
+
}
|
| 1601 |
+
}
|
| 1602 |
+
|
| 1603 |
+
func TestMuxRegexp3(t *testing.T) {
|
| 1604 |
+
r := NewRouter()
|
| 1605 |
+
r.Get("/one/{firstId:[a-z0-9-]+}/{secondId:[a-z]+}/first", func(w http.ResponseWriter, r *http.Request) {
|
| 1606 |
+
w.Write([]byte("first"))
|
| 1607 |
+
})
|
| 1608 |
+
r.Get("/one/{firstId:[a-z0-9-_]+}/{secondId:[0-9]+}/second", func(w http.ResponseWriter, r *http.Request) {
|
| 1609 |
+
w.Write([]byte("second"))
|
| 1610 |
+
})
|
| 1611 |
+
r.Delete("/one/{firstId:[a-z0-9-_]+}/{secondId:[0-9]+}/second", func(w http.ResponseWriter, r *http.Request) {
|
| 1612 |
+
w.Write([]byte("third"))
|
| 1613 |
+
})
|
| 1614 |
+
|
| 1615 |
+
r.Route("/one", func(r Router) {
|
| 1616 |
+
r.Get("/{dns:[a-z-0-9_]+}", func(writer http.ResponseWriter, request *http.Request) {
|
| 1617 |
+
writer.Write([]byte("_"))
|
| 1618 |
+
})
|
| 1619 |
+
r.Get("/{dns:[a-z-0-9_]+}/info", func(writer http.ResponseWriter, request *http.Request) {
|
| 1620 |
+
writer.Write([]byte("_"))
|
| 1621 |
+
})
|
| 1622 |
+
r.Delete("/{id:[0-9]+}", func(writer http.ResponseWriter, request *http.Request) {
|
| 1623 |
+
writer.Write([]byte("forth"))
|
| 1624 |
+
})
|
| 1625 |
+
})
|
| 1626 |
+
|
| 1627 |
+
ts := httptest.NewServer(r)
|
| 1628 |
+
defer ts.Close()
|
| 1629 |
+
|
| 1630 |
+
if _, body := testRequest(t, ts, "GET", "/one/hello/peter/first", nil); body != "first" {
|
| 1631 |
+
t.Fatal(body)
|
| 1632 |
+
}
|
| 1633 |
+
if _, body := testRequest(t, ts, "GET", "/one/hithere/123/second", nil); body != "second" {
|
| 1634 |
+
t.Fatal(body)
|
| 1635 |
+
}
|
| 1636 |
+
if _, body := testRequest(t, ts, "DELETE", "/one/hithere/123/second", nil); body != "third" {
|
| 1637 |
+
t.Fatal(body)
|
| 1638 |
+
}
|
| 1639 |
+
if _, body := testRequest(t, ts, "DELETE", "/one/123", nil); body != "forth" {
|
| 1640 |
+
t.Fatal(body)
|
| 1641 |
+
}
|
| 1642 |
+
}
|
| 1643 |
+
|
| 1644 |
+
func TestMuxSubrouterWildcardParam(t *testing.T) {
|
| 1645 |
+
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
| 1646 |
+
fmt.Fprintf(w, "param:%v *:%v", URLParam(r, "param"), URLParam(r, "*"))
|
| 1647 |
+
})
|
| 1648 |
+
|
| 1649 |
+
r := NewRouter()
|
| 1650 |
+
|
| 1651 |
+
r.Get("/bare/{param}", h)
|
| 1652 |
+
r.Get("/bare/{param}/*", h)
|
| 1653 |
+
|
| 1654 |
+
r.Route("/case0", func(r Router) {
|
| 1655 |
+
r.Get("/{param}", h)
|
| 1656 |
+
r.Get("/{param}/*", h)
|
| 1657 |
+
})
|
| 1658 |
+
|
| 1659 |
+
ts := httptest.NewServer(r)
|
| 1660 |
+
defer ts.Close()
|
| 1661 |
+
|
| 1662 |
+
if _, body := testRequest(t, ts, "GET", "/bare/hi", nil); body != "param:hi *:" {
|
| 1663 |
+
t.Fatal(body)
|
| 1664 |
+
}
|
| 1665 |
+
if _, body := testRequest(t, ts, "GET", "/bare/hi/yes", nil); body != "param:hi *:yes" {
|
| 1666 |
+
t.Fatal(body)
|
| 1667 |
+
}
|
| 1668 |
+
if _, body := testRequest(t, ts, "GET", "/case0/hi", nil); body != "param:hi *:" {
|
| 1669 |
+
t.Fatal(body)
|
| 1670 |
+
}
|
| 1671 |
+
if _, body := testRequest(t, ts, "GET", "/case0/hi/yes", nil); body != "param:hi *:yes" {
|
| 1672 |
+
t.Fatal(body)
|
| 1673 |
+
}
|
| 1674 |
+
}
|
| 1675 |
+
|
| 1676 |
+
func TestMuxContextIsThreadSafe(t *testing.T) {
|
| 1677 |
+
router := NewRouter()
|
| 1678 |
+
router.Get("/{id}", func(w http.ResponseWriter, r *http.Request) {
|
| 1679 |
+
ctx, cancel := context.WithTimeout(r.Context(), 1*time.Millisecond)
|
| 1680 |
+
defer cancel()
|
| 1681 |
+
|
| 1682 |
+
<-ctx.Done()
|
| 1683 |
+
})
|
| 1684 |
+
|
| 1685 |
+
wg := sync.WaitGroup{}
|
| 1686 |
+
|
| 1687 |
+
for i := 0; i < 100; i++ {
|
| 1688 |
+
wg.Add(1)
|
| 1689 |
+
go func() {
|
| 1690 |
+
defer wg.Done()
|
| 1691 |
+
for j := 0; j < 10000; j++ {
|
| 1692 |
+
w := httptest.NewRecorder()
|
| 1693 |
+
r, err := http.NewRequest("GET", "/ok", nil)
|
| 1694 |
+
if err != nil {
|
| 1695 |
+
t.Error(err)
|
| 1696 |
+
return
|
| 1697 |
+
}
|
| 1698 |
+
|
| 1699 |
+
ctx, cancel := context.WithCancel(r.Context())
|
| 1700 |
+
r = r.WithContext(ctx)
|
| 1701 |
+
|
| 1702 |
+
go func() {
|
| 1703 |
+
cancel()
|
| 1704 |
+
}()
|
| 1705 |
+
router.ServeHTTP(w, r)
|
| 1706 |
+
}
|
| 1707 |
+
}()
|
| 1708 |
+
}
|
| 1709 |
+
wg.Wait()
|
| 1710 |
+
}
|
| 1711 |
+
|
| 1712 |
+
func TestEscapedURLParams(t *testing.T) {
|
| 1713 |
+
m := NewRouter()
|
| 1714 |
+
m.Get("/api/{identifier}/{region}/{size}/{rotation}/*", func(w http.ResponseWriter, r *http.Request) {
|
| 1715 |
+
w.WriteHeader(200)
|
| 1716 |
+
rctx := RouteContext(r.Context())
|
| 1717 |
+
if rctx == nil {
|
| 1718 |
+
t.Error("no context")
|
| 1719 |
+
return
|
| 1720 |
+
}
|
| 1721 |
+
identifier := URLParam(r, "identifier")
|
| 1722 |
+
if identifier != "http:%2f%2fexample.com%2fimage.png" {
|
| 1723 |
+
t.Errorf("identifier path parameter incorrect %s", identifier)
|
| 1724 |
+
return
|
| 1725 |
+
}
|
| 1726 |
+
region := URLParam(r, "region")
|
| 1727 |
+
if region != "full" {
|
| 1728 |
+
t.Errorf("region path parameter incorrect %s", region)
|
| 1729 |
+
return
|
| 1730 |
+
}
|
| 1731 |
+
size := URLParam(r, "size")
|
| 1732 |
+
if size != "max" {
|
| 1733 |
+
t.Errorf("size path parameter incorrect %s", size)
|
| 1734 |
+
return
|
| 1735 |
+
}
|
| 1736 |
+
rotation := URLParam(r, "rotation")
|
| 1737 |
+
if rotation != "0" {
|
| 1738 |
+
t.Errorf("rotation path parameter incorrect %s", rotation)
|
| 1739 |
+
return
|
| 1740 |
+
}
|
| 1741 |
+
w.Write([]byte("success"))
|
| 1742 |
+
})
|
| 1743 |
+
|
| 1744 |
+
ts := httptest.NewServer(m)
|
| 1745 |
+
defer ts.Close()
|
| 1746 |
+
|
| 1747 |
+
if _, body := testRequest(t, ts, "GET", "/api/http:%2f%2fexample.com%2fimage.png/full/max/0/color.png", nil); body != "success" {
|
| 1748 |
+
t.Fatal(body)
|
| 1749 |
+
}
|
| 1750 |
+
}
|
| 1751 |
+
|
| 1752 |
+
func TestCustomHTTPMethod(t *testing.T) {
|
| 1753 |
+
// first we must register this method to be accepted, then we
|
| 1754 |
+
// can define method handlers on the router below
|
| 1755 |
+
RegisterMethod("BOO")
|
| 1756 |
+
|
| 1757 |
+
r := NewRouter()
|
| 1758 |
+
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
| 1759 |
+
w.Write([]byte("."))
|
| 1760 |
+
})
|
| 1761 |
+
|
| 1762 |
+
// note the custom BOO method for route /hi
|
| 1763 |
+
r.MethodFunc("BOO", "/hi", func(w http.ResponseWriter, r *http.Request) {
|
| 1764 |
+
w.Write([]byte("custom method"))
|
| 1765 |
+
})
|
| 1766 |
+
|
| 1767 |
+
ts := httptest.NewServer(r)
|
| 1768 |
+
defer ts.Close()
|
| 1769 |
+
|
| 1770 |
+
if _, body := testRequest(t, ts, "GET", "/", nil); body != "." {
|
| 1771 |
+
t.Fatal(body)
|
| 1772 |
+
}
|
| 1773 |
+
if _, body := testRequest(t, ts, "BOO", "/hi", nil); body != "custom method" {
|
| 1774 |
+
t.Fatal(body)
|
| 1775 |
+
}
|
| 1776 |
+
}
|
| 1777 |
+
|
| 1778 |
+
func TestMuxMatch(t *testing.T) {
|
| 1779 |
+
r := NewRouter()
|
| 1780 |
+
r.Get("/hi", func(w http.ResponseWriter, r *http.Request) {
|
| 1781 |
+
w.Header().Set("X-Test", "yes")
|
| 1782 |
+
w.Write([]byte("bye"))
|
| 1783 |
+
})
|
| 1784 |
+
r.Route("/articles", func(r Router) {
|
| 1785 |
+
r.Get("/{id}", func(w http.ResponseWriter, r *http.Request) {
|
| 1786 |
+
id := URLParam(r, "id")
|
| 1787 |
+
w.Header().Set("X-Article", id)
|
| 1788 |
+
w.Write([]byte("article:" + id))
|
| 1789 |
+
})
|
| 1790 |
+
})
|
| 1791 |
+
r.Route("/users", func(r Router) {
|
| 1792 |
+
r.Head("/{id}", func(w http.ResponseWriter, r *http.Request) {
|
| 1793 |
+
w.Header().Set("X-User", "-")
|
| 1794 |
+
w.Write([]byte("user"))
|
| 1795 |
+
})
|
| 1796 |
+
r.Get("/{id}", func(w http.ResponseWriter, r *http.Request) {
|
| 1797 |
+
id := URLParam(r, "id")
|
| 1798 |
+
w.Header().Set("X-User", id)
|
| 1799 |
+
w.Write([]byte("user:" + id))
|
| 1800 |
+
})
|
| 1801 |
+
})
|
| 1802 |
+
|
| 1803 |
+
tctx := NewRouteContext()
|
| 1804 |
+
|
| 1805 |
+
tctx.Reset()
|
| 1806 |
+
if r.Match(tctx, "GET", "/users/1") == false {
|
| 1807 |
+
t.Fatal("expecting to find match for route:", "GET", "/users/1")
|
| 1808 |
+
}
|
| 1809 |
+
|
| 1810 |
+
tctx.Reset()
|
| 1811 |
+
if r.Match(tctx, "HEAD", "/articles/10") == true {
|
| 1812 |
+
t.Fatal("not expecting to find match for route:", "HEAD", "/articles/10")
|
| 1813 |
+
}
|
| 1814 |
+
}
|
| 1815 |
+
|
| 1816 |
+
func TestMuxMatch_HasBasePath(t *testing.T) {
|
| 1817 |
+
r := NewRouter()
|
| 1818 |
+
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
| 1819 |
+
w.Header().Set("X-Test", "yes")
|
| 1820 |
+
w.Write([]byte(""))
|
| 1821 |
+
})
|
| 1822 |
+
|
| 1823 |
+
tctx := NewRouteContext()
|
| 1824 |
+
|
| 1825 |
+
tctx.Reset()
|
| 1826 |
+
if r.Match(tctx, "GET", "/") != true {
|
| 1827 |
+
t.Fatal("expecting to find match for route:", "GET", "/")
|
| 1828 |
+
}
|
| 1829 |
+
}
|
| 1830 |
+
|
| 1831 |
+
func TestMuxMatch_DoesNotHaveBasePath(t *testing.T) {
|
| 1832 |
+
r := NewRouter()
|
| 1833 |
+
|
| 1834 |
+
tctx := NewRouteContext()
|
| 1835 |
+
|
| 1836 |
+
tctx.Reset()
|
| 1837 |
+
if r.Match(tctx, "GET", "/") != false {
|
| 1838 |
+
t.Fatal("not expecting to find match for route:", "GET", "/")
|
| 1839 |
+
}
|
| 1840 |
+
}
|
| 1841 |
+
|
| 1842 |
+
func TestMuxFind(t *testing.T) {
|
| 1843 |
+
r := NewRouter()
|
| 1844 |
+
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
| 1845 |
+
w.Header().Set("X-Test", "yes")
|
| 1846 |
+
w.Write([]byte(""))
|
| 1847 |
+
})
|
| 1848 |
+
r.Get("/hi", func(w http.ResponseWriter, r *http.Request) {
|
| 1849 |
+
w.Header().Set("X-Test", "yes")
|
| 1850 |
+
w.Write([]byte("bye"))
|
| 1851 |
+
})
|
| 1852 |
+
r.Route("/yo", func(r Router) {
|
| 1853 |
+
r.Get("/sup", func(w http.ResponseWriter, r *http.Request) {
|
| 1854 |
+
w.Write([]byte("sup"))
|
| 1855 |
+
})
|
| 1856 |
+
})
|
| 1857 |
+
r.Route("/articles", func(r Router) {
|
| 1858 |
+
r.Get("/{id}", func(w http.ResponseWriter, r *http.Request) {
|
| 1859 |
+
id := URLParam(r, "id")
|
| 1860 |
+
w.Header().Set("X-Article", id)
|
| 1861 |
+
w.Write([]byte("article:" + id))
|
| 1862 |
+
})
|
| 1863 |
+
})
|
| 1864 |
+
r.Route("/users", func(r Router) {
|
| 1865 |
+
r.Head("/{id}", func(w http.ResponseWriter, r *http.Request) {
|
| 1866 |
+
w.Header().Set("X-User", "-")
|
| 1867 |
+
w.Write([]byte("user"))
|
| 1868 |
+
})
|
| 1869 |
+
r.Get("/{id}", func(w http.ResponseWriter, r *http.Request) {
|
| 1870 |
+
id := URLParam(r, "id")
|
| 1871 |
+
w.Header().Set("X-User", id)
|
| 1872 |
+
w.Write([]byte("user:" + id))
|
| 1873 |
+
})
|
| 1874 |
+
})
|
| 1875 |
+
r.Route("/api", func(r Router) {
|
| 1876 |
+
r.Route("/groups", func(r Router) {
|
| 1877 |
+
r.Route("/v2", func(r Router) {
|
| 1878 |
+
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
| 1879 |
+
w.Write([]byte("groups"))
|
| 1880 |
+
})
|
| 1881 |
+
r.Post("/{id}", func(w http.ResponseWriter, r *http.Request) {
|
| 1882 |
+
w.Write([]byte("POST groups"))
|
| 1883 |
+
})
|
| 1884 |
+
})
|
| 1885 |
+
})
|
| 1886 |
+
})
|
| 1887 |
+
|
| 1888 |
+
tctx := NewRouteContext()
|
| 1889 |
+
|
| 1890 |
+
tctx.Reset()
|
| 1891 |
+
if r.Find(tctx, "GET", "") == "/" {
|
| 1892 |
+
t.Fatal("expecting to find pattern / for route: GET")
|
| 1893 |
+
}
|
| 1894 |
+
|
| 1895 |
+
tctx.Reset()
|
| 1896 |
+
if r.Find(tctx, "GET", "/nope") != "" {
|
| 1897 |
+
t.Fatal("not expecting to find pattern for route: GET /nope")
|
| 1898 |
+
}
|
| 1899 |
+
|
| 1900 |
+
tctx.Reset()
|
| 1901 |
+
if r.Find(tctx, "GET", "/users/1") != "/users/{id}" {
|
| 1902 |
+
t.Fatal("expecting to find pattern /users/{id} for route: GET /users/1")
|
| 1903 |
+
}
|
| 1904 |
+
|
| 1905 |
+
tctx.Reset()
|
| 1906 |
+
if r.Find(tctx, "HEAD", "/articles/10") != "" {
|
| 1907 |
+
t.Fatal("not expecting to find pattern for route: HEAD /articles/10")
|
| 1908 |
+
}
|
| 1909 |
+
|
| 1910 |
+
tctx.Reset()
|
| 1911 |
+
if r.Find(tctx, "GET", "/yo/sup") != "/yo/sup" {
|
| 1912 |
+
t.Fatal("expecting to find pattern /yo/sup for route: GET /yo/sup")
|
| 1913 |
+
}
|
| 1914 |
+
|
| 1915 |
+
tctx.Reset()
|
| 1916 |
+
if r.Find(tctx, "GET", "/api/groups/v2/") != "/api/groups/v2/" {
|
| 1917 |
+
t.Fatal("expecting to find pattern /api/groups/v2/ for route: GET /api/groups/v2/")
|
| 1918 |
+
}
|
| 1919 |
+
|
| 1920 |
+
tctx.Reset()
|
| 1921 |
+
if r.Find(tctx, "POST", "/api/groups/v2/1") != "/api/groups/v2/{id}" {
|
| 1922 |
+
t.Fatal("expecting to find pattern /api/groups/v2/{id} for route: POST /api/groups/v2/1")
|
| 1923 |
+
}
|
| 1924 |
+
}
|
| 1925 |
+
|
| 1926 |
+
func TestServerBaseContext(t *testing.T) {
|
| 1927 |
+
r := NewRouter()
|
| 1928 |
+
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
| 1929 |
+
baseYes := r.Context().Value(ctxKey{"base"}).(string)
|
| 1930 |
+
if _, ok := r.Context().Value(http.ServerContextKey).(*http.Server); !ok {
|
| 1931 |
+
panic("missing server context")
|
| 1932 |
+
}
|
| 1933 |
+
if _, ok := r.Context().Value(http.LocalAddrContextKey).(net.Addr); !ok {
|
| 1934 |
+
panic("missing local addr context")
|
| 1935 |
+
}
|
| 1936 |
+
w.Write([]byte(baseYes))
|
| 1937 |
+
})
|
| 1938 |
+
|
| 1939 |
+
// Setup http Server with a base context
|
| 1940 |
+
ctx := context.WithValue(context.Background(), ctxKey{"base"}, "yes")
|
| 1941 |
+
ts := httptest.NewUnstartedServer(r)
|
| 1942 |
+
ts.Config.BaseContext = func(_ net.Listener) context.Context {
|
| 1943 |
+
return ctx
|
| 1944 |
+
}
|
| 1945 |
+
ts.Start()
|
| 1946 |
+
|
| 1947 |
+
defer ts.Close()
|
| 1948 |
+
|
| 1949 |
+
if _, body := testRequest(t, ts, "GET", "/", nil); body != "yes" {
|
| 1950 |
+
t.Fatal(body)
|
| 1951 |
+
}
|
| 1952 |
+
}
|
| 1953 |
+
|
| 1954 |
+
func testRequest(t *testing.T, ts *httptest.Server, method, path string, body io.Reader) (*http.Response, string) {
|
| 1955 |
+
req, err := http.NewRequest(method, ts.URL+path, body)
|
| 1956 |
+
if err != nil {
|
| 1957 |
+
t.Fatal(err)
|
| 1958 |
+
return nil, ""
|
| 1959 |
+
}
|
| 1960 |
+
|
| 1961 |
+
resp, err := http.DefaultClient.Do(req)
|
| 1962 |
+
if err != nil {
|
| 1963 |
+
t.Fatal(err)
|
| 1964 |
+
return nil, ""
|
| 1965 |
+
}
|
| 1966 |
+
|
| 1967 |
+
respBody, err := io.ReadAll(resp.Body)
|
| 1968 |
+
if err != nil {
|
| 1969 |
+
t.Fatal(err)
|
| 1970 |
+
return nil, ""
|
| 1971 |
+
}
|
| 1972 |
+
defer resp.Body.Close()
|
| 1973 |
+
|
| 1974 |
+
return resp, string(respBody)
|
| 1975 |
+
}
|
| 1976 |
+
|
| 1977 |
+
func testHandler(t *testing.T, h http.Handler, method, path string, body io.Reader) (*http.Response, string) {
|
| 1978 |
+
r, _ := http.NewRequest(method, path, body)
|
| 1979 |
+
w := httptest.NewRecorder()
|
| 1980 |
+
h.ServeHTTP(w, r)
|
| 1981 |
+
return w.Result(), w.Body.String()
|
| 1982 |
+
}
|
| 1983 |
+
|
| 1984 |
+
type ctxKey struct {
|
| 1985 |
+
name string
|
| 1986 |
+
}
|
| 1987 |
+
|
| 1988 |
+
func (k ctxKey) String() string {
|
| 1989 |
+
return "context value " + k.name
|
| 1990 |
+
}
|
| 1991 |
+
|
| 1992 |
+
func BenchmarkMux(b *testing.B) {
|
| 1993 |
+
h1 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
|
| 1994 |
+
h2 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
|
| 1995 |
+
h3 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
|
| 1996 |
+
h4 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
|
| 1997 |
+
h5 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
|
| 1998 |
+
h6 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
|
| 1999 |
+
|
| 2000 |
+
mx := NewRouter()
|
| 2001 |
+
mx.Get("/", h1)
|
| 2002 |
+
mx.Get("/hi", h2)
|
| 2003 |
+
mx.Post("/hi-post", h2) // used to benchmark 405 responses
|
| 2004 |
+
mx.Get("/sup/{id}/and/{this}", h3)
|
| 2005 |
+
mx.Get("/sup/{id}/{bar:foo}/{this}", h3)
|
| 2006 |
+
|
| 2007 |
+
mx.Route("/sharing/{x}/{hash}", func(mx Router) {
|
| 2008 |
+
mx.Get("/", h4) // subrouter-1
|
| 2009 |
+
mx.Get("/{network}", h5) // subrouter-1
|
| 2010 |
+
mx.Get("/twitter", h5)
|
| 2011 |
+
mx.Route("/direct", func(mx Router) {
|
| 2012 |
+
mx.Get("/", h6) // subrouter-2
|
| 2013 |
+
mx.Get("/download", h6)
|
| 2014 |
+
})
|
| 2015 |
+
})
|
| 2016 |
+
|
| 2017 |
+
routes := []string{
|
| 2018 |
+
"/",
|
| 2019 |
+
"/hi",
|
| 2020 |
+
"/hi-post",
|
| 2021 |
+
"/sup/123/and/this",
|
| 2022 |
+
"/sup/123/foo/this",
|
| 2023 |
+
"/sharing/z/aBc", // subrouter-1
|
| 2024 |
+
"/sharing/z/aBc/twitter", // subrouter-1
|
| 2025 |
+
"/sharing/z/aBc/direct", // subrouter-2
|
| 2026 |
+
"/sharing/z/aBc/direct/download", // subrouter-2
|
| 2027 |
+
}
|
| 2028 |
+
|
| 2029 |
+
for _, path := range routes {
|
| 2030 |
+
b.Run("route:"+path, func(b *testing.B) {
|
| 2031 |
+
w := httptest.NewRecorder()
|
| 2032 |
+
r, _ := http.NewRequest("GET", path, nil)
|
| 2033 |
+
|
| 2034 |
+
b.ReportAllocs()
|
| 2035 |
+
b.ResetTimer()
|
| 2036 |
+
|
| 2037 |
+
for i := 0; i < b.N; i++ {
|
| 2038 |
+
mx.ServeHTTP(w, r)
|
| 2039 |
+
}
|
| 2040 |
+
})
|
| 2041 |
+
}
|
| 2042 |
+
}
|
tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/tests/frozen/path_value_test.go
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
//go:build go1.22 && !tinygo
|
| 2 |
+
// +build go1.22,!tinygo
|
| 3 |
+
|
| 4 |
+
package chi
|
| 5 |
+
|
| 6 |
+
import (
|
| 7 |
+
"net/http"
|
| 8 |
+
"net/http/httptest"
|
| 9 |
+
"strings"
|
| 10 |
+
"testing"
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
func TestPathValue(t *testing.T) {
|
| 14 |
+
testCases := []struct {
|
| 15 |
+
name string
|
| 16 |
+
pattern string
|
| 17 |
+
method string
|
| 18 |
+
requestPath string
|
| 19 |
+
expectedBody string
|
| 20 |
+
pathKeys []string
|
| 21 |
+
}{
|
| 22 |
+
{
|
| 23 |
+
name: "Basic path value",
|
| 24 |
+
pattern: "/hubs/{hubID}",
|
| 25 |
+
method: "GET",
|
| 26 |
+
pathKeys: []string{"hubID"},
|
| 27 |
+
requestPath: "/hubs/392",
|
| 28 |
+
expectedBody: "392",
|
| 29 |
+
},
|
| 30 |
+
{
|
| 31 |
+
name: "Two path values",
|
| 32 |
+
pattern: "/users/{userID}/conversations/{conversationID}",
|
| 33 |
+
method: "POST",
|
| 34 |
+
pathKeys: []string{"userID", "conversationID"},
|
| 35 |
+
requestPath: "/users/Gojo/conversations/2948",
|
| 36 |
+
expectedBody: "Gojo 2948",
|
| 37 |
+
},
|
| 38 |
+
{
|
| 39 |
+
name: "Wildcard path",
|
| 40 |
+
pattern: "/users/{userID}/friends/*",
|
| 41 |
+
method: "POST",
|
| 42 |
+
pathKeys: []string{"userID", "*"},
|
| 43 |
+
requestPath: "/users/Gojo/friends/all-of-them/and/more",
|
| 44 |
+
expectedBody: "Gojo all-of-them/and/more",
|
| 45 |
+
},
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
for _, tc := range testCases {
|
| 49 |
+
t.Run(tc.name, func(t *testing.T) {
|
| 50 |
+
r := NewRouter()
|
| 51 |
+
|
| 52 |
+
r.Handle(tc.method+" "+tc.pattern, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
| 53 |
+
pathValues := []string{}
|
| 54 |
+
for _, pathKey := range tc.pathKeys {
|
| 55 |
+
pathValue := r.PathValue(pathKey)
|
| 56 |
+
if pathValue == "" {
|
| 57 |
+
pathValue = "NOT_FOUND:" + pathKey
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
pathValues = append(pathValues, pathValue)
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
body := strings.Join(pathValues, " ")
|
| 64 |
+
|
| 65 |
+
w.Write([]byte(body))
|
| 66 |
+
}))
|
| 67 |
+
|
| 68 |
+
ts := httptest.NewServer(r)
|
| 69 |
+
defer ts.Close()
|
| 70 |
+
|
| 71 |
+
_, body := testRequest(t, ts, tc.method, tc.requestPath, nil)
|
| 72 |
+
if body != tc.expectedBody {
|
| 73 |
+
t.Fatalf("expecting %q, got %q", tc.expectedBody, body)
|
| 74 |
+
}
|
| 75 |
+
})
|
| 76 |
+
}
|
| 77 |
+
}
|
tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/tests/frozen/testdata/cert.pem
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
-----BEGIN CERTIFICATE-----
|
| 2 |
+
MIIC/zCCAeegAwIBAgIRANioW0Re7DtpT4qZpJU1iK8wDQYJKoZIhvcNAQELBQAw
|
| 3 |
+
EjEQMA4GA1UEChMHQWNtZSBDbzAeFw0xNjEyMzExNDU0MzBaFw0xNzEyMzExNDU0
|
| 4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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
package chi
|
| 2 |
+
|
| 3 |
+
import (
|
| 4 |
+
"fmt"
|
| 5 |
+
"log"
|
| 6 |
+
"net/http"
|
| 7 |
+
"testing"
|
| 8 |
+
)
|
| 9 |
+
|
| 10 |
+
func TestTree(t *testing.T) {
|
| 11 |
+
hStub := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
|
| 12 |
+
hIndex := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
|
| 13 |
+
hFavicon := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
|
| 14 |
+
hArticleList := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
|
| 15 |
+
hArticleNear := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
|
| 16 |
+
hArticleShow := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
|
| 17 |
+
hArticleShowRelated := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
|
| 18 |
+
hArticleShowOpts := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
|
| 19 |
+
hArticleSlug := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
|
| 20 |
+
hArticleByUser := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
|
| 21 |
+
hUserList := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
|
| 22 |
+
hUserShow := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
|
| 23 |
+
hAdminCatchall := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
|
| 24 |
+
hAdminAppShow := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
|
| 25 |
+
hAdminAppShowCatchall := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
|
| 26 |
+
hUserProfile := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
|
| 27 |
+
hUserSuper := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
|
| 28 |
+
hUserAll := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
|
| 29 |
+
hHubView1 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
|
| 30 |
+
hHubView2 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
|
| 31 |
+
hHubView3 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
|
| 32 |
+
|
| 33 |
+
tr := &node{}
|
| 34 |
+
|
| 35 |
+
tr.InsertRoute(mGET, "/", hIndex)
|
| 36 |
+
tr.InsertRoute(mGET, "/favicon.ico", hFavicon)
|
| 37 |
+
|
| 38 |
+
tr.InsertRoute(mGET, "/pages/*", hStub)
|
| 39 |
+
|
| 40 |
+
tr.InsertRoute(mGET, "/article", hArticleList)
|
| 41 |
+
tr.InsertRoute(mGET, "/article/", hArticleList)
|
| 42 |
+
|
| 43 |
+
tr.InsertRoute(mGET, "/article/near", hArticleNear)
|
| 44 |
+
tr.InsertRoute(mGET, "/article/{id}", hStub)
|
| 45 |
+
tr.InsertRoute(mGET, "/article/{id}", hArticleShow)
|
| 46 |
+
tr.InsertRoute(mGET, "/article/{id}", hArticleShow) // duplicate will have no effect
|
| 47 |
+
tr.InsertRoute(mGET, "/article/@{user}", hArticleByUser)
|
| 48 |
+
|
| 49 |
+
tr.InsertRoute(mGET, "/article/{sup}/{opts}", hArticleShowOpts)
|
| 50 |
+
tr.InsertRoute(mGET, "/article/{id}/{opts}", hArticleShowOpts) // overwrite above route, latest wins
|
| 51 |
+
|
| 52 |
+
tr.InsertRoute(mGET, "/article/{iffd}/edit", hStub)
|
| 53 |
+
tr.InsertRoute(mGET, "/article/{id}//related", hArticleShowRelated)
|
| 54 |
+
tr.InsertRoute(mGET, "/article/slug/{month}/-/{day}/{year}", hArticleSlug)
|
| 55 |
+
|
| 56 |
+
tr.InsertRoute(mGET, "/admin/user", hUserList)
|
| 57 |
+
tr.InsertRoute(mGET, "/admin/user/", hStub) // will get replaced by next route
|
| 58 |
+
tr.InsertRoute(mGET, "/admin/user/", hUserList)
|
| 59 |
+
|
| 60 |
+
tr.InsertRoute(mGET, "/admin/user//{id}", hUserShow)
|
| 61 |
+
tr.InsertRoute(mGET, "/admin/user/{id}", hUserShow)
|
| 62 |
+
|
| 63 |
+
tr.InsertRoute(mGET, "/admin/apps/{id}", hAdminAppShow)
|
| 64 |
+
tr.InsertRoute(mGET, "/admin/apps/{id}/*", hAdminAppShowCatchall)
|
| 65 |
+
|
| 66 |
+
tr.InsertRoute(mGET, "/admin/*", hStub) // catchall segment will get replaced by next route
|
| 67 |
+
tr.InsertRoute(mGET, "/admin/*", hAdminCatchall)
|
| 68 |
+
|
| 69 |
+
tr.InsertRoute(mGET, "/users/{userID}/profile", hUserProfile)
|
| 70 |
+
tr.InsertRoute(mGET, "/users/super/*", hUserSuper)
|
| 71 |
+
tr.InsertRoute(mGET, "/users/*", hUserAll)
|
| 72 |
+
|
| 73 |
+
tr.InsertRoute(mGET, "/hubs/{hubID}/view", hHubView1)
|
| 74 |
+
tr.InsertRoute(mGET, "/hubs/{hubID}/view/*", hHubView2)
|
| 75 |
+
sr := NewRouter()
|
| 76 |
+
sr.Get("/users", hHubView3)
|
| 77 |
+
tr.InsertRoute(mGET, "/hubs/{hubID}/*", sr)
|
| 78 |
+
tr.InsertRoute(mGET, "/hubs/{hubID}/users", hHubView3)
|
| 79 |
+
|
| 80 |
+
tests := []struct {
|
| 81 |
+
r string // input request path
|
| 82 |
+
h http.Handler // output matched handler
|
| 83 |
+
k []string // output param keys
|
| 84 |
+
v []string // output param values
|
| 85 |
+
}{
|
| 86 |
+
{r: "/", h: hIndex, k: []string{}, v: []string{}},
|
| 87 |
+
{r: "/favicon.ico", h: hFavicon, k: []string{}, v: []string{}},
|
| 88 |
+
|
| 89 |
+
{r: "/pages", h: nil, k: []string{}, v: []string{}},
|
| 90 |
+
{r: "/pages/", h: hStub, k: []string{"*"}, v: []string{""}},
|
| 91 |
+
{r: "/pages/yes", h: hStub, k: []string{"*"}, v: []string{"yes"}},
|
| 92 |
+
|
| 93 |
+
{r: "/article", h: hArticleList, k: []string{}, v: []string{}},
|
| 94 |
+
{r: "/article/", h: hArticleList, k: []string{}, v: []string{}},
|
| 95 |
+
{r: "/article/near", h: hArticleNear, k: []string{}, v: []string{}},
|
| 96 |
+
{r: "/article/neard", h: hArticleShow, k: []string{"id"}, v: []string{"neard"}},
|
| 97 |
+
{r: "/article/123", h: hArticleShow, k: []string{"id"}, v: []string{"123"}},
|
| 98 |
+
{r: "/article/123/456", h: hArticleShowOpts, k: []string{"id", "opts"}, v: []string{"123", "456"}},
|
| 99 |
+
{r: "/article/@peter", h: hArticleByUser, k: []string{"user"}, v: []string{"peter"}},
|
| 100 |
+
{r: "/article/22//related", h: hArticleShowRelated, k: []string{"id"}, v: []string{"22"}},
|
| 101 |
+
{r: "/article/111/edit", h: hStub, k: []string{"iffd"}, v: []string{"111"}},
|
| 102 |
+
{r: "/article/slug/sept/-/4/2015", h: hArticleSlug, k: []string{"month", "day", "year"}, v: []string{"sept", "4", "2015"}},
|
| 103 |
+
{r: "/article/:id", h: hArticleShow, k: []string{"id"}, v: []string{":id"}},
|
| 104 |
+
|
| 105 |
+
{r: "/admin/user", h: hUserList, k: []string{}, v: []string{}},
|
| 106 |
+
{r: "/admin/user/", h: hUserList, k: []string{}, v: []string{}},
|
| 107 |
+
{r: "/admin/user/1", h: hUserShow, k: []string{"id"}, v: []string{"1"}},
|
| 108 |
+
{r: "/admin/user//1", h: hUserShow, k: []string{"id"}, v: []string{"1"}},
|
| 109 |
+
{r: "/admin/hi", h: hAdminCatchall, k: []string{"*"}, v: []string{"hi"}},
|
| 110 |
+
{r: "/admin/lots/of/:fun", h: hAdminCatchall, k: []string{"*"}, v: []string{"lots/of/:fun"}},
|
| 111 |
+
{r: "/admin/apps/333", h: hAdminAppShow, k: []string{"id"}, v: []string{"333"}},
|
| 112 |
+
{r: "/admin/apps/333/woot", h: hAdminAppShowCatchall, k: []string{"id", "*"}, v: []string{"333", "woot"}},
|
| 113 |
+
|
| 114 |
+
{r: "/hubs/123/view", h: hHubView1, k: []string{"hubID"}, v: []string{"123"}},
|
| 115 |
+
{r: "/hubs/123/view/index.html", h: hHubView2, k: []string{"hubID", "*"}, v: []string{"123", "index.html"}},
|
| 116 |
+
{r: "/hubs/123/users", h: hHubView3, k: []string{"hubID"}, v: []string{"123"}},
|
| 117 |
+
|
| 118 |
+
{r: "/users/123/profile", h: hUserProfile, k: []string{"userID"}, v: []string{"123"}},
|
| 119 |
+
{r: "/users/super/123/okay/yes", h: hUserSuper, k: []string{"*"}, v: []string{"123/okay/yes"}},
|
| 120 |
+
{r: "/users/123/okay/yes", h: hUserAll, k: []string{"*"}, v: []string{"123/okay/yes"}},
|
| 121 |
+
}
|
| 122 |
+
|
| 123 |
+
// log.Println("~~~~~~~~~")
|
| 124 |
+
// log.Println("~~~~~~~~~")
|
| 125 |
+
// debugPrintTree(0, 0, tr, 0)
|
| 126 |
+
// log.Println("~~~~~~~~~")
|
| 127 |
+
// log.Println("~~~~~~~~~")
|
| 128 |
+
|
| 129 |
+
for i, tt := range tests {
|
| 130 |
+
rctx := NewRouteContext()
|
| 131 |
+
|
| 132 |
+
_, handlers, _ := tr.FindRoute(rctx, mGET, tt.r)
|
| 133 |
+
|
| 134 |
+
var handler http.Handler
|
| 135 |
+
if methodHandler, ok := handlers[mGET]; ok {
|
| 136 |
+
handler = methodHandler.handler
|
| 137 |
+
}
|
| 138 |
+
|
| 139 |
+
paramKeys := rctx.routeParams.Keys
|
| 140 |
+
paramValues := rctx.routeParams.Values
|
| 141 |
+
|
| 142 |
+
if fmt.Sprintf("%v", tt.h) != fmt.Sprintf("%v", handler) {
|
| 143 |
+
t.Errorf("input [%d]: find '%s' expecting handler:%v , got:%v", i, tt.r, tt.h, handler)
|
| 144 |
+
}
|
| 145 |
+
if !stringSliceEqual(tt.k, paramKeys) {
|
| 146 |
+
t.Errorf("input [%d]: find '%s' expecting paramKeys:(%d)%v , got:(%d)%v", i, tt.r, len(tt.k), tt.k, len(paramKeys), paramKeys)
|
| 147 |
+
}
|
| 148 |
+
if !stringSliceEqual(tt.v, paramValues) {
|
| 149 |
+
t.Errorf("input [%d]: find '%s' expecting paramValues:(%d)%v , got:(%d)%v", i, tt.r, len(tt.v), tt.v, len(paramValues), paramValues)
|
| 150 |
+
}
|
| 151 |
+
}
|
| 152 |
+
}
|
| 153 |
+
|
| 154 |
+
func TestTreeMoar(t *testing.T) {
|
| 155 |
+
hStub := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
|
| 156 |
+
hStub1 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
|
| 157 |
+
hStub2 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
|
| 158 |
+
hStub3 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
|
| 159 |
+
hStub4 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
|
| 160 |
+
hStub5 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
|
| 161 |
+
hStub6 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
|
| 162 |
+
hStub7 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
|
| 163 |
+
hStub8 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
|
| 164 |
+
hStub9 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
|
| 165 |
+
hStub10 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
|
| 166 |
+
hStub11 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
|
| 167 |
+
hStub12 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
|
| 168 |
+
hStub13 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
|
| 169 |
+
hStub14 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
|
| 170 |
+
hStub15 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
|
| 171 |
+
hStub16 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
|
| 172 |
+
|
| 173 |
+
// TODO: panic if we see {id}{x} because we're missing a delimiter, its not possible.
|
| 174 |
+
// also {:id}* is not possible.
|
| 175 |
+
|
| 176 |
+
tr := &node{}
|
| 177 |
+
|
| 178 |
+
tr.InsertRoute(mGET, "/articlefun", hStub5)
|
| 179 |
+
tr.InsertRoute(mGET, "/articles/{id}", hStub)
|
| 180 |
+
tr.InsertRoute(mDELETE, "/articles/{slug}", hStub8)
|
| 181 |
+
tr.InsertRoute(mGET, "/articles/search", hStub1)
|
| 182 |
+
tr.InsertRoute(mGET, "/articles/{id}:delete", hStub8)
|
| 183 |
+
tr.InsertRoute(mGET, "/articles/{iidd}!sup", hStub4)
|
| 184 |
+
tr.InsertRoute(mGET, "/articles/{id}:{op}", hStub3)
|
| 185 |
+
tr.InsertRoute(mGET, "/articles/{id}:{op}", hStub2) // this route sets a new handler for the above route
|
| 186 |
+
tr.InsertRoute(mGET, "/articles/{slug:^[a-z]+}/posts", hStub) // up to tail '/' will only match if contents match the rex
|
| 187 |
+
tr.InsertRoute(mGET, "/articles/{id}/posts/{pid}", hStub6) // /articles/123/posts/1
|
| 188 |
+
tr.InsertRoute(mGET, "/articles/{id}/posts/{month}/{day}/{year}/{slug}", hStub7) // /articles/123/posts/09/04/1984/juice
|
| 189 |
+
tr.InsertRoute(mGET, "/articles/{id}.json", hStub10)
|
| 190 |
+
tr.InsertRoute(mGET, "/articles/{id}/data.json", hStub11)
|
| 191 |
+
tr.InsertRoute(mGET, "/articles/files/{file}.{ext}", hStub12)
|
| 192 |
+
tr.InsertRoute(mPUT, "/articles/me", hStub13)
|
| 193 |
+
|
| 194 |
+
// TODO: make a separate test case for this one..
|
| 195 |
+
// tr.InsertRoute(mGET, "/articles/{id}/{id}", hStub1) // panic expected, we're duplicating param keys
|
| 196 |
+
|
| 197 |
+
tr.InsertRoute(mGET, "/pages/*", hStub)
|
| 198 |
+
tr.InsertRoute(mGET, "/pages/*", hStub9)
|
| 199 |
+
|
| 200 |
+
tr.InsertRoute(mGET, "/users/{id}", hStub14)
|
| 201 |
+
tr.InsertRoute(mGET, "/users/{id}/settings/{key}", hStub15)
|
| 202 |
+
tr.InsertRoute(mGET, "/users/{id}/settings/*", hStub16)
|
| 203 |
+
|
| 204 |
+
tests := []struct {
|
| 205 |
+
h http.Handler
|
| 206 |
+
r string
|
| 207 |
+
k []string
|
| 208 |
+
v []string
|
| 209 |
+
m methodTyp
|
| 210 |
+
}{
|
| 211 |
+
{m: mGET, r: "/articles/search", h: hStub1, k: []string{}, v: []string{}},
|
| 212 |
+
{m: mGET, r: "/articlefun", h: hStub5, k: []string{}, v: []string{}},
|
| 213 |
+
{m: mGET, r: "/articles/123", h: hStub, k: []string{"id"}, v: []string{"123"}},
|
| 214 |
+
{m: mDELETE, r: "/articles/123mm", h: hStub8, k: []string{"slug"}, v: []string{"123mm"}},
|
| 215 |
+
{m: mGET, r: "/articles/789:delete", h: hStub8, k: []string{"id"}, v: []string{"789"}},
|
| 216 |
+
{m: mGET, r: "/articles/789!sup", h: hStub4, k: []string{"iidd"}, v: []string{"789"}},
|
| 217 |
+
{m: mGET, r: "/articles/123:sync", h: hStub2, k: []string{"id", "op"}, v: []string{"123", "sync"}},
|
| 218 |
+
{m: mGET, r: "/articles/456/posts/1", h: hStub6, k: []string{"id", "pid"}, v: []string{"456", "1"}},
|
| 219 |
+
{m: mGET, r: "/articles/456/posts/09/04/1984/juice", h: hStub7, k: []string{"id", "month", "day", "year", "slug"}, v: []string{"456", "09", "04", "1984", "juice"}},
|
| 220 |
+
{m: mGET, r: "/articles/456.json", h: hStub10, k: []string{"id"}, v: []string{"456"}},
|
| 221 |
+
{m: mGET, r: "/articles/456/data.json", h: hStub11, k: []string{"id"}, v: []string{"456"}},
|
| 222 |
+
|
| 223 |
+
{m: mGET, r: "/articles/files/file.zip", h: hStub12, k: []string{"file", "ext"}, v: []string{"file", "zip"}},
|
| 224 |
+
{m: mGET, r: "/articles/files/photos.tar.gz", h: hStub12, k: []string{"file", "ext"}, v: []string{"photos", "tar.gz"}},
|
| 225 |
+
{m: mGET, r: "/articles/files/photos.tar.gz", h: hStub12, k: []string{"file", "ext"}, v: []string{"photos", "tar.gz"}},
|
| 226 |
+
|
| 227 |
+
{m: mPUT, r: "/articles/me", h: hStub13, k: []string{}, v: []string{}},
|
| 228 |
+
{m: mGET, r: "/articles/me", h: hStub, k: []string{"id"}, v: []string{"me"}},
|
| 229 |
+
{m: mGET, r: "/pages", h: nil, k: []string{}, v: []string{}},
|
| 230 |
+
{m: mGET, r: "/pages/", h: hStub9, k: []string{"*"}, v: []string{""}},
|
| 231 |
+
{m: mGET, r: "/pages/yes", h: hStub9, k: []string{"*"}, v: []string{"yes"}},
|
| 232 |
+
|
| 233 |
+
{m: mGET, r: "/users/1", h: hStub14, k: []string{"id"}, v: []string{"1"}},
|
| 234 |
+
{m: mGET, r: "/users/", h: nil, k: []string{}, v: []string{}},
|
| 235 |
+
{m: mGET, r: "/users/2/settings/password", h: hStub15, k: []string{"id", "key"}, v: []string{"2", "password"}},
|
| 236 |
+
{m: mGET, r: "/users/2/settings/", h: hStub16, k: []string{"id", "*"}, v: []string{"2", ""}},
|
| 237 |
+
}
|
| 238 |
+
|
| 239 |
+
// log.Println("~~~~~~~~~")
|
| 240 |
+
// log.Println("~~~~~~~~~")
|
| 241 |
+
// debugPrintTree(0, 0, tr, 0)
|
| 242 |
+
// log.Println("~~~~~~~~~")
|
| 243 |
+
// log.Println("~~~~~~~~~")
|
| 244 |
+
|
| 245 |
+
for i, tt := range tests {
|
| 246 |
+
rctx := NewRouteContext()
|
| 247 |
+
|
| 248 |
+
_, handlers, _ := tr.FindRoute(rctx, tt.m, tt.r)
|
| 249 |
+
|
| 250 |
+
var handler http.Handler
|
| 251 |
+
if methodHandler, ok := handlers[tt.m]; ok {
|
| 252 |
+
handler = methodHandler.handler
|
| 253 |
+
}
|
| 254 |
+
|
| 255 |
+
paramKeys := rctx.routeParams.Keys
|
| 256 |
+
paramValues := rctx.routeParams.Values
|
| 257 |
+
|
| 258 |
+
if fmt.Sprintf("%v", tt.h) != fmt.Sprintf("%v", handler) {
|
| 259 |
+
t.Errorf("input [%d]: find '%s' expecting handler:%v , got:%v", i, tt.r, tt.h, handler)
|
| 260 |
+
}
|
| 261 |
+
if !stringSliceEqual(tt.k, paramKeys) {
|
| 262 |
+
t.Errorf("input [%d]: find '%s' expecting paramKeys:(%d)%v , got:(%d)%v", i, tt.r, len(tt.k), tt.k, len(paramKeys), paramKeys)
|
| 263 |
+
}
|
| 264 |
+
if !stringSliceEqual(tt.v, paramValues) {
|
| 265 |
+
t.Errorf("input [%d]: find '%s' expecting paramValues:(%d)%v , got:(%d)%v", i, tt.r, len(tt.v), tt.v, len(paramValues), paramValues)
|
| 266 |
+
}
|
| 267 |
+
}
|
| 268 |
+
}
|
| 269 |
+
|
| 270 |
+
func TestTreeRegexp(t *testing.T) {
|
| 271 |
+
hStub1 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
|
| 272 |
+
hStub2 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
|
| 273 |
+
hStub3 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
|
| 274 |
+
hStub4 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
|
| 275 |
+
hStub5 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
|
| 276 |
+
hStub6 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
|
| 277 |
+
hStub7 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
|
| 278 |
+
|
| 279 |
+
tr := &node{}
|
| 280 |
+
tr.InsertRoute(mGET, "/articles/{rid:^[0-9]{5,6}}", hStub7)
|
| 281 |
+
tr.InsertRoute(mGET, "/articles/{zid:^0[0-9]+}", hStub3)
|
| 282 |
+
tr.InsertRoute(mGET, "/articles/{name:^@[a-z]+}/posts", hStub4)
|
| 283 |
+
tr.InsertRoute(mGET, "/articles/{op:^[0-9]+}/run", hStub5)
|
| 284 |
+
tr.InsertRoute(mGET, "/articles/{id:^[0-9]+}", hStub1)
|
| 285 |
+
tr.InsertRoute(mGET, "/articles/{id:^[1-9]+}-{aux}", hStub6)
|
| 286 |
+
tr.InsertRoute(mGET, "/articles/{slug}", hStub2)
|
| 287 |
+
|
| 288 |
+
// log.Println("~~~~~~~~~")
|
| 289 |
+
// log.Println("~~~~~~~~~")
|
| 290 |
+
// debugPrintTree(0, 0, tr, 0)
|
| 291 |
+
// log.Println("~~~~~~~~~")
|
| 292 |
+
// log.Println("~~~~~~~~~")
|
| 293 |
+
|
| 294 |
+
tests := []struct {
|
| 295 |
+
r string // input request path
|
| 296 |
+
h http.Handler // output matched handler
|
| 297 |
+
k []string // output param keys
|
| 298 |
+
v []string // output param values
|
| 299 |
+
}{
|
| 300 |
+
{r: "/articles", h: nil, k: []string{}, v: []string{}},
|
| 301 |
+
{r: "/articles/12345", h: hStub7, k: []string{"rid"}, v: []string{"12345"}},
|
| 302 |
+
{r: "/articles/123", h: hStub1, k: []string{"id"}, v: []string{"123"}},
|
| 303 |
+
{r: "/articles/how-to-build-a-router", h: hStub2, k: []string{"slug"}, v: []string{"how-to-build-a-router"}},
|
| 304 |
+
{r: "/articles/0456", h: hStub3, k: []string{"zid"}, v: []string{"0456"}},
|
| 305 |
+
{r: "/articles/@pk/posts", h: hStub4, k: []string{"name"}, v: []string{"@pk"}},
|
| 306 |
+
{r: "/articles/1/run", h: hStub5, k: []string{"op"}, v: []string{"1"}},
|
| 307 |
+
{r: "/articles/1122", h: hStub1, k: []string{"id"}, v: []string{"1122"}},
|
| 308 |
+
{r: "/articles/1122-yes", h: hStub6, k: []string{"id", "aux"}, v: []string{"1122", "yes"}},
|
| 309 |
+
}
|
| 310 |
+
|
| 311 |
+
for i, tt := range tests {
|
| 312 |
+
rctx := NewRouteContext()
|
| 313 |
+
|
| 314 |
+
_, handlers, _ := tr.FindRoute(rctx, mGET, tt.r)
|
| 315 |
+
|
| 316 |
+
var handler http.Handler
|
| 317 |
+
if methodHandler, ok := handlers[mGET]; ok {
|
| 318 |
+
handler = methodHandler.handler
|
| 319 |
+
}
|
| 320 |
+
|
| 321 |
+
paramKeys := rctx.routeParams.Keys
|
| 322 |
+
paramValues := rctx.routeParams.Values
|
| 323 |
+
|
| 324 |
+
if fmt.Sprintf("%v", tt.h) != fmt.Sprintf("%v", handler) {
|
| 325 |
+
t.Errorf("input [%d]: find '%s' expecting handler:%v , got:%v", i, tt.r, tt.h, handler)
|
| 326 |
+
}
|
| 327 |
+
if !stringSliceEqual(tt.k, paramKeys) {
|
| 328 |
+
t.Errorf("input [%d]: find '%s' expecting paramKeys:(%d)%v , got:(%d)%v", i, tt.r, len(tt.k), tt.k, len(paramKeys), paramKeys)
|
| 329 |
+
}
|
| 330 |
+
if !stringSliceEqual(tt.v, paramValues) {
|
| 331 |
+
t.Errorf("input [%d]: find '%s' expecting paramValues:(%d)%v , got:(%d)%v", i, tt.r, len(tt.v), tt.v, len(paramValues), paramValues)
|
| 332 |
+
}
|
| 333 |
+
}
|
| 334 |
+
}
|
| 335 |
+
|
| 336 |
+
func TestTreeRegexpRecursive(t *testing.T) {
|
| 337 |
+
hStub1 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
|
| 338 |
+
hStub2 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
|
| 339 |
+
|
| 340 |
+
tr := &node{}
|
| 341 |
+
tr.InsertRoute(mGET, "/one/{firstId:[a-z0-9-]+}/{secondId:[a-z0-9-]+}/first", hStub1)
|
| 342 |
+
tr.InsertRoute(mGET, "/one/{firstId:[a-z0-9-_]+}/{secondId:[a-z0-9-_]+}/second", hStub2)
|
| 343 |
+
|
| 344 |
+
// log.Println("~~~~~~~~~")
|
| 345 |
+
// log.Println("~~~~~~~~~")
|
| 346 |
+
// debugPrintTree(0, 0, tr, 0)
|
| 347 |
+
// log.Println("~~~~~~~~~")
|
| 348 |
+
// log.Println("~~~~~~~~~")
|
| 349 |
+
|
| 350 |
+
tests := []struct {
|
| 351 |
+
r string // input request path
|
| 352 |
+
h http.Handler // output matched handler
|
| 353 |
+
k []string // output param keys
|
| 354 |
+
v []string // output param values
|
| 355 |
+
}{
|
| 356 |
+
{r: "/one/hello/world/first", h: hStub1, k: []string{"firstId", "secondId"}, v: []string{"hello", "world"}},
|
| 357 |
+
{r: "/one/hi_there/ok/second", h: hStub2, k: []string{"firstId", "secondId"}, v: []string{"hi_there", "ok"}},
|
| 358 |
+
{r: "/one///first", h: nil, k: []string{}, v: []string{}},
|
| 359 |
+
{r: "/one/hi/123/second", h: hStub2, k: []string{"firstId", "secondId"}, v: []string{"hi", "123"}},
|
| 360 |
+
}
|
| 361 |
+
|
| 362 |
+
for i, tt := range tests {
|
| 363 |
+
rctx := NewRouteContext()
|
| 364 |
+
|
| 365 |
+
_, handlers, _ := tr.FindRoute(rctx, mGET, tt.r)
|
| 366 |
+
|
| 367 |
+
var handler http.Handler
|
| 368 |
+
if methodHandler, ok := handlers[mGET]; ok {
|
| 369 |
+
handler = methodHandler.handler
|
| 370 |
+
}
|
| 371 |
+
|
| 372 |
+
paramKeys := rctx.routeParams.Keys
|
| 373 |
+
paramValues := rctx.routeParams.Values
|
| 374 |
+
|
| 375 |
+
if fmt.Sprintf("%v", tt.h) != fmt.Sprintf("%v", handler) {
|
| 376 |
+
t.Errorf("input [%d]: find '%s' expecting handler:%v , got:%v", i, tt.r, tt.h, handler)
|
| 377 |
+
}
|
| 378 |
+
if !stringSliceEqual(tt.k, paramKeys) {
|
| 379 |
+
t.Errorf("input [%d]: find '%s' expecting paramKeys:(%d)%v , got:(%d)%v", i, tt.r, len(tt.k), tt.k, len(paramKeys), paramKeys)
|
| 380 |
+
}
|
| 381 |
+
if !stringSliceEqual(tt.v, paramValues) {
|
| 382 |
+
t.Errorf("input [%d]: find '%s' expecting paramValues:(%d)%v , got:(%d)%v", i, tt.r, len(tt.v), tt.v, len(paramValues), paramValues)
|
| 383 |
+
}
|
| 384 |
+
}
|
| 385 |
+
}
|
| 386 |
+
|
| 387 |
+
func TestTreeRegexMatchWholeParam(t *testing.T) {
|
| 388 |
+
hStub1 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
|
| 389 |
+
|
| 390 |
+
rctx := NewRouteContext()
|
| 391 |
+
tr := &node{}
|
| 392 |
+
tr.InsertRoute(mGET, "/{id:[0-9]+}", hStub1)
|
| 393 |
+
tr.InsertRoute(mGET, "/{x:.+}/foo", hStub1)
|
| 394 |
+
tr.InsertRoute(mGET, "/{param:[0-9]*}/test", hStub1)
|
| 395 |
+
|
| 396 |
+
tests := []struct {
|
| 397 |
+
expectedHandler http.Handler
|
| 398 |
+
url string
|
| 399 |
+
}{
|
| 400 |
+
{url: "/13", expectedHandler: hStub1},
|
| 401 |
+
{url: "/a13", expectedHandler: nil},
|
| 402 |
+
{url: "/13.jpg", expectedHandler: nil},
|
| 403 |
+
{url: "/a13.jpg", expectedHandler: nil},
|
| 404 |
+
{url: "/a/foo", expectedHandler: hStub1},
|
| 405 |
+
{url: "//foo", expectedHandler: nil},
|
| 406 |
+
{url: "//test", expectedHandler: hStub1},
|
| 407 |
+
}
|
| 408 |
+
|
| 409 |
+
for _, tc := range tests {
|
| 410 |
+
_, _, handler := tr.FindRoute(rctx, mGET, tc.url)
|
| 411 |
+
if fmt.Sprintf("%v", tc.expectedHandler) != fmt.Sprintf("%v", handler) {
|
| 412 |
+
t.Errorf("url %v: expecting handler:%v , got:%v", tc.url, tc.expectedHandler, handler)
|
| 413 |
+
}
|
| 414 |
+
}
|
| 415 |
+
}
|
| 416 |
+
|
| 417 |
+
func TestTreeFindPattern(t *testing.T) {
|
| 418 |
+
hStub1 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
|
| 419 |
+
hStub2 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
|
| 420 |
+
hStub3 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
|
| 421 |
+
|
| 422 |
+
tr := &node{}
|
| 423 |
+
tr.InsertRoute(mGET, "/pages/*", hStub1)
|
| 424 |
+
tr.InsertRoute(mGET, "/articles/{id}/*", hStub2)
|
| 425 |
+
tr.InsertRoute(mGET, "/articles/{slug}/{uid}/*", hStub3)
|
| 426 |
+
|
| 427 |
+
if tr.findPattern("/pages") != false {
|
| 428 |
+
t.Errorf("find /pages failed")
|
| 429 |
+
}
|
| 430 |
+
if tr.findPattern("/pages*") != false {
|
| 431 |
+
t.Errorf("find /pages* failed - should be nil")
|
| 432 |
+
}
|
| 433 |
+
if tr.findPattern("/pages/*") == false {
|
| 434 |
+
t.Errorf("find /pages/* failed")
|
| 435 |
+
}
|
| 436 |
+
if tr.findPattern("/articles/{id}/*") == false {
|
| 437 |
+
t.Errorf("find /articles/{id}/* failed")
|
| 438 |
+
}
|
| 439 |
+
if tr.findPattern("/articles/{something}/*") == false {
|
| 440 |
+
t.Errorf("find /articles/{something}/* failed")
|
| 441 |
+
}
|
| 442 |
+
if tr.findPattern("/articles/{slug}/{uid}/*") == false {
|
| 443 |
+
t.Errorf("find /articles/{slug}/{uid}/* failed")
|
| 444 |
+
}
|
| 445 |
+
}
|
| 446 |
+
|
| 447 |
+
func debugPrintTree(parent int, i int, n *node, label byte) bool {
|
| 448 |
+
numEdges := 0
|
| 449 |
+
for _, nds := range n.children {
|
| 450 |
+
numEdges += len(nds)
|
| 451 |
+
}
|
| 452 |
+
|
| 453 |
+
// if n.handlers != nil {
|
| 454 |
+
// log.Printf("[node %d parent:%d] typ:%d prefix:%s label:%s tail:%s numEdges:%d isLeaf:%v handler:%v pat:%s keys:%v\n", i, parent, n.typ, n.prefix, string(label), string(n.tail), numEdges, n.isLeaf(), n.handlers, n.pattern, n.paramKeys)
|
| 455 |
+
// } else {
|
| 456 |
+
// log.Printf("[node %d parent:%d] typ:%d prefix:%s label:%s tail:%s numEdges:%d isLeaf:%v pat:%s keys:%v\n", i, parent, n.typ, n.prefix, string(label), string(n.tail), numEdges, n.isLeaf(), n.pattern, n.paramKeys)
|
| 457 |
+
// }
|
| 458 |
+
if n.endpoints != nil {
|
| 459 |
+
log.Printf("[node %d parent:%d] typ:%d prefix:%s label:%s tail:%s numEdges:%d isLeaf:%v handler:%v\n", i, parent, n.typ, n.prefix, string(label), string(n.tail), numEdges, n.isLeaf(), n.endpoints)
|
| 460 |
+
} else {
|
| 461 |
+
log.Printf("[node %d parent:%d] typ:%d prefix:%s label:%s tail:%s numEdges:%d isLeaf:%v\n", i, parent, n.typ, n.prefix, string(label), string(n.tail), numEdges, n.isLeaf())
|
| 462 |
+
}
|
| 463 |
+
parent = i
|
| 464 |
+
for _, nds := range n.children {
|
| 465 |
+
for _, e := range nds {
|
| 466 |
+
i++
|
| 467 |
+
if debugPrintTree(parent, i, e, e.label) {
|
| 468 |
+
return true
|
| 469 |
+
}
|
| 470 |
+
}
|
| 471 |
+
}
|
| 472 |
+
return false
|
| 473 |
+
}
|
| 474 |
+
|
| 475 |
+
func stringSliceEqual(a, b []string) bool {
|
| 476 |
+
if len(a) != len(b) {
|
| 477 |
+
return false
|
| 478 |
+
}
|
| 479 |
+
for i := range a {
|
| 480 |
+
if b[i] != a[i] {
|
| 481 |
+
return false
|
| 482 |
+
}
|
| 483 |
+
}
|
| 484 |
+
return true
|
| 485 |
+
}
|
| 486 |
+
|
| 487 |
+
func BenchmarkTreeGet(b *testing.B) {
|
| 488 |
+
h1 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
|
| 489 |
+
h2 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
|
| 490 |
+
|
| 491 |
+
tr := &node{}
|
| 492 |
+
tr.InsertRoute(mGET, "/", h1)
|
| 493 |
+
tr.InsertRoute(mGET, "/ping", h2)
|
| 494 |
+
tr.InsertRoute(mGET, "/pingall", h2)
|
| 495 |
+
tr.InsertRoute(mGET, "/ping/{id}", h2)
|
| 496 |
+
tr.InsertRoute(mGET, "/ping/{id}/woop", h2)
|
| 497 |
+
tr.InsertRoute(mGET, "/ping/{id}/{opt}", h2)
|
| 498 |
+
tr.InsertRoute(mGET, "/pinggggg", h2)
|
| 499 |
+
tr.InsertRoute(mGET, "/hello", h1)
|
| 500 |
+
|
| 501 |
+
mctx := NewRouteContext()
|
| 502 |
+
|
| 503 |
+
b.ReportAllocs()
|
| 504 |
+
b.ResetTimer()
|
| 505 |
+
|
| 506 |
+
for i := 0; i < b.N; i++ {
|
| 507 |
+
mctx.Reset()
|
| 508 |
+
tr.FindRoute(mctx, mGET, "/ping/123/456")
|
| 509 |
+
}
|
| 510 |
+
}
|
| 511 |
+
|
| 512 |
+
func TestWalker(t *testing.T) {
|
| 513 |
+
r := bigMux()
|
| 514 |
+
|
| 515 |
+
// Walk the muxBig router tree.
|
| 516 |
+
if err := Walk(r, func(method string, route string, handler http.Handler, middlewares ...func(http.Handler) http.Handler) error {
|
| 517 |
+
t.Logf("%v %v", method, route)
|
| 518 |
+
|
| 519 |
+
return nil
|
| 520 |
+
}); err != nil {
|
| 521 |
+
t.Error(err)
|
| 522 |
+
}
|
| 523 |
+
}
|
tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/tests/grade.py
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Frozen package-qualified Go test grading, executed only in task sandboxes."""
|
| 2 |
+
|
| 3 |
+
import hashlib
|
| 4 |
+
import json
|
| 5 |
+
import os
|
| 6 |
+
import selectors
|
| 7 |
+
import signal
|
| 8 |
+
import subprocess
|
| 9 |
+
import time
|
| 10 |
+
from pathlib import Path, PurePosixPath
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def assess(events, expected, returncode):
|
| 14 |
+
required = [(row["package"], row["test"]) for row in expected]
|
| 15 |
+
if not required or len(set(required)) != len(required):
|
| 16 |
+
raise ValueError("Required Go identities must be nonempty and unique")
|
| 17 |
+
runs, passes, package_passes, bad = {}, {}, set(), []
|
| 18 |
+
malformed = 0
|
| 19 |
+
for line in events.splitlines():
|
| 20 |
+
try:
|
| 21 |
+
event = json.loads(line)
|
| 22 |
+
except (ValueError, TypeError):
|
| 23 |
+
malformed += 1
|
| 24 |
+
continue
|
| 25 |
+
if not isinstance(event, dict) or not isinstance(event.get("Package"), str):
|
| 26 |
+
malformed += 1
|
| 27 |
+
continue
|
| 28 |
+
package, name, action = event["Package"], event.get("Test"), event.get("Action")
|
| 29 |
+
if name is None:
|
| 30 |
+
if action == "pass":
|
| 31 |
+
package_passes.add(package)
|
| 32 |
+
elif action == "fail":
|
| 33 |
+
bad.append([package, None, action])
|
| 34 |
+
continue
|
| 35 |
+
if not isinstance(name, str):
|
| 36 |
+
malformed += 1
|
| 37 |
+
continue
|
| 38 |
+
key = (package, name)
|
| 39 |
+
if action == "run":
|
| 40 |
+
runs[key] = runs.get(key, 0) + 1
|
| 41 |
+
elif action == "pass":
|
| 42 |
+
passes[key] = passes.get(key, 0) + 1
|
| 43 |
+
elif action in {"fail", "skip"}:
|
| 44 |
+
bad.append([package, name, action])
|
| 45 |
+
# Never parse Output strings as event records.
|
| 46 |
+
missing = [
|
| 47 |
+
list(key) for key in required if runs.get(key) != 1 or passes.get(key) != 1
|
| 48 |
+
]
|
| 49 |
+
passed = (
|
| 50 |
+
returncode == 0
|
| 51 |
+
and not missing
|
| 52 |
+
and not bad
|
| 53 |
+
and not malformed
|
| 54 |
+
and all(package in package_passes for package, _ in required)
|
| 55 |
+
)
|
| 56 |
+
return {
|
| 57 |
+
"passed": passed,
|
| 58 |
+
"reward": int(passed),
|
| 59 |
+
"required": len(required),
|
| 60 |
+
"missing_or_duplicate": missing,
|
| 61 |
+
"failed_or_skipped": bad,
|
| 62 |
+
"malformed_lines": malformed,
|
| 63 |
+
"exit_code": returncode,
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
def bounded_run(argv, cwd, seconds):
|
| 68 |
+
process = subprocess.Popen(
|
| 69 |
+
argv,
|
| 70 |
+
cwd=cwd,
|
| 71 |
+
stdout=subprocess.PIPE,
|
| 72 |
+
stderr=subprocess.STDOUT,
|
| 73 |
+
start_new_session=True,
|
| 74 |
+
)
|
| 75 |
+
output, deadline = bytearray(), time.monotonic() + seconds
|
| 76 |
+
try:
|
| 77 |
+
with selectors.DefaultSelector() as selector:
|
| 78 |
+
selector.register(process.stdout, selectors.EVENT_READ)
|
| 79 |
+
while selector.get_map():
|
| 80 |
+
if time.monotonic() >= deadline:
|
| 81 |
+
raise TimeoutError("Go verifier deadline exceeded")
|
| 82 |
+
for key, _ in selector.select(min(1, deadline - time.monotonic())):
|
| 83 |
+
chunk = os.read(key.fileobj.fileno(), 65536)
|
| 84 |
+
if not chunk:
|
| 85 |
+
selector.unregister(key.fileobj)
|
| 86 |
+
else:
|
| 87 |
+
output.extend(chunk)
|
| 88 |
+
if len(output) > 4 * 1024 * 1024:
|
| 89 |
+
raise ValueError("Go verifier output bound exceeded")
|
| 90 |
+
return process.wait(timeout=max(0.001, deadline - time.monotonic())), bytes(
|
| 91 |
+
output
|
| 92 |
+
)
|
| 93 |
+
except BaseException:
|
| 94 |
+
try:
|
| 95 |
+
os.killpg(process.pid, signal.SIGKILL)
|
| 96 |
+
except ProcessLookupError:
|
| 97 |
+
pass
|
| 98 |
+
process.wait()
|
| 99 |
+
raise
|
| 100 |
+
finally:
|
| 101 |
+
process.stdout.close()
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
def main():
|
| 105 |
+
private, workspace, output = (
|
| 106 |
+
Path(__file__).resolve().parent,
|
| 107 |
+
Path("/testbed"),
|
| 108 |
+
Path("/logs/verifier"),
|
| 109 |
+
)
|
| 110 |
+
output.mkdir(parents=True, exist_ok=True)
|
| 111 |
+
reward = output / "reward.txt"
|
| 112 |
+
reward.unlink(missing_ok=True)
|
| 113 |
+
try:
|
| 114 |
+
manifest = json.loads((private / "manifest.json").read_bytes())
|
| 115 |
+
fixtures = {}
|
| 116 |
+
for name, item in manifest["frozen_files"].items():
|
| 117 |
+
path = PurePosixPath(name)
|
| 118 |
+
if path.is_absolute() or path.as_posix() != name or ".." in path.parts:
|
| 119 |
+
raise ValueError("Unsafe frozen fixture path")
|
| 120 |
+
raw = (private / "frozen" / name).read_bytes()
|
| 121 |
+
if hashlib.sha256(raw).hexdigest() != item["sha256"]:
|
| 122 |
+
raise ValueError("Frozen fixture hash mismatch")
|
| 123 |
+
fixtures[name] = raw
|
| 124 |
+
if (workspace / ".git").exists():
|
| 125 |
+
raise ValueError("Task Git history is unsupported")
|
| 126 |
+
for path in workspace.rglob("*_test.go"):
|
| 127 |
+
if path.is_symlink() or not path.is_file():
|
| 128 |
+
raise ValueError("Unsafe submitted test path")
|
| 129 |
+
path.unlink()
|
| 130 |
+
for name, raw in fixtures.items():
|
| 131 |
+
target = workspace / name
|
| 132 |
+
if target.is_symlink() or any(p.is_symlink() for p in target.parents):
|
| 133 |
+
raise ValueError("Unsafe fixture destination")
|
| 134 |
+
target.parent.mkdir(parents=True, exist_ok=True)
|
| 135 |
+
target.write_bytes(raw)
|
| 136 |
+
target.chmod(manifest["frozen_files"][name]["mode"])
|
| 137 |
+
code, _ = bounded_run(["/usr/local/go/bin/go", "mod", "verify"], workspace, 30)
|
| 138 |
+
if code:
|
| 139 |
+
raise ValueError("Offline dependency verification failed")
|
| 140 |
+
code, raw = bounded_run(manifest["command"], workspace, 240)
|
| 141 |
+
if code not in {0, 1}:
|
| 142 |
+
raise ValueError("Go verifier terminated unexpectedly")
|
| 143 |
+
result = assess(raw.decode("utf-8"), manifest["expected"], code)
|
| 144 |
+
(output / "test-result.json").write_text(
|
| 145 |
+
json.dumps(result, sort_keys=True) + "\n"
|
| 146 |
+
)
|
| 147 |
+
reward.write_text(str(result["reward"]) + "\n")
|
| 148 |
+
return 0
|
| 149 |
+
except Exception as error:
|
| 150 |
+
(output / "infrastructure-error.json").write_text(
|
| 151 |
+
json.dumps({"error_type": type(error).__name__}) + "\n"
|
| 152 |
+
)
|
| 153 |
+
return 2
|
| 154 |
+
|
| 155 |
+
|
| 156 |
+
if __name__ == "__main__":
|
| 157 |
+
raise SystemExit(main())
|
tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/tests/manifest.json
ADDED
|
@@ -0,0 +1,752 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"command": [
|
| 3 |
+
"/usr/local/go/bin/go",
|
| 4 |
+
"test",
|
| 5 |
+
"-json",
|
| 6 |
+
"-count=1",
|
| 7 |
+
"./..."
|
| 8 |
+
],
|
| 9 |
+
"expected": [
|
| 10 |
+
{
|
| 11 |
+
"group": "FAIL_TO_PASS",
|
| 12 |
+
"package": "github.com/go-chi/chi/v5/middleware",
|
| 13 |
+
"test": "TestStripSlashesInRoute"
|
| 14 |
+
},
|
| 15 |
+
{
|
| 16 |
+
"group": "FAIL_TO_PASS",
|
| 17 |
+
"package": "github.com/go-chi/chi/v5/middleware",
|
| 18 |
+
"test": "TestStripSlashesWithNilContext"
|
| 19 |
+
},
|
| 20 |
+
{
|
| 21 |
+
"group": "PASS_TO_PASS",
|
| 22 |
+
"package": "github.com/go-chi/chi/v5",
|
| 23 |
+
"test": "TestRoutePattern"
|
| 24 |
+
},
|
| 25 |
+
{
|
| 26 |
+
"group": "PASS_TO_PASS",
|
| 27 |
+
"package": "github.com/go-chi/chi/v5",
|
| 28 |
+
"test": "TestMuxBasic"
|
| 29 |
+
},
|
| 30 |
+
{
|
| 31 |
+
"group": "PASS_TO_PASS",
|
| 32 |
+
"package": "github.com/go-chi/chi/v5",
|
| 33 |
+
"test": "TestMuxMounts"
|
| 34 |
+
},
|
| 35 |
+
{
|
| 36 |
+
"group": "PASS_TO_PASS",
|
| 37 |
+
"package": "github.com/go-chi/chi/v5",
|
| 38 |
+
"test": "TestMuxPlain"
|
| 39 |
+
},
|
| 40 |
+
{
|
| 41 |
+
"group": "PASS_TO_PASS",
|
| 42 |
+
"package": "github.com/go-chi/chi/v5",
|
| 43 |
+
"test": "TestMuxEmptyRoutes"
|
| 44 |
+
},
|
| 45 |
+
{
|
| 46 |
+
"group": "PASS_TO_PASS",
|
| 47 |
+
"package": "github.com/go-chi/chi/v5",
|
| 48 |
+
"test": "TestMuxTrailingSlash"
|
| 49 |
+
},
|
| 50 |
+
{
|
| 51 |
+
"group": "PASS_TO_PASS",
|
| 52 |
+
"package": "github.com/go-chi/chi/v5",
|
| 53 |
+
"test": "TestMuxNestedNotFound"
|
| 54 |
+
},
|
| 55 |
+
{
|
| 56 |
+
"group": "PASS_TO_PASS",
|
| 57 |
+
"package": "github.com/go-chi/chi/v5",
|
| 58 |
+
"test": "TestMethodNotAllowed"
|
| 59 |
+
},
|
| 60 |
+
{
|
| 61 |
+
"group": "PASS_TO_PASS",
|
| 62 |
+
"package": "github.com/go-chi/chi/v5",
|
| 63 |
+
"test": "TestMethodNotAllowed/Registered_Method"
|
| 64 |
+
},
|
| 65 |
+
{
|
| 66 |
+
"group": "PASS_TO_PASS",
|
| 67 |
+
"package": "github.com/go-chi/chi/v5",
|
| 68 |
+
"test": "TestMethodNotAllowed/Unregistered_Method"
|
| 69 |
+
},
|
| 70 |
+
{
|
| 71 |
+
"group": "PASS_TO_PASS",
|
| 72 |
+
"package": "github.com/go-chi/chi/v5",
|
| 73 |
+
"test": "TestMuxNestedMethodNotAllowed"
|
| 74 |
+
},
|
| 75 |
+
{
|
| 76 |
+
"group": "PASS_TO_PASS",
|
| 77 |
+
"package": "github.com/go-chi/chi/v5",
|
| 78 |
+
"test": "TestMuxComplicatedNotFound"
|
| 79 |
+
},
|
| 80 |
+
{
|
| 81 |
+
"group": "PASS_TO_PASS",
|
| 82 |
+
"package": "github.com/go-chi/chi/v5",
|
| 83 |
+
"test": "TestMuxComplicatedNotFound/pre"
|
| 84 |
+
},
|
| 85 |
+
{
|
| 86 |
+
"group": "PASS_TO_PASS",
|
| 87 |
+
"package": "github.com/go-chi/chi/v5",
|
| 88 |
+
"test": "TestMuxComplicatedNotFound/post"
|
| 89 |
+
},
|
| 90 |
+
{
|
| 91 |
+
"group": "PASS_TO_PASS",
|
| 92 |
+
"package": "github.com/go-chi/chi/v5",
|
| 93 |
+
"test": "TestMuxWith"
|
| 94 |
+
},
|
| 95 |
+
{
|
| 96 |
+
"group": "PASS_TO_PASS",
|
| 97 |
+
"package": "github.com/go-chi/chi/v5",
|
| 98 |
+
"test": "TestMuxHandlePatternValidation"
|
| 99 |
+
},
|
| 100 |
+
{
|
| 101 |
+
"group": "PASS_TO_PASS",
|
| 102 |
+
"package": "github.com/go-chi/chi/v5",
|
| 103 |
+
"test": "TestMuxHandlePatternValidation/Valid_pattern_without_HTTP_GET"
|
| 104 |
+
},
|
| 105 |
+
{
|
| 106 |
+
"group": "PASS_TO_PASS",
|
| 107 |
+
"package": "github.com/go-chi/chi/v5",
|
| 108 |
+
"test": "TestMuxHandlePatternValidation/Valid_pattern_with_HTTP_POST"
|
| 109 |
+
},
|
| 110 |
+
{
|
| 111 |
+
"group": "PASS_TO_PASS",
|
| 112 |
+
"package": "github.com/go-chi/chi/v5",
|
| 113 |
+
"test": "TestMuxHandlePatternValidation/Invalid_pattern_with_no_method"
|
| 114 |
+
},
|
| 115 |
+
{
|
| 116 |
+
"group": "PASS_TO_PASS",
|
| 117 |
+
"package": "github.com/go-chi/chi/v5",
|
| 118 |
+
"test": "TestMuxHandlePatternValidation/Invalid_pattern_with_supported_method"
|
| 119 |
+
},
|
| 120 |
+
{
|
| 121 |
+
"group": "PASS_TO_PASS",
|
| 122 |
+
"package": "github.com/go-chi/chi/v5",
|
| 123 |
+
"test": "TestMuxHandlePatternValidation/Invalid_pattern_with_unsupported_method"
|
| 124 |
+
},
|
| 125 |
+
{
|
| 126 |
+
"group": "PASS_TO_PASS",
|
| 127 |
+
"package": "github.com/go-chi/chi/v5",
|
| 128 |
+
"test": "TestMuxMiddlewareStack"
|
| 129 |
+
},
|
| 130 |
+
{
|
| 131 |
+
"group": "PASS_TO_PASS",
|
| 132 |
+
"package": "github.com/go-chi/chi/v5",
|
| 133 |
+
"test": "TestMuxRouteGroups"
|
| 134 |
+
},
|
| 135 |
+
{
|
| 136 |
+
"group": "PASS_TO_PASS",
|
| 137 |
+
"package": "github.com/go-chi/chi/v5",
|
| 138 |
+
"test": "TestMuxBig"
|
| 139 |
+
},
|
| 140 |
+
{
|
| 141 |
+
"group": "PASS_TO_PASS",
|
| 142 |
+
"package": "github.com/go-chi/chi/v5",
|
| 143 |
+
"test": "TestMuxSubroutesBasic"
|
| 144 |
+
},
|
| 145 |
+
{
|
| 146 |
+
"group": "PASS_TO_PASS",
|
| 147 |
+
"package": "github.com/go-chi/chi/v5",
|
| 148 |
+
"test": "TestMuxSubroutes"
|
| 149 |
+
},
|
| 150 |
+
{
|
| 151 |
+
"group": "PASS_TO_PASS",
|
| 152 |
+
"package": "github.com/go-chi/chi/v5",
|
| 153 |
+
"test": "TestSingleHandler"
|
| 154 |
+
},
|
| 155 |
+
{
|
| 156 |
+
"group": "PASS_TO_PASS",
|
| 157 |
+
"package": "github.com/go-chi/chi/v5",
|
| 158 |
+
"test": "TestServeHTTPExistingContext"
|
| 159 |
+
},
|
| 160 |
+
{
|
| 161 |
+
"group": "PASS_TO_PASS",
|
| 162 |
+
"package": "github.com/go-chi/chi/v5",
|
| 163 |
+
"test": "TestNestedGroups"
|
| 164 |
+
},
|
| 165 |
+
{
|
| 166 |
+
"group": "PASS_TO_PASS",
|
| 167 |
+
"package": "github.com/go-chi/chi/v5",
|
| 168 |
+
"test": "TestMiddlewarePanicOnLateUse"
|
| 169 |
+
},
|
| 170 |
+
{
|
| 171 |
+
"group": "PASS_TO_PASS",
|
| 172 |
+
"package": "github.com/go-chi/chi/v5",
|
| 173 |
+
"test": "TestMountingExistingPath"
|
| 174 |
+
},
|
| 175 |
+
{
|
| 176 |
+
"group": "PASS_TO_PASS",
|
| 177 |
+
"package": "github.com/go-chi/chi/v5",
|
| 178 |
+
"test": "TestMountingSimilarPattern"
|
| 179 |
+
},
|
| 180 |
+
{
|
| 181 |
+
"group": "PASS_TO_PASS",
|
| 182 |
+
"package": "github.com/go-chi/chi/v5",
|
| 183 |
+
"test": "TestMuxEmptyParams"
|
| 184 |
+
},
|
| 185 |
+
{
|
| 186 |
+
"group": "PASS_TO_PASS",
|
| 187 |
+
"package": "github.com/go-chi/chi/v5",
|
| 188 |
+
"test": "TestMuxMissingParams"
|
| 189 |
+
},
|
| 190 |
+
{
|
| 191 |
+
"group": "PASS_TO_PASS",
|
| 192 |
+
"package": "github.com/go-chi/chi/v5",
|
| 193 |
+
"test": "TestMuxWildcardRoute"
|
| 194 |
+
},
|
| 195 |
+
{
|
| 196 |
+
"group": "PASS_TO_PASS",
|
| 197 |
+
"package": "github.com/go-chi/chi/v5",
|
| 198 |
+
"test": "TestMuxWildcardRouteCheckTwo"
|
| 199 |
+
},
|
| 200 |
+
{
|
| 201 |
+
"group": "PASS_TO_PASS",
|
| 202 |
+
"package": "github.com/go-chi/chi/v5",
|
| 203 |
+
"test": "TestMuxRegexp"
|
| 204 |
+
},
|
| 205 |
+
{
|
| 206 |
+
"group": "PASS_TO_PASS",
|
| 207 |
+
"package": "github.com/go-chi/chi/v5",
|
| 208 |
+
"test": "TestMuxRegexp2"
|
| 209 |
+
},
|
| 210 |
+
{
|
| 211 |
+
"group": "PASS_TO_PASS",
|
| 212 |
+
"package": "github.com/go-chi/chi/v5",
|
| 213 |
+
"test": "TestMuxRegexp3"
|
| 214 |
+
},
|
| 215 |
+
{
|
| 216 |
+
"group": "PASS_TO_PASS",
|
| 217 |
+
"package": "github.com/go-chi/chi/v5",
|
| 218 |
+
"test": "TestMuxSubrouterWildcardParam"
|
| 219 |
+
},
|
| 220 |
+
{
|
| 221 |
+
"group": "PASS_TO_PASS",
|
| 222 |
+
"package": "github.com/go-chi/chi/v5",
|
| 223 |
+
"test": "TestMuxContextIsThreadSafe"
|
| 224 |
+
},
|
| 225 |
+
{
|
| 226 |
+
"group": "PASS_TO_PASS",
|
| 227 |
+
"package": "github.com/go-chi/chi/v5",
|
| 228 |
+
"test": "TestEscapedURLParams"
|
| 229 |
+
},
|
| 230 |
+
{
|
| 231 |
+
"group": "PASS_TO_PASS",
|
| 232 |
+
"package": "github.com/go-chi/chi/v5",
|
| 233 |
+
"test": "TestCustomHTTPMethod"
|
| 234 |
+
},
|
| 235 |
+
{
|
| 236 |
+
"group": "PASS_TO_PASS",
|
| 237 |
+
"package": "github.com/go-chi/chi/v5",
|
| 238 |
+
"test": "TestMuxMatch"
|
| 239 |
+
},
|
| 240 |
+
{
|
| 241 |
+
"group": "PASS_TO_PASS",
|
| 242 |
+
"package": "github.com/go-chi/chi/v5",
|
| 243 |
+
"test": "TestMuxMatch_HasBasePath"
|
| 244 |
+
},
|
| 245 |
+
{
|
| 246 |
+
"group": "PASS_TO_PASS",
|
| 247 |
+
"package": "github.com/go-chi/chi/v5",
|
| 248 |
+
"test": "TestMuxMatch_DoesNotHaveBasePath"
|
| 249 |
+
},
|
| 250 |
+
{
|
| 251 |
+
"group": "PASS_TO_PASS",
|
| 252 |
+
"package": "github.com/go-chi/chi/v5",
|
| 253 |
+
"test": "TestMuxFind"
|
| 254 |
+
},
|
| 255 |
+
{
|
| 256 |
+
"group": "PASS_TO_PASS",
|
| 257 |
+
"package": "github.com/go-chi/chi/v5",
|
| 258 |
+
"test": "TestServerBaseContext"
|
| 259 |
+
},
|
| 260 |
+
{
|
| 261 |
+
"group": "PASS_TO_PASS",
|
| 262 |
+
"package": "github.com/go-chi/chi/v5",
|
| 263 |
+
"test": "TestPathValue"
|
| 264 |
+
},
|
| 265 |
+
{
|
| 266 |
+
"group": "PASS_TO_PASS",
|
| 267 |
+
"package": "github.com/go-chi/chi/v5",
|
| 268 |
+
"test": "TestPathValue/Basic_path_value"
|
| 269 |
+
},
|
| 270 |
+
{
|
| 271 |
+
"group": "PASS_TO_PASS",
|
| 272 |
+
"package": "github.com/go-chi/chi/v5",
|
| 273 |
+
"test": "TestPathValue/Two_path_values"
|
| 274 |
+
},
|
| 275 |
+
{
|
| 276 |
+
"group": "PASS_TO_PASS",
|
| 277 |
+
"package": "github.com/go-chi/chi/v5",
|
| 278 |
+
"test": "TestPathValue/Wildcard_path"
|
| 279 |
+
},
|
| 280 |
+
{
|
| 281 |
+
"group": "PASS_TO_PASS",
|
| 282 |
+
"package": "github.com/go-chi/chi/v5",
|
| 283 |
+
"test": "TestTree"
|
| 284 |
+
},
|
| 285 |
+
{
|
| 286 |
+
"group": "PASS_TO_PASS",
|
| 287 |
+
"package": "github.com/go-chi/chi/v5",
|
| 288 |
+
"test": "TestTreeMoar"
|
| 289 |
+
},
|
| 290 |
+
{
|
| 291 |
+
"group": "PASS_TO_PASS",
|
| 292 |
+
"package": "github.com/go-chi/chi/v5",
|
| 293 |
+
"test": "TestTreeRegexp"
|
| 294 |
+
},
|
| 295 |
+
{
|
| 296 |
+
"group": "PASS_TO_PASS",
|
| 297 |
+
"package": "github.com/go-chi/chi/v5",
|
| 298 |
+
"test": "TestTreeRegexpRecursive"
|
| 299 |
+
},
|
| 300 |
+
{
|
| 301 |
+
"group": "PASS_TO_PASS",
|
| 302 |
+
"package": "github.com/go-chi/chi/v5",
|
| 303 |
+
"test": "TestTreeRegexMatchWholeParam"
|
| 304 |
+
},
|
| 305 |
+
{
|
| 306 |
+
"group": "PASS_TO_PASS",
|
| 307 |
+
"package": "github.com/go-chi/chi/v5",
|
| 308 |
+
"test": "TestTreeFindPattern"
|
| 309 |
+
},
|
| 310 |
+
{
|
| 311 |
+
"group": "PASS_TO_PASS",
|
| 312 |
+
"package": "github.com/go-chi/chi/v5",
|
| 313 |
+
"test": "TestWalker"
|
| 314 |
+
},
|
| 315 |
+
{
|
| 316 |
+
"group": "PASS_TO_PASS",
|
| 317 |
+
"package": "github.com/go-chi/chi/v5",
|
| 318 |
+
"test": "TestRouterFromMuxWith"
|
| 319 |
+
},
|
| 320 |
+
{
|
| 321 |
+
"group": "PASS_TO_PASS",
|
| 322 |
+
"package": "github.com/go-chi/chi/v5/middleware",
|
| 323 |
+
"test": "TestCompressor"
|
| 324 |
+
},
|
| 325 |
+
{
|
| 326 |
+
"group": "PASS_TO_PASS",
|
| 327 |
+
"package": "github.com/go-chi/chi/v5/middleware",
|
| 328 |
+
"test": "TestCompressor/no_expected_encodings_due_to_no_accepted_encodings"
|
| 329 |
+
},
|
| 330 |
+
{
|
| 331 |
+
"group": "PASS_TO_PASS",
|
| 332 |
+
"package": "github.com/go-chi/chi/v5/middleware",
|
| 333 |
+
"test": "TestCompressor/no_expected_encodings_due_to_content_type"
|
| 334 |
+
},
|
| 335 |
+
{
|
| 336 |
+
"group": "PASS_TO_PASS",
|
| 337 |
+
"package": "github.com/go-chi/chi/v5/middleware",
|
| 338 |
+
"test": "TestCompressor/gzip_is_only_encoding"
|
| 339 |
+
},
|
| 340 |
+
{
|
| 341 |
+
"group": "PASS_TO_PASS",
|
| 342 |
+
"package": "github.com/go-chi/chi/v5/middleware",
|
| 343 |
+
"test": "TestCompressor/gzip_is_preferred_over_deflate"
|
| 344 |
+
},
|
| 345 |
+
{
|
| 346 |
+
"group": "PASS_TO_PASS",
|
| 347 |
+
"package": "github.com/go-chi/chi/v5/middleware",
|
| 348 |
+
"test": "TestCompressor/deflate_is_used"
|
| 349 |
+
},
|
| 350 |
+
{
|
| 351 |
+
"group": "PASS_TO_PASS",
|
| 352 |
+
"package": "github.com/go-chi/chi/v5/middleware",
|
| 353 |
+
"test": "TestCompressor/nop_is_preferred"
|
| 354 |
+
},
|
| 355 |
+
{
|
| 356 |
+
"group": "PASS_TO_PASS",
|
| 357 |
+
"package": "github.com/go-chi/chi/v5/middleware",
|
| 358 |
+
"test": "TestCompressorWildcards"
|
| 359 |
+
},
|
| 360 |
+
{
|
| 361 |
+
"group": "PASS_TO_PASS",
|
| 362 |
+
"package": "github.com/go-chi/chi/v5/middleware",
|
| 363 |
+
"test": "TestCompressorWildcards/defaults"
|
| 364 |
+
},
|
| 365 |
+
{
|
| 366 |
+
"group": "PASS_TO_PASS",
|
| 367 |
+
"package": "github.com/go-chi/chi/v5/middleware",
|
| 368 |
+
"test": "TestCompressorWildcards/no_wildcard"
|
| 369 |
+
},
|
| 370 |
+
{
|
| 371 |
+
"group": "PASS_TO_PASS",
|
| 372 |
+
"package": "github.com/go-chi/chi/v5/middleware",
|
| 373 |
+
"test": "TestCompressorWildcards/invalid_wildcard_#1"
|
| 374 |
+
},
|
| 375 |
+
{
|
| 376 |
+
"group": "PASS_TO_PASS",
|
| 377 |
+
"package": "github.com/go-chi/chi/v5/middleware",
|
| 378 |
+
"test": "TestCompressorWildcards/invalid_wildcard_#2"
|
| 379 |
+
},
|
| 380 |
+
{
|
| 381 |
+
"group": "PASS_TO_PASS",
|
| 382 |
+
"package": "github.com/go-chi/chi/v5/middleware",
|
| 383 |
+
"test": "TestCompressorWildcards/valid_wildcard"
|
| 384 |
+
},
|
| 385 |
+
{
|
| 386 |
+
"group": "PASS_TO_PASS",
|
| 387 |
+
"package": "github.com/go-chi/chi/v5/middleware",
|
| 388 |
+
"test": "TestCompressorWildcards/mixed"
|
| 389 |
+
},
|
| 390 |
+
{
|
| 391 |
+
"group": "PASS_TO_PASS",
|
| 392 |
+
"package": "github.com/go-chi/chi/v5/middleware",
|
| 393 |
+
"test": "TestGetHead"
|
| 394 |
+
},
|
| 395 |
+
{
|
| 396 |
+
"group": "PASS_TO_PASS",
|
| 397 |
+
"package": "github.com/go-chi/chi/v5/middleware",
|
| 398 |
+
"test": "TestRequestLogger"
|
| 399 |
+
},
|
| 400 |
+
{
|
| 401 |
+
"group": "PASS_TO_PASS",
|
| 402 |
+
"package": "github.com/go-chi/chi/v5/middleware",
|
| 403 |
+
"test": "TestRequestLoggerReadFrom"
|
| 404 |
+
},
|
| 405 |
+
{
|
| 406 |
+
"group": "PASS_TO_PASS",
|
| 407 |
+
"package": "github.com/go-chi/chi/v5/middleware",
|
| 408 |
+
"test": "TestWrapWriterHTTP2"
|
| 409 |
+
},
|
| 410 |
+
{
|
| 411 |
+
"group": "PASS_TO_PASS",
|
| 412 |
+
"package": "github.com/go-chi/chi/v5/middleware",
|
| 413 |
+
"test": "TestXRealIP"
|
| 414 |
+
},
|
| 415 |
+
{
|
| 416 |
+
"group": "PASS_TO_PASS",
|
| 417 |
+
"package": "github.com/go-chi/chi/v5/middleware",
|
| 418 |
+
"test": "TestXForwardForIP"
|
| 419 |
+
},
|
| 420 |
+
{
|
| 421 |
+
"group": "PASS_TO_PASS",
|
| 422 |
+
"package": "github.com/go-chi/chi/v5/middleware",
|
| 423 |
+
"test": "TestXForwardForXRealIPPrecedence"
|
| 424 |
+
},
|
| 425 |
+
{
|
| 426 |
+
"group": "PASS_TO_PASS",
|
| 427 |
+
"package": "github.com/go-chi/chi/v5/middleware",
|
| 428 |
+
"test": "TestInvalidIP"
|
| 429 |
+
},
|
| 430 |
+
{
|
| 431 |
+
"group": "PASS_TO_PASS",
|
| 432 |
+
"package": "github.com/go-chi/chi/v5/middleware",
|
| 433 |
+
"test": "TestRecoverer"
|
| 434 |
+
},
|
| 435 |
+
{
|
| 436 |
+
"group": "PASS_TO_PASS",
|
| 437 |
+
"package": "github.com/go-chi/chi/v5/middleware",
|
| 438 |
+
"test": "TestRecovererAbortHandler"
|
| 439 |
+
},
|
| 440 |
+
{
|
| 441 |
+
"group": "PASS_TO_PASS",
|
| 442 |
+
"package": "github.com/go-chi/chi/v5/middleware",
|
| 443 |
+
"test": "TestRequestID"
|
| 444 |
+
},
|
| 445 |
+
{
|
| 446 |
+
"group": "PASS_TO_PASS",
|
| 447 |
+
"package": "github.com/go-chi/chi/v5/middleware",
|
| 448 |
+
"test": "TestStripSlashes"
|
| 449 |
+
},
|
| 450 |
+
{
|
| 451 |
+
"group": "PASS_TO_PASS",
|
| 452 |
+
"package": "github.com/go-chi/chi/v5/middleware",
|
| 453 |
+
"test": "TestRedirectSlashes"
|
| 454 |
+
},
|
| 455 |
+
{
|
| 456 |
+
"group": "PASS_TO_PASS",
|
| 457 |
+
"package": "github.com/go-chi/chi/v5/middleware",
|
| 458 |
+
"test": "TestStripPrefix"
|
| 459 |
+
},
|
| 460 |
+
{
|
| 461 |
+
"group": "PASS_TO_PASS",
|
| 462 |
+
"package": "github.com/go-chi/chi/v5/middleware",
|
| 463 |
+
"test": "TestSunset"
|
| 464 |
+
},
|
| 465 |
+
{
|
| 466 |
+
"group": "PASS_TO_PASS",
|
| 467 |
+
"package": "github.com/go-chi/chi/v5/middleware",
|
| 468 |
+
"test": "TestSunset/Sunset_without_link"
|
| 469 |
+
},
|
| 470 |
+
{
|
| 471 |
+
"group": "PASS_TO_PASS",
|
| 472 |
+
"package": "github.com/go-chi/chi/v5/middleware",
|
| 473 |
+
"test": "TestSunset/Sunset_with_link"
|
| 474 |
+
},
|
| 475 |
+
{
|
| 476 |
+
"group": "PASS_TO_PASS",
|
| 477 |
+
"package": "github.com/go-chi/chi/v5/middleware",
|
| 478 |
+
"test": "TestThrottleBacklog"
|
| 479 |
+
},
|
| 480 |
+
{
|
| 481 |
+
"group": "PASS_TO_PASS",
|
| 482 |
+
"package": "github.com/go-chi/chi/v5/middleware",
|
| 483 |
+
"test": "TestThrottleClientTimeout"
|
| 484 |
+
},
|
| 485 |
+
{
|
| 486 |
+
"group": "PASS_TO_PASS",
|
| 487 |
+
"package": "github.com/go-chi/chi/v5/middleware",
|
| 488 |
+
"test": "TestThrottleTriggerGatewayTimeout"
|
| 489 |
+
},
|
| 490 |
+
{
|
| 491 |
+
"group": "PASS_TO_PASS",
|
| 492 |
+
"package": "github.com/go-chi/chi/v5/middleware",
|
| 493 |
+
"test": "TestThrottleMaximum"
|
| 494 |
+
},
|
| 495 |
+
{
|
| 496 |
+
"group": "PASS_TO_PASS",
|
| 497 |
+
"package": "github.com/go-chi/chi/v5/middleware",
|
| 498 |
+
"test": "TestThrottleCustomStatusCode"
|
| 499 |
+
},
|
| 500 |
+
{
|
| 501 |
+
"group": "PASS_TO_PASS",
|
| 502 |
+
"package": "github.com/go-chi/chi/v5/middleware",
|
| 503 |
+
"test": "TestURLFormat"
|
| 504 |
+
},
|
| 505 |
+
{
|
| 506 |
+
"group": "PASS_TO_PASS",
|
| 507 |
+
"package": "github.com/go-chi/chi/v5/middleware",
|
| 508 |
+
"test": "TestURLFormatInSubRouter"
|
| 509 |
+
},
|
| 510 |
+
{
|
| 511 |
+
"group": "PASS_TO_PASS",
|
| 512 |
+
"package": "github.com/go-chi/chi/v5/middleware",
|
| 513 |
+
"test": "TestHttpFancyWriterRemembersWroteHeaderWhenFlushed"
|
| 514 |
+
},
|
| 515 |
+
{
|
| 516 |
+
"group": "PASS_TO_PASS",
|
| 517 |
+
"package": "github.com/go-chi/chi/v5/middleware",
|
| 518 |
+
"test": "TestHttp2FancyWriterRemembersWroteHeaderWhenFlushed"
|
| 519 |
+
},
|
| 520 |
+
{
|
| 521 |
+
"group": "PASS_TO_PASS",
|
| 522 |
+
"package": "github.com/go-chi/chi/v5/middleware",
|
| 523 |
+
"test": "TestBasicWritesTeesWritesWithoutDiscard"
|
| 524 |
+
},
|
| 525 |
+
{
|
| 526 |
+
"group": "PASS_TO_PASS",
|
| 527 |
+
"package": "github.com/go-chi/chi/v5/middleware",
|
| 528 |
+
"test": "TestBasicWriterDiscardsWritesToOriginalResponseWriter"
|
| 529 |
+
},
|
| 530 |
+
{
|
| 531 |
+
"group": "PASS_TO_PASS",
|
| 532 |
+
"package": "github.com/go-chi/chi/v5/middleware",
|
| 533 |
+
"test": "TestBasicWriterDiscardsWritesToOriginalResponseWriter/With_Tee"
|
| 534 |
+
},
|
| 535 |
+
{
|
| 536 |
+
"group": "PASS_TO_PASS",
|
| 537 |
+
"package": "github.com/go-chi/chi/v5/middleware",
|
| 538 |
+
"test": "TestBasicWriterDiscardsWritesToOriginalResponseWriter/Without_Tee"
|
| 539 |
+
},
|
| 540 |
+
{
|
| 541 |
+
"group": "PASS_TO_PASS",
|
| 542 |
+
"package": "github.com/go-chi/chi/v5/middleware",
|
| 543 |
+
"test": "TestContentEncoding"
|
| 544 |
+
},
|
| 545 |
+
{
|
| 546 |
+
"group": "PASS_TO_PASS",
|
| 547 |
+
"package": "github.com/go-chi/chi/v5/middleware",
|
| 548 |
+
"test": "TestSplit"
|
| 549 |
+
},
|
| 550 |
+
{
|
| 551 |
+
"group": "PASS_TO_PASS",
|
| 552 |
+
"package": "github.com/go-chi/chi/v5/middleware",
|
| 553 |
+
"test": "TestContentType"
|
| 554 |
+
},
|
| 555 |
+
{
|
| 556 |
+
"group": "PASS_TO_PASS",
|
| 557 |
+
"package": "github.com/go-chi/chi/v5/middleware",
|
| 558 |
+
"test": "TestContentType/should_accept_requests_with_a_matching_content_type"
|
| 559 |
+
},
|
| 560 |
+
{
|
| 561 |
+
"group": "PASS_TO_PASS",
|
| 562 |
+
"package": "github.com/go-chi/chi/v5/middleware",
|
| 563 |
+
"test": "TestContentType/should_accept_requests_with_a_matching_content_type_when_multiple_content_types_are_supported"
|
| 564 |
+
},
|
| 565 |
+
{
|
| 566 |
+
"group": "PASS_TO_PASS",
|
| 567 |
+
"package": "github.com/go-chi/chi/v5/middleware",
|
| 568 |
+
"test": "TestContentType/should_not_accept_requests_with_a_mismatching_content_type"
|
| 569 |
+
},
|
| 570 |
+
{
|
| 571 |
+
"group": "PASS_TO_PASS",
|
| 572 |
+
"package": "github.com/go-chi/chi/v5/middleware",
|
| 573 |
+
"test": "TestContentType/should_accept_requests_with_a_matching_content_type_no_charset"
|
| 574 |
+
},
|
| 575 |
+
{
|
| 576 |
+
"group": "PASS_TO_PASS",
|
| 577 |
+
"package": "github.com/go-chi/chi/v5/middleware",
|
| 578 |
+
"test": "TestContentType/should_accept_requests_with_a_matching_content-type_with_extra_values"
|
| 579 |
+
},
|
| 580 |
+
{
|
| 581 |
+
"group": "PASS_TO_PASS",
|
| 582 |
+
"package": "github.com/go-chi/chi/v5/middleware",
|
| 583 |
+
"test": "TestContentType/should_not_accept_requests_with_a_mismatching_content_type_even_if_multiple_content_types_are_allowed"
|
| 584 |
+
},
|
| 585 |
+
{
|
| 586 |
+
"group": "PASS_TO_PASS",
|
| 587 |
+
"package": "github.com/go-chi/chi/v5/middleware",
|
| 588 |
+
"test": "TestContentEncodingMiddleware"
|
| 589 |
+
},
|
| 590 |
+
{
|
| 591 |
+
"group": "PASS_TO_PASS",
|
| 592 |
+
"package": "github.com/go-chi/chi/v5/middleware",
|
| 593 |
+
"test": "TestContentEncodingMiddleware/Support_no_encoding"
|
| 594 |
+
},
|
| 595 |
+
{
|
| 596 |
+
"group": "PASS_TO_PASS",
|
| 597 |
+
"package": "github.com/go-chi/chi/v5/middleware",
|
| 598 |
+
"test": "TestContentEncodingMiddleware/No_support_for_deflate_and_br_encoding"
|
| 599 |
+
},
|
| 600 |
+
{
|
| 601 |
+
"group": "PASS_TO_PASS",
|
| 602 |
+
"package": "github.com/go-chi/chi/v5/middleware",
|
| 603 |
+
"test": "TestContentEncodingMiddleware/Support_for_deflate_and_gzip_encoding"
|
| 604 |
+
},
|
| 605 |
+
{
|
| 606 |
+
"group": "PASS_TO_PASS",
|
| 607 |
+
"package": "github.com/go-chi/chi/v5/middleware",
|
| 608 |
+
"test": "TestContentEncodingMiddleware/Support_for_gzip_and_deflate_encoding"
|
| 609 |
+
},
|
| 610 |
+
{
|
| 611 |
+
"group": "PASS_TO_PASS",
|
| 612 |
+
"package": "github.com/go-chi/chi/v5/middleware",
|
| 613 |
+
"test": "TestContentEncodingMiddleware/Support_gzip_encoding"
|
| 614 |
+
},
|
| 615 |
+
{
|
| 616 |
+
"group": "PASS_TO_PASS",
|
| 617 |
+
"package": "github.com/go-chi/chi/v5/middleware",
|
| 618 |
+
"test": "TestContentEncodingMiddleware/No_support_for_br_encoding"
|
| 619 |
+
},
|
| 620 |
+
{
|
| 621 |
+
"group": "PASS_TO_PASS",
|
| 622 |
+
"package": "github.com/go-chi/chi/v5/middleware",
|
| 623 |
+
"test": "TestContentCharset"
|
| 624 |
+
},
|
| 625 |
+
{
|
| 626 |
+
"group": "PASS_TO_PASS",
|
| 627 |
+
"package": "github.com/go-chi/chi/v5/middleware",
|
| 628 |
+
"test": "TestContentCharset/should_accept_requests_with_a_matching_charset"
|
| 629 |
+
},
|
| 630 |
+
{
|
| 631 |
+
"group": "PASS_TO_PASS",
|
| 632 |
+
"package": "github.com/go-chi/chi/v5/middleware",
|
| 633 |
+
"test": "TestContentCharset/should_not_accept_requests_with_no_charset_if_empty_charset_headers_are_not_allowed"
|
| 634 |
+
},
|
| 635 |
+
{
|
| 636 |
+
"group": "PASS_TO_PASS",
|
| 637 |
+
"package": "github.com/go-chi/chi/v5/middleware",
|
| 638 |
+
"test": "TestContentCharset/should_be_case-insensitive"
|
| 639 |
+
},
|
| 640 |
+
{
|
| 641 |
+
"group": "PASS_TO_PASS",
|
| 642 |
+
"package": "github.com/go-chi/chi/v5/middleware",
|
| 643 |
+
"test": "TestContentCharset/should_accept_requests_with_no_charset_if_empty_charset_headers_are_allowed"
|
| 644 |
+
},
|
| 645 |
+
{
|
| 646 |
+
"group": "PASS_TO_PASS",
|
| 647 |
+
"package": "github.com/go-chi/chi/v5/middleware",
|
| 648 |
+
"test": "TestContentCharset/should_accept_requests_with_a_matching_charset_when_multiple_charsets_are_supported"
|
| 649 |
+
},
|
| 650 |
+
{
|
| 651 |
+
"group": "PASS_TO_PASS",
|
| 652 |
+
"package": "github.com/go-chi/chi/v5/middleware",
|
| 653 |
+
"test": "TestContentCharset/should_accept_requests_with_a_matching_charset_with_extra_values"
|
| 654 |
+
},
|
| 655 |
+
{
|
| 656 |
+
"group": "PASS_TO_PASS",
|
| 657 |
+
"package": "github.com/go-chi/chi/v5/middleware",
|
| 658 |
+
"test": "TestContentCharset/should_not_accept_requests_with_a_mismatching_charset_even_if_empty_charsets_are_allowed"
|
| 659 |
+
},
|
| 660 |
+
{
|
| 661 |
+
"group": "PASS_TO_PASS",
|
| 662 |
+
"package": "github.com/go-chi/chi/v5/middleware",
|
| 663 |
+
"test": "TestContentCharset/should_not_accept_requests_with_a_mismatching_charset"
|
| 664 |
+
}
|
| 665 |
+
],
|
| 666 |
+
"frozen_files": {
|
| 667 |
+
"context_test.go": {
|
| 668 |
+
"mode": 420,
|
| 669 |
+
"sha256": "ca0a71488dadcf8fcecfed5d33d6d93de9b15ea34f2d89003076eb041dc14cae"
|
| 670 |
+
},
|
| 671 |
+
"middleware/compress_test.go": {
|
| 672 |
+
"mode": 420,
|
| 673 |
+
"sha256": "9278c028717563fb87223044c10efc6115ea706c0771cab7170d90aef89be2f7"
|
| 674 |
+
},
|
| 675 |
+
"middleware/content_charset_test.go": {
|
| 676 |
+
"mode": 420,
|
| 677 |
+
"sha256": "39f7d5333c1f47a11796952ed1ec13aa6dd880c940069a2c49552c2936e2c1a1"
|
| 678 |
+
},
|
| 679 |
+
"middleware/content_encoding_test.go": {
|
| 680 |
+
"mode": 420,
|
| 681 |
+
"sha256": "98bf93fb59e1613843f8f838ddb17dc160cf88dd95135b1b2637e188c69c4644"
|
| 682 |
+
},
|
| 683 |
+
"middleware/content_type_test.go": {
|
| 684 |
+
"mode": 420,
|
| 685 |
+
"sha256": "233f0c2f27f3d37719c6abc393c86804424a47d1ccebe17b018629b42f05a050"
|
| 686 |
+
},
|
| 687 |
+
"middleware/get_head_test.go": {
|
| 688 |
+
"mode": 420,
|
| 689 |
+
"sha256": "a660972a6e389bbea07db1414dd72c0593809c644a941b431ce43c902ec65099"
|
| 690 |
+
},
|
| 691 |
+
"middleware/logger_test.go": {
|
| 692 |
+
"mode": 420,
|
| 693 |
+
"sha256": "f15a9900122e075c3b2d146aeae6128dfe17d57a45ef40b06f7a9e74cc4fb35f"
|
| 694 |
+
},
|
| 695 |
+
"middleware/middleware_test.go": {
|
| 696 |
+
"mode": 420,
|
| 697 |
+
"sha256": "81744b82c9ea90b57a384c4a44cd889f7f5ab446b35eeb3d34983d0419a860bc"
|
| 698 |
+
},
|
| 699 |
+
"middleware/realip_test.go": {
|
| 700 |
+
"mode": 420,
|
| 701 |
+
"sha256": "6cab78b3ebc7fd581261afae6740423edc2199d9ba7ae3e0ffda99354f90d534"
|
| 702 |
+
},
|
| 703 |
+
"middleware/recoverer_test.go": {
|
| 704 |
+
"mode": 420,
|
| 705 |
+
"sha256": "55df30e19bcde6f044bf775e082edc445cc288594aa5b77150d99cd4c5a884fc"
|
| 706 |
+
},
|
| 707 |
+
"middleware/request_id_test.go": {
|
| 708 |
+
"mode": 420,
|
| 709 |
+
"sha256": "519e920890f2a33b77d0140a4a8ffcc933752bc04b3efb0af0793e11722b639e"
|
| 710 |
+
},
|
| 711 |
+
"middleware/strip_test.go": {
|
| 712 |
+
"mode": 420,
|
| 713 |
+
"sha256": "25f4ed91d5dbe150e54694f6b032e156fc241690fe9d9bd575f4799e0f39d9aa"
|
| 714 |
+
},
|
| 715 |
+
"middleware/sunset_test.go": {
|
| 716 |
+
"mode": 420,
|
| 717 |
+
"sha256": "ea640c109f86cc5d1fbceea558e3ab983d8f0de642c16fd2b4ad750efada0101"
|
| 718 |
+
},
|
| 719 |
+
"middleware/throttle_test.go": {
|
| 720 |
+
"mode": 420,
|
| 721 |
+
"sha256": "81e253f3039743db388ab9832fbc962271f529569440650e9c06a8270c948d2a"
|
| 722 |
+
},
|
| 723 |
+
"middleware/url_format_test.go": {
|
| 724 |
+
"mode": 420,
|
| 725 |
+
"sha256": "04efa8cfbda922ff671ab3bdbc00f6fa06bf776c300dc8605815de1def33aaa1"
|
| 726 |
+
},
|
| 727 |
+
"middleware/wrap_writer_test.go": {
|
| 728 |
+
"mode": 420,
|
| 729 |
+
"sha256": "bb07fb38cf0896a7c6bae8916bcad85002b29a71fdd5bb3058ddccf12a3dc0f8"
|
| 730 |
+
},
|
| 731 |
+
"mux_test.go": {
|
| 732 |
+
"mode": 420,
|
| 733 |
+
"sha256": "bfcf63336579f68160e44af3cef60949ebd506632950d6b584eace8be26d0037"
|
| 734 |
+
},
|
| 735 |
+
"path_value_test.go": {
|
| 736 |
+
"mode": 420,
|
| 737 |
+
"sha256": "3df65ffdb1dfc529cb9f61ed17bbd189d8c14f581473cd00d5a4949df277ad71"
|
| 738 |
+
},
|
| 739 |
+
"testdata/cert.pem": {
|
| 740 |
+
"mode": 420,
|
| 741 |
+
"sha256": "8fb47f5e036bcb4daaeed180fa7164504a8ca43bdc010fd418618da48fdd340c"
|
| 742 |
+
},
|
| 743 |
+
"testdata/key.pem": {
|
| 744 |
+
"mode": 420,
|
| 745 |
+
"sha256": "fecae623b5a0ae0a1110285c66e3e0e7f6019f49fd540ed3d9b85f65b9054f6c"
|
| 746 |
+
},
|
| 747 |
+
"tree_test.go": {
|
| 748 |
+
"mode": 420,
|
| 749 |
+
"sha256": "6d1b6f2b721c61b03dd8bbdfeccf5431ccc1f1d6080afed6fe67e677fc790ce2"
|
| 750 |
+
}
|
| 751 |
+
}
|
| 752 |
+
}
|
tasks/go-chi__chi.23c395f8.func_pm_ctrl_invert_if__xft5h5dg/tests/test.sh
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/bin/bash
|
| 2 |
+
set -eu
|
| 3 |
+
exec /usr/local/bin/python -I /tests/grade.py
|