From ff383928c7e8424f22ed652d8d774b3418e5c68c Mon Sep 17 00:00:00 2001 From: Cristian Oliveira Date: Wed, 12 Jun 2024 17:34:13 +0200 Subject: [PATCH] fix: unandled error while parsing URL (#160) * fix: unandled error while parsing URL * adds test --- .version | 2 +- proxy/service.go | 6 +++++- proxy/service_test.go | 6 ++++++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/.version b/.version index 7becae1..5aff472 100644 --- a/.version +++ b/.version @@ -1 +1 @@ -v0.3.2 +v0.4.1 diff --git a/proxy/service.go b/proxy/service.go index d53597f..e6a045a 100644 --- a/proxy/service.go +++ b/proxy/service.go @@ -24,8 +24,12 @@ func NewService(name string, rawURL string) (Service, error) { } url, err := url.ParseRequestURI(rawURL) + if err != nil { + return Service{}, fmt.Errorf("Error parsing URL '%v': %v", rawURL, err) + } + isInvalidHostname := len(url.Hostname()) == 0 || strings.Contains(url.Hostname(), ":") - if err != nil || isInvalidHostname { + if isInvalidHostname { return Service{}, fmt.Errorf("URL '%v' is invalid, example of valid URL 'http://example.com:8080'", rawURL) } diff --git a/proxy/service_test.go b/proxy/service_test.go index 6a88ae7..4e20030 100644 --- a/proxy/service_test.go +++ b/proxy/service_test.go @@ -54,6 +54,12 @@ func TestNewService(t *testing.T) { serviceURL: "http://localhost:8080", expectError: true, }, + { + title: "a service containing an invalid URL is invalid", + serviceName: "ergoproxy", + serviceURL: "http:///localhost:3000\n", + expectError: true, + }, } for _, tc := range testCases {