mirror of https://github.com/knative/func.git
feat: add echo for springboot http template (#1072)
* feat: add echo func * chore: package * docs: update docs * refactor: add space between headers * refactor: regenerate zz file * refactor: clean with pre-commit * refactor: add func_yaml-schema.json file * refactor: rename test and fix white-spaces * refactor: remove .idea files * test: change http test * test: change expected result
This commit is contained in:
parent
80093d66a4
commit
f828b0998c
|
@ -2,7 +2,7 @@
|
|||
|
||||
Welcome to your new Function project!
|
||||
|
||||
This sample project contains a single function based on Spring Cloud Function: `functions.CloudFunctionApplication.uppercase()`, which returns the uppercase of the data passed.
|
||||
This sample project contains a single function based on Spring Cloud Function: `functions.CloudFunctionApplication.echo()`, which returns an echo of the data passed via HTTP request.
|
||||
|
||||
## Local execution
|
||||
|
||||
|
@ -86,7 +86,7 @@ export URL=$(kn service describe $(basename $PWD) -ourl)
|
|||
Using `func invoke` command with Path-Based routing:
|
||||
|
||||
```shell script
|
||||
func invoke --target "$URL/uppercase" --data "$(whoami)"
|
||||
func invoke --target "$URL/echo" --data "$(whoami)"
|
||||
```
|
||||
|
||||
If your function class only contains one function, then you can leave out the target path:
|
||||
|
@ -98,7 +98,7 @@ func invoke --data "$(whoami)"
|
|||
### cURL
|
||||
|
||||
```shell script
|
||||
curl -v "$URL/uppercase" \
|
||||
curl -v "$URL/echo" \
|
||||
-H "Content-Type:text/plain" \
|
||||
-w "\n" \
|
||||
-d "$(whoami)"
|
||||
|
@ -116,7 +116,7 @@ curl -v "$URL" \
|
|||
### HTTPie
|
||||
|
||||
```shell script
|
||||
echo "$(whoami)" | http -v "$URL/uppercase"
|
||||
echo "$(whoami)" | http -v "$URL/echo"
|
||||
```
|
||||
|
||||
If your function class only contains one function, then you can leave out the target path:
|
||||
|
|
|
@ -1,9 +1,11 @@
|
|||
package functions;
|
||||
|
||||
import java.util.function.Function;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.messaging.Message;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
@SpringBootApplication
|
||||
public class CloudFunctionApplication {
|
||||
|
@ -13,8 +15,22 @@ public class CloudFunctionApplication {
|
|||
}
|
||||
|
||||
@Bean
|
||||
public Function<String, String> uppercase() {
|
||||
return String::toUpperCase;
|
||||
}
|
||||
public Function<Message<String>, String> echo() {
|
||||
return (inputMessage) -> {
|
||||
|
||||
var stringBuilder = new StringBuilder();
|
||||
inputMessage.getHeaders()
|
||||
.forEach((key, value) -> {
|
||||
stringBuilder.append(key).append(": ").append(value).append(" ");
|
||||
});
|
||||
|
||||
var payload = inputMessage.getPayload();
|
||||
|
||||
if (!payload.isBlank()) {
|
||||
stringBuilder.append("echo: ").append(payload);
|
||||
}
|
||||
|
||||
return stringBuilder.toString();
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,18 +14,29 @@ import static org.hamcrest.Matchers.*;
|
|||
|
||||
@SpringBootTest(classes = CloudFunctionApplication.class,
|
||||
webEnvironment = WebEnvironment.RANDOM_PORT)
|
||||
public class UpperCaseFunctionTest {
|
||||
public class EchoCaseFunctionTest {
|
||||
|
||||
@Autowired
|
||||
private TestRestTemplate rest;
|
||||
|
||||
@Test
|
||||
public void testUpperCase() throws Exception {
|
||||
public void testEchoWithBody() throws Exception {
|
||||
ResponseEntity<String> response = this.rest.exchange(
|
||||
RequestEntity.post(new URI("/uppercase"))
|
||||
RequestEntity.post(new URI("/echo"))
|
||||
.body("hello"), String.class);
|
||||
assertThat(response.getStatusCode()
|
||||
.value(), equalTo(200));
|
||||
assertThat(response.getBody(), equalTo("HELLO"));
|
||||
assertThat(response.getBody(), containsString("echo: hello"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEchoWithoutBody() throws Exception {
|
||||
ResponseEntity<String> response = this.rest.exchange(
|
||||
RequestEntity.get(new URI("/echo/"))
|
||||
.header("custom-header", "custom-value")
|
||||
.build(), String.class);
|
||||
assertThat(response.getStatusCode()
|
||||
.value(), equalTo(200));
|
||||
assertThat(response.getBody(), containsString("custom-header: custom-value"));
|
||||
}
|
||||
}
|
|
@ -86,10 +86,10 @@ var defaultFunctionsHttpValidators = []FunctionHttpResponsivenessValidator{
|
|||
expects: `{"message":"hello"}`,
|
||||
},
|
||||
{runtime: "springboot",
|
||||
targetUrl: "%s/uppercase",
|
||||
targetUrl: "%s/echo",
|
||||
method: "POST",
|
||||
bodyData: "message=hello",
|
||||
expects: `MESSAGE=HELLO`,
|
||||
expects: `message=hello`,
|
||||
},
|
||||
{runtime: "typescript",
|
||||
targetUrl: "%s",
|
||||
|
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue