Skip to content

Commit

Permalink
Merge pull request #107 from ashwanthkumar/set-user-agent-in-connection
Browse files Browse the repository at this point in the history
Fix the GetPipelineInstance URL for fetching pipeline run details
  • Loading branch information
ashwanthkumar authored Mar 10, 2020
2 parents 23ebba6 + b9507e3 commit c8d78ce
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 29 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>in.ashwanthkumar</groupId>
<artifactId>gocd-slack-notifier</artifactId>
<version>2.0.1</version>
<version>2.0.2-RC2</version>
<packaging>jar</packaging>

<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ JsonElement getUrl(URL url)
HttpURLConnection request = httpConnectionUtil.getConnection(normalizedUrl);
// @since 20.1.0
request.setRequestProperty("Accept", "application/vnd.go.cd.v1+json");
request.setRequestProperty("User-Agent", "plugin/slack.notifier");

// Add in our HTTP authorization credentials if we have them.
// Favor the API Token over username/password
Expand Down Expand Up @@ -84,7 +85,7 @@ public History getPipelineHistory(String pipelineName)
*/
public Pipeline getPipelineInstance(String pipelineName, int pipelineCounter)
throws MalformedURLException, IOException {
URL url = new URL(String.format("%s/go/api/pipelines/%s/instance/%d",
URL url = new URL(String.format("%s/go/api/pipelines/%s/%d",
mRules.getGoAPIServerHost(), pipelineName, pipelineCounter));
JsonElement json = getUrl(url);
return httpConnectionUtil.convertResponse(json, Pipeline.class);
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<go-plugin id="slack.notifier" version="1">
<about>
<name>Slack Notification Plugin</name>
<version>2.0.1</version>
<version>2.0.2-RC2</version>
<target-go-version>20.1.0</target-go-version>
<description>Plugin to send build notifications to slack</description>
<vendor>
Expand Down
40 changes: 14 additions & 26 deletions src/test/java/in/ashwanthkumar/gocd/slack/jsonapi/ServerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,11 @@
import java.net.HttpURLConnection;
import java.net.URL;

import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.startsWith;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.*;

public class ServerTest {

Expand Down Expand Up @@ -68,7 +64,7 @@ public void testGetPipelineInstance() throws Exception {
verify(httpConnectionUtil).getConnection(
url.capture()
);
assertThat(url.getValue().toString(), is("https://example.org/go/api/pipelines/pipeline-test/instance/42"));
assertThat(url.getValue().toString(), is("https://example.org/go/api/pipelines/pipeline-test/42"));
}

@Test
Expand All @@ -84,6 +80,7 @@ public void shouldConnectWithAPIToken() throws IOException {

server.getUrl(new URL("http://exmaple.org/"));

verify(conn).setRequestProperty(eq("User-Agent"), anyString());
verify(conn).setRequestProperty("Authorization", "Bearer a-valid-token-from-gocd-server");
}

Expand All @@ -101,6 +98,7 @@ public void shouldConnectWithUserPassCredentials() throws IOException {

server.getUrl(new URL("http://exmaple.org/"));

verify(conn).setRequestProperty(eq("User-Agent"), anyString());
verify(conn).setRequestProperty("Authorization", "Basic bG9naW46cGFzcw==");
}

Expand All @@ -119,8 +117,10 @@ public void shouldConnectWithAPITokenFavoringOverUserPassCredential() throws IOE

server.getUrl(new URL("http://exmaple.org/"));

verify(conn).setRequestProperty(eq("User-Agent"), anyString());
verify(conn).setRequestProperty("Authorization", "Bearer a-valid-token-from-gocd-server");
}

@Test
public void shouldNotSetAuthorizationHeaderWithoutCredentials() throws IOException {
HttpConnectionUtil httpConnectionUtil = mockConnection();
Expand All @@ -133,11 +133,8 @@ public void shouldNotSetAuthorizationHeaderWithoutCredentials() throws IOExcepti

server.getUrl(new URL("http://exmaple.org/"));

ArgumentCaptor<String> valueCaptor = ArgumentCaptor.forClass(String.class);
verify(conn).setRequestProperty(anyString(), valueCaptor.capture());
for (String value : valueCaptor.getAllValues()) {
assertThat(value, not(startsWith("Authorization")));
}
verify(conn).setRequestProperty(eq("User-Agent"), anyString());
verify(conn, never()).setRequestProperty(eq("Authorization"), anyString());
}

@Test
Expand All @@ -154,11 +151,8 @@ public void shouldNotSetAuthorizationHeaderWithEmptyPassword() throws IOExceptio

server.getUrl(new URL("http://exmaple.org/"));

ArgumentCaptor<String> valueCaptor = ArgumentCaptor.forClass(String.class);
verify(conn).setRequestProperty(anyString(), valueCaptor.capture());
for (String value : valueCaptor.getAllValues()) {
assertThat(value, not(startsWith("Authorization")));
}
verify(conn).setRequestProperty(eq("User-Agent"), anyString());
verify(conn, never()).setRequestProperty(eq("Authorization"), anyString());
}

@Test
Expand All @@ -175,11 +169,8 @@ public void shouldNotSetAuthorizationHeaderWithEmptyLoginName() throws IOExcepti

server.getUrl(new URL("http://exmaple.org/"));

ArgumentCaptor<String> valueCaptor = ArgumentCaptor.forClass(String.class);
verify(conn).setRequestProperty(anyString(), valueCaptor.capture());
for (String value : valueCaptor.getAllValues()) {
assertThat(value, not(startsWith("Authorization")));
}
verify(conn).setRequestProperty(eq("User-Agent"), anyString());
verify(conn, never()).setRequestProperty(eq("Authorization"), anyString());
}

@Test
Expand All @@ -196,11 +187,8 @@ public void shouldNotSetAuthorizationHeaderWithEmptyPasswordCredentials() throws

server.getUrl(new URL("http://exmaple.org/"));

ArgumentCaptor<String> valueCaptor = ArgumentCaptor.forClass(String.class);
verify(conn).setRequestProperty(anyString(), valueCaptor.capture());
for (String value : valueCaptor.getAllValues()) {
assertThat(value, not(startsWith("Authorization")));
}
verify(conn).setRequestProperty(eq("User-Agent"), anyString());
verify(conn, never()).setRequestProperty(eq("Authorization"), anyString());
}

private HttpConnectionUtil mockConnection() throws IOException {
Expand Down

0 comments on commit c8d78ce

Please sign in to comment.