Commit 6027b240 authored by Phillip Webb's avatar Phillip Webb

Add 'User-Agent' header to CLI REST calls

Update the InitializrService so that a 'SpringBootCli' User-Agent header
is sent with each request. This should allow the server-side code to
gracefully evolve the JSON format if needed.

Fixes gh-1869
parent 3c9476fb
...@@ -141,6 +141,8 @@ class InitializrService { ...@@ -141,6 +141,8 @@ class InitializrService {
private CloseableHttpResponse execute(HttpUriRequest request, Object url, private CloseableHttpResponse execute(HttpUriRequest request, Object url,
String description) { String description) {
try { try {
request.addHeader("User-Agent", "SpringBootCli/"
+ getClass().getPackage().getImplementationVersion());
return getHttp().execute(request); return getHttp().execute(request);
} }
catch (IOException ex) { catch (IOException ex) {
......
...@@ -26,15 +26,24 @@ import java.util.zip.ZipOutputStream; ...@@ -26,15 +26,24 @@ import java.util.zip.ZipOutputStream;
import joptsimple.OptionSet; import joptsimple.OptionSet;
import org.apache.http.Header;
import org.apache.http.client.methods.HttpUriRequest;
import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.TemporaryFolder; import org.junit.rules.TemporaryFolder;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.MockitoAnnotations;
import org.springframework.boot.cli.command.status.ExitStatus; import org.springframework.boot.cli.command.status.ExitStatus;
import static org.hamcrest.Matchers.startsWith;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.verify;
/** /**
* Tests for {@link InitCommand} * Tests for {@link InitCommand}
...@@ -50,6 +59,14 @@ public class InitCommandTests extends AbstractHttpClientMockTests { ...@@ -50,6 +59,14 @@ public class InitCommandTests extends AbstractHttpClientMockTests {
private final InitCommand command; private final InitCommand command;
@Captor
private ArgumentCaptor<HttpUriRequest> requestCaptor;
@Before
public void setupMocks() {
MockitoAnnotations.initMocks(this);
}
public InitCommandTests() { public InitCommandTests() {
InitializrService initializrService = new InitializrService(this.http); InitializrService initializrService = new InitializrService(this.http);
this.handler = new TestableInitCommandOptionHandler(initializrService); this.handler = new TestableInitCommandOptionHandler(initializrService);
...@@ -276,6 +293,14 @@ public class InitCommandTests extends AbstractHttpClientMockTests { ...@@ -276,6 +293,14 @@ public class InitCommandTests extends AbstractHttpClientMockTests {
assertEquals(ExitStatus.ERROR, this.command.run("foobar", "barfoo")); assertEquals(ExitStatus.ERROR, this.command.run("foobar", "barfoo"));
} }
@Test
public void userAgent() throws Exception {
this.command.run("--list", "--target=http://fake-service");
verify(this.http).execute(this.requestCaptor.capture());
Header agent = this.requestCaptor.getValue().getHeaders("User-Agent")[0];
assertThat(agent.getValue(), startsWith("SpringBootCli/"));
}
private byte[] createFakeZipArchive(String fileName, String content) private byte[] createFakeZipArchive(String fileName, String content)
throws IOException { throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream(); ByteArrayOutputStream bos = new ByteArrayOutputStream();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment