mirror of
				https://github.com/caddyserver/caddy.git
				synced 2025-11-02 18:47:22 -05:00 
			
		
		
		
	Started adding tests
This commit is contained in:
		
							parent
							
								
									04162aaa79
								
							
						
					
					
						commit
						7b3d005662
					
				@ -66,18 +66,12 @@ func (p *parser) err(kind, msg string) error {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// parseAddress takes a host:port string (val), and returns the host
 | 
					// parseAddress takes a host:port string (val), and returns the host
 | 
				
			||||||
// and port as separate values. If either value that is missing, the
 | 
					// and port as separate values. Empty strings can be returned if
 | 
				
			||||||
// default will be used.4
 | 
					// either is missing.
 | 
				
			||||||
func (p *parser) parseAddress(val string) (string, string) {
 | 
					func parseAddress(val string) (string, string) {
 | 
				
			||||||
	if val == "" {
 | 
					 | 
				
			||||||
		return defaultHost, defaultPort
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	parts := strings.SplitN(val, ":", 3)
 | 
						parts := strings.SplitN(val, ":", 3)
 | 
				
			||||||
	if parts[0] == "" {
 | 
						if len(parts) == 1 {
 | 
				
			||||||
		parts[0] = defaultHost
 | 
							return parts[0], ""
 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	if len(parts) == 1 || parts[1] == "" {
 | 
					 | 
				
			||||||
		return parts[0], defaultPort
 | 
					 | 
				
			||||||
	} else {
 | 
						} else {
 | 
				
			||||||
		return parts[0], parts[1]
 | 
							return parts[0], parts[1]
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
				
			|||||||
@ -23,7 +23,7 @@ func (p *parser) parse() error {
 | 
				
			|||||||
// address expects that the current token is a host:port
 | 
					// address expects that the current token is a host:port
 | 
				
			||||||
// combination.
 | 
					// combination.
 | 
				
			||||||
func (p *parser) address() error {
 | 
					func (p *parser) address() error {
 | 
				
			||||||
	p.cfg.Host, p.cfg.Port = p.parseAddress(p.tkn())
 | 
						p.cfg.Host, p.cfg.Port = parseAddress(p.tkn())
 | 
				
			||||||
	p.lexer.Next()
 | 
						p.lexer.Next()
 | 
				
			||||||
	return nil
 | 
						return nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										33
									
								
								config/parsing_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										33
									
								
								config/parsing_test.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,33 @@
 | 
				
			|||||||
 | 
					package config
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import "testing"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestParseAddress(t *testing.T) {
 | 
				
			||||||
 | 
						type addr struct {
 | 
				
			||||||
 | 
							host string
 | 
				
			||||||
 | 
							port string
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						testCases := []struct {
 | 
				
			||||||
 | 
							input    string
 | 
				
			||||||
 | 
							expected addr
 | 
				
			||||||
 | 
						}{
 | 
				
			||||||
 | 
							{input: "host:port", expected: addr{host: "host", port: "port"}},
 | 
				
			||||||
 | 
							{input: "localhost:1234", expected: addr{host: "localhost", port: "1234"}},
 | 
				
			||||||
 | 
							{input: "127.0.0.1:0", expected: addr{host: "127.0.0.1", port: "0"}},
 | 
				
			||||||
 | 
							{input: "127.0.0.1", expected: addr{host: "127.0.0.1", port: ""}},
 | 
				
			||||||
 | 
							{input: "somedomain.com", expected: addr{host: "somedomain.com", port: ""}},
 | 
				
			||||||
 | 
							{input: "somedomain.com:", expected: addr{host: "somedomain.com", port: ""}},
 | 
				
			||||||
 | 
							{input: ":80", expected: addr{host: "", port: "80"}},
 | 
				
			||||||
 | 
							{input: "localhost:8080", expected: addr{host: "localhost", port: "8080"}},
 | 
				
			||||||
 | 
							{input: "", expected: addr{host: "", port: ""}},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						for _, test := range testCases {
 | 
				
			||||||
 | 
							actualHost, actualPort := parseAddress(test.input)
 | 
				
			||||||
 | 
							if actualHost != test.expected.host {
 | 
				
			||||||
 | 
								t.Errorf("For '%s' expected host '%s' but got '%s'", test.input, test.expected.host, actualHost)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							if actualPort != test.expected.port {
 | 
				
			||||||
 | 
								t.Errorf("For '%s' expected port '%s' but got '%s'", test.input, test.expected.port, actualPort)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user