2016-06-06 12:39:23 +08:00
|
|
|
package fastcgi
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-01-18 01:34:17 +08:00
|
|
|
"os"
|
2016-09-28 08:12:22 +08:00
|
|
|
"reflect"
|
2016-06-06 12:39:23 +08:00
|
|
|
"testing"
|
2016-11-20 00:05:29 +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")
|
|
|
|
}
|
|
|
|
if myHandler.Rules[0].Address != "127.0.0.1:9000" {
|
|
|
|
t.Errorf("Expected 127.0.0.1:9000 as the Address")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-09-28 08:12:22 +08:00
|
|
|
func (p *persistentDialer) Equals(q *persistentDialer) bool {
|
|
|
|
if p.size != q.size {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if p.network != q.network {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if p.address != q.address {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(p.pool) != len(q.pool) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for i, client := range p.pool {
|
|
|
|
if client != q.pool[i] {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// ignore mutex state
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2016-06-06 12:39:23 +08:00
|
|
|
func TestFastcgiParse(t *testing.T) {
|
2017-01-18 01:34:17 +08:00
|
|
|
rootPath, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Can't determine current working directory; got '%v'", err)
|
|
|
|
}
|
|
|
|
|
2016-09-28 08:12:22 +08:00
|
|
|
defaultAddress := "127.0.0.1:9001"
|
|
|
|
network, address := parseAddress(defaultAddress)
|
|
|
|
t.Logf("Address '%v' was parsed to network '%v' and address '%v'", defaultAddress, network, address)
|
|
|
|
|
2016-06-06 12:39:23 +08:00
|
|
|
tests := []struct {
|
|
|
|
inputFastcgiConfig string
|
|
|
|
shouldErr bool
|
|
|
|
expectedFastcgiConfig []Rule
|
|
|
|
}{
|
|
|
|
|
|
|
|
{`fastcgi /blog 127.0.0.1:9000 php`,
|
|
|
|
false, []Rule{{
|
2017-01-18 01:34:17 +08:00
|
|
|
Root: rootPath,
|
|
|
|
Path: "/blog",
|
|
|
|
Address: "127.0.0.1:9000",
|
|
|
|
Ext: ".php",
|
|
|
|
SplitPath: ".php",
|
|
|
|
dialer: &loadBalancingDialer{dialers: []dialer{basicDialer{network: "tcp", address: "127.0.0.1:9000", timeout: 60 * time.Second}}},
|
|
|
|
IndexFiles: []string{"index.php"},
|
|
|
|
ReadTimeout: 60 * time.Second,
|
|
|
|
SendTimeout: 60 * time.Second,
|
|
|
|
}}},
|
|
|
|
{`fastcgi /blog 127.0.0.1:9000 php {
|
|
|
|
root /tmp
|
|
|
|
}`,
|
|
|
|
false, []Rule{{
|
|
|
|
Root: "/tmp",
|
2016-11-26 00:30:51 +08:00
|
|
|
Path: "/blog",
|
|
|
|
Address: "127.0.0.1:9000",
|
|
|
|
Ext: ".php",
|
|
|
|
SplitPath: ".php",
|
|
|
|
dialer: &loadBalancingDialer{dialers: []dialer{basicDialer{network: "tcp", address: "127.0.0.1:9000", timeout: 60 * time.Second}}},
|
|
|
|
IndexFiles: []string{"index.php"},
|
|
|
|
ReadTimeout: 60 * time.Second,
|
2016-11-30 10:26:51 +08:00
|
|
|
SendTimeout: 60 * time.Second,
|
2016-06-06 12:39:23 +08:00
|
|
|
}}},
|
2016-11-17 12:29:43 +08:00
|
|
|
{`fastcgi /blog 127.0.0.1:9000 php {
|
|
|
|
upstream 127.0.0.1:9001
|
|
|
|
}`,
|
|
|
|
false, []Rule{{
|
2017-01-18 01:34:17 +08:00
|
|
|
Root: rootPath,
|
2016-11-26 00:30:51 +08:00
|
|
|
Path: "/blog",
|
|
|
|
Address: "127.0.0.1:9000,127.0.0.1:9001",
|
|
|
|
Ext: ".php",
|
|
|
|
SplitPath: ".php",
|
|
|
|
dialer: &loadBalancingDialer{dialers: []dialer{basicDialer{network: "tcp", address: "127.0.0.1:9000", timeout: 60 * time.Second}, basicDialer{network: "tcp", address: "127.0.0.1:9001", timeout: 60 * time.Second}}},
|
|
|
|
IndexFiles: []string{"index.php"},
|
|
|
|
ReadTimeout: 60 * time.Second,
|
2016-11-30 10:26:51 +08:00
|
|
|
SendTimeout: 60 * time.Second,
|
2016-11-17 12:29:43 +08:00
|
|
|
}}},
|
|
|
|
{`fastcgi /blog 127.0.0.1:9000 {
|
|
|
|
upstream 127.0.0.1:9001
|
|
|
|
}`,
|
|
|
|
false, []Rule{{
|
2017-01-18 01:34:17 +08:00
|
|
|
Root: rootPath,
|
2016-11-26 00:30:51 +08:00
|
|
|
Path: "/blog",
|
|
|
|
Address: "127.0.0.1:9000,127.0.0.1:9001",
|
|
|
|
Ext: "",
|
|
|
|
SplitPath: "",
|
|
|
|
dialer: &loadBalancingDialer{dialers: []dialer{basicDialer{network: "tcp", address: "127.0.0.1:9000", timeout: 60 * time.Second}, basicDialer{network: "tcp", address: "127.0.0.1:9001", timeout: 60 * time.Second}}},
|
|
|
|
IndexFiles: []string{},
|
|
|
|
ReadTimeout: 60 * time.Second,
|
2016-11-30 10:26:51 +08:00
|
|
|
SendTimeout: 60 * time.Second,
|
2016-11-17 12:29:43 +08:00
|
|
|
}}},
|
2016-09-28 08:12:22 +08:00
|
|
|
{`fastcgi / ` + defaultAddress + ` {
|
2016-06-06 12:39:23 +08:00
|
|
|
split .html
|
|
|
|
}`,
|
|
|
|
false, []Rule{{
|
2017-01-18 01:34:17 +08:00
|
|
|
Root: rootPath,
|
2016-11-26 00:30:51 +08:00
|
|
|
Path: "/",
|
|
|
|
Address: defaultAddress,
|
|
|
|
Ext: "",
|
|
|
|
SplitPath: ".html",
|
|
|
|
dialer: &loadBalancingDialer{dialers: []dialer{basicDialer{network: network, address: address, timeout: 60 * time.Second}}},
|
|
|
|
IndexFiles: []string{},
|
|
|
|
ReadTimeout: 60 * time.Second,
|
2016-11-30 10:26:51 +08:00
|
|
|
SendTimeout: 60 * time.Second,
|
2016-06-06 12:39:23 +08:00
|
|
|
}}},
|
2016-09-28 08:12:22 +08:00
|
|
|
{`fastcgi / ` + defaultAddress + ` {
|
2016-06-06 12:39:23 +08:00
|
|
|
split .html
|
|
|
|
except /admin /user
|
|
|
|
}`,
|
|
|
|
false, []Rule{{
|
2017-01-18 01:34:17 +08:00
|
|
|
Root: rootPath,
|
2016-06-06 12:39:23 +08:00
|
|
|
Path: "/",
|
|
|
|
Address: "127.0.0.1:9001",
|
|
|
|
Ext: "",
|
|
|
|
SplitPath: ".html",
|
2016-11-26 00:30:51 +08:00
|
|
|
dialer: &loadBalancingDialer{dialers: []dialer{basicDialer{network: network, address: address, timeout: 60 * time.Second}}},
|
2016-06-06 12:39:23 +08:00
|
|
|
IndexFiles: []string{},
|
|
|
|
IgnoredSubPaths: []string{"/admin", "/user"},
|
2016-11-26 00:30:51 +08:00
|
|
|
ReadTimeout: 60 * time.Second,
|
2016-11-30 10:26:51 +08:00
|
|
|
SendTimeout: 60 * time.Second,
|
2016-06-06 12:39:23 +08:00
|
|
|
}}},
|
2016-09-28 08:12:22 +08:00
|
|
|
{`fastcgi / ` + defaultAddress + ` {
|
|
|
|
pool 0
|
|
|
|
}`,
|
|
|
|
false, []Rule{{
|
2017-01-18 01:34:17 +08:00
|
|
|
Root: rootPath,
|
2016-11-26 00:30:51 +08:00
|
|
|
Path: "/",
|
|
|
|
Address: defaultAddress,
|
|
|
|
Ext: "",
|
|
|
|
SplitPath: "",
|
|
|
|
dialer: &loadBalancingDialer{dialers: []dialer{&persistentDialer{size: 0, network: network, address: address, timeout: 60 * time.Second}}},
|
|
|
|
IndexFiles: []string{},
|
|
|
|
ReadTimeout: 60 * time.Second,
|
2016-11-30 10:26:51 +08:00
|
|
|
SendTimeout: 60 * time.Second,
|
2016-09-28 08:12:22 +08:00
|
|
|
}}},
|
2016-11-17 12:29:43 +08:00
|
|
|
{`fastcgi / 127.0.0.1:8080 {
|
|
|
|
upstream 127.0.0.1:9000
|
2016-09-28 08:12:22 +08:00
|
|
|
pool 5
|
|
|
|
}`,
|
|
|
|
false, []Rule{{
|
2017-01-18 01:34:17 +08:00
|
|
|
Root: rootPath,
|
2016-11-26 00:30:51 +08:00
|
|
|
Path: "/",
|
|
|
|
Address: "127.0.0.1:8080,127.0.0.1:9000",
|
|
|
|
Ext: "",
|
|
|
|
SplitPath: "",
|
|
|
|
dialer: &loadBalancingDialer{dialers: []dialer{&persistentDialer{size: 5, network: "tcp", address: "127.0.0.1:8080", timeout: 60 * time.Second}, &persistentDialer{size: 5, network: "tcp", address: "127.0.0.1:9000", timeout: 60 * time.Second}}},
|
|
|
|
IndexFiles: []string{},
|
|
|
|
ReadTimeout: 60 * time.Second,
|
2016-11-30 10:26:51 +08:00
|
|
|
SendTimeout: 60 * time.Second,
|
2016-09-28 08:12:22 +08:00
|
|
|
}}},
|
2016-10-17 02:11:52 +08:00
|
|
|
{`fastcgi / ` + defaultAddress + ` {
|
|
|
|
split .php
|
|
|
|
}`,
|
|
|
|
false, []Rule{{
|
2017-01-18 01:34:17 +08:00
|
|
|
Root: rootPath,
|
2016-11-26 00:30:51 +08:00
|
|
|
Path: "/",
|
|
|
|
Address: defaultAddress,
|
|
|
|
Ext: "",
|
|
|
|
SplitPath: ".php",
|
|
|
|
dialer: &loadBalancingDialer{dialers: []dialer{basicDialer{network: network, address: address, timeout: 60 * time.Second}}},
|
|
|
|
IndexFiles: []string{},
|
|
|
|
ReadTimeout: 60 * time.Second,
|
2016-11-30 10:26:51 +08:00
|
|
|
SendTimeout: 60 * time.Second,
|
2016-10-17 02:11:52 +08:00
|
|
|
}}},
|
2016-11-20 00:05:29 +08:00
|
|
|
{`fastcgi / ` + defaultAddress + ` {
|
|
|
|
connect_timeout 5s
|
|
|
|
}`,
|
|
|
|
false, []Rule{{
|
2017-01-18 01:34:17 +08:00
|
|
|
Root: rootPath,
|
2016-11-26 00:30:51 +08:00
|
|
|
Path: "/",
|
|
|
|
Address: defaultAddress,
|
|
|
|
Ext: "",
|
|
|
|
SplitPath: "",
|
|
|
|
dialer: &loadBalancingDialer{dialers: []dialer{basicDialer{network: network, address: address, timeout: 5 * time.Second}}},
|
|
|
|
IndexFiles: []string{},
|
|
|
|
ReadTimeout: 60 * time.Second,
|
2016-11-30 10:26:51 +08:00
|
|
|
SendTimeout: 60 * time.Second,
|
2016-11-20 00:05:29 +08:00
|
|
|
}}},
|
2016-11-30 10:26:51 +08:00
|
|
|
{
|
|
|
|
`fastcgi / ` + defaultAddress + ` { connect_timeout BADVALUE }`,
|
|
|
|
true,
|
|
|
|
[]Rule{},
|
|
|
|
},
|
2016-11-20 00:05:29 +08:00
|
|
|
{`fastcgi / ` + defaultAddress + ` {
|
|
|
|
read_timeout 5s
|
|
|
|
}`,
|
|
|
|
false, []Rule{{
|
2017-01-18 01:34:17 +08:00
|
|
|
Root: rootPath,
|
2016-11-20 00:05:29 +08:00
|
|
|
Path: "/",
|
|
|
|
Address: defaultAddress,
|
|
|
|
Ext: "",
|
|
|
|
SplitPath: "",
|
2016-11-26 00:30:51 +08:00
|
|
|
dialer: &loadBalancingDialer{dialers: []dialer{basicDialer{network: network, address: address, timeout: 60 * time.Second}}},
|
2016-11-20 00:05:29 +08:00
|
|
|
IndexFiles: []string{},
|
|
|
|
ReadTimeout: 5 * time.Second,
|
2016-11-30 10:26:51 +08:00
|
|
|
SendTimeout: 60 * time.Second,
|
|
|
|
}}},
|
|
|
|
{
|
|
|
|
`fastcgi / ` + defaultAddress + ` { read_timeout BADVALUE }`,
|
|
|
|
true,
|
|
|
|
[]Rule{},
|
|
|
|
},
|
|
|
|
{`fastcgi / ` + defaultAddress + ` {
|
|
|
|
send_timeout 5s
|
|
|
|
}`,
|
|
|
|
false, []Rule{{
|
2017-01-18 01:34:17 +08:00
|
|
|
Root: rootPath,
|
2016-11-30 10:26:51 +08:00
|
|
|
Path: "/",
|
|
|
|
Address: defaultAddress,
|
|
|
|
Ext: "",
|
|
|
|
SplitPath: "",
|
|
|
|
dialer: &loadBalancingDialer{dialers: []dialer{basicDialer{network: network, address: address, timeout: 60 * time.Second}}},
|
|
|
|
IndexFiles: []string{},
|
|
|
|
ReadTimeout: 60 * time.Second,
|
|
|
|
SendTimeout: 5 * time.Second,
|
2016-11-20 00:05:29 +08:00
|
|
|
}}},
|
2016-11-30 10:26:51 +08:00
|
|
|
{
|
|
|
|
`fastcgi / ` + defaultAddress + ` { send_timeout BADVALUE }`,
|
|
|
|
true,
|
|
|
|
[]Rule{},
|
|
|
|
},
|
2016-11-17 12:29:43 +08:00
|
|
|
{`fastcgi / {
|
|
|
|
|
|
|
|
}`,
|
|
|
|
true, []Rule{},
|
|
|
|
},
|
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 {
|
|
|
|
|
2017-01-18 01:34:17 +08:00
|
|
|
if actualFastcgiConfig.Root != test.expectedFastcgiConfig[j].Root {
|
|
|
|
t.Errorf("Test %d expected %dth FastCGI Root to be %s , but got %s",
|
|
|
|
i, j, test.expectedFastcgiConfig[j].Root, actualFastcgiConfig.Root)
|
|
|
|
}
|
|
|
|
|
2016-06-06 12:39:23 +08:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
if actualFastcgiConfig.Address != test.expectedFastcgiConfig[j].Address {
|
|
|
|
t.Errorf("Test %d expected %dth FastCGI Address to be %s , but got %s",
|
|
|
|
i, j, test.expectedFastcgiConfig[j].Address, actualFastcgiConfig.Address)
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2016-09-28 08:12:22 +08:00
|
|
|
if reflect.TypeOf(actualFastcgiConfig.dialer) != reflect.TypeOf(test.expectedFastcgiConfig[j].dialer) {
|
|
|
|
t.Errorf("Test %d expected %dth FastCGI dialer to be of type %T, but got %T",
|
|
|
|
i, j, test.expectedFastcgiConfig[j].dialer, actualFastcgiConfig.dialer)
|
|
|
|
} else {
|
2016-11-17 12:29:43 +08:00
|
|
|
if !areDialersEqual(actualFastcgiConfig.dialer, test.expectedFastcgiConfig[j].dialer, t) {
|
2016-09-28 08:12:22 +08:00
|
|
|
t.Errorf("Test %d expected %dth FastCGI dialer to be %v, but got %v",
|
|
|
|
i, j, test.expectedFastcgiConfig[j].dialer, actualFastcgiConfig.dialer)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-06 12:39:23 +08:00
|
|
|
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)
|
|
|
|
}
|
2016-11-30 10:26:51 +08:00
|
|
|
|
|
|
|
if fmt.Sprint(actualFastcgiConfig.ReadTimeout) != fmt.Sprint(test.expectedFastcgiConfig[j].ReadTimeout) {
|
|
|
|
t.Errorf("Test %d expected %dth FastCGI ReadTimeout to be %s , but got %s",
|
|
|
|
i, j, test.expectedFastcgiConfig[j].ReadTimeout, actualFastcgiConfig.ReadTimeout)
|
|
|
|
}
|
|
|
|
|
|
|
|
if fmt.Sprint(actualFastcgiConfig.SendTimeout) != fmt.Sprint(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
|
|
|
}
|
|
|
|
}
|
2016-11-17 12:29:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func areDialersEqual(current, expected dialer, t *testing.T) bool {
|
|
|
|
|
|
|
|
switch actual := current.(type) {
|
|
|
|
case *loadBalancingDialer:
|
|
|
|
if expected, ok := expected.(*loadBalancingDialer); ok {
|
|
|
|
for i := 0; i < len(actual.dialers); i++ {
|
|
|
|
if !areDialersEqual(actual.dialers[i], expected.dialers[i], t) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
case basicDialer:
|
|
|
|
return current == expected
|
|
|
|
case *persistentDialer:
|
|
|
|
if expected, ok := expected.(*persistentDialer); ok {
|
|
|
|
return actual.Equals(expected)
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
t.Errorf("Unknown dialer type %T", current)
|
|
|
|
}
|
2016-06-06 12:39:23 +08:00
|
|
|
|
2016-11-17 12:29:43 +08:00
|
|
|
return false
|
2016-06-06 12:39:23 +08:00
|
|
|
}
|