jdaugherty commented on code in PR #15505:
URL: https://github.com/apache/grails-core/pull/15505#discussion_r2935506349


##########
grails-test-examples/app1/src/integration-test/groovy/functionaltests/interceptors/InterceptorAdvancedMatchingSpec.groovy:
##########
@@ -36,233 +34,201 @@ import grails.testing.mixin.integration.Integration
  * - Combined matching criteria
  */
 @Integration
-class InterceptorAdvancedMatchingSpec extends Specification {
-
-    @Shared
-    HttpClient client
+class InterceptorAdvancedMatchingSpec extends Specification implements 
HttpClientSupport {
 
     def setup() {
-        client = client ?: HttpClient.create(new 
URL("http://localhost:$serverPort";))
         // Reset interceptor tracking before each test
-        client.toBlocking().exchange(
-            HttpRequest.GET('/api/advancedMatching/reset'),
-            Map
-        )
-    }
-
-    def cleanupSpec() {
-        client.close()
+        http('/api/advancedMatching/reset')
     }
 
     // ========== Namespace Matching Tests ==========
 
     def "test namespace interceptor matches controller in api namespace"() {
         when:
-        def response = client.toBlocking().exchange(
-            HttpRequest.GET('/api/advancedMatching/index'),
-            Map
-        )
+        def response = http('/api/advancedMatching/index')
 
         then:
-        response.status.code == 200
-        response.body().namespace == 'api'
-        response.body().interceptors.contains('namespace:api')
+        response.expectStatus(200)
+        with(response.json()) {
+            namespace == 'api'
+            interceptors.contains('namespace:api')
+        }
     }
 
     def "test namespace interceptor matches all actions in namespace"() {
         when:
-        def response = client.toBlocking().exchange(
-            HttpRequest.GET('/api/advancedMatching/list'),
-            Map
-        )
+        def response = http('/api/advancedMatching/list')
 
         then:
-        response.status.code == 200
-        response.body().action == 'list'
-        response.body().interceptors.contains('namespace:api')
+        response.expectStatus(200)
+        with(response.json()) {
+            action == 'list'
+            interceptors.contains('namespace:api')
+        }
     }
 
     // ========== HTTP Method Matching Tests ==========
 
     def "test method interceptor matches POST requests"() {
         when:
-        def response = client.toBlocking().exchange(
-            HttpRequest.POST('/api/advancedMatching/save', [:]),
-            Map
-        )
+        def response = httpPostJson('/api/advancedMatching/save', [:])
 
         then:
-        response.status.code == 200
-        response.body().method == 'POST'
-        response.body().interceptors.contains('method:POST')
+        response.expectStatus(200)
+        with(response.json()) {
+            method == 'POST'
+            interceptors.contains('method:POST')
+        }
     }
 
     def "test method interceptor does not match GET requests"() {
         when:
-        def response = client.toBlocking().exchange(
-            HttpRequest.GET('/api/advancedMatching/list'),
-            Map
-        )
+        def response = http('/api/advancedMatching/list')
 
         then:
-        response.status.code == 200
-        !response.body().interceptors.contains('method:POST')
+        response.expectStatus(200)
+        !response.json().interceptors.contains('method:POST')
     }
 
     def "test method interceptor matches PUT requests as POST variant"() {
         when:
-        def response = client.toBlocking().exchange(
-            HttpRequest.PUT('/api/advancedMatching/update', [:]),
-            Map
-        )
+        def response = httpPutJson('/api/advancedMatching/update', [:])
 
         then:
-        response.status.code == 200
+        response.expectStatus(200)
         // PUT is not POST, so interceptor should not match
-        !response.body().interceptors.contains('method:POST')
+        !response.json().interceptors.contains('method:POST')
     }
 
     // ========== Action Exclusion Tests ==========
 
     def "test excludes interceptor does not match excluded index action"() {
         when:
-        def response = client.toBlocking().exchange(
-            HttpRequest.GET('/api/advancedMatching/index'),
-            Map
-        )
+        def response = http('/api/advancedMatching/index')
 
         then:
-        response.status.code == 200
-        response.body().action == 'index'
-        !response.body().interceptors.contains('excludes:index,reset')
+        response.expectStatus(200)
+        with(response.json()) {
+            action == 'index'
+            !interceptors.contains('excludes:index,reset')
+        }
     }
 
     def "test excludes interceptor matches non-excluded actions"() {
         when:
-        def response = client.toBlocking().exchange(
-            HttpRequest.GET('/api/advancedMatching/list'),
-            Map
-        )
+        def response = http('/api/advancedMatching/list')
 
         then:
-        response.status.code == 200
-        response.body().action == 'list'
-        response.body().interceptors.contains('excludes:index,reset')
+        response.expectStatus(200)
+        with(response.json()) {
+            action == 'list'
+            interceptors.contains('excludes:index,reset')
+        }
     }
 
     def "test excludes interceptor matches show action"() {
         when:
-        def response = client.toBlocking().exchange(
-            HttpRequest.GET('/api/advancedMatching/show/123'),
-            Map
-        )
+        def response = http('/api/advancedMatching/show/123')
 
         then:
-        response.status.code == 200
-        response.body().action == 'show'
-        response.body().interceptors.contains('excludes:index,reset')
+        response.expectStatus(200)
+        with(response.json()) {
+            action == 'show'
+            interceptors.contains('excludes:index,reset')
+        }
     }
 
     // ========== Multiple Rules (OR) Tests ==========
 
     def "test multiple rules interceptor matches show action"() {
         when:
-        def response = client.toBlocking().exchange(
-            HttpRequest.GET('/api/advancedMatching/show/1'),
-            Map
-        )
+        def response = http('/api/advancedMatching/show/1')
 
         then:
-        response.status.code == 200
-        response.body().action == 'show'
-        response.body().interceptors.contains('multiRule:show|list')
+        response.expectStatus(200)
+        with(response.json()) {
+            action == 'show'
+            interceptors.contains('multiRule:show|list')
+        }
     }
 
     def "test multiple rules interceptor matches list action"() {
         when:
-        def response = client.toBlocking().exchange(
-            HttpRequest.GET('/api/advancedMatching/list'),
-            Map
-        )
+        def response = http('/api/advancedMatching/list')
 
         then:
-        response.status.code == 200
-        response.body().action == 'list'
-        response.body().interceptors.contains('multiRule:show|list')
+        response.expectStatus(200)
+        with(response.json()) {
+            action == 'list'
+            interceptors.contains('multiRule:show|list')
+        }
     }
 
     def "test multiple rules interceptor does not match create action"() {
         when:
-        def response = client.toBlocking().exchange(
-            HttpRequest.GET('/api/advancedMatching/create'),
-            Map
-        )
+        def response = http('/api/advancedMatching/create')
 
         then:
-        response.status.code == 200
-        response.body().action == 'create'
-        !response.body().interceptors.contains('multiRule:show|list')
+        response.expectStatus(200)
+        with(response.json()) {
+            action == 'create'
+            !interceptors.contains('multiRule:show|list')
+        }
     }
 
     // ========== Combined Matching Tests ==========
 
     def "test multiple interceptors can match same request"() {
         when: "accessing list action in api namespace"
-        def response = client.toBlocking().exchange(
-            HttpRequest.GET('/api/advancedMatching/list'),
-            Map
-        )
+        def response = http('/api/advancedMatching/list')
 
         then: "namespace, excludes, and multiRule interceptors all match"
-        response.status.code == 200
-        response.body().interceptors.contains('namespace:api')
-        response.body().interceptors.contains('excludes:index,reset')
-        response.body().interceptors.contains('multiRule:show|list')
+        response.expectStatus(200)
+        with(response.json()) {
+            interceptors.contains('namespace:api')
+            interceptors.contains('excludes:index,reset')
+            interceptors.contains('multiRule:show|list')
+        }
     }
 
     def "test POST to save triggers namespace and method interceptors"() {
         when:
-        def response = client.toBlocking().exchange(
-            HttpRequest.POST('/api/advancedMatching/save', [:]),
-            Map
-        )
+        def response = httpPostJson('/api/advancedMatching/save', [:])
 
         then:
-        response.status.code == 200
-        response.body().interceptors.contains('namespace:api')
-        response.body().interceptors.contains('method:POST')
-        response.body().interceptors.contains('excludes:index,reset')
-        // save is not in show|list, so multiRule should not match
-        !response.body().interceptors.contains('multiRule:show|list')
+        response.expectStatus(200)
+        with(response.json()) {
+            interceptors.contains('namespace:api')
+            interceptors.contains('method:POST')
+            interceptors.contains('excludes:index,reset')
+            // save is not in show|list, so multiRule should not match
+            !interceptors.contains('multiRule:show|list')
+        }
     }
 
     def "test index action only matches namespace interceptor"() {
         when:
-        def response = client.toBlocking().exchange(
-            HttpRequest.GET('/api/advancedMatching/index'),
-            Map
-        )
+        def response = http('/api/advancedMatching/index')
 
         then: "only namespace interceptor matches (others exclude index)"
-        response.status.code == 200
-        response.body().interceptors.contains('namespace:api')
-        !response.body().interceptors.contains('excludes:index,reset')
-        !response.body().interceptors.contains('multiRule:show|list')
-        !response.body().interceptors.contains('method:POST')
+        response.expectStatus(200)
+        with(response.json()) {

Review Comment:
   I never knew ... 
https://spockframework.org/spock/docs/2.4/all_in_one.html#_using_with_for_expectations



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to