Some checks failed
Test / test (1.24.x, ubuntu-latest) (push) Successful in 2m56s
Test / test (1.25.x, ubuntu-latest) (push) Successful in 2m15s
Test / test (1.26.x, ubuntu-latest) (push) Successful in 2m23s
Test / test (1.27.x, ubuntu-latest) (push) Failing after 3m56s
Test / test (1.24.x, windows-latest) (push) Has been cancelled
Test / test (1.25.x, windows-latest) (push) Has been cancelled
Test / test (1.26.x, windows-latest) (push) Has been cancelled
Test / test (1.27.x, windows-latest) (push) Has been cancelled
70 lines
1.6 KiB
Go
70 lines
1.6 KiB
Go
package middleware
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
)
|
|
|
|
func TestURLFormat(t *testing.T) {
|
|
r := chi.NewRouter()
|
|
|
|
r.Use(URLFormat)
|
|
|
|
r.NotFound(func(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(404)
|
|
w.Write([]byte("nothing here"))
|
|
})
|
|
|
|
r.Route("/samples/articles/samples.{articleID}", func(r chi.Router) {
|
|
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
|
articleID := chi.URLParam(r, "articleID")
|
|
w.Write([]byte(articleID))
|
|
})
|
|
})
|
|
|
|
r.Route("/articles/{articleID}", func(r chi.Router) {
|
|
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
|
articleID := chi.URLParam(r, "articleID")
|
|
w.Write([]byte(articleID))
|
|
})
|
|
})
|
|
|
|
ts := httptest.NewServer(r)
|
|
defer ts.Close()
|
|
|
|
if _, resp := testRequest(t, ts, "GET", "/articles/1.json", nil); resp != "1" {
|
|
t.Fatal(resp)
|
|
}
|
|
if _, resp := testRequest(t, ts, "GET", "/articles/1.xml", nil); resp != "1" {
|
|
t.Fatal(resp)
|
|
}
|
|
if _, resp := testRequest(t, ts, "GET", "/samples/articles/samples.1.json", nil); resp != "1" {
|
|
t.Fatal(resp)
|
|
}
|
|
if _, resp := testRequest(t, ts, "GET", "/samples/articles/samples.1.xml", nil); resp != "1" {
|
|
t.Fatal(resp)
|
|
}
|
|
}
|
|
|
|
func TestURLFormatInSubRouter(t *testing.T) {
|
|
r := chi.NewRouter()
|
|
|
|
r.Route("/articles/{articleID}", func(r chi.Router) {
|
|
r.Use(URLFormat)
|
|
r.Get("/subroute", func(w http.ResponseWriter, r *http.Request) {
|
|
articleID := chi.URLParam(r, "articleID")
|
|
w.Write([]byte(articleID))
|
|
})
|
|
})
|
|
|
|
ts := httptest.NewServer(r)
|
|
defer ts.Close()
|
|
|
|
if _, resp := testRequest(t, ts, "GET", "/articles/1/subroute.json", nil); resp != "1" {
|
|
t.Fatal(resp)
|
|
}
|
|
}
|