Merge pull request #117 from Codearte/issues/115-predefined-regex
[#115] added possibility to have some predefined regex
This commit is contained in:
@@ -13,6 +13,8 @@ import java.util.regex.Pattern
|
||||
@PackageScope
|
||||
class Common {
|
||||
|
||||
@Delegate private final RegexPatterns regexPatterns = new RegexPatterns()
|
||||
|
||||
Map<String, DslProperty> convertObjectsToDslProperties(Map<String, Object> body) {
|
||||
return body.collectEntries {
|
||||
Map.Entry<String, Object> entry ->
|
||||
@@ -80,12 +82,12 @@ class Common {
|
||||
return new ServerDslProperty(serverValue)
|
||||
}
|
||||
|
||||
void assertThatSidesMatch(Pattern firstSide, String secondSide) {
|
||||
assert secondSide ==~ firstSide
|
||||
void assertThatSidesMatch(Pattern pattern, String value) {
|
||||
assert value ==~ pattern
|
||||
}
|
||||
|
||||
void assertThatSidesMatch(String firstSide, Pattern secondSide) {
|
||||
assert firstSide ==~ secondSide
|
||||
void assertThatSidesMatch(String value, Pattern pattern) {
|
||||
assert value ==~ pattern
|
||||
}
|
||||
|
||||
void assertThatSidesMatch(MatchingStrategy firstSide, MatchingStrategy secondSide) {
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
package io.codearte.accurest.dsl.internal
|
||||
|
||||
import groovy.transform.CompileStatic
|
||||
|
||||
import java.util.regex.Pattern
|
||||
|
||||
@CompileStatic
|
||||
class RegexPatterns {
|
||||
|
||||
private static final Pattern IP_ADDRESS = Pattern.compile('([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])');
|
||||
private static final Pattern HOSTNAME_PATTERN = Pattern.compile('((http[s]?|ftp):\\/)\\/?([^:\\/\\s]+)(:[0-9]{1,5})?');
|
||||
private static final Pattern EMAIL = Pattern.compile('[_]*([a-z0-9]+(\\.|_*)?)+@([a-z][a-z0-9-]+(\\.|-*\\.))+[a-z]{2,6}');
|
||||
private static final Pattern URL = Pattern.compile('((www\\.|(http|https|ftp|news|file)+\\:\\/\\/)[_.a-z0-9-]+\\.[a-z0-9\\/_:@=.+?,##%&~-]*[^.|\\\'|\\# |!|\\(|?|,| |>|<|;|\\)])');
|
||||
|
||||
String ipAddress() {
|
||||
return IP_ADDRESS.pattern()
|
||||
}
|
||||
|
||||
String hostname() {
|
||||
return HOSTNAME_PATTERN.pattern()
|
||||
}
|
||||
|
||||
String email() {
|
||||
return EMAIL.pattern()
|
||||
}
|
||||
|
||||
String url() {
|
||||
return URL.pattern()
|
||||
}
|
||||
}
|
||||
@@ -361,4 +361,37 @@ class MockMvcSpockMethodBuilderSpec extends Specification {
|
||||
then:
|
||||
spockTest.contains('''response.header('Location') ==~ java.util.regex.Pattern.compile('http://localhost/partners/[0-9]+/users/[0-9]+')''')
|
||||
}
|
||||
|
||||
@Issue('115')
|
||||
def "should generate regex with helper method"() {
|
||||
given:
|
||||
GroovyDsl contractDsl = GroovyDsl.make {
|
||||
request {
|
||||
method 'POST'
|
||||
url $(client(regex('/partners/[0-9]+/users')), server('/partners/1000/users'))
|
||||
headers { header 'Content-Type': 'application/json' }
|
||||
body(
|
||||
first_name: 'John',
|
||||
last_name: 'Smith',
|
||||
personal_id: '12345678901',
|
||||
phone_number: '500500500',
|
||||
invitation_token: '00fec7141bb94793bfe7ae1d0f39bda0',
|
||||
password: 'john'
|
||||
)
|
||||
}
|
||||
response {
|
||||
status 201
|
||||
headers {
|
||||
header 'Location': $(client('http://localhost/partners/1000/users/1001'), server(regex("^${hostname()}/partners/[0-9]+/users/[0-9]+")))
|
||||
}
|
||||
}
|
||||
}
|
||||
MockMvcSpockMethodBodyBuilder builder = new MockMvcSpockMethodBodyBuilder(contractDsl)
|
||||
BlockBuilder blockBuilder = new BlockBuilder(" ")
|
||||
when:
|
||||
builder.appendTo(blockBuilder)
|
||||
def spockTest = blockBuilder.toString()
|
||||
then:
|
||||
spockTest.contains('''response.header('Location') ==~ java.util.regex.Pattern.compile('^((http[s]?|ftp):\\/)\\/?([^:\\/\\s]+)(:[0-9]{1,5})?/partners/[0-9]+/users/[0-9]+')''')
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
package io.codearte.accurest.dsl.internal
|
||||
|
||||
import spock.lang.Specification
|
||||
import spock.lang.Unroll
|
||||
|
||||
import java.util.regex.Pattern
|
||||
|
||||
class RegexPatternsSpec extends Specification {
|
||||
|
||||
RegexPatterns regexPatterns = new RegexPatterns()
|
||||
|
||||
@Unroll
|
||||
def "should generate a regex for ip address [#textToMatch] that is a match [#shouldMatch]"() {
|
||||
expect:
|
||||
shouldMatch == Pattern.compile(regexPatterns.ipAddress()).matcher(textToMatch).matches()
|
||||
where:
|
||||
textToMatch || shouldMatch
|
||||
'123.123.123.123' || true
|
||||
'a.b.' || false
|
||||
}
|
||||
|
||||
@Unroll
|
||||
def "should generate a regex for hostname [#textToMatch] that is a match [#shouldMatch]"() {
|
||||
expect:
|
||||
shouldMatch == Pattern.compile(regexPatterns.hostname()).matcher(textToMatch).matches()
|
||||
where:
|
||||
textToMatch || shouldMatch
|
||||
'https://asd.com' || true
|
||||
'https://asd.com:8080' || true
|
||||
'https://localhost' || true
|
||||
'https://localhost:8080' || true
|
||||
'https://asd.com/asd' || false
|
||||
'asd.com' || false
|
||||
}
|
||||
|
||||
@Unroll
|
||||
def "should generate a regex for email [#textToMatch] that is a match [#shouldMatch]"() {
|
||||
expect:
|
||||
shouldMatch == Pattern.compile(regexPatterns.email()).matcher(textToMatch).matches()
|
||||
where:
|
||||
textToMatch || shouldMatch
|
||||
'asd@asd.com' || true
|
||||
'a.b.' || false
|
||||
}
|
||||
|
||||
@Unroll
|
||||
def "should generate a regex for url [#textToMatch] that is a match [#shouldMatch]"() {
|
||||
expect:
|
||||
shouldMatch == Pattern.compile(regexPatterns.url()).matcher(textToMatch).matches()
|
||||
where:
|
||||
textToMatch || shouldMatch
|
||||
'ftp://asd.com:9090/asd/a?a=b' || true
|
||||
'a.b.' || false
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user