Skip to content

Commit

Permalink
Add tests for additional parameter support in subscription request
Browse files Browse the repository at this point in the history
  • Loading branch information
ayeshLK committed Sep 14, 2023
1 parent 75da134 commit 2297bd8
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions ballerina/tests/subscription_initiation_test.bal
Original file line number Diff line number Diff line change
Expand Up @@ -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", "<http://127.0.0.1:9192/common/hub>; rel=\"hub\"");
Expand All @@ -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<string> 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<string> 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 {
Expand Down Expand Up @@ -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"]
}
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 2297bd8

Please sign in to comment.