2017-02-18 00:25:22 +08:00
|
|
|
package push
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/mholt/caddy/caddyhttp/httpserver"
|
|
|
|
)
|
|
|
|
|
|
|
|
type MockedPusher struct {
|
|
|
|
http.ResponseWriter
|
|
|
|
pushed map[string]*http.PushOptions
|
|
|
|
returnedError error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *MockedPusher) Push(target string, options *http.PushOptions) error {
|
|
|
|
if w.pushed == nil {
|
|
|
|
w.pushed = make(map[string]*http.PushOptions)
|
|
|
|
}
|
|
|
|
|
|
|
|
w.pushed[target] = options
|
|
|
|
return w.returnedError
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMiddlewareWillPushResources(t *testing.T) {
|
|
|
|
|
|
|
|
// given
|
|
|
|
request, err := http.NewRequest(http.MethodGet, "/index.html", nil)
|
|
|
|
writer := httptest.NewRecorder()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not create HTTP request: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
middleware := Middleware{
|
|
|
|
Next: httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) {
|
|
|
|
return 0, nil
|
|
|
|
}),
|
|
|
|
Rules: []Rule{
|
|
|
|
{Path: "/index.html", Resources: []Resource{
|
|
|
|
{Path: "/index.css", Method: http.MethodHead, Header: http.Header{"Test": []string{"Value"}}},
|
|
|
|
{Path: "/index2.css", Method: http.MethodGet},
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
pushingWriter := &MockedPusher{ResponseWriter: writer}
|
|
|
|
|
|
|
|
// when
|
|
|
|
middleware.ServeHTTP(pushingWriter, request)
|
|
|
|
|
|
|
|
// then
|
|
|
|
expectedPushedResources := map[string]*http.PushOptions{
|
|
|
|
"/index.css": {
|
|
|
|
Method: http.MethodHead,
|
|
|
|
Header: http.Header{"Test": []string{"Value"}},
|
|
|
|
},
|
|
|
|
|
|
|
|
"/index2.css": {
|
|
|
|
Method: http.MethodGet,
|
2017-02-19 06:50:36 +08:00
|
|
|
Header: http.Header{},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
comparePushedResources(t, expectedPushedResources, pushingWriter.pushed)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMiddlewareWillPushResourcesWithMergedHeaders(t *testing.T) {
|
|
|
|
|
|
|
|
// given
|
|
|
|
request, err := http.NewRequest(http.MethodGet, "/index.html", nil)
|
|
|
|
request.Header = http.Header{"Accept-Encoding": []string{"br"}, "Invalid-Header": []string{"Should be filter out"}}
|
|
|
|
writer := httptest.NewRecorder()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not create HTTP request: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
middleware := Middleware{
|
|
|
|
Next: httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) {
|
|
|
|
return 0, nil
|
|
|
|
}),
|
|
|
|
Rules: []Rule{
|
|
|
|
{Path: "/index.html", Resources: []Resource{
|
|
|
|
{Path: "/index.css", Method: http.MethodHead, Header: http.Header{"Test": []string{"Value"}}},
|
|
|
|
{Path: "/index2.css", Method: http.MethodGet},
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
pushingWriter := &MockedPusher{ResponseWriter: writer}
|
|
|
|
|
|
|
|
// when
|
|
|
|
middleware.ServeHTTP(pushingWriter, request)
|
|
|
|
|
|
|
|
// then
|
|
|
|
expectedPushedResources := map[string]*http.PushOptions{
|
|
|
|
"/index.css": {
|
|
|
|
Method: http.MethodHead,
|
|
|
|
Header: http.Header{"Test": []string{"Value"}, "Accept-Encoding": []string{"br"}},
|
|
|
|
},
|
|
|
|
|
|
|
|
"/index2.css": {
|
|
|
|
Method: http.MethodGet,
|
|
|
|
Header: http.Header{"Accept-Encoding": []string{"br"}},
|
2017-02-18 00:25:22 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
comparePushedResources(t, expectedPushedResources, pushingWriter.pushed)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMiddlewareShouldntDoRecursivePush(t *testing.T) {
|
|
|
|
|
|
|
|
// given
|
|
|
|
request, err := http.NewRequest(http.MethodGet, "/index.css", nil)
|
|
|
|
request.Header.Add(pushHeader, "")
|
|
|
|
|
|
|
|
writer := httptest.NewRecorder()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not create HTTP request: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
middleware := Middleware{
|
|
|
|
Next: httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) {
|
|
|
|
return 0, nil
|
|
|
|
}),
|
|
|
|
Rules: []Rule{
|
|
|
|
{Path: "/", Resources: []Resource{
|
|
|
|
{Path: "/index.css", Method: http.MethodHead, Header: http.Header{"Test": []string{"Value"}}},
|
|
|
|
{Path: "/index2.css", Method: http.MethodGet},
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
pushingWriter := &MockedPusher{ResponseWriter: writer}
|
|
|
|
|
|
|
|
// when
|
|
|
|
middleware.ServeHTTP(pushingWriter, request)
|
|
|
|
|
|
|
|
// then
|
|
|
|
if len(pushingWriter.pushed) > 0 {
|
|
|
|
t.Errorf("Expected 0 pushed resources, actual %d", len(pushingWriter.pushed))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMiddlewareShouldStopPushingOnError(t *testing.T) {
|
|
|
|
|
|
|
|
// given
|
|
|
|
request, err := http.NewRequest(http.MethodGet, "/index.html", nil)
|
|
|
|
writer := httptest.NewRecorder()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not create HTTP request: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
middleware := Middleware{
|
|
|
|
Next: httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) {
|
|
|
|
return 0, nil
|
|
|
|
}),
|
|
|
|
Rules: []Rule{
|
|
|
|
{Path: "/index.html", Resources: []Resource{
|
|
|
|
{Path: "/only.css", Method: http.MethodHead, Header: http.Header{"Test": []string{"Value"}}},
|
|
|
|
{Path: "/index2.css", Method: http.MethodGet},
|
|
|
|
{Path: "/index3.css", Method: http.MethodGet},
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
pushingWriter := &MockedPusher{ResponseWriter: writer, returnedError: errors.New("Cannot push right now")}
|
|
|
|
|
|
|
|
// when
|
|
|
|
middleware.ServeHTTP(pushingWriter, request)
|
|
|
|
|
|
|
|
// then
|
|
|
|
expectedPushedResources := map[string]*http.PushOptions{
|
|
|
|
"/only.css": {
|
|
|
|
Method: http.MethodHead,
|
|
|
|
Header: http.Header{"Test": []string{"Value"}},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
comparePushedResources(t, expectedPushedResources, pushingWriter.pushed)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMiddlewareWillNotPushResources(t *testing.T) {
|
|
|
|
// given
|
|
|
|
request, err := http.NewRequest(http.MethodGet, "/index.html", nil)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not create HTTP request: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
middleware := Middleware{
|
|
|
|
Next: httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) {
|
|
|
|
return 0, nil
|
|
|
|
}),
|
|
|
|
Rules: []Rule{
|
|
|
|
{Path: "/index.html", Resources: []Resource{
|
|
|
|
{Path: "/index.css", Method: http.MethodHead, Header: http.Header{"Test": []string{"Value"}}},
|
|
|
|
{Path: "/index2.css", Method: http.MethodGet},
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
writer := httptest.NewRecorder()
|
|
|
|
|
|
|
|
// when
|
|
|
|
_, err2 := middleware.ServeHTTP(writer, request)
|
|
|
|
|
|
|
|
// then
|
|
|
|
if err2 != nil {
|
2017-02-19 06:50:36 +08:00
|
|
|
t.Error("Should not return error")
|
2017-02-18 00:25:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMiddlewareShouldInterceptLinkHeader(t *testing.T) {
|
|
|
|
// given
|
|
|
|
request, err := http.NewRequest(http.MethodGet, "/index.html", nil)
|
|
|
|
writer := httptest.NewRecorder()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not create HTTP request: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
middleware := Middleware{
|
|
|
|
Next: httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) {
|
|
|
|
w.Header().Add("Link", "</index.css>; rel=preload; as=stylesheet;")
|
|
|
|
w.Header().Add("Link", "</index2.css>; rel=preload; as=stylesheet;")
|
|
|
|
w.Header().Add("Link", "")
|
|
|
|
w.Header().Add("Link", "</index3.css>")
|
|
|
|
w.Header().Add("Link", "</index4.css>; rel=preload; nopush")
|
|
|
|
return 0, nil
|
|
|
|
}),
|
|
|
|
Rules: []Rule{},
|
|
|
|
}
|
|
|
|
|
|
|
|
pushingWriter := &MockedPusher{ResponseWriter: writer}
|
|
|
|
|
|
|
|
// when
|
|
|
|
_, err2 := middleware.ServeHTTP(pushingWriter, request)
|
|
|
|
|
|
|
|
// then
|
|
|
|
if err2 != nil {
|
2017-02-19 06:50:36 +08:00
|
|
|
t.Error("Should not return error")
|
2017-02-18 00:25:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
expectedPushedResources := map[string]*http.PushOptions{
|
|
|
|
"/index.css": {
|
|
|
|
Method: http.MethodGet,
|
2017-02-19 06:50:36 +08:00
|
|
|
Header: http.Header{},
|
2017-02-18 00:25:22 +08:00
|
|
|
},
|
|
|
|
"/index2.css": {
|
|
|
|
Method: http.MethodGet,
|
2017-02-19 06:50:36 +08:00
|
|
|
Header: http.Header{},
|
2017-02-18 00:25:22 +08:00
|
|
|
},
|
|
|
|
"/index3.css": {
|
|
|
|
Method: http.MethodGet,
|
2017-02-19 06:50:36 +08:00
|
|
|
Header: http.Header{},
|
2017-02-18 00:25:22 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
comparePushedResources(t, expectedPushedResources, pushingWriter.pushed)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMiddlewareShouldInterceptLinkHeaderPusherError(t *testing.T) {
|
|
|
|
// given
|
2017-02-19 06:50:36 +08:00
|
|
|
expectedHeaders := http.Header{"Accept-Encoding": []string{"br"}}
|
2017-02-18 00:25:22 +08:00
|
|
|
request, err := http.NewRequest(http.MethodGet, "/index.html", nil)
|
2017-02-19 06:50:36 +08:00
|
|
|
request.Header = http.Header{"Accept-Encoding": []string{"br"}, "Invalid-Header": []string{"Should be filter out"}}
|
|
|
|
|
2017-02-18 00:25:22 +08:00
|
|
|
writer := httptest.NewRecorder()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not create HTTP request: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
middleware := Middleware{
|
|
|
|
Next: httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) {
|
|
|
|
w.Header().Add("Link", "</index.css>; rel=preload; as=stylesheet;")
|
|
|
|
w.Header().Add("Link", "</index2.css>; rel=preload; as=stylesheet;")
|
|
|
|
return 0, nil
|
|
|
|
}),
|
|
|
|
Rules: []Rule{},
|
|
|
|
}
|
|
|
|
|
|
|
|
pushingWriter := &MockedPusher{ResponseWriter: writer, returnedError: errors.New("Cannot push right now")}
|
|
|
|
|
|
|
|
// when
|
|
|
|
_, err2 := middleware.ServeHTTP(pushingWriter, request)
|
|
|
|
|
|
|
|
// then
|
|
|
|
if err2 != nil {
|
2017-02-19 06:50:36 +08:00
|
|
|
t.Error("Should not return error")
|
2017-02-18 00:25:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
expectedPushedResources := map[string]*http.PushOptions{
|
|
|
|
"/index.css": {
|
|
|
|
Method: http.MethodGet,
|
2017-02-19 06:50:36 +08:00
|
|
|
Header: expectedHeaders,
|
2017-02-18 00:25:22 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
comparePushedResources(t, expectedPushedResources, pushingWriter.pushed)
|
|
|
|
}
|
|
|
|
|
|
|
|
func comparePushedResources(t *testing.T, expected, actual map[string]*http.PushOptions) {
|
|
|
|
if len(expected) != len(actual) {
|
|
|
|
t.Errorf("Expected %d pushed resources, actual: %d", len(expected), len(actual))
|
|
|
|
}
|
|
|
|
|
|
|
|
for target, expectedTarget := range expected {
|
|
|
|
if actualTarget, exists := actual[target]; exists {
|
|
|
|
|
|
|
|
if expectedTarget.Method != actualTarget.Method {
|
|
|
|
t.Errorf("Expected %s resource method to be %s, actual: %s", target, expectedTarget.Method, actualTarget.Method)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(expectedTarget.Header, actualTarget.Header) {
|
2017-02-19 06:50:36 +08:00
|
|
|
t.Errorf("Expected %s resource push headers to be %+v, actual: %+v", target, expectedTarget.Header, actualTarget.Header)
|
2017-02-18 00:25:22 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
t.Errorf("Expected %s to be pushed", target)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|