|
| 1 | +package graphql.kickstart.servlet.cache |
| 2 | + |
| 3 | +import graphql.kickstart.execution.input.GraphQLInvocationInput |
| 4 | +import spock.lang.Specification |
| 5 | + |
| 6 | +import javax.servlet.ServletOutputStream |
| 7 | +import javax.servlet.http.HttpServletRequest |
| 8 | +import javax.servlet.http.HttpServletResponse |
| 9 | + |
| 10 | +class CacheReaderTest extends Specification { |
| 11 | + |
| 12 | + def cacheManager |
| 13 | + def invocationInput |
| 14 | + def request |
| 15 | + def response |
| 16 | + def cacheReader |
| 17 | + def cachedResponse |
| 18 | + |
| 19 | + def setup() { |
| 20 | + cacheManager = Mock(GraphQLResponseCacheManager) |
| 21 | + invocationInput = Mock(GraphQLInvocationInput) |
| 22 | + request = Mock(HttpServletRequest) |
| 23 | + response = Mock(HttpServletResponse) |
| 24 | + cacheReader = new CacheReader() |
| 25 | + cachedResponse = Mock(CachedResponse) |
| 26 | + } |
| 27 | + |
| 28 | + def "should return false if no cached response"() { |
| 29 | + given: |
| 30 | + cacheManager.get(request, invocationInput) >> null |
| 31 | + |
| 32 | + when: |
| 33 | + def result = cacheReader.responseFromCache(invocationInput, request, response, cacheManager) |
| 34 | + |
| 35 | + then: |
| 36 | + !result |
| 37 | + } |
| 38 | + |
| 39 | + def "should send error response if cached response is error"() { |
| 40 | + given: |
| 41 | + cachedResponse.isError() >> true |
| 42 | + cachedResponse.getErrorStatusCode() >> 10 |
| 43 | + cachedResponse.getErrorMessage() >> "some error" |
| 44 | + cacheManager.get(request, invocationInput) >> cachedResponse |
| 45 | + |
| 46 | + when: |
| 47 | + def result = cacheReader.responseFromCache(invocationInput, request, response, cacheManager) |
| 48 | + |
| 49 | + then: |
| 50 | + result |
| 51 | + 1 * response.sendError(10, "some error") |
| 52 | + } |
| 53 | + |
| 54 | + def "should send success response if cached response is ok"() { |
| 55 | + given: |
| 56 | + def outputStream = Mock(ServletOutputStream) |
| 57 | + cachedResponse.isError() >> false |
| 58 | + cachedResponse.getContentBytes() >> [ 00, 01, 02 ] |
| 59 | + response.getOutputStream() >> outputStream |
| 60 | + cacheManager.get(request, invocationInput) >> cachedResponse |
| 61 | + |
| 62 | + when: |
| 63 | + def result = cacheReader.responseFromCache(invocationInput, request, response, cacheManager) |
| 64 | + |
| 65 | + then: |
| 66 | + result |
| 67 | + 1 * response.setContentType("application/json;charset=UTF-8") |
| 68 | + 1 * response.setStatus(200) |
| 69 | + 1 * response.setCharacterEncoding("UTF-8") |
| 70 | + 1 * response.setContentLength(3) |
| 71 | + 1 * outputStream.write([ 00, 01, 02 ]) |
| 72 | + } |
| 73 | + |
| 74 | + def "should return false if exception is thrown"() { |
| 75 | + given: |
| 76 | + cacheManager.get(request, invocationInput) >> {throw new RuntimeException()} |
| 77 | + |
| 78 | + when: |
| 79 | + def result = cacheReader.responseFromCache(invocationInput, request, response, cacheManager) |
| 80 | + |
| 81 | + then: |
| 82 | + !result |
| 83 | + } |
| 84 | +} |
0 commit comments