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
pull/1872/head
Phillip Webb 10 years ago
parent 3c9476fbe6
commit 6027b2405c

@ -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) {

@ -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<HttpUriRequest> 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();

Loading…
Cancel
Save