Quantcast

integration testing REST services/controllers

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

integration testing REST services/controllers

eugene yusim-2
Hi,

I am new to Grails and this email list. Starting up on a project that will be using Grails with Flex and we're trying to expose some REST services with Grails.

In the Grails User Guide (Chapter 9), there's an example of doing an integration test of a controller like so :

Controller method under test:
def create = {
[book: new Book(params['book']) ]
}

Test:
void testCreateWithJSON() {
def controller = new BookController()
controller.request.contentType = "text/json"
controller.request.content = '{"id":1,"class":"Book","title":"The Stand"}'.getBytes()

def model = controller.create()
assert model.book
assertEquals "The Stand", model.book.title

}



I've tried doing something very similar but when I call controller.create() the "params" map is empty, it's never initialized from the JSON content in the request. So my question is, at what point is that params object initialized? Would it happen before Grails calls create? 

Thanks in advance for any help.
Eugene
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: integration testing REST services/controllers

Tom Nichols
I think this is because 'content' refers to the HTTP request body,
which, if the request content-type is 'text/xml' will not be
automatically parsed as a url-encoded form post.  I believe "params"
refers to the url-encoded form data if the request is a POST, or the
request URL query parameters if it's a GET.  Does that sound right to
anyone else?

See: http://www.grails.org/doc/1.0.x/ref/Servlet%20API/request.html
You should get the parsed content if you reference "request.json" in
your controller.



On Thu, Jul 30, 2009 at 11:59 PM, eugene yusim<[hidden email]> wrote:

> Hi,
>
> I am new to Grails and this email list. Starting up on a project that will
> be using Grails with Flex and we're trying to expose some REST services with
> Grails.
>
> In the Grails User Guide (Chapter 9), there's an example of doing an
> integration test of a controller like so :
>
> Controller method under test:
>
> def create = {
> [book: new Book(params['book']) ]
> }
>
> Test:
>
> void testCreateWithJSON() {
> def controller = new BookController()
>
>   controller.request.contentType = "text/json"
>   controller.request.content = '{"id":1,"class":"Book","title":"The
> Stand"}'.getBytes()
>
> def model = controller.create()
> assert model.book
> assertEquals "The Stand", model.book.title
>
> }
>
> I've tried doing something very similar but when I call controller.create()
> the "params" map is empty, it's never initialized from the JSON content in
> the request. So my question is, at what point is that params object
> initialized? Would it happen before Grails calls create?
>
> Thanks in advance for any help.
> Eugene
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: integration testing REST services/controllers

eugene yusim-2
Well I got it figured out after digging in the Grails code base for a little while. I ended up having to call:

   controller.request.getAttribute("org.codehaus.groovy.grails.WEB_REQUEST").informParameterCreationListeners()

before calling my controller method. This kicks of the JSONParsingParameterCreationListener to initialize the params. It is something that is done in UrlMappingsFilter which is not getting used in the integration test.

On Fri, Jul 31, 2009 at 6:21 AM, Tom Nichols <[hidden email]> wrote:
I think this is because 'content' refers to the HTTP request body,
which, if the request content-type is 'text/xml' will not be
automatically parsed as a url-encoded form post.  I believe "params"
refers to the url-encoded form data if the request is a POST, or the
request URL query parameters if it's a GET.  Does that sound right to
anyone else?

See: http://www.grails.org/doc/1.0.x/ref/Servlet%20API/request.html
You should get the parsed content if you reference "request.json" in
your controller.



On Thu, Jul 30, 2009 at 11:59 PM, eugene yusim<[hidden email]> wrote:
> Hi,
>
> I am new to Grails and this email list. Starting up on a project that will
> be using Grails with Flex and we're trying to expose some REST services with
> Grails.
>
> In the Grails User Guide (Chapter 9), there's an example of doing an
> integration test of a controller like so :
>
> Controller method under test:
>
> def create = {
>       [book: new Book(params['book']) ]
> }
>
> Test:
>
> void testCreateWithJSON() {
>       def controller = new BookController()
>
>       controller.request.contentType = "text/json"
>       controller.request.content = '{"id":1,"class":"Book","title":"The
> Stand"}'.getBytes()
>
>       def model = controller.create()
>       assert model.book
>       assertEquals "The Stand", model.book.title
>
> }
>
> I've tried doing something very similar but when I call controller.create()
> the "params" map is empty, it's never initialized from the JSON content in
> the request. So my question is, at what point is that params object
> initialized? Would it happen before Grails calls create?
>
> Thanks in advance for any help.
> Eugene
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email



Loading...