From 2297bd8f0c4ea3bbeebd3768852a9c814b6f3499 Mon Sep 17 00:00:00 2001 From: ayeshLK Date: Wed, 13 Sep 2023 14:01:34 +0530 Subject: [PATCH] Add tests for additional parameter support in subscription request --- .../tests/subscription_initiation_test.bal | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/ballerina/tests/subscription_initiation_test.bal b/ballerina/tests/subscription_initiation_test.bal index 4fa7aff5..0135dc73 100644 --- a/ballerina/tests/subscription_initiation_test.bal +++ b/ballerina/tests/subscription_initiation_test.bal @@ -26,6 +26,13 @@ const string HUB_FAILURE_URL = "http://127.0.0.1:9192/common/failed"; const string COMMON_TOPIC = "https://sample.topic.com"; service /common on new http:Listener(9192) { + private final readonly & [string, string][] expectedAdditionalParams = [ + ["param1", "value1"], ["param2", "value2"] + ]; + private final readonly & [string, string][] expectedAdditionalHeaders = [ + ["head1", "headValue1"], ["head2", "headValue2"] + ]; + isolated resource function get discovery(http:Caller caller, http:Request request) returns error? { http:Response response = new; response.addHeader("Link", "; rel=\"hub\""); @@ -36,6 +43,30 @@ service /common on new http:Listener(9192) { isolated resource function post hub(http:Caller caller, http:Request request) returns error? { check caller->respond(); } + + isolated resource function post hub/additional(http:Caller caller, http:Request request) returns error? { + map params = check request.getFormParams(); + foreach var ['key, value] in self.expectedAdditionalParams { + test:assertTrue(params.hasKey('key), "expected additional parameter not present"); + test:assertEquals(params.get('key), value, "expected parameter value not present"); + } + foreach var [header, headerValue] in self.expectedAdditionalHeaders { + test:assertTrue(request.hasHeader(header), "expected additional header not present"); + test:assertEquals(check request.getHeader(header), headerValue, "expected header value not present"); + } + check caller->respond(); + } + + isolated resource function post hub/additional/unsub(http:Caller caller, http:Request request) returns error? { + map params = check request.getFormParams(); + foreach var ['key, _] in self.expectedAdditionalParams { + test:assertTrue(!params.hasKey('key), "unexpected additional parameter present"); + } + foreach var [header, _] in self.expectedAdditionalHeaders { + test:assertTrue(!request.hasHeader(header), "unexpected additional header present"); + } + check caller->respond(); + } } isolated function getServiceAnnotationConfig(string|[string, string] target) returns SubscriberServiceConfiguration { @@ -115,6 +146,22 @@ isolated function testSubscriptionInitiationFailureWithHubAndTopic() returns err } } +@test:Config { + groups: ["subscriptionInitiation"] +} +isolated function testSubscriptionInitiationSuccessWithAdditionalParams() returns error? { + SubscriberServiceConfiguration config = getServiceAnnotationConfig([ "http://127.0.0.1:9192/common/hub/additional", COMMON_TOPIC ]); + config.additionalParams = { + "param1": "value1", + "param2": "value2" + }; + config.additionalHeaders = { + "head1": "headValue1", + "head2": "headValue2" + }; + check subscribe(config, "https://sample.com/sub1"); +} + @test:Config { groups: ["unSubscriptionInitiation"] } @@ -187,6 +234,27 @@ isolated function testUnSubscriptionInitiationDisable() returns error? { check unsubscribe(config, "https://sample.com/sub1"); } +@test:Config { + groups: ["unSubscriptionInitiation"] +} +isolated function testUnSubscriptionInitiationSuccessWithAdditionalParams() returns error? { + SubscriberServiceConfiguration config = { + target: ["http://127.0.0.1:9192/common/hub/additional/unsub", COMMON_TOPIC], + leaseSeconds: 36000, + callback: CALLBACK, + unsubscribeOnShutdown: true + }; + config.additionalParams = { + "param1": "value1", + "param2": "value2" + }; + config.additionalHeaders = { + "head1": "headValue1", + "head2": "headValue2" + }; + check unsubscribe(config, "https://sample.com/sub1"); +} + listener Listener ls = new (9100); @test:Config {