From e0436ace9fd62744b12b17994dc51eb9e455537d Mon Sep 17 00:00:00 2001 From: Marcin Grzejszczak Date: Fri, 7 Aug 2015 21:38:54 +0200 Subject: [PATCH 1/2] [#115] added possibility to have some predefined regex --- .../accurest/dsl/internal/Common.groovy | 10 ++-- .../dsl/internal/RegexPatterns.groovy | 30 +++++++++++ .../MockMvcSpockMethodBuilderSpec.groovy | 33 ++++++++++++ .../dsl/internal/RegexPatternsSpec.groovy | 53 +++++++++++++++++++ 4 files changed, 122 insertions(+), 4 deletions(-) create mode 100644 accurest-core/src/main/groovy/io/codearte/accurest/dsl/internal/RegexPatterns.groovy create mode 100644 accurest-core/src/test/groovy/io/codearte/accurest/dsl/internal/RegexPatternsSpec.groovy diff --git a/accurest-core/src/main/groovy/io/codearte/accurest/dsl/internal/Common.groovy b/accurest-core/src/main/groovy/io/codearte/accurest/dsl/internal/Common.groovy index 87d509de59..9030d9b9f3 100644 --- a/accurest-core/src/main/groovy/io/codearte/accurest/dsl/internal/Common.groovy +++ b/accurest-core/src/main/groovy/io/codearte/accurest/dsl/internal/Common.groovy @@ -13,6 +13,8 @@ import java.util.regex.Pattern @PackageScope class Common { + @Delegate private final RegexPatterns regexPatterns = new RegexPatterns() + Map convertObjectsToDslProperties(Map body) { return body.collectEntries { Map.Entry 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) { diff --git a/accurest-core/src/main/groovy/io/codearte/accurest/dsl/internal/RegexPatterns.groovy b/accurest-core/src/main/groovy/io/codearte/accurest/dsl/internal/RegexPatterns.groovy new file mode 100644 index 0000000000..210668a2a9 --- /dev/null +++ b/accurest-core/src/main/groovy/io/codearte/accurest/dsl/internal/RegexPatterns.groovy @@ -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() + } +} diff --git a/accurest-core/src/test/groovy/io/codearte/accurest/builder/MockMvcSpockMethodBuilderSpec.groovy b/accurest-core/src/test/groovy/io/codearte/accurest/builder/MockMvcSpockMethodBuilderSpec.groovy index d29c1f87b5..6d2ed106d1 100644 --- a/accurest-core/src/test/groovy/io/codearte/accurest/builder/MockMvcSpockMethodBuilderSpec.groovy +++ b/accurest-core/src/test/groovy/io/codearte/accurest/builder/MockMvcSpockMethodBuilderSpec.groovy @@ -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]+')''') + } } diff --git a/accurest-core/src/test/groovy/io/codearte/accurest/dsl/internal/RegexPatternsSpec.groovy b/accurest-core/src/test/groovy/io/codearte/accurest/dsl/internal/RegexPatternsSpec.groovy new file mode 100644 index 0000000000..38430aa42f --- /dev/null +++ b/accurest-core/src/test/groovy/io/codearte/accurest/dsl/internal/RegexPatternsSpec.groovy @@ -0,0 +1,53 @@ +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://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 + } +} From f3efc6b9dd3deb72071146643bd6bd7017304fbe Mon Sep 17 00:00:00 2001 From: Adam Wojszczyk Date: Mon, 10 Aug 2015 11:49:35 +0200 Subject: [PATCH 2/2] Add localhost assertions to regexPatternsSpec --- .../accurest/dsl/internal/RegexPatternsSpec.groovy | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/accurest-core/src/test/groovy/io/codearte/accurest/dsl/internal/RegexPatternsSpec.groovy b/accurest-core/src/test/groovy/io/codearte/accurest/dsl/internal/RegexPatternsSpec.groovy index 38430aa42f..848ed8511d 100644 --- a/accurest-core/src/test/groovy/io/codearte/accurest/dsl/internal/RegexPatternsSpec.groovy +++ b/accurest-core/src/test/groovy/io/codearte/accurest/dsl/internal/RegexPatternsSpec.groovy @@ -24,11 +24,13 @@ class RegexPatternsSpec extends Specification { expect: shouldMatch == Pattern.compile(regexPatterns.hostname()).matcher(textToMatch).matches() where: - textToMatch || shouldMatch - 'https://asd.com' || true - 'https://asd.com:8080' || true - 'https://asd.com/asd' || false - 'asd.com' || false + 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