mirror of
https://github.com/cloudflare/cloudflared.git
synced 2025-07-29 10:09:58 +00:00
TUN-528: Move cloudflared into a separate repo
This commit is contained in:
144
vendor/github.com/mholt/caddy/caddyhttp/push/handler.go
generated
vendored
Normal file
144
vendor/github.com/mholt/caddy/caddyhttp/push/handler.go
generated
vendored
Normal file
@@ -0,0 +1,144 @@
|
||||
// Copyright 2015 Light Code Labs, LLC
|
||||
//
|
||||
// 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 push
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/mholt/caddy/caddyhttp/httpserver"
|
||||
)
|
||||
|
||||
func (h Middleware) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
|
||||
pusher, hasPusher := w.(http.Pusher)
|
||||
|
||||
// no push possible, carry on
|
||||
if !hasPusher {
|
||||
return h.Next.ServeHTTP(w, r)
|
||||
}
|
||||
|
||||
// check if this is a request for the pushed resource (avoid recursion)
|
||||
if _, exists := r.Header[pushHeader]; exists {
|
||||
return h.Next.ServeHTTP(w, r)
|
||||
}
|
||||
|
||||
headers := h.filterProxiedHeaders(r.Header)
|
||||
|
||||
// push first
|
||||
outer:
|
||||
for _, rule := range h.Rules {
|
||||
urlPath := r.URL.Path
|
||||
matches := httpserver.Path(urlPath).Matches(rule.Path)
|
||||
// Also check IndexPages when requesting a directory
|
||||
if !matches {
|
||||
indexFile, isIndexFile := httpserver.IndexFile(h.Root, urlPath, h.indexPages)
|
||||
if isIndexFile {
|
||||
matches = httpserver.Path(indexFile).Matches(rule.Path)
|
||||
}
|
||||
}
|
||||
if matches {
|
||||
for _, resource := range rule.Resources {
|
||||
pushErr := pusher.Push(resource.Path, &http.PushOptions{
|
||||
Method: resource.Method,
|
||||
Header: h.mergeHeaders(headers, resource.Header),
|
||||
})
|
||||
if pushErr != nil {
|
||||
// if we cannot push (either not supported or concurrent streams are full - break)
|
||||
break outer
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// serve later
|
||||
code, err := h.Next.ServeHTTP(w, r)
|
||||
|
||||
// push resources returned in Link headers from upstream middlewares or proxied apps
|
||||
if links, exists := w.Header()["Link"]; exists {
|
||||
h.servePreloadLinks(pusher, headers, links)
|
||||
}
|
||||
|
||||
return code, err
|
||||
}
|
||||
|
||||
// servePreloadLinks parses Link headers from backend and pushes resources found in them.
|
||||
// For accepted header formats check parseLinkHeader function.
|
||||
//
|
||||
// If resource has 'nopush' attribute then it will be omitted.
|
||||
func (h Middleware) servePreloadLinks(pusher http.Pusher, headers http.Header, resources []string) {
|
||||
outer:
|
||||
for _, resource := range resources {
|
||||
for _, resource := range parseLinkHeader(resource) {
|
||||
if _, exists := resource.params["nopush"]; exists {
|
||||
continue
|
||||
}
|
||||
|
||||
if h.isRemoteResource(resource.uri) {
|
||||
continue
|
||||
}
|
||||
|
||||
err := pusher.Push(resource.uri, &http.PushOptions{
|
||||
Method: http.MethodGet,
|
||||
Header: headers,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
break outer
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (h Middleware) isRemoteResource(resource string) bool {
|
||||
return strings.HasPrefix(resource, "//") ||
|
||||
strings.HasPrefix(resource, "http://") ||
|
||||
strings.HasPrefix(resource, "https://")
|
||||
}
|
||||
|
||||
func (h Middleware) mergeHeaders(l, r http.Header) http.Header {
|
||||
out := http.Header{}
|
||||
|
||||
for k, v := range l {
|
||||
out[k] = v
|
||||
}
|
||||
|
||||
for k, vv := range r {
|
||||
for _, v := range vv {
|
||||
out.Add(k, v)
|
||||
}
|
||||
}
|
||||
|
||||
return out
|
||||
}
|
||||
|
||||
func (h Middleware) filterProxiedHeaders(headers http.Header) http.Header {
|
||||
filter := http.Header{}
|
||||
|
||||
for _, header := range proxiedHeaders {
|
||||
if val, ok := headers[header]; ok {
|
||||
filter[header] = val
|
||||
}
|
||||
}
|
||||
|
||||
return filter
|
||||
}
|
||||
|
||||
var proxiedHeaders = []string{
|
||||
"Accept-Encoding",
|
||||
"Accept-Language",
|
||||
"Cache-Control",
|
||||
"Host",
|
||||
"User-Agent",
|
||||
}
|
499
vendor/github.com/mholt/caddy/caddyhttp/push/handler_test.go
generated
vendored
Normal file
499
vendor/github.com/mholt/caddy/caddyhttp/push/handler_test.go
generated
vendored
Normal file
@@ -0,0 +1,499 @@
|
||||
// Copyright 2015 Light Code Labs, LLC
|
||||
//
|
||||
// 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 push
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"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,
|
||||
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"}},
|
||||
},
|
||||
}
|
||||
|
||||
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 {
|
||||
t.Error("Should not return error")
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
t.Error("Should not return error")
|
||||
}
|
||||
|
||||
expectedPushedResources := map[string]*http.PushOptions{
|
||||
"/index.css": {
|
||||
Method: http.MethodGet,
|
||||
Header: http.Header{},
|
||||
},
|
||||
"/index2.css": {
|
||||
Method: http.MethodGet,
|
||||
Header: http.Header{},
|
||||
},
|
||||
"/index3.css": {
|
||||
Method: http.MethodGet,
|
||||
Header: http.Header{},
|
||||
},
|
||||
}
|
||||
|
||||
comparePushedResources(t, expectedPushedResources, pushingWriter.pushed)
|
||||
}
|
||||
|
||||
func TestMiddlewareShouldInterceptLinkHeaderWithMultipleResources(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", "</assets/css/screen.css?v=5fc240c512>; rel=preload; as=style,</content/images/2016/06/Timeouts-001.png>; rel=preload; as=image,</content/images/2016/06/Timeouts-002.png>; rel=preload; as=image")
|
||||
w.Header().Add("Link", "<//cdn.bizible.com/scripts/bizible.js>; rel=preload; as=script,</resource.png>; rel=preload; as=script; nopush")
|
||||
return 0, nil
|
||||
}),
|
||||
Rules: []Rule{},
|
||||
}
|
||||
|
||||
pushingWriter := &MockedPusher{ResponseWriter: writer}
|
||||
|
||||
// when
|
||||
_, err2 := middleware.ServeHTTP(pushingWriter, request)
|
||||
|
||||
// then
|
||||
if err2 != nil {
|
||||
t.Error("Should not return error")
|
||||
}
|
||||
|
||||
expectedPushedResources := map[string]*http.PushOptions{
|
||||
"/assets/css/screen.css?v=5fc240c512": {
|
||||
Method: http.MethodGet,
|
||||
Header: http.Header{},
|
||||
},
|
||||
"/content/images/2016/06/Timeouts-001.png": {
|
||||
Method: http.MethodGet,
|
||||
Header: http.Header{},
|
||||
},
|
||||
"/content/images/2016/06/Timeouts-002.png": {
|
||||
Method: http.MethodGet,
|
||||
Header: http.Header{},
|
||||
},
|
||||
}
|
||||
|
||||
comparePushedResources(t, expectedPushedResources, pushingWriter.pushed)
|
||||
}
|
||||
|
||||
func TestMiddlewareShouldInterceptLinkHeaderPusherError(t *testing.T) {
|
||||
// given
|
||||
expectedHeaders := http.Header{"Accept-Encoding": []string{"br"}}
|
||||
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) {
|
||||
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 {
|
||||
t.Error("Should not return error")
|
||||
}
|
||||
|
||||
expectedPushedResources := map[string]*http.PushOptions{
|
||||
"/index.css": {
|
||||
Method: http.MethodGet,
|
||||
Header: expectedHeaders,
|
||||
},
|
||||
}
|
||||
|
||||
comparePushedResources(t, expectedPushedResources, pushingWriter.pushed)
|
||||
}
|
||||
|
||||
func TestMiddlewareShouldPushIndexFile(t *testing.T) {
|
||||
// given
|
||||
indexFile := "/index.html"
|
||||
request, err := http.NewRequest(http.MethodGet, "/", nil) // Request root directory, not indexfile itself
|
||||
if err != nil {
|
||||
t.Fatalf("Could not create HTTP request: %v", err)
|
||||
}
|
||||
|
||||
root, err := ioutil.TempDir("", "caddy")
|
||||
if err != nil {
|
||||
t.Fatalf("Could not create temporary directory: %v", err)
|
||||
}
|
||||
defer os.Remove(root)
|
||||
|
||||
middleware := Middleware{
|
||||
Next: httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) {
|
||||
return 0, nil
|
||||
}),
|
||||
Rules: []Rule{
|
||||
{Path: indexFile, Resources: []Resource{
|
||||
{Path: "/index.css", Method: http.MethodGet},
|
||||
}},
|
||||
},
|
||||
Root: http.Dir(root),
|
||||
indexPages: []string{indexFile},
|
||||
}
|
||||
|
||||
indexFilePath := filepath.Join(root, indexFile)
|
||||
_, err = os.Create(indexFilePath)
|
||||
if err != nil {
|
||||
t.Fatalf("Could not create index file: %s: %v", indexFile, err)
|
||||
}
|
||||
defer os.Remove(indexFilePath)
|
||||
|
||||
pushingWriter := &MockedPusher{
|
||||
ResponseWriter: httptest.NewRecorder(),
|
||||
returnedError: errors.New("Cannot push right now"),
|
||||
}
|
||||
|
||||
// when
|
||||
_, err2 := middleware.ServeHTTP(pushingWriter, request)
|
||||
|
||||
// then
|
||||
if err2 != nil {
|
||||
t.Error("Should not return error")
|
||||
}
|
||||
|
||||
expectedPushedResources := map[string]*http.PushOptions{
|
||||
"/index.css": {
|
||||
Method: http.MethodGet,
|
||||
Header: http.Header{},
|
||||
},
|
||||
}
|
||||
|
||||
comparePushedResources(t, expectedPushedResources, pushingWriter.pushed)
|
||||
}
|
||||
|
||||
func TestMiddlewareShouldNotPushIndexFileWhenNotARule(t *testing.T) {
|
||||
// given
|
||||
indexFile := "/index.html"
|
||||
request, err := http.NewRequest(http.MethodGet, "/", nil) // Request root directory, not indexfile itself
|
||||
if err != nil {
|
||||
t.Fatalf("Could not create HTTP request: %v", err)
|
||||
}
|
||||
|
||||
root, err := ioutil.TempDir("", "caddy")
|
||||
if err != nil {
|
||||
t.Fatalf("Could not create temporary directory: %v", err)
|
||||
}
|
||||
defer os.Remove(root)
|
||||
|
||||
middleware := Middleware{
|
||||
Next: httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) {
|
||||
return 0, nil
|
||||
}),
|
||||
Rules: []Rule{
|
||||
{Path: "dummy.html", Resources: []Resource{
|
||||
{Path: "/index.css", Method: http.MethodGet},
|
||||
}}},
|
||||
Root: http.Dir(root),
|
||||
}
|
||||
|
||||
indexFilePath := filepath.Join(root, indexFile)
|
||||
_, err = os.Create(indexFilePath)
|
||||
if err != nil {
|
||||
t.Fatalf("Could not create index file: %s: %v", indexFile, err)
|
||||
}
|
||||
defer os.Remove(indexFilePath)
|
||||
|
||||
pushingWriter := &MockedPusher{
|
||||
ResponseWriter: httptest.NewRecorder(),
|
||||
returnedError: errors.New("Cannot push right now"),
|
||||
}
|
||||
|
||||
// when
|
||||
_, err2 := middleware.ServeHTTP(pushingWriter, request)
|
||||
|
||||
// then
|
||||
if err2 != nil {
|
||||
t.Error("Should not return error")
|
||||
}
|
||||
|
||||
expectedPushedResources := map[string]*http.PushOptions{}
|
||||
|
||||
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) {
|
||||
t.Errorf("Expected %s resource push headers to be %+v, actual: %+v", target, expectedTarget.Header, actualTarget.Header)
|
||||
}
|
||||
} else {
|
||||
t.Errorf("Expected %s to be pushed", target)
|
||||
}
|
||||
}
|
||||
}
|
77
vendor/github.com/mholt/caddy/caddyhttp/push/link_parser.go
generated
vendored
Normal file
77
vendor/github.com/mholt/caddy/caddyhttp/push/link_parser.go
generated
vendored
Normal file
@@ -0,0 +1,77 @@
|
||||
// Copyright 2015 Light Code Labs, LLC
|
||||
//
|
||||
// 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 push
|
||||
|
||||
import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
commaSeparator = ","
|
||||
semicolonSeparator = ";"
|
||||
equalSeparator = "="
|
||||
)
|
||||
|
||||
type linkResource struct {
|
||||
uri string
|
||||
params map[string]string
|
||||
}
|
||||
|
||||
// parseLinkHeader is responsible for parsing Link header and returning list of found resources.
|
||||
//
|
||||
// Accepted formats are:
|
||||
// Link: </resource>; as=script
|
||||
// Link: </resource>; as=script,</resource2>; as=style
|
||||
// Link: </resource>;</resource2>
|
||||
func parseLinkHeader(header string) []linkResource {
|
||||
resources := []linkResource{}
|
||||
|
||||
if header == "" {
|
||||
return resources
|
||||
}
|
||||
|
||||
for _, link := range strings.Split(header, commaSeparator) {
|
||||
l := linkResource{params: make(map[string]string)}
|
||||
|
||||
li, ri := strings.Index(link, "<"), strings.Index(link, ">")
|
||||
|
||||
if li == -1 || ri == -1 {
|
||||
continue
|
||||
}
|
||||
|
||||
l.uri = strings.TrimSpace(link[li+1 : ri])
|
||||
|
||||
for _, param := range strings.Split(strings.TrimSpace(link[ri+1:]), semicolonSeparator) {
|
||||
parts := strings.SplitN(strings.TrimSpace(param), equalSeparator, 2)
|
||||
key := strings.TrimSpace(parts[0])
|
||||
|
||||
if key == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
if len(parts) == 1 {
|
||||
l.params[key] = key
|
||||
}
|
||||
|
||||
if len(parts) == 2 {
|
||||
l.params[key] = strings.TrimSpace(parts[1])
|
||||
}
|
||||
}
|
||||
|
||||
resources = append(resources, l)
|
||||
}
|
||||
|
||||
return resources
|
||||
}
|
87
vendor/github.com/mholt/caddy/caddyhttp/push/link_parser_test.go
generated
vendored
Normal file
87
vendor/github.com/mholt/caddy/caddyhttp/push/link_parser_test.go
generated
vendored
Normal file
@@ -0,0 +1,87 @@
|
||||
// Copyright 2015 Light Code Labs, LLC
|
||||
//
|
||||
// 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 push
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestDifferentParserInputs(t *testing.T) {
|
||||
testCases := []struct {
|
||||
header string
|
||||
expectedResources []linkResource
|
||||
}{
|
||||
{
|
||||
header: "</resource>; as=script",
|
||||
expectedResources: []linkResource{{uri: "/resource", params: map[string]string{"as": "script"}}},
|
||||
},
|
||||
{
|
||||
header: "</resource>",
|
||||
expectedResources: []linkResource{{uri: "/resource", params: map[string]string{}}},
|
||||
},
|
||||
{
|
||||
header: "</resource>; nopush",
|
||||
expectedResources: []linkResource{{uri: "/resource", params: map[string]string{"nopush": "nopush"}}},
|
||||
},
|
||||
{
|
||||
header: "</resource>;nopush;rel=next",
|
||||
expectedResources: []linkResource{{uri: "/resource", params: map[string]string{"nopush": "nopush", "rel": "next"}}},
|
||||
},
|
||||
{
|
||||
header: "</resource>;nopush;rel=next,</resource2>;nopush",
|
||||
expectedResources: []linkResource{
|
||||
{uri: "/resource", params: map[string]string{"nopush": "nopush", "rel": "next"}},
|
||||
{uri: "/resource2", params: map[string]string{"nopush": "nopush"}},
|
||||
},
|
||||
},
|
||||
{
|
||||
header: "</resource>,</resource2>",
|
||||
expectedResources: []linkResource{
|
||||
{uri: "/resource", params: map[string]string{}},
|
||||
{uri: "/resource2", params: map[string]string{}},
|
||||
},
|
||||
},
|
||||
{
|
||||
header: "malformed",
|
||||
expectedResources: []linkResource{},
|
||||
},
|
||||
{
|
||||
header: "<malformed",
|
||||
expectedResources: []linkResource{},
|
||||
},
|
||||
{
|
||||
header: ",",
|
||||
expectedResources: []linkResource{},
|
||||
},
|
||||
{
|
||||
header: ";",
|
||||
expectedResources: []linkResource{},
|
||||
},
|
||||
{
|
||||
header: "</resource> ; ",
|
||||
expectedResources: []linkResource{{uri: "/resource", params: map[string]string{}}},
|
||||
},
|
||||
}
|
||||
|
||||
for i, test := range testCases {
|
||||
|
||||
actualResources := parseLinkHeader(test.header)
|
||||
|
||||
if !reflect.DeepEqual(actualResources, test.expectedResources) {
|
||||
t.Errorf("Test %d (header: %s) - expected resources %v, got %v", i, test.header, test.expectedResources, actualResources)
|
||||
}
|
||||
}
|
||||
}
|
46
vendor/github.com/mholt/caddy/caddyhttp/push/push.go
generated
vendored
Normal file
46
vendor/github.com/mholt/caddy/caddyhttp/push/push.go
generated
vendored
Normal file
@@ -0,0 +1,46 @@
|
||||
// Copyright 2015 Light Code Labs, LLC
|
||||
//
|
||||
// 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 push
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/mholt/caddy/caddyhttp/httpserver"
|
||||
)
|
||||
|
||||
type (
|
||||
// Rule describes conditions on which resources will be pushed
|
||||
Rule struct {
|
||||
Path string
|
||||
Resources []Resource
|
||||
}
|
||||
|
||||
// Resource describes resource to be pushed
|
||||
Resource struct {
|
||||
Path string
|
||||
Method string
|
||||
Header http.Header
|
||||
}
|
||||
|
||||
// Middleware supports pushing resources to clients
|
||||
Middleware struct {
|
||||
Next httpserver.Handler
|
||||
Rules []Rule
|
||||
Root http.FileSystem
|
||||
indexPages []string // will be injected from SiteConfig on setup
|
||||
}
|
||||
|
||||
ruleOp func([]Resource)
|
||||
)
|
193
vendor/github.com/mholt/caddy/caddyhttp/push/setup.go
generated
vendored
Normal file
193
vendor/github.com/mholt/caddy/caddyhttp/push/setup.go
generated
vendored
Normal file
@@ -0,0 +1,193 @@
|
||||
// Copyright 2015 Light Code Labs, LLC
|
||||
//
|
||||
// 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 push
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/mholt/caddy"
|
||||
"github.com/mholt/caddy/caddyhttp/httpserver"
|
||||
)
|
||||
|
||||
func init() {
|
||||
caddy.RegisterPlugin("push", caddy.Plugin{
|
||||
ServerType: "http",
|
||||
Action: setup,
|
||||
})
|
||||
}
|
||||
|
||||
var errInvalidHeader = errors.New("header directive requires [name] [value]")
|
||||
|
||||
var errHeaderStartsWithColon = errors.New("header cannot start with colon")
|
||||
var errMethodNotSupported = errors.New("push supports only GET and HEAD methods")
|
||||
|
||||
const pushHeader = "X-Push"
|
||||
|
||||
var emptyRules = []Rule{}
|
||||
|
||||
// setup configures a new Push middleware
|
||||
func setup(c *caddy.Controller) error {
|
||||
rules, err := parsePushRules(c)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
cfg := httpserver.GetConfig(c)
|
||||
cfg.AddMiddleware(func(next httpserver.Handler) httpserver.Handler {
|
||||
return Middleware{Next: next, Rules: rules, Root: http.Dir(cfg.Root), indexPages: cfg.IndexPages}
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func parsePushRules(c *caddy.Controller) ([]Rule, error) {
|
||||
var rules = make(map[string]*Rule)
|
||||
|
||||
for c.NextLine() {
|
||||
var rule *Rule
|
||||
var resources []Resource
|
||||
var ops []ruleOp
|
||||
|
||||
parseBlock := func() error {
|
||||
for c.NextBlock() {
|
||||
val := c.Val()
|
||||
|
||||
switch val {
|
||||
case "method":
|
||||
if !c.NextArg() {
|
||||
return c.ArgErr()
|
||||
}
|
||||
|
||||
method := c.Val()
|
||||
|
||||
if err := validateMethod(method); err != nil {
|
||||
return errMethodNotSupported
|
||||
}
|
||||
|
||||
ops = append(ops, setMethodOp(method))
|
||||
|
||||
case "header":
|
||||
args := c.RemainingArgs()
|
||||
|
||||
if len(args) != 2 {
|
||||
return errInvalidHeader
|
||||
}
|
||||
|
||||
if err := validateHeader(args[0]); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ops = append(ops, setHeaderOp(args[0], args[1]))
|
||||
default:
|
||||
resources = append(resources, Resource{
|
||||
Path: val,
|
||||
Method: http.MethodGet,
|
||||
Header: http.Header{pushHeader: []string{}},
|
||||
})
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
args := c.RemainingArgs()
|
||||
|
||||
if len(args) == 0 {
|
||||
rule = new(Rule)
|
||||
rule.Path = "/"
|
||||
rules["/"] = rule
|
||||
err := parseBlock()
|
||||
if err != nil {
|
||||
return emptyRules, err
|
||||
}
|
||||
} else {
|
||||
path := args[0]
|
||||
|
||||
if existingRule, ok := rules[path]; ok {
|
||||
rule = existingRule
|
||||
} else {
|
||||
rule = new(Rule)
|
||||
rule.Path = path
|
||||
rules[rule.Path] = rule
|
||||
}
|
||||
|
||||
for i := 1; i < len(args); i++ {
|
||||
resources = append(resources, Resource{
|
||||
Path: args[i],
|
||||
Method: http.MethodGet,
|
||||
Header: http.Header{pushHeader: []string{}},
|
||||
})
|
||||
}
|
||||
|
||||
err := parseBlock()
|
||||
if err != nil {
|
||||
return emptyRules, err
|
||||
}
|
||||
}
|
||||
|
||||
for _, op := range ops {
|
||||
op(resources)
|
||||
}
|
||||
rule.Resources = append(rule.Resources, resources...)
|
||||
}
|
||||
|
||||
var returnRules []Rule
|
||||
for _, rule := range rules {
|
||||
returnRules = append(returnRules, *rule)
|
||||
}
|
||||
|
||||
return returnRules, nil
|
||||
}
|
||||
|
||||
func setHeaderOp(key, value string) func(resources []Resource) {
|
||||
return func(resources []Resource) {
|
||||
for index := range resources {
|
||||
resources[index].Header.Set(key, value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func setMethodOp(method string) func(resources []Resource) {
|
||||
return func(resources []Resource) {
|
||||
for index := range resources {
|
||||
resources[index].Method = method
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func validateHeader(header string) error {
|
||||
if strings.HasPrefix(header, ":") {
|
||||
return errHeaderStartsWithColon
|
||||
}
|
||||
|
||||
switch strings.ToLower(header) {
|
||||
case "content-length", "content-encoding", "trailer", "te", "expect", "host":
|
||||
return fmt.Errorf("push headers cannot include %s", header)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// rules based on https://go-review.googlesource.com/#/c/29439/4/http2/go18.go#94
|
||||
func validateMethod(method string) error {
|
||||
if method != http.MethodGet && method != http.MethodHead {
|
||||
return errMethodNotSupported
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
281
vendor/github.com/mholt/caddy/caddyhttp/push/setup_test.go
generated
vendored
Normal file
281
vendor/github.com/mholt/caddy/caddyhttp/push/setup_test.go
generated
vendored
Normal file
@@ -0,0 +1,281 @@
|
||||
// Copyright 2015 Light Code Labs, LLC
|
||||
//
|
||||
// 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 push
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/mholt/caddy"
|
||||
"github.com/mholt/caddy/caddyhttp/httpserver"
|
||||
)
|
||||
|
||||
func TestPushAvailable(t *testing.T) {
|
||||
err := setup(caddy.NewTestController("http", "push /index.html /available.css"))
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("Error %s occurred, expected none", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigParse(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
shouldErr bool
|
||||
expected []Rule
|
||||
}{
|
||||
{
|
||||
"ParseInvalidEmptyConfig", `push`, false, []Rule{{Path: "/"}},
|
||||
},
|
||||
{
|
||||
"ParseInvalidConfig", `push /index.html`, false, []Rule{{Path: "/index.html"}},
|
||||
},
|
||||
{
|
||||
"ParseInvalidConfigBlock", `push /index.html /index.css {
|
||||
method
|
||||
}`, true, []Rule{},
|
||||
},
|
||||
{
|
||||
"ParseInvalidHeaderFormat", `push /index.html /index.css {
|
||||
header :invalid value
|
||||
}`, true, []Rule{},
|
||||
},
|
||||
{
|
||||
"ParseForbiddenHeader", `push /index.html /index.css {
|
||||
header Content-Length 1000
|
||||
}`, true, []Rule{},
|
||||
},
|
||||
{
|
||||
"ParseInvalidMethod", `push /index.html /index.css {
|
||||
method POST
|
||||
}`, true, []Rule{},
|
||||
},
|
||||
{
|
||||
"ParseInvalidHeaderBlock", `push /index.html /index.css {
|
||||
header
|
||||
}`, true, []Rule{},
|
||||
},
|
||||
{
|
||||
"ParseInvalidHeaderBlock2", `push /index.html /index.css {
|
||||
header name
|
||||
}`, true, []Rule{},
|
||||
},
|
||||
{
|
||||
"ParseProperConfig", `push /index.html /style.css /style2.css`, false, []Rule{
|
||||
{
|
||||
Path: "/index.html",
|
||||
Resources: []Resource{
|
||||
{
|
||||
Path: "/style.css",
|
||||
Method: http.MethodGet,
|
||||
Header: http.Header{pushHeader: []string{}},
|
||||
},
|
||||
{
|
||||
Path: "/style2.css",
|
||||
Method: http.MethodGet,
|
||||
Header: http.Header{pushHeader: []string{}},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
"ParseSimpleInlinePush", `push /index.html {
|
||||
/style.css
|
||||
/style2.css
|
||||
}`, false, []Rule{
|
||||
{
|
||||
Path: "/index.html",
|
||||
Resources: []Resource{
|
||||
{
|
||||
Path: "/style.css",
|
||||
Method: http.MethodGet,
|
||||
Header: http.Header{pushHeader: []string{}},
|
||||
},
|
||||
{
|
||||
Path: "/style2.css",
|
||||
Method: http.MethodGet,
|
||||
Header: http.Header{pushHeader: []string{}},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
"ParseSimpleInlinePushWithOps", `push /index.html {
|
||||
/style.css
|
||||
/style2.css
|
||||
header Test Value
|
||||
}`, false, []Rule{
|
||||
{
|
||||
Path: "/index.html",
|
||||
Resources: []Resource{
|
||||
{
|
||||
Path: "/style.css",
|
||||
Method: http.MethodGet,
|
||||
Header: http.Header{pushHeader: []string{}, "Test": []string{"Value"}},
|
||||
},
|
||||
{
|
||||
Path: "/style2.css",
|
||||
Method: http.MethodGet,
|
||||
Header: http.Header{pushHeader: []string{}, "Test": []string{"Value"}},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
"ParseProperConfigWithBlock", `push /index.html /style.css /style2.css {
|
||||
method HEAD
|
||||
header Own-Header Value
|
||||
header Own-Header2 Value2
|
||||
}`, false, []Rule{
|
||||
{
|
||||
Path: "/index.html",
|
||||
Resources: []Resource{
|
||||
{
|
||||
Path: "/style.css",
|
||||
Method: http.MethodHead,
|
||||
Header: http.Header{
|
||||
"Own-Header": []string{"Value"},
|
||||
"Own-Header2": []string{"Value2"},
|
||||
"X-Push": []string{},
|
||||
},
|
||||
},
|
||||
{
|
||||
Path: "/style2.css",
|
||||
Method: http.MethodHead,
|
||||
Header: http.Header{
|
||||
"Own-Header": []string{"Value"},
|
||||
"Own-Header2": []string{"Value2"},
|
||||
"X-Push": []string{},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
"ParseMergesRules", `push /index.html /index.css {
|
||||
header name value
|
||||
}
|
||||
|
||||
push /index.html /index2.css {
|
||||
header name2 value2
|
||||
method HEAD
|
||||
}
|
||||
`, false, []Rule{
|
||||
{
|
||||
Path: "/index.html",
|
||||
Resources: []Resource{
|
||||
{
|
||||
Path: "/index.css",
|
||||
Method: http.MethodGet,
|
||||
Header: http.Header{
|
||||
"Name": []string{"value"},
|
||||
"X-Push": []string{},
|
||||
},
|
||||
},
|
||||
{
|
||||
Path: "/index2.css",
|
||||
Method: http.MethodHead,
|
||||
Header: http.Header{
|
||||
"Name2": []string{"value2"},
|
||||
"X-Push": []string{},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t2 *testing.T) {
|
||||
actual, err := parsePushRules(caddy.NewTestController("http", test.input))
|
||||
|
||||
if err == nil && test.shouldErr {
|
||||
t2.Errorf("Test %s didn't error, but it should have", test.name)
|
||||
} else if err != nil && !test.shouldErr {
|
||||
t2.Errorf("Test %s errored, but it shouldn't have; got '%v'", test.name, err)
|
||||
}
|
||||
|
||||
if len(actual) != len(test.expected) {
|
||||
t2.Fatalf("Test %s expected %d rules, but got %d",
|
||||
test.name, len(test.expected), len(actual))
|
||||
}
|
||||
|
||||
for j, expectedRule := range test.expected {
|
||||
actualRule := actual[j]
|
||||
|
||||
if actualRule.Path != expectedRule.Path {
|
||||
t.Errorf("Test %s, rule %d: Expected path %s, but got %s",
|
||||
test.name, j, expectedRule.Path, actualRule.Path)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(actualRule.Resources, expectedRule.Resources) {
|
||||
t.Errorf("Test %s, rule %d: Expected resources %v, but got %v",
|
||||
test.name, j, expectedRule.Resources, actualRule.Resources)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetupInstalledMiddleware(t *testing.T) {
|
||||
|
||||
// given
|
||||
c := caddy.NewTestController("http", `push /index.html /test.js`)
|
||||
|
||||
// when
|
||||
err := setup(c)
|
||||
|
||||
// then
|
||||
if err != nil {
|
||||
t.Errorf("Expected no errors, but got: %v", err)
|
||||
}
|
||||
|
||||
middlewares := httpserver.GetConfig(c).Middleware()
|
||||
|
||||
if len(middlewares) != 1 {
|
||||
t.Fatalf("Expected 1 middleware, had %d instead", len(middlewares))
|
||||
}
|
||||
|
||||
handler := middlewares[0](httpserver.EmptyNext)
|
||||
pushHandler, ok := handler.(Middleware)
|
||||
|
||||
if !ok {
|
||||
t.Fatalf("Expected handler to be type Middleware, got: %#v", handler)
|
||||
}
|
||||
|
||||
if !httpserver.SameNext(pushHandler.Next, httpserver.EmptyNext) {
|
||||
t.Error("'Next' field of handler Middleware was not set properly")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetupWithError(t *testing.T) {
|
||||
// given
|
||||
c := caddy.NewTestController("http", "push {\nmethod\n}")
|
||||
|
||||
// when
|
||||
err := setup(c)
|
||||
|
||||
// then
|
||||
if err == nil {
|
||||
t.Error("Expected error but none occurred")
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user