Add test
This commit is contained in:
		
							parent
							
								
									99b50e6caf
								
							
						
					
					
						commit
						ec68ab8609
					
				@ -30,20 +30,11 @@ func main() {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	mux := &gemini.ServeMux{}
 | 
						mux := &gemini.ServeMux{}
 | 
				
			||||||
	// mux.HandleFunc("/", func(rw *gemini.ResponseWriter, req *gemini.Request) {
 | 
						mux.HandleFunc("/", func(rw *gemini.ResponseWriter, req *gemini.Request) {
 | 
				
			||||||
	// 	log.Printf("Request from %s for %s with certificates %v", req.RemoteAddr.String(), req.URL.String(), req.TLS.PeerCertificates)
 | 
							log.Printf("Request from %s for %s with certificates %v", req.RemoteAddr.String(), req.URL.String(), req.TLS.PeerCertificates)
 | 
				
			||||||
	// 	rw.WriteHeader(gemini.StatusSuccess, "text/gemini")
 | 
							rw.WriteHeader(gemini.StatusSuccess, "text/gemini")
 | 
				
			||||||
	// 	rw.Write([]byte("You requested " + req.URL.String()))
 | 
							rw.Write([]byte("You requested " + req.URL.String()))
 | 
				
			||||||
	// })
 | 
						})
 | 
				
			||||||
	// mux.HandleFunc("/cert", func(rw *gemini.ResponseWriter, req *gemini.Request) {
 | 
					 | 
				
			||||||
	// 	rw.WriteHeader(gemini.StatusClientCertificateRequired, "Certificate required")
 | 
					 | 
				
			||||||
	// })
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	mux.HandleFunc("https://example.com/path", nil)
 | 
					 | 
				
			||||||
	mux.HandleFunc("http://example.com/path", nil)
 | 
					 | 
				
			||||||
	mux.HandleFunc("example.com/path", nil)
 | 
					 | 
				
			||||||
	mux.HandleFunc("/path", nil)
 | 
					 | 
				
			||||||
	mux.HandleFunc("/longpath", nil)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	server := gemini.Server{
 | 
						server := gemini.Server{
 | 
				
			||||||
		TLSConfig: config,
 | 
							TLSConfig: config,
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										21
									
								
								gemini.go
									
									
									
									
									
								
							
							
						
						
									
										21
									
								
								gemini.go
									
									
									
									
									
								
							@ -415,17 +415,15 @@ type ServeMux struct {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type muxEntry struct {
 | 
					type muxEntry struct {
 | 
				
			||||||
	scheme  string
 | 
						u       *url.URL
 | 
				
			||||||
	host    string
 | 
					 | 
				
			||||||
	path    string
 | 
					 | 
				
			||||||
	handler Handler
 | 
						handler Handler
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (m *ServeMux) match(url *url.URL) Handler {
 | 
					func (m *ServeMux) match(url *url.URL) Handler {
 | 
				
			||||||
	for _, e := range m.entries {
 | 
						for _, e := range m.entries {
 | 
				
			||||||
		if (e.scheme == "" || url.Scheme == e.scheme) &&
 | 
							if (e.u.Scheme == "" || url.Scheme == e.u.Scheme) &&
 | 
				
			||||||
			(e.host == "" || url.Host == e.host) &&
 | 
								(e.u.Host == "" || url.Host == e.u.Host) &&
 | 
				
			||||||
			strings.HasPrefix(url.Path, e.path) {
 | 
								strings.HasPrefix(url.Path, e.u.Path) {
 | 
				
			||||||
			return e.handler
 | 
								return e.handler
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@ -439,13 +437,10 @@ func (m *ServeMux) Handle(pattern string, handler Handler) {
 | 
				
			|||||||
		panic(err)
 | 
							panic(err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	e := muxEntry{
 | 
						e := muxEntry{
 | 
				
			||||||
		url.Scheme,
 | 
							url,
 | 
				
			||||||
		url.Host,
 | 
					 | 
				
			||||||
		url.Path,
 | 
					 | 
				
			||||||
		handler,
 | 
							handler,
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	m.entries = appendSorted(m.entries, e)
 | 
						m.entries = appendSorted(m.entries, e)
 | 
				
			||||||
	log.Print(m.entries)
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// HandleFunc registers a HandlerFunc for the given pattern.
 | 
					// HandleFunc registers a HandlerFunc for the given pattern.
 | 
				
			||||||
@ -490,9 +485,9 @@ func appendSorted(es []muxEntry, e muxEntry) []muxEntry {
 | 
				
			|||||||
		// return len(es[i].path) < len(e.path)
 | 
							// return len(es[i].path) < len(e.path)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// Condensed version:
 | 
							// Condensed version:
 | 
				
			||||||
		return (es[i].scheme == "" || (e.scheme != "" && len(es[i].scheme) < len(e.scheme))) &&
 | 
							return (es[i].u.Scheme == "" || (e.u.Scheme != "" && len(es[i].u.Scheme) < len(e.u.Scheme))) &&
 | 
				
			||||||
			(es[i].host == "" || (e.host != "" && len(es[i].host) < len(e.host))) &&
 | 
								(es[i].u.Host == "" || (e.u.Host != "" && len(es[i].u.Host) < len(e.u.Host))) &&
 | 
				
			||||||
			len(es[i].path) < len(e.path)
 | 
								len(es[i].u.Path) < len(e.u.Path)
 | 
				
			||||||
	})
 | 
						})
 | 
				
			||||||
	if i == n {
 | 
						if i == n {
 | 
				
			||||||
		return append(es, e)
 | 
							return append(es, e)
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										41
									
								
								gemini_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										41
									
								
								gemini_test.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,41 @@
 | 
				
			|||||||
 | 
					package gemini
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"math/rand"
 | 
				
			||||||
 | 
						"testing"
 | 
				
			||||||
 | 
						"time"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestServeMuxEntryOrder(t *testing.T) {
 | 
				
			||||||
 | 
						expected := []string{
 | 
				
			||||||
 | 
							"https://example.com/longpath",
 | 
				
			||||||
 | 
							"https://example.com/path",
 | 
				
			||||||
 | 
							"https://example.com",
 | 
				
			||||||
 | 
							"http://example.com/longpath",
 | 
				
			||||||
 | 
							"http://example.com/path",
 | 
				
			||||||
 | 
							"http://example.com",
 | 
				
			||||||
 | 
							"example.com/longpath",
 | 
				
			||||||
 | 
							"example.com/path",
 | 
				
			||||||
 | 
							"example.com",
 | 
				
			||||||
 | 
							"/longpath",
 | 
				
			||||||
 | 
							"/path",
 | 
				
			||||||
 | 
							"/",
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Shuffle input
 | 
				
			||||||
 | 
						a := make([]string, len(expected))
 | 
				
			||||||
 | 
						copy(expected, a)
 | 
				
			||||||
 | 
						rand.Seed(time.Now().UnixNano())
 | 
				
			||||||
 | 
						rand.Shuffle(len(a), func(i, j int) { a[i], a[j] = a[j], a[i] })
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						mux := &ServeMux{}
 | 
				
			||||||
 | 
						for _, s := range a {
 | 
				
			||||||
 | 
							mux.Handle(s, nil)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						for i, e := range mux.entries {
 | 
				
			||||||
 | 
							s := e.u.String()
 | 
				
			||||||
 | 
							if s != expected[i] {
 | 
				
			||||||
 | 
								t.Errorf("wrong order of mux entries: expected %s, got %s", expected[i], s)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user