-
Notifications
You must be signed in to change notification settings - Fork 25
/
OkHttpMockInterceptor.java
158 lines (139 loc) · 5.8 KB
/
OkHttpMockInterceptor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package ir.mirrajabi.okhttpjsonmock;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;
import com.google.gson.internal.LinkedTreeMap;
import java.io.IOException;
import java.util.Random;
import ir.mirrajabi.okhttpjsonmock.helpers.ResourcesHelper;
import ir.mirrajabi.okhttpjsonmock.models.MockedResponse;
import ir.mirrajabi.okhttpjsonmock.providers.DefaultInputStreamProvider;
import ir.mirrajabi.okhttpjsonmock.providers.InputStreamProvider;
import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.Protocol;
import okhttp3.Response;
import okhttp3.ResponseBody;
public class OkHttpMockInterceptor implements Interceptor {
private final static String DEFAULT_BASE_PATH = "";
private final static int DELAY_DEFAULT_MIN = 500;
private final static int DELAY_DEFAULT_MAX = 1500;
private int failurePercentage;
private String basePath;
private InputStreamProvider inputStreamProvider;
private int minDelayMilliseconds;
private int maxDelayMilliseconds;
public OkHttpMockInterceptor(int failurePercentage) {
this(new DefaultInputStreamProvider(), failurePercentage, DEFAULT_BASE_PATH,
DELAY_DEFAULT_MIN, DELAY_DEFAULT_MAX);
}
public OkHttpMockInterceptor(InputStreamProvider inputStreamProvider, int failurePercentage) {
this(inputStreamProvider, failurePercentage, DEFAULT_BASE_PATH,
DELAY_DEFAULT_MIN, DELAY_DEFAULT_MAX);
}
public OkHttpMockInterceptor(
InputStreamProvider inputStreamProvider,
int failurePercentage,
int minDelayMilliseconds,
int maxDelayMilliseconds) {
this(inputStreamProvider, failurePercentage, DEFAULT_BASE_PATH,
minDelayMilliseconds, maxDelayMilliseconds);
}
public OkHttpMockInterceptor(
InputStreamProvider inputStreamProvider,
int failurePercentage,
String basePath,
int minDelayMilliseconds,
int maxDelayMilliseconds) {
this.inputStreamProvider = inputStreamProvider;
this.failurePercentage = failurePercentage;
this.basePath = basePath;
this.minDelayMilliseconds = minDelayMilliseconds;
this.maxDelayMilliseconds = maxDelayMilliseconds;
}
@Override
public Response intercept(Chain chain) throws IOException {
Gson gson = new GsonBuilder().setLenient().create();
HttpUrl url = chain.request().url();
String sym = "";
String query = url.encodedQuery() == null ? "" : url.encodedQuery();
if (!query.equals(""))
sym = "/";
String path = url.encodedPath() + sym + query;
String responseString = ResourcesHelper.loadFileAsString(inputStreamProvider,
basePath + path.substring(1) + ".json");
if (responseString == null)
responseString = ResourcesHelper.loadFileAsString(inputStreamProvider,
basePath + url.encodedPath().substring(1) + ".json");
MockedResponse mockedResponse = new MockedResponse()
.setResponse(new LinkedTreeMap())
.setStatusCode(404);
if (responseString != null) {
try {
mockedResponse = gson.fromJson(responseString, MockedResponse.class);
} catch (JsonSyntaxException e) {
e.printStackTrace();
return null;
}
}
JsonObject jsonObject = gson.toJsonTree(mockedResponse.getResponse()).getAsJsonObject();
String result = jsonObject.toString();
JsonArray items = jsonObject.getAsJsonArray("items");
if (items != null)
result = gson.toJson(items);
try {
Thread.sleep(Math.abs(new Random()
.nextInt() % (maxDelayMilliseconds - minDelayMilliseconds))
+ minDelayMilliseconds);
} catch (InterruptedException e) {
e.printStackTrace();
}
boolean failure = Math.abs(new Random().nextInt() % 100) < failurePercentage;
int statusCode = failure ? 504 : mockedResponse.getStatusCode(); /*504 or 408*/
if (failure)
System.out.print("JsonMockServer: Returning result from " +
path + "\t\tStatusCode : " + statusCode);
else
System.out.print("JsonMockServer: Returning result from " +
path + "\t\tStatusCode : " + statusCode);
return new Response.Builder()
.code(statusCode)
.message(responseString)
.request(chain.request())
.protocol(Protocol.HTTP_1_0)
.body(ResponseBody.create(MediaType.parse("application/json"), result))
.addHeader("content-type", "application/json")
.build();
}
public int getFailurePercentage() {
return failurePercentage;
}
public OkHttpMockInterceptor setFailurePercentage(int failurePercentage) {
this.failurePercentage = failurePercentage;
return this;
}
public String getBasePath() {
return basePath;
}
public OkHttpMockInterceptor setBasePath(String basePath) {
this.basePath = basePath;
return this;
}
public int getMinDelayMilliseconds() {
return minDelayMilliseconds;
}
public OkHttpMockInterceptor setMinDelayMilliseconds(int minDelayMilliseconds) {
this.minDelayMilliseconds = minDelayMilliseconds;
return this;
}
public int getMaxDelayMilliseconds() {
return maxDelayMilliseconds;
}
public OkHttpMockInterceptor setMaxDelayMilliseconds(int maxDelayMilliseconds) {
this.maxDelayMilliseconds = maxDelayMilliseconds;
return this;
}
}