2017-09-23 13:56:58 +08:00
|
|
|
// 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.
|
|
|
|
|
2016-06-06 12:39:23 +08:00
|
|
|
package fastcgi
|
|
|
|
|
|
|
|
import (
|
2017-10-03 21:17:54 +08:00
|
|
|
"context"
|
2016-06-06 12:39:23 +08:00
|
|
|
"fmt"
|
2017-10-03 21:17:54 +08:00
|
|
|
"net"
|
2016-06-06 12:39:23 +08:00
|
|
|
"testing"
|
2018-09-03 05:28:10 +08:00
|
|
|
"time"
|
2016-06-06 12:39:23 +08:00
|
|
|
|
|
|
|
"github.com/mholt/caddy"
|
|
|
|
"github.com/mholt/caddy/caddyhttp/httpserver"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSetup(t *testing.T) {
|
2016-06-21 01:44:20 +08:00
|
|
|
c := caddy.NewTestController("http", `fastcgi / 127.0.0.1:9000`)
|
|
|
|
err := setup(c)
|
2016-06-06 12:39:23 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Expected no errors, got: %v", err)
|
|
|
|
}
|
2016-06-21 01:44:20 +08:00
|
|
|
mids := httpserver.GetConfig(c).Middleware()
|
2016-06-06 12:39:23 +08:00
|
|
|
if len(mids) == 0 {
|
|
|
|
t.Fatal("Expected middleware, got 0 instead")
|
|
|
|
}
|
|
|
|
|
|
|
|
handler := mids[0](httpserver.EmptyNext)
|
|
|
|
myHandler, ok := handler.(Handler)
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Expected handler to be type , got: %#v", handler)
|
|
|
|
}
|
|
|
|
|
|
|
|
if myHandler.Rules[0].Path != "/" {
|
|
|
|
t.Errorf("Expected / as the Path")
|
|
|
|
}
|
2017-10-03 21:17:54 +08:00
|
|
|
addr, err := myHandler.Rules[0].Address()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error in trying to retrieve address: %s", err.Error())
|
2016-06-06 12:39:23 +08:00
|
|
|
}
|
|
|
|
|
2017-10-03 21:17:54 +08:00
|
|
|
if addr != "127.0.0.1:9000" {
|
|
|
|
t.Errorf("Expected 127.0.0.1:9000 as the Address")
|
|
|
|
}
|
2018-09-03 05:28:10 +08:00
|
|
|
|
|
|
|
if myHandler.Rules[0].ConnectTimeout != 60*time.Second {
|
|
|
|
t.Errorf("Expected default value of 60 seconds")
|
|
|
|
}
|
|
|
|
|
|
|
|
if myHandler.Rules[0].ReadTimeout != 60*time.Second {
|
|
|
|
t.Errorf("Expected default value of 60 seconds")
|
|
|
|
}
|
|
|
|
|
|
|
|
if myHandler.Rules[0].SendTimeout != 60*time.Second {
|
|
|
|
t.Errorf("Expected default value of 60 seconds")
|
|
|
|
}
|
2016-06-06 12:39:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFastcgiParse(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
inputFastcgiConfig string
|
|
|
|
shouldErr bool
|
|
|
|
expectedFastcgiConfig []Rule
|
|
|
|
}{
|
|
|
|
|
|
|
|
{`fastcgi /blog 127.0.0.1:9000 php`,
|
|
|
|
false, []Rule{{
|
2018-09-03 05:28:10 +08:00
|
|
|
Path: "/blog",
|
|
|
|
balancer: &roundRobin{addresses: []string{"127.0.0.1:9000"}},
|
|
|
|
Ext: ".php",
|
|
|
|
SplitPath: ".php",
|
|
|
|
IndexFiles: []string{"index.php"},
|
|
|
|
SendTimeout: 60 * time.Second,
|
2016-11-17 12:29:43 +08:00
|
|
|
}}},
|
2017-07-19 02:52:53 +08:00
|
|
|
{`fastcgi / 127.0.0.1:9001 {
|
2016-06-06 12:39:23 +08:00
|
|
|
split .html
|
|
|
|
}`,
|
|
|
|
false, []Rule{{
|
2018-09-03 05:28:10 +08:00
|
|
|
Path: "/",
|
|
|
|
balancer: &roundRobin{addresses: []string{"127.0.0.1:9001"}},
|
|
|
|
Ext: "",
|
|
|
|
SplitPath: ".html",
|
|
|
|
IndexFiles: []string{},
|
|
|
|
SendTimeout: 60 * time.Second,
|
2016-06-06 12:39:23 +08:00
|
|
|
}}},
|
2017-07-19 02:52:53 +08:00
|
|
|
{`fastcgi / 127.0.0.1:9001 {
|
2016-06-06 12:39:23 +08:00
|
|
|
split .html
|
|
|
|
except /admin /user
|
|
|
|
}`,
|
|
|
|
false, []Rule{{
|
|
|
|
Path: "/",
|
2017-07-19 02:52:53 +08:00
|
|
|
balancer: &roundRobin{addresses: []string{"127.0.0.1:9001"}},
|
2016-06-06 12:39:23 +08:00
|
|
|
Ext: "",
|
|
|
|
SplitPath: ".html",
|
|
|
|
IndexFiles: []string{},
|
|
|
|
IgnoredSubPaths: []string{"/admin", "/user"},
|
2018-09-03 05:28:10 +08:00
|
|
|
SendTimeout: 60 * time.Second,
|
|
|
|
}}},
|
|
|
|
{`fastcgi / 127.0.0.1:9001 {
|
|
|
|
send_timeout 30s
|
|
|
|
}`,
|
|
|
|
false, []Rule{{
|
|
|
|
Path: "/",
|
|
|
|
balancer: &roundRobin{addresses: []string{"127.0.0.1:9001"}},
|
|
|
|
Ext: "",
|
|
|
|
IndexFiles: []string{},
|
|
|
|
SendTimeout: 30 * time.Second,
|
2016-10-17 02:11:52 +08:00
|
|
|
}}},
|
2016-06-06 12:39:23 +08:00
|
|
|
}
|
|
|
|
for i, test := range tests {
|
2016-06-21 01:44:20 +08:00
|
|
|
actualFastcgiConfigs, err := fastcgiParse(caddy.NewTestController("http", test.inputFastcgiConfig))
|
2016-06-06 12:39:23 +08:00
|
|
|
|
|
|
|
if err == nil && test.shouldErr {
|
|
|
|
t.Errorf("Test %d didn't error, but it should have", i)
|
|
|
|
} else if err != nil && !test.shouldErr {
|
|
|
|
t.Errorf("Test %d errored, but it shouldn't have; got '%v'", i, err)
|
|
|
|
}
|
|
|
|
if len(actualFastcgiConfigs) != len(test.expectedFastcgiConfig) {
|
|
|
|
t.Fatalf("Test %d expected %d no of FastCGI configs, but got %d ",
|
|
|
|
i, len(test.expectedFastcgiConfig), len(actualFastcgiConfigs))
|
|
|
|
}
|
|
|
|
for j, actualFastcgiConfig := range actualFastcgiConfigs {
|
|
|
|
|
|
|
|
if actualFastcgiConfig.Path != test.expectedFastcgiConfig[j].Path {
|
|
|
|
t.Errorf("Test %d expected %dth FastCGI Path to be %s , but got %s",
|
|
|
|
i, j, test.expectedFastcgiConfig[j].Path, actualFastcgiConfig.Path)
|
|
|
|
}
|
|
|
|
|
2017-10-03 21:17:54 +08:00
|
|
|
actualAddr, err := actualFastcgiConfig.Address()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Test %d unexpected error in trying to retrieve %dth actual address: %s", i, j, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedAddr, err := test.expectedFastcgiConfig[j].Address()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Test %d unexpected error in trying to retrieve %dth expected address: %s", i, j, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if actualAddr != expectedAddr {
|
2016-06-06 12:39:23 +08:00
|
|
|
t.Errorf("Test %d expected %dth FastCGI Address to be %s , but got %s",
|
2017-10-03 21:17:54 +08:00
|
|
|
i, j, expectedAddr, actualAddr)
|
2016-06-06 12:39:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if actualFastcgiConfig.Ext != test.expectedFastcgiConfig[j].Ext {
|
|
|
|
t.Errorf("Test %d expected %dth FastCGI Ext to be %s , but got %s",
|
|
|
|
i, j, test.expectedFastcgiConfig[j].Ext, actualFastcgiConfig.Ext)
|
|
|
|
}
|
|
|
|
|
|
|
|
if actualFastcgiConfig.SplitPath != test.expectedFastcgiConfig[j].SplitPath {
|
|
|
|
t.Errorf("Test %d expected %dth FastCGI SplitPath to be %s , but got %s",
|
|
|
|
i, j, test.expectedFastcgiConfig[j].SplitPath, actualFastcgiConfig.SplitPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
if fmt.Sprint(actualFastcgiConfig.IndexFiles) != fmt.Sprint(test.expectedFastcgiConfig[j].IndexFiles) {
|
|
|
|
t.Errorf("Test %d expected %dth FastCGI IndexFiles to be %s , but got %s",
|
|
|
|
i, j, test.expectedFastcgiConfig[j].IndexFiles, actualFastcgiConfig.IndexFiles)
|
|
|
|
}
|
|
|
|
|
|
|
|
if fmt.Sprint(actualFastcgiConfig.IgnoredSubPaths) != fmt.Sprint(test.expectedFastcgiConfig[j].IgnoredSubPaths) {
|
|
|
|
t.Errorf("Test %d expected %dth FastCGI IgnoredSubPaths to be %s , but got %s",
|
|
|
|
i, j, test.expectedFastcgiConfig[j].IgnoredSubPaths, actualFastcgiConfig.IgnoredSubPaths)
|
|
|
|
}
|
2018-09-03 05:28:10 +08:00
|
|
|
|
|
|
|
if actualFastcgiConfig.SendTimeout != test.expectedFastcgiConfig[j].SendTimeout {
|
|
|
|
t.Errorf("Test %d expected %dth FastCGI SendTimeout to be %s , but got %s",
|
|
|
|
i, j, test.expectedFastcgiConfig[j].SendTimeout, actualFastcgiConfig.SendTimeout)
|
|
|
|
}
|
2016-06-06 12:39:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2017-10-03 21:17:54 +08:00
|
|
|
|
|
|
|
func TestFastCGIResolveSRV(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
inputFastcgiConfig string
|
|
|
|
locator string
|
|
|
|
target string
|
|
|
|
port uint16
|
|
|
|
shouldErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
`fastcgi / srv://fpm.tcp.service.consul {
|
|
|
|
upstream yolo
|
|
|
|
}`,
|
|
|
|
"fpm.tcp.service.consul",
|
|
|
|
"127.0.0.1",
|
|
|
|
9000,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`fastcgi / srv://fpm.tcp.service.consul`,
|
|
|
|
"fpm.tcp.service.consul",
|
|
|
|
"127.0.0.1",
|
|
|
|
9000,
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
|
|
|
actualFastcgiConfigs, err := fastcgiParse(caddy.NewTestController("http", test.inputFastcgiConfig))
|
|
|
|
|
|
|
|
if err == nil && test.shouldErr {
|
|
|
|
t.Errorf("Test %d didn't error, but it should have", i)
|
|
|
|
} else if err != nil && !test.shouldErr {
|
|
|
|
t.Errorf("Test %d errored, but it shouldn't have; got '%v'", i, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, actualFastcgiConfig := range actualFastcgiConfigs {
|
|
|
|
resolver, ok := (actualFastcgiConfig.balancer).(*srv)
|
|
|
|
if !ok {
|
|
|
|
t.Errorf("Test %d upstream balancer is not srv", i)
|
|
|
|
}
|
|
|
|
resolver.resolver = buildTestResolver(test.target, test.port)
|
|
|
|
|
|
|
|
addr, err := actualFastcgiConfig.Address()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Test %d failed to retrieve upstream address. %s", i, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedAddr := fmt.Sprintf("%s:%d", test.target, test.port)
|
|
|
|
if addr != expectedAddr {
|
|
|
|
t.Errorf("Test %d expected upstream address to be %s, got %s", i, expectedAddr, addr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func buildTestResolver(target string, port uint16) srvResolver {
|
|
|
|
return &testSRVResolver{target, port}
|
|
|
|
}
|
|
|
|
|
|
|
|
type testSRVResolver struct {
|
|
|
|
target string
|
|
|
|
port uint16
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *testSRVResolver) LookupSRV(ctx context.Context, service, proto, name string) (string, []*net.SRV, error) {
|
|
|
|
return "", []*net.SRV{
|
|
|
|
{Target: r.target,
|
|
|
|
Port: r.port,
|
|
|
|
Priority: 1,
|
|
|
|
Weight: 1}}, nil
|
|
|
|
}
|