Skip to content

Commit

Permalink
Split subscription test suite into sub-modules
Browse files Browse the repository at this point in the history
  • Loading branch information
MohamedSabthar committed Oct 3, 2024
1 parent 78757f9 commit 5d42785
Show file tree
Hide file tree
Showing 35 changed files with 1,317 additions and 897 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ isolated function testDataLoaderWithDifferentAliasForSameField() returns error?
}
isolated function testDataLoaderWithSubscription() returns error? {
string document = check common:getGraphqlDocumentFromFile("dataloader_with_subscription");
websocket:ClientConfiguration config = {subProtocols: [GRAPHQL_TRANSPORT_WS]};
websocket:ClientConfiguration config = {subProtocols: [common:GRAPHQL_TRANSPORT_WS]};
websocket:Client wsClient = check new ("ws://localhost:9090/dataloader", config);
check common:initiateGraphqlWsConnection(wsClient);
check common:sendSubscriptionMessage(wsClient, document, "1");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
// specific language governing permissions and limitations
// under the License.

const GRAPHQL_TRANSPORT_WS = "graphql-transport-ws";

const AUTHOR_LOADER = "authorLoader";
const AUTHOR_UPDATE_LOADER = "authorUpdateLoader";
const BOOK_LOADER = "bookLoader";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ function dataProviderInterceptors() returns string[][] {
isolated function testInterceptorsWithSubscriptionReturningScalar() returns error? {
string document = string `subscription { messages }`;
string url = "ws://localhost:9091/subscription_interceptor1";
websocket:ClientConfiguration config = {subProtocols: [GRAPHQL_TRANSPORT_WS]};
websocket:ClientConfiguration config = {subProtocols: [common:GRAPHQL_TRANSPORT_WS]};
websocket:Client wsClient1 = check new (url, config);
check common:initiateGraphqlWsConnection(wsClient1);
check common:sendSubscriptionMessage(wsClient1, document, "1");
Expand All @@ -119,7 +119,7 @@ isolated function testInterceptorsWithSubscriptionReturningScalar() returns erro
isolated function testInterceptorsWithSubscriptionReturningRecord() returns error? {
string document = check common:getGraphqlDocumentFromFile("interceptors_with_subscription_return_records");
string url = "ws://localhost:9091/subscription_interceptor2";
websocket:ClientConfiguration config = {subProtocols: [GRAPHQL_TRANSPORT_WS]};
websocket:ClientConfiguration config = {subProtocols: [common:GRAPHQL_TRANSPORT_WS]};
websocket:Client wsClient1 = check new (url, config);
check common:initiateGraphqlWsConnection(wsClient1);
check common:sendSubscriptionMessage(wsClient1, document, operationName = "A");
Expand All @@ -141,7 +141,7 @@ isolated function testInterceptorsWithSubscriptionReturningRecord() returns erro
isolated function testInterceptorsWithSubscriptionAndFragments() returns error? {
string document = check common:getGraphqlDocumentFromFile("interceptors_with_fragments_and_subscription");
string url = "ws://localhost:9091/subscription_interceptor3";
websocket:ClientConfiguration config = {subProtocols: [GRAPHQL_TRANSPORT_WS]};
websocket:ClientConfiguration config = {subProtocols: [common:GRAPHQL_TRANSPORT_WS]};
websocket:Client wsClient1 = check new (url, config);
check common:initiateGraphqlWsConnection(wsClient1);
check common:sendSubscriptionMessage(wsClient1, document, operationName = "getStudents");
Expand All @@ -163,7 +163,7 @@ isolated function testInterceptorsWithSubscriptionAndFragments() returns error?
isolated function testInterceptorsWithUnionTypeSubscription() returns error? {
string document = check common:getGraphqlDocumentFromFile("interceptors_with_subscription_return_union_type");
string url = "ws://localhost:9091/subscription_interceptor4";
websocket:ClientConfiguration config = {subProtocols: [GRAPHQL_TRANSPORT_WS]};
websocket:ClientConfiguration config = {subProtocols: [common:GRAPHQL_TRANSPORT_WS]};
websocket:Client wsClient1 = check new (url, config);
check common:initiateGraphqlWsConnection(wsClient1);
check common:sendSubscriptionMessage(wsClient1, document, operationName = "unionTypes1");
Expand Down Expand Up @@ -206,7 +206,7 @@ isolated function testInterceptorsWithUnionTypeSubscription() returns error? {
isolated function testInterceptorsReturnBeforeResolverWithSubscription() returns error? {
string document = string `subscription { messages }`;
string url = "ws://localhost:9091/subscription_interceptor5";
websocket:ClientConfiguration config = {subProtocols: [GRAPHQL_TRANSPORT_WS]};
websocket:ClientConfiguration config = {subProtocols: [common:GRAPHQL_TRANSPORT_WS]};
websocket:Client wsClient1 = check new (url, config);
check common:initiateGraphqlWsConnection(wsClient1);
check common:sendSubscriptionMessage(wsClient1, document, "1");
Expand All @@ -228,7 +228,7 @@ isolated function testInterceptorsReturnBeforeResolverWithSubscription() returns
isolated function testInterceptorsDestructiveModificationWithSubscription() returns error? {
string document = string `subscription { messages }`;
string url = "ws://localhost:9091/subscription_interceptor6";
websocket:ClientConfiguration config = {subProtocols: [GRAPHQL_TRANSPORT_WS]};
websocket:ClientConfiguration config = {subProtocols: [common:GRAPHQL_TRANSPORT_WS]};
websocket:Client wsClient1 = check new (url, config);
check common:initiateGraphqlWsConnection(wsClient1);
check common:sendSubscriptionMessage(wsClient1, document, "1");
Expand Down Expand Up @@ -276,7 +276,7 @@ isolated function testInterceptorsDestructiveModificationWithSubscription() retu
isolated function testInterceptorsWithSubscribersRunSimultaniously1() returns error? {
final string document = string `subscription { messages }`;
string url = "ws://localhost:9091/subscription_interceptor1";
websocket:ClientConfiguration config = {subProtocols: [GRAPHQL_TRANSPORT_WS]};
websocket:ClientConfiguration config = {subProtocols: [common:GRAPHQL_TRANSPORT_WS]};
final websocket:Client wsClient1 = check new (url, config);
check common:initiateGraphqlWsConnection(wsClient1);

Expand Down Expand Up @@ -315,7 +315,7 @@ isolated function testInterceptorsWithSubscribersRunSimultaniously1() returns er
isolated function testInterceptorsWithSubscribersRunSimultaniously2() returns error? {
final string document = check common:getGraphqlDocumentFromFile("interceptors_with_subscription_return_union_type");
string url = "ws://localhost:9091/subscription_interceptor4";
websocket:ClientConfiguration config = {subProtocols: [GRAPHQL_TRANSPORT_WS]};
websocket:ClientConfiguration config = {subProtocols: [common:GRAPHQL_TRANSPORT_WS]};
final websocket:Client wsClient1 = check new (url, config);
check common:initiateGraphqlWsConnection(wsClient1);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
// specific language governing permissions and limitations
// under the License.

const GRAPHQL_TRANSPORT_WS = "graphql-transport-ws";

final readonly & Person p2 = {
name: "Walter White",
age: 50,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,6 @@ final readonly & TaskTable tasks = table [
];

// WebSocket Sub Protocols
const GRAPHQL_TRANSPORT_WS = "graphql-transport-ws";
const WS_SUB_PROTOCOL = "Sec-WebSocket-Protocol";

const int DEFAULT_INT_VALUE = 20;
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright (c) 2022, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
//
// WSO2 Inc. 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/graphql;
import ballerina/graphql_test_common as common;
import ballerina/test;
import ballerina/websocket;

@test:Config {
groups: ["listener", "subscriptions"]
}
function testAttachServiceWithSubscriptionToHttp2BasedListener() returns error? {
graphql:Error? result = http2BasedListener.attach(subscriptionService);
test:assertTrue(result is graphql:Error);
graphql:Error err = <graphql:Error>result;
string expecctedMessage = string `Websocket listener initialization failed due to the incompatibility of ` +
string `provided HTTP(version 2.0) listener`;
test:assertEquals(err.message(), expecctedMessage);
}

@test:Config {
groups: ["listener", "subscriptions"]
}
function testAttachServiceWithSubscriptionToHttp1BasedListener() returns error? {
string document = string `subscription { messages }`;
string url = "ws://localhost:9091/service_with_http1";
websocket:ClientConfiguration config = {subProtocols: [common:GRAPHQL_TRANSPORT_WS]};
websocket:Client wsClient1 = check new (url, config);
check common:initiateGraphqlWsConnection(wsClient1);
check common:sendSubscriptionMessage(wsClient1, document, "1");

websocket:Client wsClient2 = check new (url, config);
check common:initiateGraphqlWsConnection(wsClient2);
check common:sendSubscriptionMessage(wsClient2, document, "2");

foreach int i in 1 ..< 4 {
json expectedMsgPayload = {data: {messages: i}};
check common:validateNextMessage(wsClient1, expectedMsgPayload, id = "1");
check common:validateNextMessage(wsClient2, expectedMsgPayload, id = "2");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ import ballerina/websocket;
groups: ["context", "subscriptions"]
}
isolated function testContextWithSubscriptions() returns error? {
string url = "ws://localhost:9092/context";
string url = "ws://localhost:9091/context";
string document = string `subscription { messages }`;
websocket:ClientConfiguration configs = {
customHeaders: {
"scope": "admin"
},
subProtocols: [GRAPHQL_TRANSPORT_WS]
subProtocols: [common:GRAPHQL_TRANSPORT_WS]
};
websocket:Client wsClient = check new (url, configs);
check common:initiateGraphqlWsConnection(wsClient);
Expand All @@ -43,13 +43,13 @@ isolated function testContextWithSubscriptions() returns error? {
groups: ["context", "subscriptions"]
}
isolated function testContextWithInvalidScopeInSubscriptions() returns error? {
string url = "ws://localhost:9092/context";
string url = "ws://localhost:9091/context";
string document = string `subscription { messages }`;
websocket:ClientConfiguration configs = {
customHeaders: {
"scope": "user"
},
subProtocols: [GRAPHQL_TRANSPORT_WS]
subProtocols: [common:GRAPHQL_TRANSPORT_WS]
};
websocket:Client wsClient = check new (url, configs);
check common:initiateGraphqlWsConnection(wsClient);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import ballerina/websocket;
isolated function testSubscriptionWithConstraints() returns error? {
string document = check common:getGraphqlDocumentFromFile("constraints");
string url = "ws://localhost:9091/constraints";
websocket:ClientConfiguration config = {subProtocols: [GRAPHQL_TRANSPORT_WS]};
websocket:ClientConfiguration config = {subProtocols: [common:GRAPHQL_TRANSPORT_WS]};
websocket:Client wsClient = check new (url, config);
check common:initiateGraphqlWsConnection(wsClient);
check common:sendSubscriptionMessage(wsClient, document, operationName = "Sub");
Expand All @@ -38,7 +38,7 @@ isolated function testSubscriptionWithConstraints() returns error? {
isolated function testMultipleSubscriptionClientsWithConstraints() returns error? {
string document = check common:getGraphqlDocumentFromFile("constraints");
string url = "ws://localhost:9091/constraints";
websocket:ClientConfiguration config = {subProtocols: [GRAPHQL_TRANSPORT_WS]};
websocket:ClientConfiguration config = {subProtocols: [common:GRAPHQL_TRANSPORT_WS]};
websocket:Client wsClient1 = check new (url, config);
check common:initiateGraphqlWsConnection(wsClient1);
check common:sendSubscriptionMessage(wsClient1, document, "1", operationName = "Sub");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (c) 2024 WSO2 LLC. (http://www.wso2.com).
//
// 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/graphql;
import ballerina/http;

listener http:Listener http2Listener = new http:Listener(9090);
listener graphql:Listener http2BasedListener = new (http2Listener);

listener graphql:Listener subscriptionListener = new (9091);
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright (c) 2024 WSO2 LLC. (http://www.wso2.com).
//
// 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.

public service class AccountDetails {
final string name;
final int createdYear;

function init(string name, int createdYear) {
self.name = name;
self.createdYear = createdYear;
}

resource function get name() returns string {
return self.name;
}

resource function get createdYear() returns int {
return self.createdYear;
}
}

public service class Product {
private final string id;

function init(string id) {
self.id = id;
}

resource function get id() returns string {
return self.id;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,6 @@ public type Reviews readonly & record {|
int stars;
|};

public type Review record {|
Product product;
int score;
string description;
|};

public type AccountRecords record {|
map<AccountDetails> details;
|};

public type Book readonly & record {
string name;
string author;
};

public enum Weekday {
SUNDAY,
MONDAY,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright (c) 2024 WSO2 LLC. (http://www.wso2.com).
//
// 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/graphql;
import ballerina/http;

graphql:Service subscriptionService = service object {
isolated resource function get name() returns string {
return "Walter White";
}

isolated resource function subscribe messages() returns stream<int, error?> {
int[] intArray = [1, 2, 3, 4, 5];
return intArray.toStream();
}
};

isolated service /service_with_http1 on subscriptionListener {
isolated resource function get greet() returns string {
return "welcome!";
}

isolated resource function subscribe messages() returns stream<int, error?> {
int[] intArray = [1, 2, 3, 4, 5];
return intArray.toStream();
}
}

@graphql:ServiceConfig {
contextInit:
isolated function(http:RequestContext requestContext, http:Request request) returns graphql:Context|error {
graphql:Context context = new;
context.set("scope", check request.getHeader("scope"));
return context;
}
}
service /context on subscriptionListener {
isolated resource function get greet() returns string {
return "welcome!";
}

isolated resource function subscribe messages(graphql:Context context) returns stream<int, error?>|error {
var scope = context.get("scope");
if scope is string && scope == "admin" {
int[] intArray = [1, 2, 3, 4, 5];
return intArray.toStream();
}
return error("You don't have permission to retrieve data");
}
}

service /constraints on subscriptionListener {
isolated resource function get greet() returns string {
return "welcome!";
}

isolated resource function subscribe movie(MovieDetails movie) returns stream<Reviews?, error?> {
return movie.reviews.toStream();
}
}
Loading

0 comments on commit 5d42785

Please sign in to comment.