From 6027b2405ca60a22a1ba18af4479b2ef4f62537f Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Mon, 10 Nov 2014 21:44:35 -0800 Subject: [PATCH] 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 --- .../cli/command/init/InitializrService.java | 2 ++ .../cli/command/init/InitCommandTests.java | 25 +++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/init/InitializrService.java b/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/init/InitializrService.java index 34c3ceac7d..b0a8060922 100644 --- a/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/init/InitializrService.java +++ b/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/init/InitializrService.java @@ -141,6 +141,8 @@ class InitializrService { private CloseableHttpResponse execute(HttpUriRequest request, Object url, String description) { try { + request.addHeader("User-Agent", "SpringBootCli/" + + getClass().getPackage().getImplementationVersion()); return getHttp().execute(request); } catch (IOException ex) { diff --git a/spring-boot-cli/src/test/java/org/springframework/boot/cli/command/init/InitCommandTests.java b/spring-boot-cli/src/test/java/org/springframework/boot/cli/command/init/InitCommandTests.java index af203bdd92..f976c41016 100644 --- a/spring-boot-cli/src/test/java/org/springframework/boot/cli/command/init/InitCommandTests.java +++ b/spring-boot-cli/src/test/java/org/springframework/boot/cli/command/init/InitCommandTests.java @@ -26,15 +26,24 @@ import java.util.zip.ZipOutputStream; 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.Test; 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 static org.hamcrest.Matchers.startsWith; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.verify; /** * Tests for {@link InitCommand} @@ -50,6 +59,14 @@ public class InitCommandTests extends AbstractHttpClientMockTests { private final InitCommand command; + @Captor + private ArgumentCaptor requestCaptor; + + @Before + public void setupMocks() { + MockitoAnnotations.initMocks(this); + } + public InitCommandTests() { InitializrService initializrService = new InitializrService(this.http); this.handler = new TestableInitCommandOptionHandler(initializrService); @@ -276,6 +293,14 @@ public class InitCommandTests extends AbstractHttpClientMockTests { 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) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream();