mirror of
https://github.com/caddyserver/caddy.git
synced 2025-05-23 11:00:00 +08:00
Move common function into existing file
This commit is contained in:
@ -1,8 +1,11 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestIndexfile(t *testing.T) {
|
||||
@ -42,3 +45,64 @@ func TestIndexfile(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetLastModified(t *testing.T) {
|
||||
nowTime := time.Now()
|
||||
|
||||
// ovewrite the function to return reliable time
|
||||
originalGetCurrentTimeFunc := currentTime
|
||||
currentTime = func() time.Time {
|
||||
return nowTime
|
||||
}
|
||||
defer func() {
|
||||
currentTime = originalGetCurrentTimeFunc
|
||||
}()
|
||||
|
||||
pastTime := nowTime.Truncate(1 * time.Hour)
|
||||
futureTime := nowTime.Add(1 * time.Hour)
|
||||
|
||||
tests := []struct {
|
||||
inputModTime time.Time
|
||||
expectedIsHeaderSet bool
|
||||
expectedLastModified string
|
||||
}{
|
||||
{
|
||||
inputModTime: pastTime,
|
||||
expectedIsHeaderSet: true,
|
||||
expectedLastModified: pastTime.UTC().Format(http.TimeFormat),
|
||||
},
|
||||
{
|
||||
inputModTime: nowTime,
|
||||
expectedIsHeaderSet: true,
|
||||
expectedLastModified: nowTime.UTC().Format(http.TimeFormat),
|
||||
},
|
||||
{
|
||||
inputModTime: futureTime,
|
||||
expectedIsHeaderSet: true,
|
||||
expectedLastModified: nowTime.UTC().Format(http.TimeFormat),
|
||||
},
|
||||
{
|
||||
inputModTime: time.Time{},
|
||||
expectedIsHeaderSet: false,
|
||||
},
|
||||
}
|
||||
|
||||
for i, test := range tests {
|
||||
responseRecorder := httptest.NewRecorder()
|
||||
errorPrefix := fmt.Sprintf("Test [%d]: ", i)
|
||||
SetLastModifiedHeader(responseRecorder, test.inputModTime)
|
||||
actualLastModifiedHeader := responseRecorder.Header().Get("Last-Modified")
|
||||
|
||||
if test.expectedIsHeaderSet && actualLastModifiedHeader == "" {
|
||||
t.Fatalf(errorPrefix + "Expected to find Last-Modified header, but found nothing")
|
||||
}
|
||||
|
||||
if !test.expectedIsHeaderSet && actualLastModifiedHeader != "" {
|
||||
t.Fatalf(errorPrefix+"Did not expect to find Last-Modified header, but found one [%s].", actualLastModifiedHeader)
|
||||
}
|
||||
|
||||
if test.expectedLastModified != actualLastModifiedHeader {
|
||||
t.Errorf(errorPrefix+"Expected Last-Modified content [%s], found [%s}", test.expectedLastModified, actualLastModifiedHeader)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user