Skip to content

Commit

Permalink
allow 127.0.0.1 for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vcheung-stripe committed Oct 18, 2024
1 parent fabfd65 commit 1bc08a2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
31 changes: 25 additions & 6 deletions pkg/stripe/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ const DefaultDashboardBaseURL = "https://dashboard.stripe.com"
const qaDashboardBaseURL = "https://qa-dashboard.stripe.com"

// devDashboardBaseURLRegexp is the base URL for dashboard requests in dev
const devDashboardBaseURLRegexp = `http(s)?:\/\/[A-Za-z0-9\-]+manage-mydev.dev.stripe.me`
const devDashboardBaseURLRegexp = `http(s)?:\/\/[A-Za-z0-9\-]+manage-mydev\.dev\.stripe\.me`

// localhostURLRegexp is used in tests
const localhostURLRegexp = `http:\/\/127\.0\.0\.1(:[0-9]+)?`

var errInvalidAPIBaseURL = errors.New("invalid API base URL")
var errInvalidDashboardBaseURL = errors.New("invalid dashboard base URL")
Expand All @@ -37,13 +40,21 @@ func ValidateAPIBaseURL(apiBaseURL string) error {
if apiBaseURL == qaAPIBaseURL {
return nil
}
matched, err := regexp.Match(devAPIBaseURLRegexp, []byte(apiBaseURL))

matchedDev, err := regexp.Match(devAPIBaseURLRegexp, []byte(apiBaseURL))
if err != nil {
return errInvalidAPIBaseURL
}
if !matched {

matchedLocalhost, err := regexp.Match(localhostURLRegexp, []byte(apiBaseURL))
if err != nil {
return errInvalidAPIBaseURL
}

if !matchedDev && !matchedLocalhost {
return errInvalidAPIBaseURL
}

return nil
}

Expand All @@ -55,12 +66,20 @@ func ValidateDashboardBaseURL(dashboardBaseURL string) error {
if dashboardBaseURL == qaDashboardBaseURL {
return nil
}
matched, err := regexp.Match(devDashboardBaseURLRegexp, []byte(dashboardBaseURL))

matchedDev, err := regexp.Match(devDashboardBaseURLRegexp, []byte(dashboardBaseURL))
if err != nil {
return errInvalidDashboardBaseURL
}
if !matched {
return errInvalidDashboardBaseURL

matchedLocalhost, err := regexp.Match(localhostURLRegexp, []byte(dashboardBaseURL))
if err != nil {
return errInvalidAPIBaseURL
}

if !matchedDev && !matchedLocalhost {
return errInvalidAPIBaseURL
}

return nil
}
6 changes: 4 additions & 2 deletions pkg/stripe/url_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ func TestValidateAPIBaseURLWorks(t *testing.T) {
assert.Nil(t, ValidateAPIBaseURL("https://qa-api.stripe.com"))
assert.Nil(t, ValidateAPIBaseURL("http://foo-api-mydev.dev.stripe.me"))
assert.Nil(t, ValidateAPIBaseURL("https://foo-lv5r9y--api-mydev.dev.stripe.me/"))
assert.Nil(t, ValidateAPIBaseURL("http://127.0.0.1"))
assert.Nil(t, ValidateAPIBaseURL("http://127.0.0.1:1337"))

assert.Error(t, ValidateAPIBaseURL("https://example.com"))
assert.Error(t, ValidateAPIBaseURL("https://unknowndomain"))
assert.Error(t, ValidateAPIBaseURL("http://127.0.0.1:1337 "))
assert.Error(t, ValidateAPIBaseURL("localhost"))
assert.Error(t, ValidateAPIBaseURL("anything_else"))
}
Expand All @@ -24,10 +25,11 @@ func TestValidateDashboardBaseURLWorks(t *testing.T) {
assert.Nil(t, ValidateDashboardBaseURL("https://qa-dashboard.stripe.com"))
assert.Nil(t, ValidateDashboardBaseURL("http://foo-manage-mydev.dev.stripe.me"))
assert.Nil(t, ValidateDashboardBaseURL("https://foo-lv5r9y--manage-mydev.dev.stripe.me/"))
assert.Nil(t, ValidateDashboardBaseURL("http://127.0.0.1"))
assert.Nil(t, ValidateDashboardBaseURL("http://127.0.0.1:1337"))

assert.Error(t, ValidateDashboardBaseURL("https://example.com"))
assert.Error(t, ValidateDashboardBaseURL("https://unknowndomain"))
assert.Error(t, ValidateDashboardBaseURL("http://127.0.0.1:1337 "))
assert.Error(t, ValidateDashboardBaseURL("localhost"))
assert.Error(t, ValidateDashboardBaseURL("anything_else"))
}

0 comments on commit 1bc08a2

Please sign in to comment.