setup_test.go 11.9 KB
Newer Older
1 2 3 4
package fastcgi

import (
	"fmt"
5
	"os"
6
	"reflect"
7
	"testing"
8
	"time"
9 10 11 12 13 14

	"github.com/mholt/caddy"
	"github.com/mholt/caddy/caddyhttp/httpserver"
)

func TestSetup(t *testing.T) {
15 16
	c := caddy.NewTestController("http", `fastcgi / 127.0.0.1:9000`)
	err := setup(c)
17 18 19
	if err != nil {
		t.Errorf("Expected no errors, got: %v", err)
	}
20
	mids := httpserver.GetConfig(c).Middleware()
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
	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")
	}

}

41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
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
}

64
func TestFastcgiParse(t *testing.T) {
65 66 67 68 69
	rootPath, err := os.Getwd()
	if err != nil {
		t.Errorf("Can't determine current working directory; got '%v'", err)
	}

70 71 72 73
	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)

74 75 76 77 78 79 80 81
	tests := []struct {
		inputFastcgiConfig    string
		shouldErr             bool
		expectedFastcgiConfig []Rule
	}{

		{`fastcgi /blog 127.0.0.1:9000 php`,
			false, []Rule{{
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
				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",
97 98 99 100 101 102 103
				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,
104
				SendTimeout: 60 * time.Second,
105
			}}},
106 107 108 109
		{`fastcgi /blog 127.0.0.1:9000 php {
			upstream 127.0.0.1:9001
		}`,
			false, []Rule{{
110
				Root:        rootPath,
111 112 113 114 115 116 117
				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,
118
				SendTimeout: 60 * time.Second,
119 120 121 122 123
			}}},
		{`fastcgi /blog 127.0.0.1:9000 {
			upstream 127.0.0.1:9001 
		}`,
			false, []Rule{{
124
				Root:        rootPath,
125 126 127 128 129 130 131
				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,
132
				SendTimeout: 60 * time.Second,
133
			}}},
134
		{`fastcgi / ` + defaultAddress + ` {
135 136 137
	              split .html
	              }`,
			false, []Rule{{
138
				Root:        rootPath,
139 140 141 142 143 144 145
				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,
146
				SendTimeout: 60 * time.Second,
147
			}}},
148
		{`fastcgi / ` + defaultAddress + ` {
149 150 151 152
	              split .html
	              except /admin /user
	              }`,
			false, []Rule{{
153
				Root:            rootPath,
154 155 156 157
				Path:            "/",
				Address:         "127.0.0.1:9001",
				Ext:             "",
				SplitPath:       ".html",
158
				dialer:          &loadBalancingDialer{dialers: []dialer{basicDialer{network: network, address: address, timeout: 60 * time.Second}}},
159 160
				IndexFiles:      []string{},
				IgnoredSubPaths: []string{"/admin", "/user"},
161
				ReadTimeout:     60 * time.Second,
162
				SendTimeout:     60 * time.Second,
163
			}}},
164 165 166 167
		{`fastcgi / ` + defaultAddress + ` {
	              pool 0
	              }`,
			false, []Rule{{
168
				Root:        rootPath,
169 170 171 172 173 174 175
				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,
176
				SendTimeout: 60 * time.Second,
177
			}}},
178 179
		{`fastcgi / 127.0.0.1:8080  {
			upstream 127.0.0.1:9000
180 181 182
	              pool 5
	              }`,
			false, []Rule{{
183
				Root:        rootPath,
184 185 186 187 188 189 190
				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,
191
				SendTimeout: 60 * time.Second,
192
			}}},
193 194 195 196
		{`fastcgi / ` + defaultAddress + ` {
	              split .php
	              }`,
			false, []Rule{{
197
				Root:        rootPath,
198 199 200 201 202 203 204
				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,
205
				SendTimeout: 60 * time.Second,
206
			}}},
207 208 209 210
		{`fastcgi / ` + defaultAddress + ` {
	              connect_timeout 5s
	              }`,
			false, []Rule{{
211
				Root:        rootPath,
212 213 214 215 216 217 218
				Path:        "/",
				Address:     defaultAddress,
				Ext:         "",
				SplitPath:   "",
				dialer:      &loadBalancingDialer{dialers: []dialer{basicDialer{network: network, address: address, timeout: 5 * time.Second}}},
				IndexFiles:  []string{},
				ReadTimeout: 60 * time.Second,
219
				SendTimeout: 60 * time.Second,
220
			}}},
221 222 223 224 225
		{
			`fastcgi / ` + defaultAddress + ` { connect_timeout BADVALUE }`,
			true,
			[]Rule{},
		},
226 227 228 229
		{`fastcgi / ` + defaultAddress + ` {
	              read_timeout 5s
	              }`,
			false, []Rule{{
230
				Root:        rootPath,
231 232 233 234
				Path:        "/",
				Address:     defaultAddress,
				Ext:         "",
				SplitPath:   "",
235
				dialer:      &loadBalancingDialer{dialers: []dialer{basicDialer{network: network, address: address, timeout: 60 * time.Second}}},
236 237
				IndexFiles:  []string{},
				ReadTimeout: 5 * time.Second,
238 239 240 241 242 243 244 245 246 247 248
				SendTimeout: 60 * time.Second,
			}}},
		{
			`fastcgi / ` + defaultAddress + ` { read_timeout BADVALUE }`,
			true,
			[]Rule{},
		},
		{`fastcgi / ` + defaultAddress + ` {
	              send_timeout 5s
	              }`,
			false, []Rule{{
249
				Root:        rootPath,
250 251 252 253 254 255 256 257
				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,
258
			}}},
259 260 261 262 263
		{
			`fastcgi / ` + defaultAddress + ` { send_timeout BADVALUE }`,
			true,
			[]Rule{},
		},
264 265 266 267 268
		{`fastcgi / {

		              }`,
			true, []Rule{},
		},
269 270
	}
	for i, test := range tests {
271
		actualFastcgiConfigs, err := fastcgiParse(caddy.NewTestController("http", test.inputFastcgiConfig))
272 273 274 275 276 277 278 279 280 281 282 283

		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 {

284 285 286 287 288
			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)
			}

289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
			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)
			}

309 310 311 312
			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 {
313
				if !areDialersEqual(actualFastcgiConfig.dialer, test.expectedFastcgiConfig[j].dialer, t) {
314 315 316 317 318
					t.Errorf("Test %d expected %dth FastCGI dialer to be %v, but got %v",
						i, j, test.expectedFastcgiConfig[j].dialer, actualFastcgiConfig.dialer)
				}
			}

319 320 321 322 323 324 325 326 327
			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)
			}
328 329 330 331 332 333 334 335 336 337

			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)
			}
338 339
		}
	}
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
}

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)
	}
365

366
	return false
367
}