Grails Canoo Webtest plugin: invoke() fails to send JSON data in POST request - webtest

I'm evaluating Canoo Webtest for automated integration/functional testing as a Grails plugin.
I have a REST app which I'm attempting to test, but Canoo Webtest doesn't seem to properly send the JSON data in POST request. My test code is like below
invoke( description:"Add a product to shopping cart",
url:'shoppingCart/add', method:'POST',
content:'{"class":"shop.service.Product", "name":"A product", "description":"Manufactured by X", "price":99.9}'
//contentFile: '../product.json'
)
The request body is empty no matter whether I use contentFile approach or inline the data as content attribute. The test report shows the data as being sent correctly, but error page shows an error stating 'JSONException: Missing value. at character 0 of '. JSON data as a response of GET request is coming back fine.
I have tested the same functionality with curl and it works perfectly fine. Is there something I'm missing in the Canoo Webtest setup?
Thanks.
EDIT: I'm using Grails 1.3.7 in case that makes any difference

OK. I started to just experiment with the invoke() and seems that setting attribute soapAction value to true does the trick.
I have no clue why this works. The documentation of invoke() for the attribute says
soapAction
Required? no
If the HTTP method is POST and is in fact a SOAP POST request,
this allows the SOAP Action header to be set. Ignored for GETs.
Apparently it sets some needed request header. Haven't checked which one.
Thank you for reading :)
Cannot yet mark this answer as the correct one, but will do so when it's possible.

Related

Http Post request in Microsoft Bot Composer

I have tried below but http request got fails is there I missing something can anyone guide me please. In postman by using qnaId in the body I am getting the correspond queastion and answer in json response. The same I have tried in composer with Http request but it fails.
{
"qnaId" : "1164"
}
enter image description here
enter image description here
The same I tried in Postman and successfully got the response.
enter image description here
Thanks....
I think the issue is that $Qna is not a valid memory variable. Per documentation, you need to set this in either conversation, dialog, turn or user scopes. I had missed the section in the post here where the response was being set to $Qna. That's not valid. So, it needs something like turn.Qna or dialog.Qna.
Additionally, the use of $ should be used only for string interpolation, not in setting the variable name. turn.Qna would set it and you would utilize it in a string as: "You said: ${turn.Qna}"

Grails Spring Security only responds with ajax/JSON on login fail

I'm using spring security in a grails app and when a login fails it always redirects to "login/authfail?ajax=true" and a json is rendered.
I even tried to redefine the 'ajaxAuthFailUrl' to be the same as 'defaultFailureUrl', but even though it redirects to '/login/authfail?login_error=1' I still getting the same json.
I dig the spring plugin to see why it 'thinks' it's an ajax request. Then on 'SpringSecurityService.java' I put some debug messages on 'isAjax' method. The result was, first of all, it calls 'isAjax' 2 times in a row. And the request is considered an ajax for different reasons:
there is a header 'ajaxHeader'
there is an ajax parameter on the request
and finally when it checks the SavedRequest's headers, it also finds it to be an ajax call.
I confess I'm lost, I don't have a clue about how to try to fix it...
I'm using:
Grails 2.3.8
spring-security-core:2.0-RC4
runtime ':spring-security-acl:2.0-RC1'
and I have a custom authentication handler that is set on BootStrap:
authenticationProcessingFilter.authenticationSuccessHandler = new MyUrlAuthenticationSuccessHandler()

Load data from ajax request (json) on startup in AngularJs

I have this simple page that just needs to show contents that is loaded from an external url (ajax request, response in json format)
I should say I'm a AngularJS newbie.
I've googled a bunch and found different ways of doing this and couldn't manage to determine which is the correct/simple/up-to-date way to achieve this.
My 2 challenges -
Making the AJAX request run on startup (I can load the page before that happens and just load the contents one the ajax request finishes. Maybe show a 'Loading..' indicator)
Doing a ajax request correctly.
Here is my attempt. I know that the ajax request is never made because its not setup correctly.
You are getting into .error function:
http://jsbin.com/oDUsuVA/3/edit
For jsonp your response should be something like:
callback([
{
"title":"License Title 1",
"licenseUrl":"http://cnn.com",
"licenseText": " test"
}]);
Edit:
You can simply do .get() request too, but if you had to use jsonp request interface, you would have to correct response.
A Jsonp request always wraps the logic into a json callback wrapper function.
I just did $http.get instead of your $http.jsonp and it did work for me.

HTTP GET request with a separate entity body in JMeter?

I want to send a JSON payload with HTTP GET request but I want to prevent it to be viewable in URL.
GET http://<domain>/school/search.json
{
schoolId: ["S1","S2","S3"],
location: "Pune"
}
How can I achieve this in JMeter Apache?
Get implies visible in Url, what exactly do you want to do ?
Sending Body data along with HTTP GET request is available for default (HttpClient4) implementation since ver.3.1 (as Bugzilla #60358), as well as request retrying behavior both for PUT and GET with body fixed since ver.3.2 (as Bugzilla #60837).
Just as additional note: you will likely encounter problems if you have cache/proxies in your setup and if you plan to take advantage from their usage.

How to set the default serializer in ASP.NET Web API?

I'm currently watching a video course about ASP.NET Web API. When a controller gets called, the data gets returned in JSON by default. I was just wondering, because when I copy this sample project from the video, I get XML.
The frustration is big, please help me to solve this.
I'm pretty new to ASP.NET Web API, so please bear with me.
UPDATE
The controller doesn't contain special code. It's the simple code, which gets generated from the API Controller with empty read/write actions template.
ASP.NET WebAPI comes with built-in content negotitation therefore the format of the return value is determined by the request itself - more specifically by the Accept/Content-Type headers (depending on which ones are present, Accept header appears to be favoured over the Content-type).
I assume you're viewing the results in a browser and by default it's probably asking for application/xml. You will need to toy around with some settings/developer tools on the browser and force it to send Content-Type: application/json to get the correct response (assuming your returning HttpResponseMessage).
in Global.asax: add the line:
GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();
It'll look like this.
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
BundleTable.Bundles.RegisterTemplateBundles();
GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();
}
James is close, but the content negotiation actually uses the [Accept] header,
not [Content-Type]
As with nearly everything else in MVC, you can override the content negotiation components to ensure the desire content is returned
W3c clearly states-
14.1 Accept
The Accept request-header field can be used to specify certain media types which are acceptable for the response.
-and-
14.17 Content-Type
The Content-Type entity-header field indicates the media type of the entity-body sent to the recipient or, in the case of the HEAD method, the media type that would have been sent had the request been a GET.
This page headers is very useful to understand request/response negotiation.

Resources