1818import static org .assertj .core .api .Assertions .assertThat ;
1919import static org .assertj .core .api .Assertions .assertThatThrownBy ;
2020
21+ import feign .Client ;
2122import feign .Feign ;
2223import feign .FeignException ;
2324import feign .Param ;
2425import feign .RequestLine ;
26+ import feign .Response ;
27+ import feign .Util ;
28+ import java .util .Collection ;
29+ import java .util .Collections ;
30+ import java .util .HashMap ;
31+ import java .util .Map ;
32+ import java .util .concurrent .atomic .AtomicInteger ;
2533import okhttp3 .mockwebserver .MockResponse ;
2634import okhttp3 .mockwebserver .MockWebServer ;
2735import okhttp3 .mockwebserver .RecordedRequest ;
@@ -44,6 +52,9 @@ interface Api {
4452
4553 @ RequestLine ("POST /things" )
4654 String create (String body );
55+
56+ @ RequestLine ("QUERY /things" )
57+ String query (String body );
4758 }
4859
4960 private Api api () {
@@ -120,6 +131,47 @@ void postRequestsBypassCache() throws Exception {
120131 assertThat (recorded .getHeader ("If-None-Match" )).isNull ();
121132 }
122133
134+ @ Test
135+ void queryRequestsParticipateInCache () {
136+ AtomicInteger calls = new AtomicInteger ();
137+ Client client =
138+ (request , options ) -> {
139+ calls .incrementAndGet ();
140+ if (request .headers ().containsKey ("If-None-Match" )) {
141+ return Response .builder ()
142+ .status (304 )
143+ .headers (Collections .emptyMap ())
144+ .request (request )
145+ .build ();
146+ }
147+ String body = request .body () != null ? new String (request .body (), Util .UTF_8 ) : "" ;
148+ Map <String , Collection <String >> headers = new HashMap <>();
149+ headers .put ("ETag" , Collections .singletonList ("\" " + body + "\" " ));
150+ return Response .builder ()
151+ .status (200 )
152+ .headers (headers )
153+ .body ("result-" + body , Util .UTF_8 )
154+ .request (request )
155+ .build ();
156+ };
157+
158+ Api api =
159+ Feign .builder ()
160+ .client (client )
161+ .methodInterceptor (new HttpCacheInterceptor (store ))
162+ .target (Api .class , "http://localhost:0" );
163+
164+ String first = api .query ("q1" );
165+ String second = api .query ("q2" );
166+ String third = api .query ("q1" );
167+
168+ assertThat (first ).isEqualTo ("result-q1" );
169+ assertThat (second ).isEqualTo ("result-q2" );
170+ assertThat (third ).isEqualTo ("result-q1" );
171+ assertThat (calls .get ()).isEqualTo (3 );
172+ assertThat (store .size ()).isEqualTo (2 );
173+ }
174+
123175 @ Test
124176 void noStoreCacheControlPreventsStorage () throws Exception {
125177 server .enqueue (
0 commit comments