Skip to content

Commit

Permalink
Add tests for http default listener
Browse files Browse the repository at this point in the history
  • Loading branch information
TharmiganK committed Jan 30, 2025
1 parent 6fb0507 commit e4acd20
Show file tree
Hide file tree
Showing 9 changed files with 191 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright (c) 2025 WSO2 LLC. (http://www.wso2.org).
//
// WSO2 LLC. licenses this file to you under the Apache License,
// Version 2.0 (the "License"); you may not use this file except
// in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

import ballerina/http;
import ballerina/http_test_common.service1 as _;
import ballerina/http_test_common.service2 as _;
import ballerina/test;

listener http:Listener defaultListener = http:getDefaultListener();

service /api/v3 on defaultListener {

resource function get greeting() returns string {
return "Hello, World from service 3!";
}
}

service /api/v4 on defaultListener {

resource function get greeting() returns string {
return "Hello, World from service 4!";
}
}

listener http:Listener defaultListenerNew = http:getDefaultListener();

service /api/v5 on defaultListenerNew {

resource function get greeting() returns string {
return "Hello, World from service 5!";
}
}

final http:Client defaultListenerClient = check new(string `localhost:${http:defaultListenerPort}/api`);

@test:Config {}
function testDefaultListener() returns error? {
foreach int i in 1...5 {
string response = check defaultListenerClient->/[string `v${i}`]/greeting;
test:assertEquals(response, string `Hello, World from service ${i}!`);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import ballerina/mime;
import ballerina/test;
import ballerina/http_test_common as common;

listener http:Listener mockEP2 = new (9091, httpVersion = http:HTTP_1_1);
final http:Client multipartRespClient = check new ("http://localhost:9091", httpVersion = http:HTTP_1_1);
listener http:Listener mockEP2 = new (multipartResponseTestPort, httpVersion = http:HTTP_1_1);
final http:Client multipartRespClient = check new (string `http://localhost:${multipartResponseTestPort}`, httpVersion = http:HTTP_1_1);

service /multipart on mockEP2 {
resource function get encode_out_response(http:Caller caller, http:Request request) returns error? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ const int trailingHeaderTestPort2 = 9527;

const int corsConfigTestPort = 9013;
const int multipartRequestTestPort = 9018;
const int multipartResponseTestPort = 9019;
const int serviceMediaTypeSubtypePrefixPort = 9579;

const int statusCodeErrorUseCasePort = 9090;
const int statusCodeErrorUseCasePort = 9089;
const int statusCodeErrorPort = 9092;

const int identicalCookiePort = 9093;
Expand Down
10 changes: 10 additions & 0 deletions ballerina-tests/http-security-tests/tests/Config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,13 @@ scopes=["read"]
[[ballerina.auth.users]]
username="eve"
password="123"

[ballerina.http]
defaultListenerPort = 8080

[ballerina.http.defaultListenerConfig]
httpVersion = "1.1"

[ballerina.http.defaultListenerConfig.secureSocket.key]
path = "../resources/certsandkeys/ballerinaKeystore.p12"
password = "ballerina"
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright (c) 2025 WSO2 LLC. (http://www.wso2.org).
//
// WSO2 LLC. licenses this file to you under the Apache License,
// Version 2.0 (the "License"); you may not use this file except
// in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

import ballerina/http;
import ballerina/http_test_common as common;
import ballerina/http_test_common.service1 as _;
import ballerina/http_test_common.service2 as _;
import ballerina/test;

listener http:Listener defaultListener = http:getDefaultListener();

service /api/v3 on defaultListener {

resource function get greeting() returns string {
return "Hello, World from service 3!";
}
}

service /api/v4 on defaultListener {

resource function get greeting() returns string {
return "Hello, World from service 4!";
}
}

listener http:Listener defaultListenerNew = http:getDefaultListener();

service /api/v5 on defaultListenerNew {

resource function get greeting() returns string {
return "Hello, World from service 5!";
}
}

final http:Client defaultListenerClient = check new(string `localhost:${http:defaultListenerPort}/api`,
secureSocket = {
cert: {
path: common:TRUSTSTORE_PATH,
password: "ballerina"
}
},
httpVersion = "1.1"
);

@test:Config {}
function testDefaultListenerWithConfiguration() returns error? {
foreach int i in 1...5 {
string response = check defaultListenerClient->/[string `v${i}`]/greeting;
test:assertEquals(response, string `Hello, World from service ${i}!`);
}
}
3 changes: 3 additions & 0 deletions ballerina-tests/http-test-common/modules/service1/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## Module overview

This module provides a mock HTTP service - service1.
26 changes: 26 additions & 0 deletions ballerina-tests/http-test-common/modules/service1/service.bal
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) 2025 WSO2 LLC. (http://www.wso2.org).
//
// WSO2 LLC. licenses this file to you under the Apache License,
// Version 2.0 (the "License"); you may not use this file except
// in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

import ballerina/http;

listener http:Listener defaultListener = http:getDefaultListener();

service /api/v1 on defaultListener {

resource function get greeting() returns string {
return "Hello, World from service 1!";
}
}
3 changes: 3 additions & 0 deletions ballerina-tests/http-test-common/modules/service2/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## Module overview

This module provides a mock HTTP service - service2.
26 changes: 26 additions & 0 deletions ballerina-tests/http-test-common/modules/service2/service.bal
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) 2025 WSO2 LLC. (http://www.wso2.org).
//
// WSO2 LLC. licenses this file to you under the Apache License,
// Version 2.0 (the "License"); you may not use this file except
// in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

import ballerina/http;

listener http:Listener defaultListener = http:getDefaultListener();

service /api/v2 on defaultListener {

resource function get greeting() returns string {
return "Hello, World from service 2!";
}
}

0 comments on commit e4acd20

Please sign in to comment.