简体   繁体   中英

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

I am developing a RESTful service which has two severs talking to each other, which may be named as external and internal. Internal registers requests to external for further processing. Only external is accessible to users.

For testing reliability my link between internal and external server, I want to simulate HTTP error as returned by external to internal. Is there any easy way to do so or I'll have to hardcode the response being sent by external to internal for testing mainly 5xx and 4xx errors.

Server I am using is JBoss if at all this info is needed.

On searching Google, I found this data for iPlanet

1. edit the obj.conf or your virtual server specific obj.conf

2. Add the following line under

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

this would return 503 even for successful serving (which would cause 200 to be returned by the default).

I am looking for something similar for JBoss

I am not aware of a JBoss configuration that will allow you to do this outside of the application. But its easy enough to setup a Resource within your application that would simulate this behavior and remove dependency on vendor specific application server:

@GET @POST @PUT @DELETE
@Path("/echostatus/{statusCode}")
@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 simple stub web app that will return 5xx code for any URI. To make it more flexible, you can add some handles to be able to tweak behavior of this test app in run time (eg based on URI, on various parameters of the request, ...).

I am not aware of any component in JBoss that will do the thing with rewriting status code, but it is simple to do it on your own. Just write your own Tomcat Valve and put it in server.xml

import java.io.IOException;
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 {

  // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  // NOTE: THIS IS NOT COMPLETE IMPLEMENTATION
  // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

  private int statusCode;

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

    // Tweak the response
    response.setContentType("text/plain");
    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 is very easy to produce error 500. Just throw exception into web service method. JBoss will generate response code 500.

Other way it to use HTTP Resptonse API. Just set status as you want. If you want you can write this code in HTTP filter that can be installed for testing purposes only. This way you can simulate any HTTP status (both 400 or 500 series)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM