Is there a way to simulate HTTP Error 5xx and 4xx responses

I am developing a RESTful service that has two disconnectors talking to each other, which can be called external and internal. Internal registers request external ones for further processing. Access only for users.

To test the reliability of my communication between the back-end and the front-end server, I want to simulate an HTTP error returned by the outside against the back-end. Is there any easy way to do this, or will I have to hardcode the response sent external to internal for testing mostly 5xx and 4xx errors.

The server I am using is JBoss, if this information is not needed at all.

When searching on Google I found this data for iPlanet

1.edit obj.conf or your virtual server defined by obj.conf

2. Add the following line below

Error code = "200" fn = "set-variable" error = "503"

this will return 503 even for a successful feed (which will return 200 by default).

I am looking for something similar for JBoss


source to share

3 answers

I am not aware of the JBoss configuration that will allow you to do this outside of the application. But it's easy enough to set up a resource in your application that will mimic this behavior and remove the dependency on a specific vendor application server:

@Produces(MediaType.TEXT_PLAIN, MediaType.TEXT_HTML)
public Response echoStatus(@PathParam("statusCode") final Integer statusCode) {
    return Response.status(statusCode.intValue()).build();




In my opinion, to test this integration, it would be much easier to develop a simple stub web application that will return code 5xx

for any URI. To make it more flexible, you can add a few handles to be able to customize the behavior of this test application at runtime (e.g. based on URI, across different request parameters, ...).

I don't know of a single component in JBoss that will do anything with a rewritten status code, but it's easy to do it yourself. Just write your own Tomcat Valve and put it inserver.xml

import javax.servlet.ServletException;
import org.apache.catalina.Lifecycle;
import org.apache.catalina.connector.Request;
import org.apache.catalina.connector.Response;
import org.apache.catalina.valves.ValveBase;

public class RequestThroughputLimitValve extends ValveBase implements Lifecycle {

  // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

  private int statusCode;

  public void invoke(Request request, Response response) throws IOException, ServletException {
    // Pass the request further, if necessary
    getNext().invoke(request, response);

    // Tweak the response
    response.sendError(this.statusCode, "Forced error.");       

  // This will make the status code configurable in valve
  public void setStatusCode(int statusCode) {
    this.statusCode = statusCode

  public int getStatusCode() {
    return this.statusCode;




It's very easy to create a 500 error. Just throw an exception in the web service method. JBoss will generate a 500 response code.

Another way to use the HTTP Retry API. Just set the status as you like. If you want, you can write this code in an HTTP filter, which can only be set for testing. This way you can simulate any HTTP status (both 400 and 500)



All Articles