Skip to content

Commit

Permalink
IntrospectingClientHttpResponse#hasEmptyMessageBody
Browse files Browse the repository at this point in the history
should not throw an exception when the response body
is an empty lazy gzip stream.
  • Loading branch information
davidvieiratrustly committed Jun 13, 2022
1 parent 472af9c commit 52df119
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package org.springframework.web.client;

import java.io.ByteArrayInputStream;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.PushbackInputStream;
Expand Down Expand Up @@ -96,14 +98,20 @@ public boolean hasEmptyMessageBody() throws IOException {
}
}
else {
this.pushbackInputStream = new PushbackInputStream(body);
int b = this.pushbackInputStream.read();
if (b == -1) {
return true;
try {
this.pushbackInputStream = new PushbackInputStream(body);
int b = this.pushbackInputStream.read();
if (b == -1) {
return true;
}
else {
this.pushbackInputStream.unread(b);
return false;
}
}
else {
this.pushbackInputStream.unread(b);
return false;
catch (EOFException ex) {
this.pushbackInputStream = new PushbackInputStream(new ByteArrayInputStream(new byte[0]));
return true;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.List;
import java.util.zip.GZIPInputStream;

import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -114,6 +116,22 @@ void emptyMessageBody() throws IOException {
assertThat(result).isNull();
}

@Test
void emptyLazyGzipMessageBody() throws IOException {
given(response.getStatusCode()).willReturn(HttpStatus.BAD_REQUEST);
given(response.getHeaders()).willReturn(responseHeaders);
given(response.getBody()).willReturn(new InputStream() {
@Override
public int read() throws IOException {
// Simulates lazy gzip stream reading
return new GZIPInputStream(new ByteArrayInputStream("".getBytes())).read();
}
});

Object result = extractor.extractData(response);
assertThat(result).isNull();
}

@Test // gh-22265
void nullMessageBody() throws IOException {
given(response.getStatusCode()).willReturn(HttpStatus.OK);
Expand Down

0 comments on commit 52df119

Please sign in to comment.