212 lines
4.2 KiB
Go
212 lines
4.2 KiB
Go
// 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 (
|
|
"embed"
|
|
"net/http"
|
|
"net/url"
|
|
"testing"
|
|
)
|
|
|
|
//go:embed README.md LICENSE.txt
|
|
var staticFiles embed.FS
|
|
|
|
type testWriter struct {
|
|
header http.Header
|
|
written []byte
|
|
statusCode int
|
|
}
|
|
|
|
func (tw *testWriter) Header() http.Header {
|
|
if tw.header == nil {
|
|
tw.header = make(http.Header)
|
|
}
|
|
return tw.header
|
|
}
|
|
|
|
func (tw *testWriter) Write(bytes []byte) (int, error) {
|
|
tw.written = append(tw.written, bytes...)
|
|
return len(bytes), nil
|
|
}
|
|
|
|
func (tw *testWriter) WriteHeader(statusCode int) {
|
|
tw.statusCode = statusCode
|
|
}
|
|
|
|
func TestFSReadmeFile(t *testing.T) {
|
|
// create new FS
|
|
fs := NewFS(&staticFiles)
|
|
|
|
// serve the request
|
|
tw := &testWriter{}
|
|
tr := &http.Request{
|
|
Method: http.MethodGet,
|
|
URL: &url.URL{
|
|
Path: "/README.md",
|
|
},
|
|
}
|
|
fs.ServeHTTP(tw, tr, RoutingInfo{})
|
|
|
|
// check data
|
|
if tw.statusCode != http.StatusOK {
|
|
t.Error("received invalid http status code", tw.statusCode)
|
|
}
|
|
}
|
|
|
|
func TestFSReadmeFileLocal(t *testing.T) {
|
|
// create new FS
|
|
fs := NewFS(&staticFiles)
|
|
fs.UseLocalFolder = true
|
|
fs.LocalFolderPrefix = "/"
|
|
|
|
// serve the request
|
|
tw := &testWriter{}
|
|
tr := &http.Request{
|
|
Method: http.MethodGet,
|
|
URL: &url.URL{
|
|
Path: "/README.md",
|
|
},
|
|
}
|
|
fs.ServeHTTP(tw, tr, RoutingInfo{})
|
|
|
|
// check data
|
|
if tw.statusCode != http.StatusOK {
|
|
t.Error("received invalid http status code", tw.statusCode)
|
|
}
|
|
}
|
|
|
|
func TestFSReadmeFileHead(t *testing.T) {
|
|
// create new FS
|
|
fs := NewFS(&staticFiles)
|
|
|
|
// serve the request
|
|
tw := &testWriter{}
|
|
tr := &http.Request{
|
|
Method: http.MethodHead,
|
|
URL: &url.URL{
|
|
Path: "/README.md",
|
|
},
|
|
}
|
|
fs.ServeHTTP(tw, tr, RoutingInfo{})
|
|
|
|
// check data
|
|
if tw.statusCode != http.StatusOK {
|
|
t.Error("received invalid http status code", tw.statusCode)
|
|
}
|
|
}
|
|
|
|
func TestFSLicense(t *testing.T) {
|
|
// create new FS
|
|
fs := NewFS(&staticFiles)
|
|
|
|
// serve the request
|
|
tw := &testWriter{}
|
|
tr := &http.Request{
|
|
Method: http.MethodHead,
|
|
URL: &url.URL{
|
|
Path: "/LICENSE.txt",
|
|
},
|
|
}
|
|
fs.ServeHTTP(tw, tr, RoutingInfo{})
|
|
|
|
// check data
|
|
if tw.statusCode != http.StatusOK {
|
|
t.Error("received invalid http status code", tw.statusCode)
|
|
}
|
|
}
|
|
|
|
func TestFSFolder(t *testing.T) {
|
|
// create new FS
|
|
fs := NewFS(&staticFiles)
|
|
|
|
// serve the request
|
|
tw := &testWriter{}
|
|
tr := &http.Request{
|
|
Method: http.MethodHead,
|
|
URL: &url.URL{
|
|
Path: "/",
|
|
},
|
|
}
|
|
fs.ServeHTTP(tw, tr, RoutingInfo{})
|
|
|
|
// check data
|
|
if tw.statusCode != http.StatusNotFound {
|
|
t.Error("received invalid http status code", tw.statusCode)
|
|
}
|
|
}
|
|
|
|
func TestFSFolderLocal(t *testing.T) {
|
|
// create new FS
|
|
fs := NewFS(&staticFiles)
|
|
fs.UseLocalFolder = true
|
|
fs.LocalFolderPrefix = "/"
|
|
|
|
// serve the request
|
|
tw := &testWriter{}
|
|
tr := &http.Request{
|
|
Method: http.MethodHead,
|
|
URL: &url.URL{
|
|
Path: "/",
|
|
},
|
|
}
|
|
fs.ServeHTTP(tw, tr, RoutingInfo{})
|
|
|
|
// check data
|
|
if tw.statusCode != http.StatusNotFound {
|
|
t.Error("received invalid http status code", tw.statusCode)
|
|
}
|
|
}
|
|
|
|
func TestFSFolderIndex(t *testing.T) {
|
|
// create new FS
|
|
fs := NewFS(&staticFiles)
|
|
fs.IndexFileNames = []string{"README.md"}
|
|
|
|
// serve the request
|
|
tw := &testWriter{}
|
|
tr := &http.Request{
|
|
Method: http.MethodHead,
|
|
URL: &url.URL{
|
|
Path: "/",
|
|
},
|
|
}
|
|
fs.ServeHTTP(tw, tr, RoutingInfo{})
|
|
|
|
// check data
|
|
if tw.statusCode != http.StatusOK {
|
|
t.Error("received invalid http status code", tw.statusCode)
|
|
}
|
|
}
|
|
|
|
func TestFSNotFound(t *testing.T) {
|
|
// create new FS
|
|
fs := NewFS(&staticFiles)
|
|
|
|
// serve the request
|
|
tw := &testWriter{}
|
|
tr := &http.Request{
|
|
Method: http.MethodGet,
|
|
URL: &url.URL{
|
|
Path: "/not-exists.txt",
|
|
},
|
|
}
|
|
fs.ServeHTTP(tw, tr, RoutingInfo{})
|
|
|
|
// check data
|
|
if tw.statusCode != http.StatusNotFound {
|
|
t.Error("received invalid http status code", tw.statusCode)
|
|
}
|
|
}
|