replaceTemplateVariables replaces template variables with placeholder values for URL validation
(rawURL string)
| 47 | |
| 48 | // replaceTemplateVariables replaces template variables with placeholder values for URL validation |
| 49 | func replaceTemplateVariables(rawURL string) string { |
| 50 | // Replace common template variables with valid placeholder values for parsing |
| 51 | templateReplacements := map[string]string{ |
| 52 | "{host}": "example.com", |
| 53 | "{port}": "8080", |
| 54 | "{path}": "api", |
| 55 | "{protocol}": "http", |
| 56 | "{scheme}": "http", |
| 57 | } |
| 58 | |
| 59 | result := rawURL |
| 60 | for placeholder, replacement := range templateReplacements { |
| 61 | result = strings.ReplaceAll(result, placeholder, replacement) |
| 62 | } |
| 63 | |
| 64 | // Handle any remaining {variable} patterns with context-appropriate placeholders |
| 65 | // If the variable is in a port position (after a colon in the host), use a numeric placeholder |
| 66 | // Pattern: :/{variable} or :{variable}/ or :{variable} at end |
| 67 | portRe := regexp.MustCompile(`:(\{[^}]+\})(/|$)`) |
| 68 | result = portRe.ReplaceAllString(result, ":8080$2") |
| 69 | |
| 70 | // Replace any other remaining {variable} patterns with generic placeholder |
| 71 | re := regexp.MustCompile(`\{[^}]+\}`) |
| 72 | result = re.ReplaceAllString(result, "placeholder") |
| 73 | |
| 74 | return result |
| 75 | } |
| 76 | |
| 77 | // IsValidURL checks if a URL is in valid format (basic structure validation) |
| 78 | func IsValidURL(rawURL string) bool { |
no outgoing calls
no test coverage detected
searching dependent graphs…