PageRenderTime 39ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/test/integration/org/grails/jaxrs/itest/JaxrsControllerIntegrationTests.groovy

http://github.com/krasserm/grails-jaxrs
Groovy | 188 lines | 144 code | 24 blank | 20 comment | 0 complexity | eab0bd5c3743bcbbd7375f930f3bf90f MD5 | raw file
  1. package org.grails.jaxrs.itest
  2. /*
  3. * Copyright 2009 - 2011 the original author or authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. import java.util.List;
  18. import groovy.util.GroovyTestCase
  19. import org.codehaus.groovy.grails.commons.ConfigurationHolder
  20. import org.grails.jaxrs.itest.IntegrationTestCase
  21. import org.junit.Test
  22. import static org.junit.Assert.*
  23. /**
  24. * @author Martin Krasser
  25. */
  26. abstract class JaxrsControllerIntegrationTests extends IntegrationTestCase {
  27. // not in grails-app/resources or grails-app/providers,
  28. // so jaxrs classes are added here ...
  29. protected List getJaxrsClasses() {
  30. [TestResource01.class,
  31. TestResource02.class,
  32. TestResource03.class,
  33. TestResource04.class,
  34. TestResource05.class,
  35. TestResource06.class,
  36. CustomRequestEntityReader.class,
  37. CustomResponseEntityWriter.class]
  38. }
  39. @Test
  40. void testGet01() {
  41. sendRequest('/test/01', 'GET')
  42. assertEquals(200, response.status)
  43. assertEquals('test01', response.contentAsString)
  44. assertTrue(response.getHeader('Content-Type').startsWith('text/plain'))
  45. }
  46. @Test
  47. void testPost02() {
  48. sendRequest('/test/02', 'POST', ['Content-Type':'text/plain'], 'hello'.bytes)
  49. assertEquals(200, response.status)
  50. assertEquals('response:hello', response.contentAsString)
  51. assertTrue(response.getHeader('Content-Type').startsWith('text/plain'))
  52. }
  53. @Test
  54. void testPost03() {
  55. sendRequest('/test/03', 'POST', ['Content-Type':'application/json'], '{"class":"TestPerson","age":38,"name":"mike"}'.bytes)
  56. assertEquals(200, response.status)
  57. assertTrue(response.contentAsString.contains('"age":39'))
  58. assertTrue(response.contentAsString.contains('"name":"ekim"'))
  59. assertTrue(response.getHeader('Content-Type').startsWith('application/json'))
  60. }
  61. @Test
  62. void testPost06() {
  63. sendRequest('/test/06', 'POST', ['Content-Type':'application/json'], '{"class":"TestPerson","age":38,"name":"mike"}'.bytes)
  64. assertEquals(200, response.status)
  65. assertTrue(response.contentAsString.contains('<age>39</age>'))
  66. assertTrue(response.contentAsString.contains('<name>ekim</name>'))
  67. assertTrue(response.getHeader('Content-Type').startsWith('application/xml'))
  68. }
  69. @Test
  70. void testRoundtrip04XmlSingle() {
  71. def headers = ['Content-Type':'application/xml', 'Accept':'application/xml']
  72. sendRequest('/test/04/single', 'POST', headers, '<testPerson><name>james</name></testPerson>'.bytes)
  73. assertEquals(200, response.status)
  74. assertTrue(response.contentAsString.contains('<name>semaj</name>'))
  75. assertTrue(response.getHeader('Content-Type').startsWith('application/xml'))
  76. }
  77. @Test
  78. void testRoundtrip04JsonSingle() {
  79. def headers = ['Content-Type':'application/json', 'Accept':'application/json']
  80. sendRequest('/test/04/single', 'POST', headers, '{"class":"TestPerson","age":25,"name":"james"}'.bytes)
  81. assertEquals(200, response.status)
  82. assertTrue(response.contentAsString.contains('"age":26'))
  83. assertTrue(response.contentAsString.contains('"name":"semaj"'))
  84. assertTrue(response.getHeader('Content-Type').startsWith('application/json'))
  85. }
  86. @Test
  87. void testGet04XmlCollectionGeneric() {
  88. sendRequest('/test/04/multi/generic', 'GET', ['Accept':'application/xml'])
  89. assertTrue(response.contentAsString.contains('<list>'))
  90. assertTrue(response.contentAsString.contains('<name>n1</name>'))
  91. assertTrue(response.contentAsString.contains('<name>n2</name>'))
  92. assertTrue(response.getHeader('Content-Type').startsWith('application/xml'))
  93. }
  94. @Test
  95. void testGet04XmlCollectionGenericGenericOnly() {
  96. ConfigurationHolder.config.org.grails.jaxrs.dowriter.require.generic.collections = true
  97. testGet04XmlCollectionGeneric()
  98. }
  99. @Test
  100. void testGet04XmlCollectionRaw() {
  101. sendRequest('/test/04/multi/raw', 'GET', ['Accept':'application/xml'])
  102. assertEquals(200, response.status)
  103. assertTrue(response.contentAsString.contains('<list>'))
  104. assertTrue(response.contentAsString.contains('<name>n1</name>'))
  105. assertTrue(response.contentAsString.contains('<name>n2</name>'))
  106. assertTrue(response.getHeader('Content-Type').startsWith('application/xml'))
  107. }
  108. @Test
  109. void testGet04XmlCollectionRawGenericOnly() {
  110. ConfigurationHolder.config.org.grails.jaxrs.dowriter.require.generic.collections = true
  111. sendRequest('/test/04/multi/raw', 'GET', ['Accept':'application/xml'])
  112. assertEquals(500, response.status)
  113. }
  114. @Test
  115. void testGet04XmlCollectionObject() {
  116. sendRequest('/test/04/multi/object', 'GET', ['Accept':'application/xml'])
  117. assertEquals(200, response.status)
  118. assertTrue(response.contentAsString.contains('<list>'))
  119. assertTrue(response.contentAsString.contains('<name>n1</name>'))
  120. assertTrue(response.contentAsString.contains('<name>n2</name>'))
  121. assertTrue(response.getHeader('Content-Type').startsWith('application/xml'))
  122. }
  123. @Test
  124. void testGet04XmlCollectionObjectGenericOnly() {
  125. ConfigurationHolder.config.org.grails.jaxrs.dowriter.require.generic.collections = true
  126. sendRequest('/test/04/multi/object', 'GET', ['Accept':'application/xml'])
  127. assertEquals(500, response.status)
  128. }
  129. @Test
  130. void testGet04JsonCollectionGeneric() {
  131. sendRequest('/test/04/multi/generic', 'GET', ['Accept':'application/json'])
  132. assertEquals(200, response.status)
  133. assertTrue(response.contentAsString.contains('"name":"n1"'))
  134. assertTrue(response.contentAsString.contains('"name":"n2"'))
  135. assertTrue(response.getHeader('Content-Type').startsWith('application/json'))
  136. }
  137. @Test
  138. void testPost04ReaderDisabled() {
  139. ConfigurationHolder.config.org.grails.jaxrs.doreader.disable = true
  140. sendRequest('/test/04/single', 'POST', ['Content-Type':'application/xml'], '<testPerson><name>james</name></testPerson>'.bytes)
  141. assertEquals(415, response.status)
  142. }
  143. @Test
  144. void testPost04WriterDisabled() {
  145. ConfigurationHolder.config.org.grails.jaxrs.dowriter.disable = true
  146. def headers = ['Content-Type':'application/xml', 'Accept':'application/xml']
  147. sendRequest('/test/04/single', 'POST', headers, '<testPerson><name>james</name></testPerson>'.bytes)
  148. assertEquals(500, response.status)
  149. }
  150. @Test
  151. void testPost04DefaultResponse() {
  152. sendRequest('/test/04/single', 'POST', ['Content-Type':'application/xml'], '<testPerson><name>james</name></testPerson>'.bytes)
  153. assertEquals(200, response.status)
  154. println 'default: ' + response.contentAsString
  155. println 'content: ' + response.getHeader('Content-Type')
  156. }
  157. @Test
  158. void testGet05HtmlResponse() {
  159. sendRequest('/test/05', 'GET')
  160. assertEquals(200, response.status)
  161. assertEquals('<html><body>test05</body></html>', response.contentAsString)
  162. assertTrue(response.getHeader('Content-Type').startsWith('text/html'))
  163. }
  164. }