Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Follow up source repo changes #254

Closed
wants to merge 10 commits into from
  •  
  •  
  •  
19 changes: 13 additions & 6 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
"strings"
"time"

"github.com/ghostmonitor/twilio-go/client/form"
"github.com/pkg/errors"
"github.com/twilio/twilio-go/client/form"
)

var alphanumericRegex *regexp.Regexp
Expand Down Expand Up @@ -97,7 +97,10 @@ func (c *Client) doWithErr(req *http.Request) (*http.Response, error) {
if res.StatusCode < 200 || res.StatusCode >= 400 {
err = &TwilioRestError{}
if decodeErr := json.NewDecoder(res.Body).Decode(err); decodeErr != nil {
err = errors.Wrap(decodeErr, "error decoding the response for an HTTP error code: "+strconv.Itoa(res.StatusCode))
err = errors.Wrap(
decodeErr,
"error decoding the response for an HTTP error code: "+strconv.Itoa(res.StatusCode),
)
return nil, err
}

Expand All @@ -114,21 +117,25 @@ func (c *Client) validateCredentials() error {
Status: 400,
Code: 21222,
Message: "Invalid Username. Illegal chars",
MoreInfo: "https://www.twilio.com/docs/errors/21222"}
MoreInfo: "https://www.twilio.com/docs/errors/21222",
}
}
if !alphanumericRegex.MatchString(password) {
return &TwilioRestError{
Status: 400,
Code: 21224,
Message: "Invalid Password. Illegal chars",
MoreInfo: "https://www.twilio.com/docs/errors/21224"}
MoreInfo: "https://www.twilio.com/docs/errors/21224",
}
}
return nil
}

