Fix handling of parameters when documenting non-GET requests

Fixes gh-683
This commit is contained in:
Andy Wilkinson
2020-05-06 13:35:19 +01:00
parent f5dda622d0
commit 0146f1a8ca
6 changed files with 66 additions and 12 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2019 the original author or authors.
* Copyright 2014-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -91,8 +91,10 @@ public class CurlRequestSnippet extends TemplatedSnippet {
}
private boolean includeParametersInUri(OperationRequest request) {
return request.getMethod() == HttpMethod.GET || (request.getContent().length > 0
&& !MediaType.APPLICATION_FORM_URLENCODED.isCompatibleWith(request.getHeaders().getContentType()));
HttpMethod method = request.getMethod();
return (method != HttpMethod.PUT && method != HttpMethod.POST && method != HttpMethod.PATCH)
|| (request.getContent().length > 0 && !MediaType.APPLICATION_FORM_URLENCODED
.isCompatibleWith(request.getHeaders().getContentType()));
}
private String getOptions(Operation operation) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2019 the original author or authors.
* Copyright 2014-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -129,8 +129,10 @@ public class HttpieRequestSnippet extends TemplatedSnippet {
}
private boolean includeParametersInUri(OperationRequest request) {
return request.getMethod() == HttpMethod.GET || (request.getContent().length > 0
&& !MediaType.APPLICATION_FORM_URLENCODED.isCompatibleWith(request.getHeaders().getContentType()));
HttpMethod method = request.getMethod();
return (method != HttpMethod.PUT && method != HttpMethod.POST && method != HttpMethod.PATCH)
|| (request.getContent().length > 0 && !MediaType.APPLICATION_FORM_URLENCODED
.isCompatibleWith(request.getHeaders().getContentType()));
}
private boolean includeParametersAsFormOptions(OperationRequest request) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2019 the original author or authors.
* Copyright 2014-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -92,8 +92,10 @@ public class HttpRequestSnippet extends TemplatedSnippet {
}
private boolean includeParametersInUri(OperationRequest request) {
return request.getMethod() == HttpMethod.GET || (request.getContent().length > 0
&& !MediaType.APPLICATION_FORM_URLENCODED.isCompatibleWith(request.getHeaders().getContentType()));
HttpMethod method = request.getMethod();
return (method != HttpMethod.PUT && method != HttpMethod.POST && method != HttpMethod.PATCH)
|| (request.getContent().length > 0 && !MediaType.APPLICATION_FORM_URLENCODED
.isCompatibleWith(request.getHeaders().getContentType()));
}
private List<Map<String, String>> getHeaders(OperationRequest request) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2019 the original author or authors.
* Copyright 2014-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -326,4 +326,20 @@ public class CurlRequestSnippetTests extends AbstractSnippetTests {
.withContent("$ curl 'http://localhost/foo?a=alpha&b=bravo' -i " + "-X POST -d 'Some content'"));
}
@Test
public void deleteWithParameters() throws IOException {
new CurlRequestSnippet(this.commandFormatter).document(this.operationBuilder.request("http://localhost/foo")
.method("DELETE").param("a", "alpha").param("b", "bravo").build());
assertThat(this.generatedSnippets.curlRequest())
.is(codeBlock("bash").withContent("$ curl 'http://localhost/foo?a=alpha&b=bravo' -i " + "-X DELETE"));
}
@Test
public void deleteWithQueryString() throws IOException {
new CurlRequestSnippet(this.commandFormatter).document(
this.operationBuilder.request("http://localhost/foo?a=alpha&b=bravo").method("DELETE").build());
assertThat(this.generatedSnippets.curlRequest())
.is(codeBlock("bash").withContent("$ curl 'http://localhost/foo?a=alpha&b=bravo' -i " + "-X DELETE"));
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2019 the original author or authors.
* Copyright 2014-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -330,4 +330,20 @@ public class HttpieRequestSnippetTests extends AbstractSnippetTests {
.withContent("$ echo 'Some content' | http POST " + "'http://localhost/foo?a=alpha&b=bravo'"));
}
@Test
public void deleteWithParameters() throws IOException {
new HttpieRequestSnippet(this.commandFormatter).document(this.operationBuilder.request("http://localhost/foo")
.method("DELETE").param("a", "alpha").param("b", "bravo").build());
assertThat(this.generatedSnippets.httpieRequest())
.is(codeBlock("bash").withContent("$ http DELETE 'http://localhost/foo?a=alpha&b=bravo'"));
}
@Test
public void deleteWithQueryString() throws IOException {
new HttpieRequestSnippet(this.commandFormatter).document(
this.operationBuilder.request("http://localhost/foo?a=alpha&b=bravo").method("DELETE").build());
assertThat(this.generatedSnippets.httpieRequest())
.is(codeBlock("bash").withContent("$ http DELETE 'http://localhost/foo?a=alpha&b=bravo'"));
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2019 the original author or authors.
* Copyright 2014-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -307,6 +307,22 @@ public class HttpRequestSnippetTests extends AbstractSnippetTests {
assertThat(this.generatedSnippets.httpRequest()).contains("Title for the request");
}
@Test
public void deleteWithParameters() throws IOException {
new HttpRequestSnippet().document(this.operationBuilder.request("http://localhost/foo").method("DELETE")
.param("a", "alpha").param("b", "bravo").build());
assertThat(this.generatedSnippets.httpRequest())
.is(httpRequest(RequestMethod.DELETE, "/foo?a=alpha&b=bravo").header("Host", "localhost"));
}
@Test
public void deleteWithQueryString() throws IOException {
new HttpRequestSnippet().document(
this.operationBuilder.request("http://localhost/foo?a=alpha&b=bravo").method("DELETE").build());
assertThat(this.generatedSnippets.httpRequest())
.is(httpRequest(RequestMethod.DELETE, "/foo?a=alpha&b=bravo").header("Host", "localhost"));
}
private String createPart(String content) {
return this.createPart(content, true);
}