mirror of
https://gitee.com/dcren/initializr.git
synced 2025-11-08 10:24:58 +08:00
Add support for HTTPie
Closes gh-70
This commit is contained in:
@@ -7,6 +7,7 @@ order.
|
||||
|
||||
=== Release 1.0.0 (In progress)
|
||||
|
||||
* https://github.com/spring-io/initializr/issues/70[#70]: explicit support for HTTPie (similar to curl).
|
||||
* https://github.com/spring-io/initializr/issues/67[#67]: explicit support for curl by returning a text
|
||||
description of the service instead of the raw meta-data. curl users can still discover the json metadata
|
||||
by setting the appropriate Accept header.
|
||||
|
||||
@@ -57,6 +57,17 @@ class CommandLineHelpGenerator {
|
||||
doGenerateCapabilities(model)
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate the capabilities of the service using "HTTPie" as a plain text
|
||||
* document.
|
||||
*/
|
||||
String generateHttpieCapabilities(InitializrMetadata metadata, String serviceUrl) {
|
||||
def model = initializeModel(metadata, serviceUrl)
|
||||
model['examples'] = template 'httpie-examples.txt', model
|
||||
model['hasExamples'] = true
|
||||
doGenerateCapabilities(model)
|
||||
}
|
||||
|
||||
private doGenerateCapabilities(def model) {
|
||||
template 'cli-capabilities.txt', model
|
||||
}
|
||||
|
||||
@@ -77,12 +77,15 @@ class MainController extends AbstractInitializrController {
|
||||
def metadata = metadataProvider.get()
|
||||
|
||||
def builder = ResponseEntity.ok().contentType(MediaType.TEXT_PLAIN)
|
||||
if (userAgent && userAgent.startsWith(WebConfig.CURL_USER_AGENT_PREFIX)) {
|
||||
builder.body(commandLineHelpGenerator.generateCurlCapabilities(metadata,appUrl))
|
||||
}
|
||||
else {
|
||||
builder.body(commandLineHelpGenerator.generateGenericCapabilities(metadata, appUrl))
|
||||
if (userAgent) {
|
||||
if (userAgent.startsWith(WebConfig.CURL_USER_AGENT_PREFIX)) {
|
||||
return builder.body(commandLineHelpGenerator.generateCurlCapabilities(metadata, appUrl))
|
||||
}
|
||||
if (userAgent.startsWith(WebConfig.HTTPIE_USER_AGENT_PREFIX)) {
|
||||
return builder.body(commandLineHelpGenerator.generateHttpieCapabilities(metadata, appUrl))
|
||||
}
|
||||
}
|
||||
builder.body(commandLineHelpGenerator.generateGenericCapabilities(metadata, appUrl))
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/", produces = ["application/vnd.initializr.v2+json", "application/json"])
|
||||
|
||||
@@ -37,6 +37,8 @@ class WebConfig extends WebMvcConfigurerAdapter {
|
||||
|
||||
static final String CURL_USER_AGENT_PREFIX = 'curl'
|
||||
|
||||
static final String HTTPIE_USER_AGENT_PREFIX = 'HTTPie'
|
||||
|
||||
@Override
|
||||
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
|
||||
configurer.defaultContentTypeStrategy(new CommandLineContentNegotiationStrategy())
|
||||
@@ -59,7 +61,7 @@ class WebConfig extends WebMvcConfigurerAdapter {
|
||||
}
|
||||
String userAgent = request.getHeader(HttpHeaders.USER_AGENT)
|
||||
if (userAgent) {
|
||||
if (userAgent.startsWith(CURL_USER_AGENT_PREFIX)) {
|
||||
if (userAgent.startsWith(CURL_USER_AGENT_PREFIX) || userAgent.startsWith(HTTPIE_USER_AGENT_PREFIX)) {
|
||||
return Collections.singletonList(MediaType.TEXT_PLAIN)
|
||||
}
|
||||
}
|
||||
|
||||
13
initializr/src/main/resources/templates/httpie-examples.txt
Normal file
13
initializr/src/main/resources/templates/httpie-examples.txt
Normal file
@@ -0,0 +1,13 @@
|
||||
To create a default project:
|
||||
\$ http ${serviceUrl}/starter.zip -d
|
||||
|
||||
To create a web project using Java 8:
|
||||
\$ http ${serviceUrl}/starter.zip dependencies==web \\
|
||||
javaVersion==1.8 -d
|
||||
|
||||
To create a web/data-jpa gradle project unpacked:
|
||||
\$ http ${serviceUrl}/starter.tgz dependencies==web,data-jpa \\
|
||||
type==gradle-project baseDir==my-dir | tar -xzvf -
|
||||
|
||||
To generate a Maven POM with war packaging:
|
||||
\$ http ${serviceUrl}/pom.xml packaging==war -o pom.xml
|
||||
@@ -65,6 +65,19 @@ class CommandLineHelpGeneratorTest {
|
||||
assertThat content, containsString('curl')
|
||||
}
|
||||
|
||||
@Test
|
||||
void generateHttpCapabilities() {
|
||||
def metadata = InitializrMetadataBuilder.withDefaults().addDependencyGroup("test",
|
||||
createDependency('id-b', 'depB'),
|
||||
createDependency('id-a', 'depA', 'and some description')).validateAndGet()
|
||||
String content = generator.generateHttpieCapabilities(metadata, "https://fake-service")
|
||||
assertThat content, containsString('id-a - depA: and some description')
|
||||
assertThat content, containsString('id-b - depB')
|
||||
assertThat content, containsString("https://fake-service")
|
||||
assertThat content, containsString('Examples:')
|
||||
assertThat content, not(containsString('curl'))
|
||||
assertThat content, containsString("http https://fake-service")
|
||||
}
|
||||
|
||||
private static def createDependency(String id, String name) {
|
||||
createDependency(id, name, null)
|
||||
|
||||
@@ -182,6 +182,25 @@ class MainControllerIntegrationTests extends AbstractInitializrControllerIntegra
|
||||
validateCurrentMetadata(response)
|
||||
}
|
||||
|
||||
@Test
|
||||
void httpieReceivesTextByDefault() {
|
||||
ResponseEntity<String> response = invokeHome('HTTPie/0.8.0', "*/*")
|
||||
validateHttpIeHelpContent(response)
|
||||
}
|
||||
|
||||
@Test // make sure curl can still receive metadata with json
|
||||
void httpieWithAcceptHeaderJson() {
|
||||
ResponseEntity<String> response = invokeHome('HTTPie/0.8.0', "application/json")
|
||||
validateContentType(response, CURRENT_METADATA_MEDIA_TYPE)
|
||||
validateCurrentMetadata(new JSONObject(response.body))
|
||||
}
|
||||
|
||||
@Test
|
||||
void httpieWithAcceptHeaderTextPlain() {
|
||||
ResponseEntity<String> response = invokeHome('HTTPie/0.8.0', "text/plain")
|
||||
validateHttpIeHelpContent(response)
|
||||
}
|
||||
|
||||
@Test
|
||||
void unknownCliWithTextPlain() {
|
||||
ResponseEntity<String> response = invokeHome(null, "text/plain")
|
||||
@@ -212,6 +231,15 @@ class MainControllerIntegrationTests extends AbstractInitializrControllerIntegra
|
||||
containsString("curl")))
|
||||
}
|
||||
|
||||
private void validateHttpIeHelpContent(ResponseEntity<String> response) {
|
||||
validateContentType(response, MediaType.TEXT_PLAIN)
|
||||
assertThat(response.body, allOf(
|
||||
containsString("Spring Initializr"),
|
||||
containsString('Examples:'),
|
||||
not(containsString("curl")),
|
||||
containsString("http")))
|
||||
}
|
||||
|
||||
private void validateGenericHelpContent(ResponseEntity<String> response) {
|
||||
validateContentType(response, MediaType.TEXT_PLAIN)
|
||||
assertThat(response.body, allOf(
|
||||
|
||||
Reference in New Issue
Block a user