httprouter/router_test.go
Martin Riedl 2fb1b35713
feat: renamed goHTTPRouter to httprouter
BREAKING CHANGE: new package name git.martin-riedl.de/golang/httprouter
2025-02-27 20:34:47 +01:00

116 lines
3.1 KiB
Go
Executable file

// Copyright 2025 Martin Riedl
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package httprouter
import (
"net"
"net/http"
"strings"
"testing"
)
func TestRouterInvalidPath(t *testing.T) {
router := New()
if err := router.HandleFunc(http.MethodGet, "missing/slash", emptyHandlerFunction); err == nil {
t.Error("an error was expected during path parsing (missing slash)")
}
if err := router.HandleFunc(http.MethodGet, "/invalid/shash/", emptyHandlerFunction); err == nil {
t.Error("an error was expected during path parsing (ending slash)")
}
}
func TestRouterSamePath(t *testing.T) {
router := New()
Must(router.HandleFunc(http.MethodGet, "/user/:id/name", emptyHandlerFunction))
if err := router.HandleFunc(http.MethodGet, "/user/:name/name", emptyHandlerFunction); err == nil {
t.Error("missing exception; path was defined twice")
}
}
func TestRouterServer(t *testing.T) {
quitChan := make(chan bool, 2)
router := New()
router.HandleFunc(http.MethodGet, "/home", func(response http.ResponseWriter, request *http.Request, info RoutingInfo) {
quitChan <- true
})
listener, err := net.Listen("tcp", "localhost:8080")
if err != nil {
t.Error(err)
}
go http.Serve(listener, router)
// call method (not existing)
resp, err := http.Get("http://localhost:8080/home/test")
if err != nil {
t.Error(err)
}
if resp.StatusCode != http.StatusNotFound {
t.Error("server should return 404 but returned", resp.StatusCode)
}
// call method (POST not defined)
resp, err = http.Post("http://localhost:8080/home", "text/text", strings.NewReader("yea"))
if err != nil {
t.Error(err)
}
if resp.StatusCode != http.StatusNotFound {
t.Error("server should return 404 but returned", resp.StatusCode)
}
// call method
_, err = http.Get("http://localhost:8080/home")
if err != nil {
t.Error(err)
}
// wait for completion
<-quitChan
listener.Close()
}
func TestRouterServerHandler(t *testing.T) {
testHandler := TestHandler{CloseChannel: make(chan bool, 2)}
router := New()
router.Handle(http.MethodGet, "/", testHandler)
listener, err := net.Listen("tcp", "localhost:8081")
if err != nil {
t.Error(err)
}
go http.Serve(listener, router)
// call method
_, err = http.Get("http://localhost:8081/?test=false")
if err != nil {
t.Error(err)
}
// wait for completion
<-testHandler.CloseChannel
listener.Close()
}
func emptyHandlerFunction(response http.ResponseWriter, request *http.Request, routerInfo RoutingInfo) {
// it's fine
}
type TestHandler struct {
CloseChannel chan bool
}
func (t TestHandler) ServeHTTP(response http.ResponseWriter, request *http.Request, routerInfo RoutingInfo) {
t.CloseChannel <- true
}