11package client_test
22
33import (
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
1517func 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
2628func 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
3335func 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
4648func 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