1515 */
1616package feign .assertj ;
1717
18+ import com .squareup .okhttp .Headers ;
1819import com .squareup .okhttp .mockwebserver .RecordedRequest ;
1920
2021import org .assertj .core .api .AbstractAssert ;
22+ import org .assertj .core .data .MapEntry ;
2123import org .assertj .core .internal .ByteArrays ;
2224import org .assertj .core .internal .Failures ;
23- import org .assertj .core .internal .Iterables ;
25+ import org .assertj .core .internal .Maps ;
2426import org .assertj .core .internal .Objects ;
2527
2628import java .io .ByteArrayInputStream ;
2729import java .io .IOException ;
30+ import java .util .ArrayList ;
2831import java .util .LinkedHashSet ;
32+ import java .util .List ;
33+ import java .util .Map ;
2934import java .util .Set ;
3035import java .util .zip .GZIPInputStream ;
3136import java .util .zip .InflaterInputStream ;
3237
3338import feign .Util ;
3439
40+ import static org .assertj .core .data .MapEntry .entry ;
3541import static org .assertj .core .error .ShouldNotContain .shouldNotContain ;
3642
3743public final class RecordedRequestAssert
3844 extends AbstractAssert <RecordedRequestAssert , RecordedRequest > {
3945
4046 ByteArrays arrays = ByteArrays .instance ();
4147 Objects objects = Objects .instance ();
42- Iterables iterables = Iterables .instance ();
48+ Maps maps = Maps .instance ();
4349 Failures failures = Failures .instance ();
4450
4551 public RecordedRequestAssert (RecordedRequest actual ) {
@@ -60,13 +66,13 @@ public RecordedRequestAssert hasPath(String expected) {
6066
6167 public RecordedRequestAssert hasBody (String utf8Expected ) {
6268 isNotNull ();
63- objects .assertEqual (info , actual .getUtf8Body (), utf8Expected );
69+ objects .assertEqual (info , actual .getBody (). readUtf8 (), utf8Expected );
6470 return this ;
6571 }
6672
6773 public RecordedRequestAssert hasGzippedBody (byte [] expectedUncompressed ) {
6874 isNotNull ();
69- byte [] compressedBody = actual .getBody ();
75+ byte [] compressedBody = actual .getBody (). readByteArray () ;
7076 byte [] uncompressedBody ;
7177 try {
7278 uncompressedBody =
@@ -80,7 +86,7 @@ public RecordedRequestAssert hasGzippedBody(byte[] expectedUncompressed) {
8086
8187 public RecordedRequestAssert hasDeflatedBody (byte [] expectedUncompressed ) {
8288 isNotNull ();
83- byte [] compressedBody = actual .getBody ();
89+ byte [] compressedBody = actual .getBody (). readByteArray () ;
8490 byte [] uncompressedBody ;
8591 try {
8692 uncompressedBody =
@@ -94,20 +100,38 @@ public RecordedRequestAssert hasDeflatedBody(byte[] expectedUncompressed) {
94100
95101 public RecordedRequestAssert hasBody (byte [] expected ) {
96102 isNotNull ();
97- arrays .assertContains (info , actual .getBody (), expected );
103+ arrays .assertContains (info , actual .getBody (). readByteArray () , expected );
98104 return this ;
99105 }
100106
101- public RecordedRequestAssert hasHeaders (String ... headers ) {
107+ /**
108+ * @deprecated use {@link #hasHeaders(MapEntry...)}
109+ */
110+ @ Deprecated
111+ public RecordedRequestAssert hasHeaders (String ... headerLines ) {
102112 isNotNull ();
103- iterables .assertContainsSubsequence (info , actual .getHeaders (), headers );
113+ Headers .Builder builder = new Headers .Builder ();
114+ for (String next : headerLines ) {
115+ builder .add (next );
116+ }
117+ List <MapEntry > expected = new ArrayList <MapEntry >();
118+ for (Map .Entry <String , List <String >> next : builder .build ().toMultimap ().entrySet ()) {
119+ expected .add (entry (next .getKey (), next .getValue ()));
120+ }
121+ hasHeaders (expected .toArray (new MapEntry [expected .size ()]));
122+ return this ;
123+ }
124+
125+ public RecordedRequestAssert hasHeaders (MapEntry ... expected ) {
126+ isNotNull ();
127+ maps .assertContains (info , actual .getHeaders ().toMultimap (), expected );
104128 return this ;
105129 }
106130
107131 public RecordedRequestAssert hasNoHeaderNamed (final String ... names ) {
108132 isNotNull ();
109133 Set <String > found = new LinkedHashSet <String >();
110- for (String header : actual .getHeaders ()) {
134+ for (String header : actual .getHeaders (). names () ) {
111135 for (String name : names ) {
112136 if (header .toLowerCase ().startsWith (name .toLowerCase () + ":" )) {
113137 found .add (header );
0 commit comments