Skip to content

Commit

Permalink
fix IOException after MS changed response
Browse files Browse the repository at this point in the history
use correct ContentType to avoid 400 error
  • Loading branch information
Eric-Maddox authored and spyder007 committed Feb 2, 2023
1 parent 0ebda50 commit 803d27b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public class MsTeamsNotificationImpl implements MsTeamsNotification {
private List<NameValuePair> params = new ArrayList<NameValuePair>();
private BuildState states;
private String botName;
private final static String CONTENT_TYPE = "application/x-www-form-urlencoded";
private final static String CONTENT_TYPE = "application/json";
private PostMessageResponse response;
private Boolean showBuildAgent;
private Boolean showElapsedBuildTime;
Expand Down Expand Up @@ -181,7 +181,7 @@ private void postViaWebHook() throws IOException {
}

this.response = resp;
this.content = EntityUtils.toString(response.getEntity());
// this.content = EntityUtils.toString(response.getEntity());

} finally {
httppost.releaseConnection();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.BasicHttpEntity;
import org.apache.http.message.BasicHttpResponse;
import org.apache.http.message.BasicStatusLine;
import org.junit.Ignore;
Expand All @@ -18,6 +19,7 @@

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.ConnectException;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -146,6 +148,33 @@ public void post_whenResponseIsFailure_logsException() throws IOException {
assertFalse(w.getResponse().getOk());
}

@Test
public void post_whenResponseIsFailure_readsResponseOnce() throws IOException {
ArgumentCaptor<HttpPost> requestCaptor = ArgumentCaptor.forClass(HttpPost.class);
HttpClient httpClient = mock(HttpClient.class);
BasicHttpResponse response = new BasicHttpResponse(new BasicStatusLine(new ProtocolVersion("http", 1, 1), 400, ""));
BasicHttpEntity readOnce = new BasicHttpEntity();
// the nullInputStream gets closed when it is read and doesn't allow being read a second time.
InputStream readOnceStream = InputStream.nullInputStream();
readOnce.setContent(readOnceStream);
response.setEntity(readOnce);

when(httpClient.execute(requestCaptor.capture())).thenReturn(response);

MsTeamsNotification w = factory.getMsTeamsNotification(httpClient);

MsTeamsNotificationPayloadContent content = new MsTeamsNotificationPayloadContent();
content.setBuildDescriptionWithLinkSyntax("http://foo");
content.setCommits(new ArrayList<Commit>());

w.setPayload(content);
w.setEnabled(true);
w.post();

assertNotNull(w.getResponse());
assertFalse(w.getResponse().getOk());
}

@Test
@Ignore
public void actualTest() throws IOException {
Expand Down

0 comments on commit 803d27b

Please sign in to comment.