Added unit tests to server functions.

This commit is contained in:
Jeromy Streets 2018-07-29 11:34:12 -07:00
parent 8ef4d4c425
commit 10b35029f5
2 changed files with 102 additions and 6 deletions

View File

@ -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
View 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()
})
}
}