๐Ÿ’พ Archived View for source.community โ€บ ckaznocha โ€บ gemini โ€บ blob โ€บ main โ€บ mux_test.go captured on 2023-07-10 at 13:30:10. Gemini links have been rewritten to link to archived content

View Raw

More Information

โฌ…๏ธ Previous capture (2023-01-29)

โžก๏ธ Next capture (2024-02-05)

-=-=-=-=-=-=-

                                                         .
,-. ,-. . . ,-. ,-. ,-.    ,-. ,-. ,-,-. ,-,-. . . ,-. . |- . .
`-. | | | | |   |   |-'    |   | | | | | | | | | | | | | |  | |
`-' `-' `-^ '   `-' `-' :: `-' `-' ' ' ' ' ' ' `-^ ' ' ' `' `-|
                                                             /|
                                                            `-'

Profile for ckaznocha

ckaznocha / gemini

git clone https://source.community/ckaznocha/gemini.git

Branches

Log

Tree

/mux_test.go (main)

โ†‘ /

blob

View raw contents of /mux_test.go (main)

โ”€โ”€โ”€โ”€โ•ฎ
   1โ”‚ package gemini_test
   2โ”‚ 
   3โ”‚ import (
   4โ”‚ 	"context"
   5โ”‚ 	"fmt"
   6โ”‚ 	"testing"
   7โ”‚ 
   8โ”‚ 	"source.community/ckaznocha/gemini"
   9โ”‚ 	"source.community/ckaznocha/gemini/geminitest"
  10โ”‚ )
  11โ”‚ 
  12โ”‚ func TestServeMux(t *testing.T) {
  13โ”‚ 	t.Parallel()
  14โ”‚ 
  15โ”‚ 	tests := []struct {
  16โ”‚ 		name        string
  17โ”‚ 		requestPath string
  18โ”‚ 		want        string
  19โ”‚ 		patterns    []string
  20โ”‚ 		want404     bool
  21โ”‚ 	}{
  22โ”‚ 		{
  23โ”‚ 			name:        "returns the root handler",
  24โ”‚ 			patterns:    []string{"/", "/foo", "/foo/bar"},
  25โ”‚ 			requestPath: "/",
  26โ”‚ 			want:        "/",
  27โ”‚ 		},
  28โ”‚ 		{
  29โ”‚ 			name:        "returns the root handler",
  30โ”‚ 			patterns:    []string{"/", "/foo", "/foo/bar"},
  31โ”‚ 			requestPath: "",
  32โ”‚ 			want:        "/",
  33โ”‚ 		},
  34โ”‚ 		{
  35โ”‚ 			name:        "returns the subtree root",
  36โ”‚ 			patterns:    []string{"/", "/foo", "/foo/bar"},
  37โ”‚ 			requestPath: "/foo/baz",
  38โ”‚ 			want:        "/foo",
  39โ”‚ 		},
  40โ”‚ 		{
  41โ”‚ 			name:        "returns the logest match",
  42โ”‚ 			patterns:    []string{"/", "/foo", "/foo/bar"},
  43โ”‚ 			requestPath: "/foo/bar",
  44โ”‚ 			want:        "/foo/bar",
  45โ”‚ 		},
  46โ”‚ 		{
  47โ”‚ 			name:        "returns a route with a trailing slash if both are registered",
  48โ”‚ 			patterns:    []string{"/foo", "/foo/"},
  49โ”‚ 			requestPath: "/foo/",
  50โ”‚ 			want:        "/foo/",
  51โ”‚ 		},
  52โ”‚ 		{
  53โ”‚ 			name:        "returns a route without a trailing slash if both are registered",
  54โ”‚ 			patterns:    []string{"/foo", "/foo/"},
  55โ”‚ 			requestPath: "/foo",
  56โ”‚ 			want:        "/foo",
  57โ”‚ 		},
  58โ”‚ 		{
  59โ”‚ 			name:        "returns a 404 if no route matches",
  60โ”‚ 			patterns:    []string{"/foo"},
  61โ”‚ 			requestPath: "/bar",
  62โ”‚ 			want:        "",
  63โ”‚ 			want404:     true,
  64โ”‚ 		},
  65โ”‚ 	}
  66โ”‚ 
  67โ”‚ 	for _, tt := range tests {
  68โ”‚ 		tt := tt
  69โ”‚ 		t.Run(tt.name, func(t *testing.T) {
  70โ”‚ 			t.Parallel()
  71โ”‚ 			sm := gemini.NewServeMux()
  72โ”‚ 			for _, pattern := range tt.patterns {
  73โ”‚ 				pattern := pattern
  74โ”‚ 				sm.HandleFunc(pattern, func(ctx context.Context, w gemini.ResponseWriter, _ *gemini.Request) {
  75โ”‚ 					fmt.Fprint(w.Success(ctx, ""), pattern)
  76โ”‚ 				})
  77โ”‚ 			}
  78โ”‚ 			recorder := geminitest.NewResponseRecorder()
  79โ”‚ 			req := &gemini.Request{URI: &gemini.URI{Path: tt.requestPath}}
  80โ”‚ 			sm.ServeGemini(context.Background(), recorder, req)
  81โ”‚ 			if recorder.Body.String() != tt.want {
  82โ”‚ 				t.Errorf("ServeMux.ServeGemini() = %v, want %v", recorder.Body.String(), tt.want)
  83โ”‚ 			}
  84โ”‚ 
  85โ”‚ 			if tt.want404 && recorder.Code != gemini.StatusNotFound {
  86โ”‚ 				t.Errorf("ServeMux.ServeGemini() = %v, want %v", recorder.Code, gemini.StatusNotFound)
  87โ”‚ 			}
  88โ”‚ 		})
  89โ”‚ 	}
  90โ”‚ }
โ”€โ”€โ”€โ”€โ•ฏ

ยท ยท ยท

๐Ÿก Home

FAQs

Privacy Policy

Terms & Conditions

Official Gemlog

info@source.community

ยฉ 2023 source.community