mirror of
				https://github.com/caddyserver/caddy.git
				synced 2025-10-30 18:22:49 -04:00 
			
		
		
		
	- Server types no longer need to store their own contexts; they are stored on the caddy.Instance, which means each context will be properly GC'ed when the instance is stopped. Server types should use type assertions to convert from caddy.Context to their concrete context type when they need to use it. - Pass the entire context into httpserver.GetConfig instead of only the Key field. - caddy.NewTestController now requires a server type string so it can create a controller with the proper concrete context associated with that server type. Tests still need more attention so that we can test the proper creation of startup functions, etc.
		
			
				
	
	
		
			142 lines
		
	
	
		
			3.1 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			142 lines
		
	
	
		
			3.1 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package proxy
 | |
| 
 | |
| import (
 | |
| 	"reflect"
 | |
| 	"testing"
 | |
| 
 | |
| 	"github.com/mholt/caddy"
 | |
| 	"github.com/mholt/caddy/caddyhttp/httpserver"
 | |
| )
 | |
| 
 | |
| func TestSetup(t *testing.T) {
 | |
| 	for i, test := range []struct {
 | |
| 		input         string
 | |
| 		shouldErr     bool
 | |
| 		expectedHosts map[string]struct{}
 | |
| 	}{
 | |
| 		// test #0 test usual to destination still works normally
 | |
| 		{
 | |
| 			"proxy / localhost:80",
 | |
| 			false,
 | |
| 			map[string]struct{}{
 | |
| 				"http://localhost:80": {},
 | |
| 			},
 | |
| 		},
 | |
| 
 | |
| 		// test #1 test usual to destination with port range
 | |
| 		{
 | |
| 			"proxy / localhost:8080-8082",
 | |
| 			false,
 | |
| 			map[string]struct{}{
 | |
| 				"http://localhost:8080": {},
 | |
| 				"http://localhost:8081": {},
 | |
| 				"http://localhost:8082": {},
 | |
| 			},
 | |
| 		},
 | |
| 
 | |
| 		// test #2 test upstream directive
 | |
| 		{
 | |
| 			"proxy / {\n upstream localhost:8080\n}",
 | |
| 			false,
 | |
| 			map[string]struct{}{
 | |
| 				"http://localhost:8080": {},
 | |
| 			},
 | |
| 		},
 | |
| 
 | |
| 		// test #3 test upstream directive with port range
 | |
| 		{
 | |
| 			"proxy / {\n upstream localhost:8080-8081\n}",
 | |
| 			false,
 | |
| 			map[string]struct{}{
 | |
| 				"http://localhost:8080": {},
 | |
| 				"http://localhost:8081": {},
 | |
| 			},
 | |
| 		},
 | |
| 
 | |
| 		// test #4 test to destination with upstream directive
 | |
| 		{
 | |
| 			"proxy / localhost:8080 {\n upstream localhost:8081-8082\n}",
 | |
| 			false,
 | |
| 			map[string]struct{}{
 | |
| 				"http://localhost:8080": {},
 | |
| 				"http://localhost:8081": {},
 | |
| 				"http://localhost:8082": {},
 | |
| 			},
 | |
| 		},
 | |
| 
 | |
| 		// test #5 test with unix sockets
 | |
| 		{
 | |
| 			"proxy / localhost:8080 {\n upstream unix:/var/foo\n}",
 | |
| 			false,
 | |
| 			map[string]struct{}{
 | |
| 				"http://localhost:8080": {},
 | |
| 				"unix:/var/foo":         {},
 | |
| 			},
 | |
| 		},
 | |
| 
 | |
| 		// test #6 test fail on malformed port range
 | |
| 		{
 | |
| 			"proxy / localhost:8090-8080",
 | |
| 			true,
 | |
| 			nil,
 | |
| 		},
 | |
| 
 | |
| 		// test #7 test fail on malformed port range 2
 | |
| 		{
 | |
| 			"proxy / {\n upstream localhost:80-A\n}",
 | |
| 			true,
 | |
| 			nil,
 | |
| 		},
 | |
| 
 | |
| 		// test #8 test upstreams without ports work correctly
 | |
| 		{
 | |
| 			"proxy / http://localhost {\n upstream testendpoint\n}",
 | |
| 			false,
 | |
| 			map[string]struct{}{
 | |
| 				"http://localhost":    {},
 | |
| 				"http://testendpoint": {},
 | |
| 			},
 | |
| 		},
 | |
| 
 | |
| 		// test #9 test several upstream directives
 | |
| 		{
 | |
| 			"proxy / localhost:8080 {\n upstream localhost:8081-8082\n upstream localhost:8083-8085\n}",
 | |
| 			false,
 | |
| 			map[string]struct{}{
 | |
| 				"http://localhost:8080": {},
 | |
| 				"http://localhost:8081": {},
 | |
| 				"http://localhost:8082": {},
 | |
| 				"http://localhost:8083": {},
 | |
| 				"http://localhost:8084": {},
 | |
| 				"http://localhost:8085": {},
 | |
| 			},
 | |
| 		},
 | |
| 	} {
 | |
| 		c := caddy.NewTestController("http", test.input)
 | |
| 		err := setup(c)
 | |
| 		if err != nil && !test.shouldErr {
 | |
| 			t.Errorf("Test case #%d received an error of %v", i, err)
 | |
| 		} else if test.shouldErr {
 | |
| 			continue
 | |
| 		}
 | |
| 
 | |
| 		mids := httpserver.GetConfig(c).Middleware()
 | |
| 		mid := mids[len(mids)-1]
 | |
| 
 | |
| 		upstreams := mid(nil).(Proxy).Upstreams
 | |
| 		for _, upstream := range upstreams {
 | |
| 			val := reflect.ValueOf(upstream).Elem()
 | |
| 			hosts := val.FieldByName("Hosts").Interface().(HostPool)
 | |
| 			if len(hosts) != len(test.expectedHosts) {
 | |
| 				t.Errorf("Test case #%d expected %d hosts but received %d", i, len(test.expectedHosts), len(hosts))
 | |
| 			} else {
 | |
| 				for _, host := range hosts {
 | |
| 					if _, found := test.expectedHosts[host.Name]; !found {
 | |
| 						t.Errorf("Test case #%d has an unexpected host %s", i, host.Name)
 | |
| 					}
 | |
| 				}
 | |
| 			}
 | |
| 		}
 | |
| 	}
 | |
| }
 |