-
-
Notifications
You must be signed in to change notification settings - Fork 376
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(publicip): support custom API url
echoip#https://...
(#2529)
- Loading branch information
Showing
3 changed files
with
181 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package api | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_ParseProvider(t *testing.T) { | ||
t.Parallel() | ||
|
||
testCases := map[string]struct { | ||
s string | ||
provider Provider | ||
errWrapped error | ||
errMessage string | ||
}{ | ||
"empty": { | ||
errWrapped: ErrProviderNotValid, | ||
errMessage: `API name is not valid: "" can only be ` + | ||
`"cloudflare", "ifconfigco", "ip2location", "ipinfo" or a custom echoip# url`, | ||
}, | ||
"invalid": { | ||
s: "xyz", | ||
errWrapped: ErrProviderNotValid, | ||
errMessage: `API name is not valid: "xyz" can only be ` + | ||
`"cloudflare", "ifconfigco", "ip2location", "ipinfo" or a custom echoip# url`, | ||
}, | ||
"ipinfo": { | ||
s: "ipinfo", | ||
provider: IPInfo, | ||
}, | ||
"IpInfo": { | ||
s: "IpInfo", | ||
provider: IPInfo, | ||
}, | ||
"echoip_url_empty": { | ||
s: "echoip#", | ||
errWrapped: ErrCustomURLNotValid, | ||
errMessage: `echoip# custom URL is not valid: "" ` + | ||
`does not match regular expression: ^http(s|):\/\/.+$`, | ||
}, | ||
"echoip_url_invalid": { | ||
s: "echoip#postgres://localhost:3451", | ||
errWrapped: ErrCustomURLNotValid, | ||
errMessage: `echoip# custom URL is not valid: "postgres://localhost:3451" ` + | ||
`does not match regular expression: ^http(s|):\/\/.+$`, | ||
}, | ||
"echoip_url_valid": { | ||
s: "echoip#http://localhost:3451", | ||
provider: Provider("echoip#http://localhost:3451"), | ||
}, | ||
} | ||
|
||
for name, testCase := range testCases { | ||
t.Run(name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
provider, err := ParseProvider(testCase.s) | ||
|
||
assert.Equal(t, testCase.provider, provider) | ||
assert.ErrorIs(t, err, testCase.errWrapped) | ||
if testCase.errWrapped != nil { | ||
assert.EqualError(t, err, testCase.errMessage) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters