mirror of
https://github.com/halverneus/static-file-server.git
synced 2024-11-24 09:05:30 +00:00
Added unit tests to server functions.
This commit is contained in:
parent
8ef4d4c425
commit
10b35029f5
@ -8,10 +8,29 @@ import (
|
||||
"github.com/halverneus/static-file-server/handle"
|
||||
)
|
||||
|
||||
var (
|
||||
// Values to be overridden to simplify unit testing.
|
||||
selectHandler = handlerSelector
|
||||
selectListener = listenerSelector
|
||||
)
|
||||
|
||||
// Run server.
|
||||
func Run() error {
|
||||
// Choose and set the appropriate, optimized static file serving function.
|
||||
var handler http.HandlerFunc
|
||||
handler := selectHandler()
|
||||
|
||||
// Serve files over HTTP or HTTPS based on paths to TLS files being
|
||||
// provided.
|
||||
listener := selectListener()
|
||||
|
||||
binding := fmt.Sprintf("%s:%d", config.Get.Host, config.Get.Port)
|
||||
return listener(binding, handler)
|
||||
}
|
||||
|
||||
// handlerSelector returns the appropriate request handler based on
|
||||
// configuration.
|
||||
func handlerSelector() (handler http.HandlerFunc) {
|
||||
// Choose and set the appropriate, optimized static file serving function.
|
||||
if 0 == len(config.Get.URLPrefix) {
|
||||
handler = handle.Basic(config.Get.Folder)
|
||||
} else {
|
||||
@ -22,9 +41,14 @@ func Run() error {
|
||||
if !config.Get.ShowListing {
|
||||
handler = handle.IgnoreIndex(handler)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Serve files over HTTP or HTTPS based on paths to TLS files being provided.
|
||||
var listener handle.ListenerFunc
|
||||
// listenerSelector returns the appropriate listener handler based on
|
||||
// configuration.
|
||||
func listenerSelector() (listener handle.ListenerFunc) {
|
||||
// Serve files over HTTP or HTTPS based on paths to TLS files being
|
||||
// provided.
|
||||
if 0 < len(config.Get.TLSCert) {
|
||||
listener = handle.TLSListening(
|
||||
config.Get.TLSCert,
|
||||
@ -33,7 +57,5 @@ func Run() error {
|
||||
} else {
|
||||
listener = handle.Listening()
|
||||
}
|
||||
|
||||
binding := fmt.Sprintf("%s:%d", config.Get.Host, config.Get.Port)
|
||||
return listener(binding, handler)
|
||||
return
|
||||
}
|
||||
|
74
cli/server/server_test.go
Normal file
74
cli/server/server_test.go
Normal file
@ -0,0 +1,74 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"github.com/halverneus/static-file-server/config"
|
||||
"github.com/halverneus/static-file-server/handle"
|
||||
)
|
||||
|
||||
func TestRun(t *testing.T) {
|
||||
listenerError := errors.New("listener")
|
||||
selectListener = func() handle.ListenerFunc {
|
||||
return func(string, http.HandlerFunc) error {
|
||||
return listenerError
|
||||
}
|
||||
}
|
||||
|
||||
if err := Run(); listenerError != err {
|
||||
t.Errorf("Expected %v but got %v", listenerError, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandlerSelector(t *testing.T) {
|
||||
// This test only exercises function branches.
|
||||
testFolder := "/web"
|
||||
testPrefix := "/url/prefix"
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
folder string
|
||||
prefix string
|
||||
listing bool
|
||||
}{
|
||||
{"Basic handler", testFolder, "", true},
|
||||
{"Prefix handler", testFolder, testPrefix, true},
|
||||
{"Basic and hide listing handler", testFolder, "", false},
|
||||
{"Prefix and hide listing handler", testFolder, testPrefix, false},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
config.Get.Folder = tc.folder
|
||||
config.Get.URLPrefix = tc.prefix
|
||||
config.Get.ShowListing = tc.listing
|
||||
|
||||
handlerSelector()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestListenerSelector(t *testing.T) {
|
||||
// This test only exercises function branches.
|
||||
testCert := "file.crt"
|
||||
testKey := "file.key"
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
cert string
|
||||
key string
|
||||
}{
|
||||
{"HTTP", "", ""},
|
||||
{"HTTPS", testCert, testKey},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
config.Get.TLSCert = tc.cert
|
||||
config.Get.TLSKey = tc.key
|
||||
listenerSelector()
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user