Skip to content

Commit 95b6e78

Browse files
authored
Merge pull request #2 from wego/fix-body
Fix duplicated responseId with different body
2 parents 0abe30f + 4265079 commit 95b6e78

File tree

4 files changed

+63
-24
lines changed

4 files changed

+63
-24
lines changed

src/main/java/com/wego/httpcache/services/impl/AsyncHttpCacheServiceImpl.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717

1818
public class AsyncHttpCacheServiceImpl implements AsyncHttpCacheService {
1919

20-
@Inject private CachedResponseService cachedResponseService;
20+
@Inject
21+
private CachedResponseService cachedResponseService;
2122

22-
@Inject private AsyncHttpClient asyncHttpClient;
23+
@Inject
24+
private AsyncHttpClient asyncHttpClient;
2325

2426
private long ttl;
2527

@@ -59,7 +61,7 @@ private String buildResponseId(Request request) {
5961
String requestStringId =
6062
StringUtils.join(
6163
request,
62-
request.getBodyEncoding(),
64+
request.getStringData(),
6365
Lists.newArrayList(request.getCookies()).toString());
6466
return String.valueOf(MurmurHash.hash64A(requestStringId.getBytes(), 0));
6567
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.wego.httpcache.dao.impl.guava;
2+
3+
import org.junit.Before;
4+
import org.junit.Test;
5+
6+
public class TestGuavaCachedResponseImpl {
7+
8+
@Before
9+
public void setUp() throws Exception {}
10+
11+
@Test
12+
public void save() throws Exception {}
13+
14+
@Test
15+
public void findById() throws Exception {}
16+
}

src/test/java/com/wego/httpcache/fixtures/RequestFixture.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.apache.commons.lang3.StringUtils;
77

88
public class RequestFixture {
9+
910
public static Request create(String method, String url) {
1011
return new RequestBuilder().setMethod(method).setUrl(url).build();
1112
}
@@ -27,6 +28,10 @@ public static Request createWithParams(String method, String url, String key, St
2728
return new RequestBuilder().setMethod(method).setUrl(url).addParameter(key, value).build();
2829
}
2930

31+
public static Request createWithBody(String method, String url, String body) {
32+
return new RequestBuilder().setMethod(method).setUrl(url).setBody(body).build();
33+
}
34+
3035
public static Request createWithFullParams() {
3136
return new RequestBuilder()
3237
.setMethod("GET")
@@ -35,6 +40,7 @@ public static Request createWithFullParams() {
3540
.addQueryParameter("queryParam", "test")
3641
.addParameter("param", "test")
3742
.addCookie(new Cookie("cookie", "test", "", "", "", 1, 2, true, true))
43+
.setBody("test")
3844
.build();
3945
}
4046
}

src/test/java/com/wego/httpcache/services/impl/TestAsyncHttpCachedServiceImpl.java renamed to src/test/java/com/wego/httpcache/services/impl/TestAsyncHttpCacheServiceImpl.java

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,23 @@
4242
import org.mockito.runners.MockitoJUnitRunner;
4343

4444
@RunWith(MockitoJUnitRunner.class)
45-
public class TestAsyncHttpCachedServiceImpl {
45+
public class TestAsyncHttpCacheServiceImpl {
46+
4647
private static long CACHING_TTL = 60;
47-
@Rule public WireMockRule wireMockRule = new WireMockRule(8089);
48+
@Rule
49+
public WireMockRule wireMockRule = new WireMockRule(8089);
4850

49-
@Spy private AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
51+
@Spy
52+
private AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
5053

5154
@InjectMocks
5255
private AsyncHttpCacheService asyncHttpCacheService =
5356
new AsyncHttpCacheServiceImpl(asyncHttpClient, CACHING_TTL);
5457

55-
@Mock private CachedResponseService cachedResponseService;
56-
@Mock private Request request;
58+
@Mock
59+
private CachedResponseService cachedResponseService;
60+
@Mock
61+
private Request request;
5762

5863
@Test
5964
public void executeRequest_whenWasCached_getResponseFromCacheAndCallOnComplete()
@@ -155,11 +160,11 @@ public void buildResponseId_returnsDifferentIdsForDifferentRequest() throws Exce
155160
Request requestWithDifferentUrl =
156161
RequestFixture.create("GET", "http://localhost:8089/resources/2");
157162

158-
Request requestWithParams =
163+
Request requestWithQueryParams =
159164
RequestFixture.createWithQueryParam(
160165
"GET", "http://localhost:8089/resources/2", "queryParam", "test1");
161166

162-
Request requestWithDifferentParams =
167+
Request requestWithDifferentQueryParams =
163168
RequestFixture.createWithQueryParam(
164169
"GET", "http://localhost:8089/resources/2", "queryParam", "test2");
165170

@@ -182,31 +187,41 @@ public void buildResponseId_returnsDifferentIdsForDifferentRequest() throws Exce
182187
Request requestWithDifferentMethod =
183188
RequestFixture.create("POST", "http://localhost:8089/resources/");
184189

185-
Request requestWithBody =
190+
Request requestWithParams =
186191
RequestFixture.createWithParams(
187192
"POST", "http://localhost:8089/resources/", "param", "test1");
188193

189-
Request requestWithDifferentBody =
194+
Request requestWithDifferentParams =
190195
RequestFixture.createWithParams(
191196
"POST", "http://localhost:8089/resources/", "param", "test2");
192197

198+
Request requestWithBody =
199+
RequestFixture.createWithBody(
200+
"POST", "http://localhost:8089/resources/", "test1");
201+
202+
Request requestWithDifferentBody =
203+
RequestFixture.createWithBody(
204+
"POST", "http://localhost:8089/resources/", "test2");
205+
193206
Method method =
194207
AsyncHttpCacheServiceImpl.class.getDeclaredMethod("buildResponseId", Request.class);
195208
method.setAccessible(true);
196209

197210
final List<String> responseIds =
198211
Stream.of(
199-
request,
200-
requestWithDifferentUrl,
201-
requestWithDifferentMethod,
202-
requestWithBody,
203-
requestWithDifferentBody,
204-
requestWithCookie,
205-
requestWithDifferentCookie,
206-
requestWithHeader,
207-
requestWithDifferentHeader,
208-
requestWithParams,
209-
requestWithDifferentParams)
212+
request,
213+
requestWithDifferentUrl,
214+
requestWithDifferentMethod,
215+
requestWithParams,
216+
requestWithDifferentParams,
217+
requestWithCookie,
218+
requestWithDifferentCookie,
219+
requestWithHeader,
220+
requestWithDifferentHeader,
221+
requestWithQueryParams,
222+
requestWithDifferentQueryParams,
223+
requestWithBody,
224+
requestWithDifferentBody)
210225
.map(
211226
rq -> {
212227
try {
@@ -219,7 +234,7 @@ public void buildResponseId_returnsDifferentIdsForDifferentRequest() throws Exce
219234
.filter(Objects::nonNull)
220235
.collect(Collectors.toList());
221236

222-
assertThat(Sets.newHashSet(responseIds).size()).isEqualTo(11);
237+
assertThat(Sets.newHashSet(responseIds).size()).isEqualTo(13);
223238
}
224239

225240
@Test

0 commit comments

Comments
 (0)