@@ -117,9 +117,25 @@ public HttpRequest removeHeader(String header) {
117117 return new FakeHttpRequest (method , uri , body , filteredHeaders , inboundException );
118118 }
119119
120+ public int contentLength () {
121+ return Integer .parseInt (headers .getOrDefault ("Content-Length" , List .of ("0" )).getFirst ());
122+ }
123+
124+ public void setContentLength (int contentLength ) {
125+ headers .put ("Content-Length" , List .of (String .valueOf (contentLength )));
126+ }
127+
128+ public void setContentLength (HttpBody body ) {
129+ if (body .isEmpty () || body instanceof HttpBody .NoopStream ) {
130+ setContentLength (0 );
131+ } else if (body .isFull ()) {
132+ setContentLength (body .asFull ().bytes ().length ());
133+ }
134+ }
135+
120136 @ Override
121137 public boolean hasContent () {
122- return body . isEmpty () == false ;
138+ return contentLength () > 0 ;
123139 }
124140
125141 @ Override
@@ -215,7 +231,7 @@ public Builder(NamedXContentRegistry registry) {
215231 }
216232
217233 public Builder withHeaders (Map <String , List <String >> headers ) {
218- this .headers = headers ;
234+ this .headers = new HashMap <>( headers ) ;
219235 return this ;
220236 }
221237
@@ -259,6 +275,7 @@ public Builder withInboundException(Exception exception) {
259275
260276 public FakeRestRequest build () {
261277 FakeHttpRequest fakeHttpRequest = new FakeHttpRequest (method , path , content , headers , inboundException );
278+ fakeHttpRequest .setContentLength (content );
262279 return new FakeRestRequest (parserConfig , fakeHttpRequest , params , new FakeHttpChannel (address ));
263280 }
264281 }
0 commit comments