byoutline / AndroidStubServer

Simple Http server that makes simulating API easy.
Apache License 2.0
81 stars 3 forks source link

Add ability to specify different delays for different request in json config. #5

Open preetb123 opened 8 years ago

preetb123 commented 8 years ago

Delay is not being applied when server sends the response.

{
  "port": 8098,
  "requests": [
    {
      "method": "POST",
      "path": "/login",
      "headers": {
        "api_key":"ksldfjkdlsjfksldjfdks"
      },
      "body": {
        "username":"9090989098",
        "password":"abc123"
      },
      "code": 200,
      "response":{
        "ok":true,
        "response":{
          "user":{
            "name":"Ram",
            "mobile_no":"9090989098"
          },
          "session_token":"qwektrlktrltrltkrgmkmfg"
        }
      },
      "delay": {
        "timeUnit": "SECONDS",
        "value": 4
      }
    }
  ]
}
new OkHttpClient().newCall(
                new Request.Builder().url("http://localhost:8098/login")
                .addHeader("api_key", "ksldfjkdlsjfksldjfdks")
                .post(RequestBody.create(MediaType.parse("application/json"), jsonObj.toString()))
                .build())
                .enqueue(new Callback() {
                    @Override
                    public void onFailure(Request request, IOException e) {
                        Log.d(TAG, "onFailure() called with: request = [" + request + "], e = [" + e + "]");
                    }

                    @Override
                    public void onResponse(Response response) throws IOException {
                        Log.d(TAG, "onResponse() called with: response = [" + response + "]");
                        response.body().close();
                    }
                });
Naitbit commented 8 years ago

You can specify a delay by passing in network type when starting server for example: AndroidStubServer.start(this, NetworkType.EDGE) or NetworkType.UMTS or other value from https://github.com/byoutline/MockServer/blob/master/src/main/java/com/byoutline/mockserver/NetworkType.java

AFAIK specifying "delay" in config.json was never supported. Is this a feature request?

preetb123 commented 8 years ago

it is nice to have. Could you please add it? This would allow different requests to have different time delay before the response is sent.