-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
locohamster
committed
Sep 3, 2024
1 parent
6b233c2
commit 711d1f0
Showing
10 changed files
with
341 additions
and
33 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
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
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,22 @@ | ||
package cfg | ||
|
||
import ( | ||
"regexp" | ||
) | ||
|
||
var ( | ||
matchesRestEndpointRegexp = regexp.MustCompile(`^/v\d+/cas/[A-Za-z0-9_-]+$`) | ||
containsSlashRegex = regexp.MustCompile(`/`) | ||
) | ||
|
||
func caIDInUnsupportedFormat(input string) bool { | ||
return matchesFullRestCasPattern(input) || containSlash(input) | ||
} | ||
|
||
func matchesFullRestCasPattern(input string) bool { | ||
return matchesRestEndpointRegexp.MatchString(input) | ||
} | ||
|
||
func containSlash(input string) bool { | ||
return containsSlashRegex.MatchString(input) | ||
} |
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,97 @@ | ||
package cfg | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestUnsupportedCaIdPatterns(t *testing.T) { | ||
tests := []struct { | ||
input string | ||
looksLikeMistake bool | ||
testCaseDescription string | ||
}{ | ||
{ | ||
input: "/v1/cas/nZ2QfzjGv0U3HmJuVxEsZQ", | ||
looksLikeMistake: true, | ||
testCaseDescription: "api-path-copied-from-the-ncm-ca-details-pasted"}, | ||
{ | ||
input: "/v1/cas/nZ2QfzjGv0U3HmJuVxEsZQ/", | ||
looksLikeMistake: true, | ||
testCaseDescription: "api-path-copied-from-the-ncm-ca-details-pasted-trailing-slash", | ||
}, | ||
{ | ||
input: "v1/cas/nZ2QfzjGv0U3HmJuVxEsZQ", | ||
looksLikeMistake: true, | ||
testCaseDescription: "api-path-copied-from-the-ncm-ca-details-pasted-no-initial-slash", | ||
}, | ||
{ | ||
input: "v1/cas/nZ2QfzjGv0U3HmJuVxEsZQ/", | ||
looksLikeMistake: true, | ||
testCaseDescription: "api-path-copied-from-the-ncm-ca-details-pasted-no-initial-slash-trailing-slash", | ||
}, | ||
{ | ||
input: "/v2/cas/abcdef123456", | ||
looksLikeMistake: true, | ||
testCaseDescription: "api-path-copied-from-the-ncm-ca-details-pasted-new-api-version-test", | ||
}, | ||
{ | ||
input: "/v1/cas/123-456_ABC", | ||
looksLikeMistake: true, | ||
testCaseDescription: "api-path-copied-from-the-ncm-ca-details-pasted-different-ID", | ||
}, | ||
{ | ||
input: "/v1/cas", | ||
looksLikeMistake: true, | ||
testCaseDescription: "api-path-copied-from-the-ncm-ca-details-missing-id", | ||
}, | ||
{ | ||
input: "v1/cas/", | ||
looksLikeMistake: true, | ||
testCaseDescription: "api-path-copied-from-the-ncm-ca-details-missing-id-no-initial-slash", | ||
}, | ||
{ | ||
input: "v1/cas/", | ||
looksLikeMistake: true, | ||
testCaseDescription: "api-path-copied-from-the-ncm-ca-details-missing-id-no-initial-slas-trailing-slash", | ||
}, | ||
{ | ||
input: "https://ncm.domain.example/v1/cas/123-456_ABC", | ||
looksLikeMistake: true, | ||
testCaseDescription: "full-url-provided-https", | ||
}, | ||
{ | ||
input: "http://ncm.domain.example/v1/cas/123-456_ABC", | ||
looksLikeMistake: true, | ||
testCaseDescription: "full-url-provided-http", | ||
}, | ||
{ | ||
input: "ncm.domain.example/v1/cas/123-456_ABC", | ||
looksLikeMistake: true, | ||
testCaseDescription: "full-url-provided-no-protocol", | ||
}, | ||
{ | ||
input: "https://ncm.ca/cas/123-456_ABC", | ||
looksLikeMistake: true, | ||
testCaseDescription: "another-full-url-provided", | ||
}, | ||
{ | ||
input: "", | ||
looksLikeMistake: false, | ||
testCaseDescription: "empty-should-not-happen-as-this-is-mandatory-non-empty-field-in-spec", | ||
}, | ||
{ | ||
input: "nZ2QfzjGv0U3HmJuVxEsZQ", | ||
looksLikeMistake: false, | ||
testCaseDescription: "corrent-caID", | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.testCaseDescription, func(t *testing.T) { | ||
result := caIDInUnsupportedFormat(test.input) | ||
if result != test.looksLikeMistake { | ||
t.Errorf("Description: %s - matchPattern(%q) = %v; looksLikeMistake %v", test.testCaseDescription, test.input, result, test.looksLikeMistake) | ||
} | ||
}) | ||
} | ||
} |
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
Oops, something went wrong.