How can I test for an implicit HTTP status code (200) in ASP.NET MVC 2? - asp.net-mvc-2

I'm unit-testing some caching code in an ASP.NET MVC2 controller. My code explicitly sets a 304 Not Modified if the cache headers are OK - which is great, because I can mock the HttpResponse in the controller context and verify that the StatusCode has been set accordingly.
If I don't explicitly set a response code, the controller returns a 200 OK (which I can see in the HTTP response headers in Chrome), but I can't see any way to verify this in a unit test - any expectations for setting the response status or status code just don't get satisfied. It appears to be some sort of default behaviour created by the ASP.NET MVC controller pipeline - and even after invoking result.ExecuteResult(controller.ControllerContext) I can't see the response code being set to 200 anywhere. Anyone written a test for this scenario?

Related

UseProtocolCachePolicy hide 304 status code

I am setting cachePolicy on url request.
First call to an url (GET) return status code in 200.
Second call to the same url return 200.
If use a rest client i obtain for the first call 200, and for the second, with properly header ( If-None-Match) 304
Reading online i understand that iOS serialize first request with status code 200 and retrieve it for the second call.
There is a way to obtain 304 instead of 200 in UrlCache or understand if it the data came from cache and not from network.
From http://jonathanblog2000.blogspot.com/2017/07/ios-uiwebview-nsurlsession-cache.html :
If you want to test and verify the cache works properly in your app, you have to log the device http traffic, as the delegate of NSURLSession will never see the 304 status code returned from server, it will be automatically replaced by the status code in the cached response.
This is a good thing. This means your app can just pretend you got a successful response, and not worry about how the response was obtained or what sort of optimizations was happening. You just deal with the response the same way regardless of whether it was pulled from the cache or not.

HTTP Options method is not working as expected

I have a Jersey 2.x application running in tomcat. All the method implementations are working as expected, and even I am able to get WADL by navigating to http://{host}:{port}/JerseyRESTWebapp/ws/rest/application.wadl.
Everything is great so far.
Now, Out of curiosity I tried navigating to http://{host}:{port}/JerseyRESTWebapp/ws/rest/employees URL using using HTTP OPTIONS method expecting i will get 405 Method not allowed but i got the 200 OK and response body contains the WADL. Can someone let me know why is this happening? I am using POSTMAN chrome extension as REST client.
Also in the Response Allow Header, i am getting POST,GET,DELETE,OPTIONS,HEAD. I am missing PUT method here. why?
This is how the resource discovery works by default. It's implemented to follow the spec in regards to OPTIONS resource discovery
This method allows the client to determine the options and/or requirements associated with a resource, or the capabilities of a server, without implying a resource action or initiating a resource retrieval.
If you want to disable the WADL, you can by setting the property ServerProperties.WADL_FEATURE_DISABLE to true.
If you're curious about how this is implemented, check out the source for the WadlModelProcessor. It goes through all the resource models and adds an extra OPTIONS resource method. You can read more about the ModelProcessor in the Jersey docs Programmatic API for Building Resources

API response on bad resource id

I've got api controller with route /resource/{id} where id is required to be numeric. If request url is /resource/abc, then I'll get 404 HTTP response. Is it ok or I should pass any id inside controller, then validate it and response with 400 code?
If you check out the specification you can see that 400 would be the best way to go. HTTP Status Codes
404 suggests that there currently is no resource with that ID. But since the ID has to be numeric and this is not. That will never change.
400 Says that the syntax is incorrect and more importantly, that the client should not repeat the request without making changes.
Also, keep in mind that you need to include an explanation of the error situation. So the ideal situation would be a 400 status code, telling the client it needs to make changes to the request, and the explanation telling them what changes to make.

NSURLConnection request to REST WebService with faulty Status Code 204

I have an iOS app in which a REST WebService request is made through NSURLConnection. It works fine in all cases except when the service (implemented with .NET - IIS 7) has no data to return. When it has no data, the response shows status 204 alright, and has an empty response body, but it takes a very long time (excess of 45-60 seconds) to trigger the connection:didReceiveResponse delegate.
I noticed that in the C# code, the status code is explicitly set to 204 in these cases, and "null" is returned (which I know is incorrect in itself: 204 responses should return nothing, not even null).
Debugging this WebService with Fiddler, I found a warning stating that there was a mismatch between the response-length header field (which had a value of '4', perhaps from "null"?), and the response-body, which of course was empty.
Now this whole issue (of the NSURLConnection delegates taking a long time to get triggered) was fixed when I stopped setting the status code to 204, but the strange part is, this only happened in my production environment, and not in the staging environment.
Is there any reasonable explanation for this? Perhaps some IIS setting?

Zend Framework set HTTP response code from inside a Handler

I'm developing a REST API using Zend Framework 1.12.3. I would like to know whether it's possible to set a HTTP response code from inside a Handler.
I'm using the Handler to check the "Accept" header. In case the requested format type is not supported, I should set a 415 HTTP error (Unsupported Media Type). However, I'm not able to set a response code from inside the Handler.
What do you mean by handler?
You can set a response code anywhere you have access to the Response object.
Technically, you can access the Response object nearly anywhere (after Bootstrap, at least) using:
$response = Zend_Controller_Front::getInstance()->getResponse();
The set your response code using:
$response->setHttpResponseCode($code);
It's most natural to do this in controllers since each controller already has a reference to the Response object:
$this->_response