Skip to content

Commit 5dab393

Browse files
authored
fix: handle buildUrl errors gracefully (#143)
1 parent 90cc0cc commit 5dab393

File tree

2 files changed

+40
-14
lines changed

2 files changed

+40
-14
lines changed

client/request_handler.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,20 @@ func NewRequestHandler(client BaseClient) *RequestHandler {
2424

2525
func (c *RequestHandler) sendRequest(method string, rawURL string, data url.Values,
2626
headers map[string]interface{}) (*http.Response, error) {
27-
return c.Client.SendRequest(method, c.BuildUrl(rawURL), data, headers)
27+
parsedURL, err := c.BuildUrl(rawURL)
28+
if err != nil {
29+
return nil, err
30+
}
31+
32+
return c.Client.SendRequest(method, parsedURL, data, headers)
2833
}
2934

3035
// BuildUrl builds the target host string taking into account region and edge configurations.
31-
func (c *RequestHandler) BuildUrl(rawURL string) string {
32-
u, _ := url.Parse(rawURL)
36+
func (c *RequestHandler) BuildUrl(rawURL string) (string, error) {
37+
u, err := url.Parse(rawURL)
38+
if err != nil {
39+
return "", err
40+
}
3341

3442
var (
3543
edge = ""
@@ -43,7 +51,7 @@ func (c *RequestHandler) BuildUrl(rawURL string) string {
4351
if len(pieces) >= 3 {
4452
suffix = strings.Join(pieces[len(pieces)-2:], ".")
4553
} else {
46-
return u.String()
54+
return u.String(), nil
4755
}
4856

4957
if len(pieces) == 4 {
@@ -72,7 +80,7 @@ func (c *RequestHandler) BuildUrl(rawURL string) string {
7280
}
7381

7482
u.Host = strings.Join(result, ".")
75-
return u.String()
83+
return u.String(), nil
7684
}
7785

7886
func (c *RequestHandler) Post(path string, bodyData url.Values, headers map[string]interface{}) (*http.Response, error) {

client/request_handler_test.go

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package client_test
22

33
import (
4+
"errors"
5+
"net/url"
46
"testing"
57

68
"github.com/stretchr/testify/assert"
@@ -15,35 +17,51 @@ func NewRequestHandler(accountSid string, authToken string) *client.RequestHandl
1517
func TestRequestHandler_BuildUrlSetRegion(t *testing.T) {
1618
// Region set via url
1719
requestHandler := NewRequestHandler("user", "pass")
18-
assert.Equal(t, "https://api.region.twilio.com", requestHandler.BuildUrl("https://api.region.twilio.com"))
20+
assert.Equal(t, "https://api.region.twilio.com", assertAndGetURL(t, requestHandler, "https://api.region.twilio.com"))
1921

2022
// Region set via requestHandler
2123
requestHandler.Region = "region"
22-
assert.Equal(t, "https://api.region.twilio.com", requestHandler.BuildUrl("https://api.twilio.com"))
23-
assert.Equal(t, "https://api.region.twilio.com", requestHandler.BuildUrl("https://api.urlRegion.twilio.com"))
24+
assert.Equal(t, "https://api.region.twilio.com", assertAndGetURL(t, requestHandler, "https://api.twilio.com"))
25+
assert.Equal(t, "https://api.region.twilio.com", assertAndGetURL(t, requestHandler, "https://api.urlRegion.twilio.com"))
2426
}
2527

2628
func TestRequestHandler_BuildUrlSetEdgeDefaultRegion(t *testing.T) {
2729
// Edge set via client
2830
requestHandler := NewRequestHandler("user", "pass")
2931
requestHandler.Edge = "edge"
30-
assert.Equal(t, "https://api.edge.us1.twilio.com", requestHandler.BuildUrl("https://api.twilio.com"))
32+
assert.Equal(t, "https://api.edge.us1.twilio.com", assertAndGetURL(t, requestHandler, "https://api.twilio.com"))
3133
}
3234

3335
func TestRequestHandler_BuildUrlSetEdgeRegion(t *testing.T) {
3436
//Edge and Region set via url
3537
requestHandler := NewRequestHandler("user", "pass")
36-
assert.Equal(t, "https://api.edge.region.twilio.com", requestHandler.BuildUrl("https://api.edge.region.twilio.com"))
38+
assert.Equal(t, "https://api.edge.region.twilio.com", assertAndGetURL(t, requestHandler, "https://api.edge.region.twilio.com"))
3739

3840
// Edge and Region set via client
3941
requestHandler.Edge = "edge"
40-
assert.Equal(t, "https://api.edge.region.twilio.com", requestHandler.BuildUrl("https://api.region.twilio.com"))
42+
assert.Equal(t, "https://api.edge.region.twilio.com", assertAndGetURL(t, requestHandler, "https://api.region.twilio.com"))
4143
requestHandler.Region = "region"
42-
assert.Equal(t, "https://api.edge.region.twilio.com", requestHandler.BuildUrl("https://api.twilio.com"))
43-
assert.Equal(t, "https://api.edge.region.twilio.com", requestHandler.BuildUrl("https://api.urlEdge.urlRegion.twilio.com"))
44+
assert.Equal(t, "https://api.edge.region.twilio.com", assertAndGetURL(t, requestHandler, "https://api.twilio.com"))
45+
assert.Equal(t, "https://api.edge.region.twilio.com", assertAndGetURL(t, requestHandler, "https://api.urlEdge.urlRegion.twilio.com"))
4446
}
4547

4648
func TestRequestHandler_BuildHostRawHostWithoutPeriods(t *testing.T) {
4749
requestHandler := NewRequestHandler("user", "pass")
48-
assert.Equal(t, "https://prism_twilio:4010", requestHandler.BuildUrl("https://prism_twilio:4010"))
50+
assert.Equal(t, "https://prism_twilio:4010", assertAndGetURL(t, requestHandler, "https://prism_twilio:4010"))
51+
}
52+
53+
func TestRequestHandler_BuildUrlInvalidCTLCharacter(t *testing.T) {
54+
requestHandler := NewRequestHandler("user", "pass")
55+
rawURL := "https://api.twilio.com/ServiceId\n"
56+
parsedURL, err := requestHandler.BuildUrl(rawURL)
57+
58+
expectedErr := url.Error{Op: "parse", URL: rawURL, Err: errors.New("net/url: invalid control character in URL")}
59+
assert.Equal(t, &expectedErr, err)
60+
assert.Equal(t, parsedURL, "")
61+
}
62+
63+
func assertAndGetURL(t *testing.T, requestHandler *client.RequestHandler, rawURL string) string {
64+
parsedURL, err := requestHandler.BuildUrl(rawURL)
65+
assert.Nil(t, err)
66+
return parsedURL
4967
}

0 commit comments

Comments
 (0)