How to test 404 json response on ZF2 - zend-framework2

Considering that in my Zend Framework module I'm able to access via browser or REST. When I got a 404 via REST, this is the content:
{"httpStatus":404,"title":"Not Found"}
So how do I test this response using PHPUnit?
I have already a test for another route that is working:
public function testIndexActionCanBeAccessed()
{
$this->dispatch('/ws/dashboard');
$this->assertResponseStatusCode(200);
$this->assertModuleName('Ws');
$this->assertControllerName('Ws\Controller\Dashboard');
$this->assertControllerClass('DashboardController');
$this->assertMatchedRouteName('application/ws');
}
But I don't know how to do this other case.
public function testIndexActionCanBeAccessed()
{
$this->dispatch('/ws/asd');
$this->assertResponseStatusCode(404); // this is OK
// but how check the json response?
}
Also, if I do this $response = $this->getResponse(); I got the entire HTML 404 html file, which is not what I want.

You may want to check to ensure that the controller is actually returning a JSON response.
Also, your Accept header for the AJAX request should specify application/json.

Related

Saturn API not responding to GET when using acceptJson

The F# Saturn web framework fails on retrieving a value for GET method when acceptJson is a part of pipeline.
Below a sample code that I run to reproduce the issue:
let api = pipeline {
plug acceptJson
set_header "x-pipeline-type" "Api"
}
let apiRouter = router {
not_found_handler (setStatusCode 404 >=> text "Api 404")
pipe_through api
get "/test" (text "Hello world")
}
let appRouter = router {
forward "/api" apiRouter
}
appRouter is then added in the use_router section of the application code.
When I'm sending the request with a header Content-Type:application/json the response is "404 not found". But if I remove plug acceptJson from the api pipeline definition I get a correct response.
How to make Saturn work with the plug acceptJson?
I suspect 3615 is right - this seems similar to a problem I just solved yesterday after beating my head against the wall for a week. A request to my app (just a straight app from "dotnew new Saturn") from my browser was accepted. But a request to the same url from a test method returned a 404. It boiled down to the fact that the test request was missing an "Accept" header. When I added "Accept text/html", it worked. What I deduced from that is that, if the app can't find a content type that will be accepted according to the request, then it will report a 404. Your situation is the same - you're trying to return Json, but the request didn't include an "Accept application/json", so it can't find any page that the request would accept.
Of course, I could be wrong.

Different response for same API method

New to the RestAssured and just checking different ways of working with REST APIs. For testing I am using http://dummy.restapiexample.com/api/v1. In this I am trying the GET employee method using RequestSpecification and groovy way but I am getting different response.
My short code is:
RestAssured.baseURI = "http://dummy.restapiexample.com/api/v1";
RequestSpecification request = RestAssured.given();
Response response = request.get("/employee/72100");
System.out.println(response.getBody().asString());
given().baseUri("http://dummy.restapiexample.com/api/v1").get("/employee/72100").then().log().body();
And output I am getting is:
{"id":"72100","employee_name":"mpr51_0280","employee_salary":"123","employee_age":"23","profile_image":""}
<html>
<body>{"id":"72100","employee_name":"mpr51_0280","employee_salary":"123","employee_age":"23","profile_image":""}</body>
</html>
I am not getting why it is returning response with HTML tags. Can anyone explain or give hint to get same response as first call to get method.
NOTE: You may or may not get details for employeeID 72100
You can use any employee ID from response of:
http://dummy.restapiexample.com/api/v1/employees
Because the Body contains it .
You can use below statement if you need only the response
given().baseUri("http://dummy.restapiexample.com/api/v1").get("/employee/72100").then().log();
OR
Response resp = given().baseUri("http://dummy.restapiexample.com/api/v1").get("/employee/72100");
System.out.println(resp.asString());

How do you provide frombody data to webapp

I'm looking at somebody else webapp written in asp.net and it has a post request handler with this prototype
public class NotificationsController : ApiController
{
public async Task<HttpResponseMessage> Post(string pns, [FromBody]string message, string to_tag)
.....
.....
I'm testing that post request handler with postman (Chrome extension) and providing pns and to_tag as url parameters. But how do I provide message parameter? I tried setting content-type as json and provide message in body as
{ "message" : "hello"} OR { "body" : "hello" }
But I'm getting 500 error code.
It kind of depends on what version of Postman you are using. But you will put your JSON in the Body tab and choose the 'raw' radio button also on the Body tab.
You will want to set the Content-Type header to application/json. If you are still having issues, please add an image to your question of the Postman screen.
You will want to use: { "message" : "hello"} for the body message.
Finally, the pns and to_tag parameters should probably also be given [FromUri] annotations so it is more clear that they are to be passed in as URL Parameters.

grails controller handling ajax file upload

I am implementing a file uplaod via jquery ajax but I fail to do something with the data on the controller side. I get the params but not the File which is in this example a jpeg. The data is in the Request Payload.
My problem is on server side. How do I get a File on server side?
#Secured(['ROLE_USER', 'ROLE_ADMIN', 'ROLE_PARTNER', 'ROLE_READ_ONLY', 'ROLE_USER_PAYING'])
def fileupload () {
println "---------------------------------------------------"
params.each{
println it.key +"="+ it.value
}
request.getHeaderNames().each{
println (it)
}
render("OK")
}
This is the output i receive:
---------------------------------------------------
filename=1506368_10152113826431683_327028558_o.jpg
apiKey=c7937acaf6d5411d8920d194dc48c041
action=fileupload
controller=post
host
connection
content-length
...
How do I get the file in my controller?
I'm doing the same, and using request.getInputStream() in the controller for getting the binary data. This actually comes from the ServletRequest interface - see http://docs.oracle.com/javaee/7/api/javax/servlet/ServletRequest.html#getInputStream--

$httpProvider interceptor how to read all headers

I have an interceptor using $httpProvider as follwos (taken from Angular doc)
$httpProvider.interceptors.push(function ($q) {
return {
'response': function (response) {
// look for the antiforgerytoken and update dataService with the same.
return response || $q.when(response);
}
};
});
using the debugger I see that this method is called on each response and when I look at the 'response' object pased I see only one header, namely the Accept-Header as shown below
When I look at the response packet in the chrome I see many more headers
My question is why am I not able to see all the headers in the response?
Specifically I want to read the cookie for AntiforgeryToken and store it, how can I do that?
I believe the cookie AntiforgeryToken is a HttpOnly cookie and cannot be accessed from javascript. Can you check
See some details here http://blog.codinghorror.com/protecting-your-cookies-httponly/
Such cookies are mean for server exclusively.

Resources