// SendRequest verifies, constructs, and authorizes an HTTP request.
func (c *Client) SendRequest(method string, rawURL string, data url.Values,
headers map[string]interface{}, body ...byte) (*http.Response, error) {
func (c *Client) SendRequest(
method string, rawURL string, data url.Values,
headers map[string]interface{}, body ...byte,
) (*http.Response, error) {

contentType := extractContentTypeHeader(headers)

Expand Down
238 changes: 139 additions & 99 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
"testing"
"time"

twilio "github.com/ghostmonitor/twilio-go/client"
"github.com/stretchr/testify/assert"
twilio "github.com/twilio/twilio-go/client"
)

var mockServer *httptest.Server
Expand All @@ -29,14 +29,17 @@ func NewClient(accountSid string, authToken string) *twilio.Client {
}

func TestMain(m *testing.M) {
mockServer = httptest.NewServer(http.HandlerFunc(
func(writer http.ResponseWriter, request *http.Request) {
d := map[string]interface{}{
"response": "ok",
}
encoder := json.NewEncoder(writer)
_ = encoder.Encode(&d)
}))
mockServer = httptest.NewServer(
http.HandlerFunc(
func(writer http.ResponseWriter, request *http.Request) {
d := map[string]interface{}{
"response": "ok",
}
encoder := json.NewEncoder(writer)
_ = encoder.Encode(&d)
},
),
)
defer mockServer.Close()

testClient = NewClient("user", "pass")
Expand All @@ -50,11 +53,14 @@ func TestClient_SendRequestError(t *testing.T) {
"message":"Bad request",
"more_info":"https://www.twilio.com/docs/errors/20001"
}`
errorServer := httptest.NewServer(http.HandlerFunc(
func(resp http.ResponseWriter, req *http.Request) {
resp.WriteHeader(400)
_, _ = resp.Write([]byte(errorResponse))
}))
errorServer := httptest.NewServer(
http.HandlerFunc(
func(resp http.ResponseWriter, req *http.Request) {
resp.WriteHeader(400)
_, _ = resp.Write([]byte(errorResponse))
},
),
)
defer errorServer.Close()

resp, err := testClient.SendRequest("GET", errorServer.URL, nil, nil) //nolint:bodyclose
Expand All @@ -74,11 +80,14 @@ func TestClient_SendRequestDecodeError(t *testing.T) {
"message":"Bad request",
"more_info":"https://www.twilio.com/docs/errors/20001",
}`
errorServer := httptest.NewServer(http.HandlerFunc(
func(resp http.ResponseWriter, req *http.Request) {
resp.WriteHeader(400)
_, _ = resp.Write([]byte(errorResponse))
}))
errorServer := httptest.NewServer(
http.HandlerFunc(
func(resp http.ResponseWriter, req *http.Request) {
resp.WriteHeader(400)
_, _ = resp.Write([]byte(errorResponse))
},
),
)
defer errorServer.Close()

resp, err := testClient.SendRequest("GET", errorServer.URL, nil, nil) //nolint:bodyclose
Expand All @@ -97,11 +106,14 @@ func TestClient_SendRequestErrorWithDetails(t *testing.T) {
"foo": "bar"
}
}`)
errorServer := httptest.NewServer(http.HandlerFunc(
func(resp http.ResponseWriter, req *http.Request) {
resp.WriteHeader(400)
_, _ = resp.Write(errorResponse)
}))
errorServer := httptest.NewServer(
http.HandlerFunc(
func(resp http.ResponseWriter, req *http.Request) {
resp.WriteHeader(400)
_, _ = resp.Write(errorResponse)
},
),
)
defer errorServer.Close()

resp, err := testClient.SendRequest("GET", errorServer.URL, nil, nil) //nolint:bodyclose
Expand Down Expand Up @@ -139,11 +151,14 @@ func TestClient_SendRequestPasswordError(t *testing.T) {
}

func TestClient_SendRequestWithRedirect(t *testing.T) {
redirectServer := httptest.NewServer(http.HandlerFunc(
func(writer http.ResponseWriter, request *http.Request) {
writer.WriteHeader(307)
_, _ = writer.Write([]byte(`{"redirect_to": "some_place"}`))
}))
redirectServer := httptest.NewServer(
http.HandlerFunc(
func(writer http.ResponseWriter, request *http.Request) {
writer.WriteHeader(307)
_, _ = writer.Write([]byte(`{"redirect_to": "some_place"}`))
},
),
)
defer redirectServer.Close()

resp, _ := testClient.SendRequest("GET", redirectServer.URL, nil, nil) //nolint:bodyclose
Expand All @@ -160,19 +175,22 @@ func TestClient_SendRequestCreatesClient(t *testing.T) {
}

func TestClient_SendRequestWithData(t *testing.T) {
dataServer := httptest.NewServer(http.HandlerFunc(
func(writer http.ResponseWriter, request *http.Request) {
_ = request.ParseForm()
assert.Equal(t, "bar", request.FormValue("foo"))
d := map[string]interface{}{
"response": "ok",
}
encoder := json.NewEncoder(writer)
err := encoder.Encode(&d)
if err != nil {
t.Error(err)
}
}))
dataServer := httptest.NewServer(
http.HandlerFunc(
func(writer http.ResponseWriter, request *http.Request) {
_ = request.ParseForm()
assert.Equal(t, "bar", request.FormValue("foo"))
d := map[string]interface{}{
"response": "ok",
}
encoder := json.NewEncoder(writer)
err := encoder.Encode(&d)
if err != nil {
t.Error(err)
}
},
),
)
defer dataServer.Close()

tests := []string{http.MethodGet, http.MethodPost}
Expand All @@ -191,18 +209,21 @@ func TestClient_SendRequestWithData(t *testing.T) {
}

func TestClient_SendRequestWithHeaders(t *testing.T) {
headerServer := httptest.NewServer(http.HandlerFunc(
func(writer http.ResponseWriter, request *http.Request) {
assert.Equal(t, "bar", request.Header.Get("foo"))
d := map[string]interface{}{
"response": "ok",
}
encoder := json.NewEncoder(writer)
err := encoder.Encode(&d)
if err != nil {
t.Error(err)
}
}))
headerServer := httptest.NewServer(
http.HandlerFunc(
func(writer http.ResponseWriter, request *http.Request) {
assert.Equal(t, "bar", request.Header.Get("foo"))
d := map[string]interface{}{
"response": "ok",
}
encoder := json.NewEncoder(writer)
err := encoder.Encode(&d)
if err != nil {
t.Error(err)
}
},
),
)
defer headerServer.Close()

headers := map[string]interface{}{
Expand All @@ -214,19 +235,22 @@ func TestClient_SendRequestWithHeaders(t *testing.T) {
}

func TestClient_SetTimeoutTimesOut(t *testing.T) {
timeoutServer := httptest.NewServer(http.HandlerFunc(
func(writer http.ResponseWriter, request *http.Request) {
d := map[string]interface{}{
"response": "ok",
}
time.Sleep(100 * time.Microsecond)
encoder := json.NewEncoder(writer)
err := encoder.Encode(&d)
if err != nil {
t.Error(err)
}
writer.WriteHeader(http.StatusOK)
}))
timeoutServer := httptest.NewServer(
http.HandlerFunc(
func(writer http.ResponseWriter, request *http.Request) {
d := map[string]interface{}{
"response": "ok",
}
time.Sleep(100 * time.Microsecond)
encoder := json.NewEncoder(writer)
err := encoder.Encode(&d)
if err != nil {
t.Error(err)
}
writer.WriteHeader(http.StatusOK)
},
),
)
defer timeoutServer.Close()

c := NewClient("user", "pass")
Expand All @@ -236,18 +260,21 @@ func TestClient_SetTimeoutTimesOut(t *testing.T) {
}

func TestClient_SetTimeoutSucceeds(t *testing.T) {
timeoutServer := httptest.NewServer(http.HandlerFunc(
func(writer http.ResponseWriter, request *http.Request) {
d := map[string]interface{}{
"response": "ok",
}
time.Sleep(100 * time.Microsecond)
encoder := json.NewEncoder(writer)
err := encoder.Encode(&d)
if err != nil {
t.Error(err)
}
}))
timeoutServer := httptest.NewServer(
http.HandlerFunc(
func(writer http.ResponseWriter, request *http.Request) {
d := map[string]interface{}{
"response": "ok",
}
time.Sleep(100 * time.Microsecond)
encoder := json.NewEncoder(writer)
err := encoder.Encode(&d)
if err != nil {
t.Error(err)
}
},
),
)
defer timeoutServer.Close()

c := NewClient("user", "pass")
Expand All @@ -268,17 +295,20 @@ func TestClient_SetTimeoutCreatesClient(t *testing.T) {
}

func TestClient_UnicodeResponse(t *testing.T) {
unicodeServer := httptest.NewServer(http.HandlerFunc(
func(writer http.ResponseWriter, request *http.Request) {
d := map[string]interface{}{
"testing-unicode": "Ω≈ç√, 💩",
}
encoder := json.NewEncoder(writer)
err := encoder.Encode(&d)
if err != nil {
t.Error(err)
}
}))
unicodeServer := httptest.NewServer(
http.HandlerFunc(
func(writer http.ResponseWriter, request *http.Request) {
d := map[string]interface{}{
"testing-unicode": "Ω≈ç√, 💩",
}
encoder := json.NewEncoder(writer)
err := encoder.Encode(&d)
if err != nil {
t.Error(err)
}
},
),
)
defer unicodeServer.Close()

c := NewClient("user", "pass")
Expand All @@ -295,10 +325,17 @@ func TestClient_SetAccountSid(t *testing.T) {
}

func TestClient_DefaultUserAgentHeaders(t *testing.T) {
headerServer := httptest.NewServer(http.HandlerFunc(
func(writer http.ResponseWriter, request *http.Request) {
assert.Regexp(t, regexp.MustCompile(`^twilio-go/[0-9.]+(-rc.[0-9])*\s\(\w+\s\w+\)\sgo/\S+$`), request.Header.Get("User-Agent"))
}))
headerServer := httptest.NewServer(
http.HandlerFunc(
func(writer http.ResponseWriter, request *http.Request) {
assert.Regexp(
t,
regexp.MustCompile(`^twilio-go/[0-9.]+(-rc.[0-9])*\s\(\w+\s\w+\)\sgo/\S+$`),
request.Header.Get("User-Agent"),
)
},
),
)

resp, _ := testClient.SendRequest("GET", headerServer.URL, nil, nil)
assert.Equal(t, 200, resp.StatusCode)
Expand All @@ -307,11 +344,14 @@ func TestClient_DefaultUserAgentHeaders(t *testing.T) {
func TestClient_UserAgentExtensionsHeaders(t *testing.T) {
var expectedExtensions = []string{"twilio-run/2.0.0-test", "flex-plugin/3.4.0"}
testClient.UserAgentExtensions = expectedExtensions
headerServer := httptest.NewServer(http.HandlerFunc(
func(writer http.ResponseWriter, request *http.Request) {
var headersList = strings.Split(request.Header.Get("User-Agent"), " ")
assert.Equal(t, headersList[len(headersList)-len(expectedExtensions):], expectedExtensions)
}))
headerServer := httptest.NewServer(
http.HandlerFunc(
func(writer http.ResponseWriter, request *http.Request) {
var headersList = strings.Split(request.Header.Get("User-Agent"), " ")
assert.Equal(t, headersList[len(headersList)-len(expectedExtensions):], expectedExtensions)
},
),
)
resp, _ := testClient.SendRequest("GET", headerServer.URL, nil, nil)
assert.Equal(t, 200, resp.StatusCode)
}
2 changes: 1 addition & 1 deletion client/error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"strings"
"testing"

"github.com/ghostmonitor/twilio-go/client"
assert "github.com/stretchr/testify/require"
"github.com/twilio/twilio-go/client"
)

const (
Expand Down
Loading
Loading