Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle escape sequences in artifacts generation #496

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions ballerina-tests/main.bal
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ type Person record {
int age;
};

listener af:HTTPListener ep1 = new ();

service /hello\- on ep1 {

resource function post hello\-query() returns string|error {
return "Hello from the hello-query";
}
}

service /hello on ep {
resource function default all() returns @af:HttpOutput string {
return "Hello from all";
Expand Down
108 changes: 108 additions & 0 deletions ballerina-tests/tests/resources/escape-seq.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
{
"Data": {
"httpPayload": {
"Url": "https://bal-dev.azurewebsites.net/api/hello-/hello-query",
"Method": "POST",
"Query": {},
"Headers": {
"Accept": [
"*/*"
],
"Content-Length": [
"4"
],
"Content-Type": [
"application/x-www-form-urlencoded"
],
"Host": [
"bal-dev.azurewebsites.net"
],
"Max-Forwards": [
"10"
],
"User-Agent": [
"curl/7.78.0"
],
"X-ARR-LOG-ID": [
"28d97039-ef3e-4e6f-948b-680f7ff166f7"
],
"CLIENT-IP": [
"112.134.128.105:41856"
],
"DISGUISED-HOST": [
"bal-dev.azurewebsites.net"
],
"X-SITE-DEPLOYMENT-ID": [
"bal-dev"
],
"WAS-DEFAULT-HOSTNAME": [
"bal-dev.azurewebsites.net"
],
"X-Forwarded-Proto": [
"https"
],
"X-AppService-Proto": [
"https"
],
"X-ARR-SSL": [
"2048|256|CN=Microsoft Azure TLS Issuing CA 01, O=Microsoft Corporation, C=US|CN=*.azurewebsites.net, O=Microsoft Corporation, L=Redmond, S=WA, C=US"
],
"X-Forwarded-TlsVersion": [
"1.2"
],
"X-Forwarded-For": [
"112.134.128.105:41856"
],
"X-Original-URL": [
"/api/hello-/hello-query"
],
"X-WAWS-Unencoded-URL": [
"/api/hello-/hello-query"
]
},
"Params": {},
"Identities": [
{
"AuthenticationType": null,
"IsAuthenticated": false,
"Actor": null,
"BootstrapContext": null,
"Claims": [],
"Label": null,
"Name": null,
"NameClaimType": "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name",
"RoleClaimType": "http://schemas.microsoft.com/ws/2008/06/identity/claims/role"
}
],
"Body": "Jack"
}
},
"Metadata": {
"Query": {},
"Headers": {
"Accept": "*/*",
"Content-Length": "4",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "bal-dev.azurewebsites.net",
"Max-Forwards": "10",
"User-Agent": "curl/7.78.0",
"X-ARR-LOG-ID": "28d97039-ef3e-4e6f-948b-680f7ff166f7",
"CLIENT-IP": "112.134.128.105:41856",
"DISGUISED-HOST": "bal-dev.azurewebsites.net",
"X-SITE-DEPLOYMENT-ID": "bal-dev",
"WAS-DEFAULT-HOSTNAME": "bal-dev.azurewebsites.net",
"X-Forwarded-Proto": "https",
"X-AppService-Proto": "https",
"X-ARR-SSL": "2048|256|CN=Microsoft Azure TLS Issuing CA 01, O=Microsoft Corporation, C=US|CN=*.azurewebsites.net, O=Microsoft Corporation, L=Redmond, S=WA, C=US",
"X-Forwarded-TlsVersion": "1.2",
"X-Forwarded-For": "112.134.128.105:41856",
"X-Original-URL": "/api/hello-/hello-query",
"X-WAWS-Unencoded-URL": "/api/hello-/hello-query"
},
"sys": {
"MethodName": "post-hello--hello-query",
"UtcNow": "2022-06-10T07:10:30.1722785Z",
"RandGuid": "19f5a752-e046-4f7f-964f-e53207baed7b"
}
}
}
10 changes: 10 additions & 0 deletions ballerina-tests/tests/test.bal
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ import ballerina/lang.value;
import ballerina/regex;
import ballerina/test;

@test:Config {}
function testEscapeSequences() returns error? {
final http:Client clientEndpoint = check new ("http://localhost:3000");
string jsonFilePath = "./tests/resources/escape-seq.json";
json readJson = check io:fileReadJson(jsonFilePath);
json resp = check clientEndpoint->post("/post-hello--hello-query", readJson);
json expectedResp = {"Outputs": {"resp": {"statusCode": "201", "headers": {"Content-Type": "text/plain"}, "body": "Hello from the hello-query"}}, "Logs": [], "ReturnValue": null};
test:assertEquals(resp, expectedResp);
}

@test:Config {}
function testDefault() returns error? {
final http:Client clientEndpoint = check new ("http://localhost:3000");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public void compileSample() {

DiagnosticResult diagnosticResult = compilation.diagnosticResult();
Assert.assertFalse(diagnosticResult.hasErrors());
Assert.assertEquals(generatedFunctions.size(), 18);
Assert.assertEquals(generatedFunctions.size(), 19);
}

@Test
Expand Down Expand Up @@ -186,4 +186,15 @@ public void testBlobTrigger() {
JsonElement parse = jsonParser.parse(str);
Assert.assertEquals(actual, parse);
}

@Test
public void testEscapeSequence() {
JsonObject httpHello = generatedFunctions.get("post-hello--hello-query");
String str =
"{\"bindings\":[{\"type\":\"httpTrigger\",\"authLevel\":\"anonymous\",\"methods\":[\"post\"]," +
"\"direction\":\"in\",\"name\":\"httpPayload\",\"route\":\"hello-/hello-query\"}," +
"{\"type\":\"http\",\"direction\":\"out\",\"name\":\"resp\"}]}";
JsonElement parse = jsonParser.parse(str);
Assert.assertEquals(httpHello, parse);
}
}
9 changes: 9 additions & 0 deletions compiler-plugin-tests/src/test/resources/handlers/main.bal
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ type Person record {
int age;
};

listener af:HTTPListener ep1 = new ();

service /hello\- on ep1 {

resource function post hello\-query() returns string|error {
return "Hello from the hello-query";
}
}

// @af:HTTPTest
service /hello on ep {
resource function default all() returns @af:HttpOutput string {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,12 @@ public AzureFunctionNameGenerator(ServiceDeclarationNode serviceDeclarationNode)
private String getFunctionName(String servicePath, FunctionDefinitionNode functionDefinitionNode) {
String method = functionDefinitionNode.functionName().text();
StringBuilder resourcePath = new StringBuilder();
servicePath = servicePath.replace("\\", "");
resourcePath.append(servicePath);
for (Node pathBlock : functionDefinitionNode.relativeResourcePath()) {
if (pathBlock.kind() == SyntaxKind.IDENTIFIER_TOKEN) {
resourcePath.append("/").append(((IdentifierToken) pathBlock).text());
String specialCharReplacedPathBlock = (((IdentifierToken) pathBlock).text()).replace("\\", "");
resourcePath.append("/").append(specialCharReplacedPathBlock);
} else if (pathBlock.kind() == SyntaxKind.RESOURCE_PATH_SEGMENT_PARAM) {
Token token = ((ResourcePathParameterNode) pathBlock).paramName();
resourcePath.append("/").append(token.text());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public List<FunctionContext> getBindings() {
Optional<AnnotationNode> httpTriggerAnnot =
getListenerAnnotation(this.serviceDeclarationNode, Constants.ANNOTATION_HTTP_TRIGGER);
String servicePath = Util.resourcePathToString(serviceDeclarationNode.absoluteResourcePath());
servicePath = servicePath.replace("\\", "");
List<FunctionContext> functionContexts = new ArrayList<>();
NodeList<Node> members = this.serviceDeclarationNode.members();
for (Node node : members) {
Expand All @@ -76,7 +77,8 @@ public List<FunctionContext> getBindings() {
resourcePath.append(servicePath);
for (Node pathBlock : functionDefinitionNode.relativeResourcePath()) {
if (pathBlock.kind() == SyntaxKind.IDENTIFIER_TOKEN) {
resourcePath.append("/" + ((IdentifierToken) pathBlock).text());
String specialCharReplacedPathBlock = (((IdentifierToken) pathBlock).text()).replace("\\", "");
resourcePath.append("/").append(specialCharReplacedPathBlock);
continue;
}
if (pathBlock.kind() == SyntaxKind.RESOURCE_PATH_SEGMENT_PARAM) {
Expand Down