matrei commented on code in PR #15478:
URL: https://github.com/apache/grails-core/pull/15478#discussion_r2890728421
##########
grails-testing-support-httpclient/src/main/resources/META-INF/groovy/org.codehaus.groovy.runtime.ExtensionModule:
##########
@@ -0,0 +1,3 @@
+moduleName=grails-testing-support-httpclient
+moduleVersion=1.0
+extensionClasses=org.apache.grails.testing.httpclient.HttpResponseExtensions
Review Comment:
Good catch!
##########
grails-test-examples/cache/src/integration-test/groovy/com/demo/AdvancedCachingIntegrationSpec.groovy:
##########
@@ -67,257 +60,146 @@ class AdvancedCachingIntegrationSpec extends
ContainerGebSpec {
// ========== collection caching integration tests ==========
def "list data is cached via HTTP"() {
- given:
- def client = createClient()
-
when: "fetching list data twice"
- HttpResponse<String> response1 = client.toBlocking().exchange(
- HttpRequest.GET('/advancedCaching/listData?category=books'),
- String
- )
- HttpResponse<String> response2 = client.toBlocking().exchange(
- HttpRequest.GET('/advancedCaching/listData?category=books'),
- String
- )
+ def response1 = http('/advancedCaching/listData?category=books')
+ def response2 = http('/advancedCaching/listData?category=books')
then: "both calls return same data (cached)"
- response1.status == HttpStatus.OK
- response2.status == HttpStatus.OK
- def json1 = new JsonSlurper().parseText(response1.body())
- def json2 = new JsonSlurper().parseText(response2.body())
+ response1.expectStatus(200)
+ response2.expectStatus(200)
+ def json1 = response1.json()
+ def json2 = response2.json()
json1.data == json2.data
json1.data.size() == 3
json1.data[0].startsWith('Item 1 for books')
-
- cleanup:
- client?.close()
}
def "map data is cached via HTTP"() {
- given:
- def client = createClient()
-
when: "fetching map data twice"
- HttpResponse<String> response1 = client.toBlocking().exchange(
- HttpRequest.GET('/advancedCaching/mapData?key=mykey'),
- String
- )
- HttpResponse<String> response2 = client.toBlocking().exchange(
- HttpRequest.GET('/advancedCaching/mapData?key=mykey'),
- String
- )
+ def response1 = http('/advancedCaching/mapData?key=mykey')
+ def response2 = http('/advancedCaching/mapData?key=mykey')
then: "both calls return same data (cached)"
- response1.status == HttpStatus.OK
- response2.status == HttpStatus.OK
- def json1 = new JsonSlurper().parseText(response1.body())
- def json2 = new JsonSlurper().parseText(response2.body())
+ response1.expectStatus(200)
+ response2.expectStatus(200)
+ def json1 = response1.json()
+ def json2 = response2.json()
json1.data == json2.data
json1.data.key == 'mykey'
json1.data.value == 'Value for mykey'
json1.data.nested.a == 1
-
- cleanup:
- client?.close()
}
def "different categories have separate list cache entries via HTTP"() {
- given:
- def client = createClient()
-
when: "fetching different categories"
- HttpResponse<String> booksResponse = client.toBlocking().exchange(
- HttpRequest.GET('/advancedCaching/listData?category=books'),
- String
- )
- HttpResponse<String> moviesResponse = client.toBlocking().exchange(
- HttpRequest.GET('/advancedCaching/listData?category=movies'),
- String
- )
+ def booksResponse = http('/advancedCaching/listData?category=books')
+ def moviesResponse = http('/advancedCaching/listData?category=movies')
then: "different categories return different data"
- booksResponse.status == HttpStatus.OK
- moviesResponse.status == HttpStatus.OK
- def books = new JsonSlurper().parseText(booksResponse.body())
- def movies = new JsonSlurper().parseText(moviesResponse.body())
+ booksResponse.expectStatus(200)
+ moviesResponse.expectStatus(200)
+ def books = booksResponse.json()
+ def movies = moviesResponse.json()
books.data != movies.data
books.data[0].startsWith('Item 1 for books')
movies.data[0].startsWith('Item 1 for movies')
-
- cleanup:
- client?.close()
}
// ========== exception handling integration tests ==========
def "exception is thrown and not cached via HTTP"() {
- given:
- def client = createClient()
-
when: "calling endpoint that throws exception"
- client.toBlocking().exchange(
- HttpRequest.GET('/advancedCaching/dataOrThrow?input=error'),
- String
- )
+ def response = http('/advancedCaching/dataOrThrow?input=error')
then: "exception results in error response"
- thrown(Exception)
-
- cleanup:
- client?.close()
+ response.expectStatus(500)
}
def "successful calls are cached even after exceptions via HTTP"() {
- given:
- def client = createClient()
-
when: "calling with normal value twice"
- HttpResponse<String> response1 = client.toBlocking().exchange(
- HttpRequest.GET('/advancedCaching/dataOrThrow?input=normal'),
- String
- )
- HttpResponse<String> response2 = client.toBlocking().exchange(
- HttpRequest.GET('/advancedCaching/dataOrThrow?input=normal'),
- String
- )
+ def response1 = http('/advancedCaching/dataOrThrow?input=normal')
+ def response2 = http('/advancedCaching/dataOrThrow?input=normal')
then: "second call returns cached result"
- response1.status == HttpStatus.OK
- response2.status == HttpStatus.OK
- def json1 = new JsonSlurper().parseText(response1.body())
- def json2 = new JsonSlurper().parseText(response2.body())
+ response1.expectStatus(200)
+ response2.expectStatus(200)
+ def json1 = response1.json()
+ def json2 = response2.json()
json1.data == json2.data
-
- cleanup:
- client?.close()
}
// ========== eviction integration tests ==========
def "eviction clears list cache via HTTP"() {
given:
- def client = createClient()
// First call to populate cache
- def first = client.toBlocking().exchange(
- HttpRequest.GET('/advancedCaching/listData?category=books'),
- String
- )
- def firstData = new JsonSlurper().parseText(first.body()).data
+ def first = http('/advancedCaching/listData?category=books')
+ def firstData = first.json().data
when: "evicting cache and fetching again"
-
client.toBlocking().exchange(HttpRequest.GET('/advancedCaching/evictListCache'),
String)
- HttpResponse<String> second = client.toBlocking().exchange(
- HttpRequest.GET('/advancedCaching/listData?category=books'),
- String
- )
- def secondData = new JsonSlurper().parseText(second.body()).data
+ http('/advancedCaching/evictListCache')
+ def second = http('/advancedCaching/listData?category=books')
+ def secondData = second.json().data
then: "new data is generated after eviction"
firstData != secondData
-
- cleanup:
- client?.close()
}
def "eviction clears map cache via HTTP"() {
given:
- def client = createClient()
// First call to populate cache
- def first = client.toBlocking().exchange(
- HttpRequest.GET('/advancedCaching/mapData?key=mykey'),
- String
- )
- def firstData = new JsonSlurper().parseText(first.body()).data
+ def first = http('/advancedCaching/mapData?key=mykey')
+ def firstData = first.json().data
when: "evicting cache and fetching again"
-
client.toBlocking().exchange(HttpRequest.GET('/advancedCaching/evictMapCache'),
String)
- HttpResponse<String> second = client.toBlocking().exchange(
- HttpRequest.GET('/advancedCaching/mapData?key=mykey'),
- String
- )
- def secondData = new JsonSlurper().parseText(second.body()).data
+ http('/advancedCaching/evictMapCache')
+ def second = http('/advancedCaching/mapData?key=mykey')
+ def secondData = second.json().data
then: "new data is generated after eviction"
firstData != secondData
-
- cleanup:
- client?.close()
}
// ========== custom key caching integration tests ==========
def "custom key caching works via HTTP"() {
- given:
- def client = createClient()
-
when: "fetching data by custom key twice"
- HttpResponse<String> response1 = client.toBlocking().exchange(
- HttpRequest.GET('/advancedCaching/getDataByKey?key=testkey'),
- String
- )
- HttpResponse<String> response2 = client.toBlocking().exchange(
- HttpRequest.GET('/advancedCaching/getDataByKey?key=testkey'),
- String
- )
+ def response1 = http('/advancedCaching/getDataByKey?key=testkey')
+ def response2 = http('/advancedCaching/getDataByKey?key=testkey')
then: "second call returns cached result"
- response1.status == HttpStatus.OK
- response2.status == HttpStatus.OK
- def json1 = new JsonSlurper().parseText(response1.body())
- def json2 = new JsonSlurper().parseText(response2.body())
- json1.data == json2.data
-
- cleanup:
- client?.close()
+ response1.expectStatus(200)
+ response2.expectStatus(200)
+ response1.json().data == response2.json().data
}
def "eviction by custom key works via HTTP"() {
given:
- def client = createClient()
// First call to populate cache
- def first = client.toBlocking().exchange(
- HttpRequest.GET('/advancedCaching/getDataByKey?key=mykey'),
- String
- )
- def firstData = new JsonSlurper().parseText(first.body()).data
+ def first = http('/advancedCaching/getDataByKey?key=mykey')
+ def firstData = first.json().data
when: "evicting by key and fetching again"
-
client.toBlocking().exchange(HttpRequest.GET('/advancedCaching/evictByKey?key=mykey'),
String)
- HttpResponse<String> second = client.toBlocking().exchange(
- HttpRequest.GET('/advancedCaching/getDataByKey?key=mykey'),
- String
- )
- def secondData = new JsonSlurper().parseText(second.body()).data
+ http('/advancedCaching/evictByKey?key=mykey')
+ def second = http('/advancedCaching/getDataByKey?key=mykey')
+ def secondData = second.json().data
then: "new data is generated after eviction"
firstData != secondData
-
- cleanup:
- client?.close()
}
def "eviction of all custom key cache works via HTTP"() {
given:
- def client = createClient()
// First call to populate cache
- def first = client.toBlocking().exchange(
- HttpRequest.GET('/advancedCaching/getDataByKey?key=anykey'),
- String
- )
- def firstData = new JsonSlurper().parseText(first.body()).data
+ def first = http('/advancedCaching/getDataByKey?key=anykey')
+ def firstData = first.json().data
when: "evicting all and fetching again"
-
client.toBlocking().exchange(HttpRequest.GET('/advancedCaching/evictAllKeyCache'),
String)
- HttpResponse<String> second = client.toBlocking().exchange(
- HttpRequest.GET('/advancedCaching/getDataByKey?key=anykey'),
- String
- )
- def secondData = new JsonSlurper().parseText(second.body()).data
+ http('/advancedCaching/evictAllKeyCache')
+ def second = http('/advancedCaching/getDataByKey?key=anykey',)
+ def secondData = second.json().data
Review Comment:
Good catch!
--
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]