In Angular6, what is the way to simulate receiving 4xx and 5xx responses from server - angular-test

I am using HttpClient’s post method to send messages to the server. To test it, I want to simulate receiving 404 and 500 responses from the server. What is the way to simulate receiving 4xx and 5xx error messages in Angular6? As far as I know, I cannot use spyOn().and.returnValue because this will not simulate server error and will not trigger the error code handler passes to the post method.

Related

How to detect akka-http server request timeout?

When writing an HTTP server using akka-http’s high-level Route API, is there a way to be notified that the request timeout has expired, or to check whether it is expired, or to check how much time remains? In implementing an expensive route, I would like to stop performing work on a request if the request times out.
Akka-http’s request timeout responds to HTTP requests with status code 503 Service Unavailable: “The server was not able to produce a timely response to your request.
Please try again in a short while!” It is configured using akka.http.server.request-timeout, which is 20 s by default.

What's the correct HTTP response code to return for Denial of Service (DoS) attack?

I have some logic in the web server to find out if the user is trying to do a DoS attack. But what's the correct HTTP response code to return for that? Also what's a good error message I can put in HTTP body to tell the user politely that he's got into the DoS attack path?
But what's the correct HTTP response code to return for that?
RFC 6585 suggests 429 Too Many Requests
The 429 status code indicates that the user has sent too many requests in a given amount of time ("rate limiting").
If the attack has compromised your ability to handle legitimate requests, then to those you might respond with 503 Service Unavailable.
Note the change in semantics - the person sending the bad requests gets a status out of the 4NN Client Error class, while those not at fault get a status from the 5NN Internal Service Error class.
what's a good error message I can put in HTTP body to tell the user politely that he's got into the DoS attack path?
Please stop that?

NodeJS Express - 404/400 Responses

I am just wondering if 404/400 Responses in a node express app is consider an error and if it will be caught by the error handling middle ware?
I had a hard time finding this in the documentation
In HTTP/1.1, a response code of 400 or higher is considered an error.
However, Express error handling only deals with programming errors, not HTTP errors. If your app detects an error condition and sends back a 400 or 404 response, Express doesn't care (i.e. it won't call the error handler).

How to show the internal server errors to the user?

I am working in an API . I want to throw detailed error messages to the user. Now i am in a situation to decide what kind of error code should be sent or how to explain user if any error occurs in the application internally. For example if database connection fails , what kind of http status code i want to send to the user ?
Can anyone help ?
An HTTP status code generally refers to the status of the HTTP request itself, not the status of the application handling the request. Therefore, most server-side errors are covered by 500 Internal Server Error. Any additional info about the error should be described in the response body. For APIs, the response body will often be JSON or XML, so you can use those formats for your errors. Something like this:
HTTP/1.1 500 Internal Server Error
[headers]
{"status":"error", "message":"The request failed due to database connectivity."}
There are, however, two cases I can think of when you might want another status code. If the user has requested an API method that is not implemented, you might want a 501 Not Implemented, and when there is a temporary service outage, you can use 503 Service Unavailable.
More info about server-side status codes here.

Receive custom HTTP statusCode in NSURLConnection sendSynchronousRequest

I send a GET request with [NSURLConnection sendSynchronousRequest...] to our web service. The server overrides the default HTTP status codes with his one codes if an error occurred.
I will test the error handling but always become a timeout (Error Domain=NSURLErrorDomain Code=-1001 "The request timed out.") if server send a custom HTTP code. If server sends the standard HTTP code (e.g. 400) the NSURLConnection works correct and gives me this code immediately.
Has anybody used NSURLConnection with NON-Standard HTTP failure codes and can help me to get this setup working?
Thanks