Skip to content

Commit

Permalink
Different port handling tooltip
Browse files Browse the repository at this point in the history
  • Loading branch information
evg4b committed Oct 10, 2022
1 parent 3be2ce3 commit 9f90069
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 22 deletions.
12 changes: 2 additions & 10 deletions internal/urlreplacer/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func wildCardToRegexp(parsedPattern *url.URL) (*regexp.Regexp, int, error) {

result.WriteString(`^(?P<scheme>(http(s?):)?\/\/)?`)

host, port, err := urlx.SplitHostPort(parsedPattern)
host, _, err := urlx.SplitHostPort(parsedPattern)
if err != nil {
return nil, 0, fmt.Errorf("filed to build url glob: %w", err)
}
Expand All @@ -32,15 +32,7 @@ func wildCardToRegexp(parsedPattern *url.URL) (*regexp.Regexp, int, error) {
}
}

if len(port) > 0 {
// TODO: correctly handle default ports
if port == "80" || port == "443" {
fmt.Fprintf(&result, "(:%s)?", port)
} else {
fmt.Fprintf(&result, ":%s", port)
}
}

result.WriteString(`(:\d+)?`)
result.WriteString(`(?P<path>[\/?].*)?$`)

compiledRegexp, err := regexp.Compile(result.String())
Expand Down
20 changes: 10 additions & 10 deletions internal/urlreplacer/helpers_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,61 +18,61 @@ var testCases = []struct {
{
name: "localhost",
url: "localhost",
expectedRegexp: `^(?P<scheme>(http(s?):)?\/\/)?localhost(?P<path>[\/?].*)?$`,
expectedRegexp: `^(?P<scheme>(http(s?):)?\/\/)?localhost(:\d+)?(?P<path>[\/?].*)?$`,
expectedPattern: "${scheme}localhost${path}",
},
{
name: "localhost with port",
url: "localhost:3000",
expectedRegexp: `^(?P<scheme>(http(s?):)?\/\/)?localhost:3000(?P<path>[\/?].*)?$`,
expectedRegexp: `^(?P<scheme>(http(s?):)?\/\/)?localhost(:\d+)?(?P<path>[\/?].*)?$`,
expectedPattern: "${scheme}localhost:3000${path}",
},
{
name: "single star",
url: "*",
expectedRegexp: `^(?P<scheme>(http(s?):)?\/\/)?(?P<part1>.+)(?P<path>[\/?].*)?$`,
expectedRegexp: `^(?P<scheme>(http(s?):)?\/\/)?(?P<part1>.+)(:\d+)?(?P<path>[\/?].*)?$`,
expectedPattern: "${scheme}${part1}${path}",
},
{
name: "single star with port",
url: "*:3001",
expectedRegexp: `^(?P<scheme>(http(s?):)?\/\/)?(?P<part1>.+):3001(?P<path>[\/?].*)?$`,
expectedRegexp: `^(?P<scheme>(http(s?):)?\/\/)?(?P<part1>.+)(:\d+)?(?P<path>[\/?].*)?$`,
expectedPattern: "${scheme}${part1}:3001${path}",
},
{
name: "single star with url part",
url: "demo.*.com",
expectedRegexp: `^(?P<scheme>(http(s?):)?\/\/)?demo\.(?P<part1>.+)\.com(?P<path>[\/?].*)?$`,
expectedRegexp: `^(?P<scheme>(http(s?):)?\/\/)?demo\.(?P<part1>.+)\.com(:\d+)?(?P<path>[\/?].*)?$`,
expectedPattern: "${scheme}demo.${part1}.com${path}",
},
{
name: "single star with url part and port",
url: "api.*.com:3001",
expectedRegexp: `^(?P<scheme>(http(s?):)?\/\/)?api\.(?P<part1>.+)\.com:3001(?P<path>[\/?].*)?$`,
expectedRegexp: `^(?P<scheme>(http(s?):)?\/\/)?api\.(?P<part1>.+)\.com(:\d+)?(?P<path>[\/?].*)?$`,
expectedPattern: "${scheme}api.${part1}.com:3001${path}",
},
{
name: "multiple stars with url part",
url: "*.host.*.com",
expectedRegexp: `^(?P<scheme>(http(s?):)?\/\/)?(?P<part1>.+)\.host\.(?P<part2>.+)\.com(?P<path>[\/?].*)?$`,
expectedRegexp: `^(?P<scheme>(http(s?):)?\/\/)?(?P<part1>.+)\.host\.(?P<part2>.+)\.com(:\d+)?(?P<path>[\/?].*)?$`,
expectedPattern: "${scheme}${part1}.host.${part2}.com${path}",
},
{
name: "multiple stars with url part and port",
url: "*.host.*.com:3001",
expectedRegexp: `^(?P<scheme>(http(s?):)?\/\/)?(?P<part1>.+)\.host\.(?P<part2>.+)\.com:3001(?P<path>[\/?].*)?$`,
expectedRegexp: `^(?P<scheme>(http(s?):)?\/\/)?(?P<part1>.+)\.host\.(?P<part2>.+)\.com(:\d+)?(?P<path>[\/?].*)?$`,
expectedPattern: "${scheme}${part1}.host.${part2}.com:3001${path}",
},
{
name: "host with default http port",
url: "*.api.com:80",
expectedRegexp: `^(?P<scheme>(http(s?):)?\/\/)?(?P<part1>.+)\.api\.com(:80)?(?P<path>[\/?].*)?$`,
expectedRegexp: `^(?P<scheme>(http(s?):)?\/\/)?(?P<part1>.+)\.api\.com(:\d+)?(?P<path>[\/?].*)?$`,
expectedPattern: "${scheme}${part1}.api.com:80${path}",
},
{
name: "host with default https port",
url: "*.api.com:443",
expectedRegexp: `^(?P<scheme>(http(s?):)?\/\/)?(?P<part1>.+)\.api\.com(:443)?(?P<path>[\/?].*)?$`,
expectedRegexp: `^(?P<scheme>(http(s?):)?\/\/)?(?P<part1>.+)\.api\.com(:\d+)?(?P<path>[\/?].*)?$`,
expectedPattern: "${scheme}${part1}.api.com:443${path}",
},
}
Expand Down
9 changes: 7 additions & 2 deletions internal/urlreplacer/replacer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,12 +293,12 @@ func TestReplacerIsMatched(t *testing.T) {
{
name: "matched domain with different port",
url: "premium.my.cc:2900",
expected: false,
expected: true,
},
{
name: "matched domain without port",
url: "standard.my.cc",
expected: false,
expected: true,
},
{
name: "matched domain with same scheme and correct port",
Expand All @@ -315,6 +315,11 @@ func TestReplacerIsMatched(t *testing.T) {
url: "http//test.my.cc:3000",
expected: true,
},
{
name: "not matched to different domain",
url: "http//localhost",
expected: false,
},
}
for _, testsCase := range testsCases {
t.Run(testsCase.name, func(t *testing.T) {
Expand Down

0 comments on commit 9f90069

Please sign in to comment.