💾 Archived View for source.community › ckaznocha › gemini › raw › main › mux_test.go captured on 2024-02-05 at 09:58:47.
⬅️ Previous capture (2021-12-17)
-=-=-=-=-=-=-
package gemini_test import ( "context" "fmt" "testing" "source.community/ckaznocha/gemini" "source.community/ckaznocha/gemini/geminitest" ) func TestServeMux(t *testing.T) { t.Parallel() tests := []struct { name string requestPath string want string patterns []string want404 bool }{ { name: "returns the root handler", patterns: []string{"/", "/foo", "/foo/bar"}, requestPath: "/", want: "/", }, { name: "returns the root handler", patterns: []string{"/", "/foo", "/foo/bar"}, requestPath: "", want: "/", }, { name: "returns the subtree root", patterns: []string{"/", "/foo", "/foo/bar"}, requestPath: "/foo/baz", want: "/foo", }, { name: "returns the logest match", patterns: []string{"/", "/foo", "/foo/bar"}, requestPath: "/foo/bar", want: "/foo/bar", }, { name: "returns a route with a trailing slash if both are registered", patterns: []string{"/foo", "/foo/"}, requestPath: "/foo/", want: "/foo/", }, { name: "returns a route without a trailing slash if both are registered", patterns: []string{"/foo", "/foo/"}, requestPath: "/foo", want: "/foo", }, { name: "returns a 404 if no route matches", patterns: []string{"/foo"}, requestPath: "/bar", want: "", want404: true, }, } for _, tt := range tests { tt := tt t.Run(tt.name, func(t *testing.T) { t.Parallel() sm := gemini.NewServeMux() for _, pattern := range tt.patterns { pattern := pattern sm.HandleFunc(pattern, func(ctx context.Context, w gemini.ResponseWriter, _ *gemini.Request) { fmt.Fprint(w.Success(ctx, ""), pattern) }) } recorder := geminitest.NewResponseRecorder() req := &gemini.Request{URI: &gemini.URI{Path: tt.requestPath}} sm.ServeGemini(context.Background(), recorder, req) if recorder.Body.String() != tt.want { t.Errorf("ServeMux.ServeGemini() = %v, want %v", recorder.Body.String(), tt.want) } if tt.want404 && recorder.Code != gemini.StatusNotFound { t.Errorf("ServeMux.ServeGemini() = %v, want %v", recorder.Code, gemini.StatusNotFound) } }) } }