diff --git a/templates/android/library/src/main/java/io/package/Client.kt.twig b/templates/android/library/src/main/java/io/package/Client.kt.twig index 564d3600f..74c45ec29 100644 --- a/templates/android/library/src/main/java/io/package/Client.kt.twig +++ b/templates/android/library/src/main/java/io/package/Client.kt.twig @@ -25,6 +25,7 @@ import java.io.BufferedReader import java.io.File import java.io.RandomAccessFile import java.io.IOException +import java.lang.IllegalArgumentException import java.net.CookieManager import java.net.CookiePolicy import java.security.SecureRandom @@ -176,24 +177,31 @@ class Client @JvmOverloads constructor( * * @return this */ + @Throws(IllegalArgumentException::class) fun setEndpoint(endpoint: String): Client { - this.endpoint = endpoint - - if (this.endpointRealtime == null && endpoint.startsWith("http")) { - this.endpointRealtime = endpoint.replaceFirst("http", "ws") + require(endpoint.startsWith("http://") || endpoint.startsWith("https://")) { + "Invalid endpoint URL: $endpoint" } + this.endpoint = endpoint + this.endpointRealtime = endpoint.replaceFirst("http", "ws") + return this } /** - * Set realtime endpoint - * - * @param endpoint - * - * @return this - */ + * Set realtime endpoint + * + * @param endpoint + * + * @return this + */ + @Throws(IllegalArgumentException::class) fun setEndpointRealtime(endpoint: String): Client { + require(endpoint.startsWith("ws://") || endpoint.startsWith("wss://")) { + "Invalid realtime endpoint URL: $endpoint" + } + this.endpointRealtime = endpoint return this } diff --git a/templates/apple/Sources/Client.swift.twig b/templates/apple/Sources/Client.swift.twig index b9cf1d5ff..d38e6ef6f 100644 --- a/templates/apple/Sources/Client.swift.twig +++ b/templates/apple/Sources/Client.swift.twig @@ -135,14 +135,15 @@ open class Client { /// @return Client /// open func setEndpoint(_ endPoint: String) -> Client { - self.endPoint = endPoint - - if (self.endPointRealtime == nil && endPoint.starts(with: "http")) { - self.endPointRealtime = endPoint - .replacingOccurrences(of: "http://", with: "ws://") - .replacingOccurrences(of: "https://", with: "wss://") + if !endPoint.hasPrefix("http://") && !endPoint.hasPrefix("https://") { + fatalError("Invalid endpoint URL: \(endPoint)") } + self.endPoint = endPoint + self.endPointRealtime = endPoint + .replacingOccurrences(of: "http://", with: "ws://") + .replacingOccurrences(of: "https://", with: "wss://") + return self } @@ -154,8 +155,11 @@ open class Client { /// @return Client /// open func setEndpointRealtime(_ endPoint: String) -> Client { - self.endPointRealtime = endPoint + if !endPoint.hasPrefix("ws://") && !endPoint.hasPrefix("wss://") { + fatalError("Invalid realtime endpoint URL: \(endPoint)") + } + self.endPointRealtime = endPoint return self } diff --git a/templates/cli/lib/client.js.twig b/templates/cli/lib/client.js.twig index 082b4de09..c9b38cc59 100644 --- a/templates/cli/lib/client.js.twig +++ b/templates/cli/lib/client.js.twig @@ -79,8 +79,11 @@ class Client { * @return this */ setEndpoint(endpoint) { - this.endpoint = endpoint; + if (!endpoint.startsWith('http://') && !endpoint.startsWith('https://')) { + throw new {{spec.title | caseUcfirst}}Exception('Invalid endpoint URL: ' + endpoint); + } + this.endpoint = endpoint; return this; } diff --git a/templates/dart/lib/src/client_browser.dart.twig b/templates/dart/lib/src/client_browser.dart.twig index 64ead5fe8..05e646ce0 100644 --- a/templates/dart/lib/src/client_browser.dart.twig +++ b/templates/dart/lib/src/client_browser.dart.twig @@ -67,6 +67,10 @@ class ClientBrowser extends ClientBase with ClientMixin { @override ClientBrowser setEndpoint(String endPoint) { + if (!endPoint.startsWith('http://') && !endPoint.startsWith('https://')) { + throw {{spec.title | caseUcfirst}}Exception('Invalid endpoint URL: $endPoint'); + } + _endPoint = endPoint; return this; } diff --git a/templates/dart/lib/src/client_io.dart.twig b/templates/dart/lib/src/client_io.dart.twig index bdf45ec99..952e361f5 100644 --- a/templates/dart/lib/src/client_io.dart.twig +++ b/templates/dart/lib/src/client_io.dart.twig @@ -79,6 +79,10 @@ class ClientIO extends ClientBase with ClientMixin { @override ClientIO setEndpoint(String endPoint) { + if (!endPoint.startsWith('http://') && !endPoint.startsWith('https://')) { + throw {{spec.title | caseUcfirst}}Exception('Invalid endpoint URL: $endPoint'); + } + _endPoint = endPoint; return this; } diff --git a/templates/deno/src/client.ts.twig b/templates/deno/src/client.ts.twig index 891fd8ce6..fa263da6c 100644 --- a/templates/deno/src/client.ts.twig +++ b/templates/deno/src/client.ts.twig @@ -46,8 +46,11 @@ export class Client { * @return this */ setEndpoint(endpoint: string): this { - this.endpoint = endpoint; + if (!endpoint.startsWith('http://') && !endpoint.startsWith('https://')) { + throw new {{spec.title | caseUcfirst}}Exception('Invalid endpoint URL: ' + endpoint); + } + this.endpoint = endpoint; return this; } diff --git a/templates/dotnet/Package/Client.cs.twig b/templates/dotnet/Package/Client.cs.twig index a6b906f24..8e1ef34a9 100644 --- a/templates/dotnet/Package/Client.cs.twig +++ b/templates/dotnet/Package/Client.cs.twig @@ -106,8 +106,11 @@ namespace {{ spec.title | caseUcfirst }} public Client SetEndpoint(string endpoint) { - _endpoint = endpoint; + if (!endpoint.StartsWith("http://") && !endpoint.StartsWith("https://")) { + throw new {{spec.title | caseUcfirst}}Exception("Invalid endpoint URL: " + endpoint); + } + _endpoint = endpoint; return this; } diff --git a/templates/flutter/lib/src/client_browser.dart.twig b/templates/flutter/lib/src/client_browser.dart.twig index b6c3401a9..4bbc53b8d 100644 --- a/templates/flutter/lib/src/client_browser.dart.twig +++ b/templates/flutter/lib/src/client_browser.dart.twig @@ -78,15 +78,24 @@ class ClientBrowser extends ClientBase with ClientMixin { @override ClientBrowser setEndpoint(String endPoint) { + if (!endPoint.startsWith('http://') && !endPoint.startsWith('https://')) { + throw {{spec.title | caseUcfirst}}Exception('Invalid endpoint URL: $endPoint'); + } + _endPoint = endPoint; _endPointRealtime = endPoint .replaceFirst('https://', 'wss://') .replaceFirst('http://', 'ws://'); + return this; } @override ClientBrowser setEndPointRealtime(String endPoint) { + if (!endPoint.startsWith('ws://') && !endPoint.startsWith('wss://')) { + throw {{spec.title | caseUcfirst}}Exception('Invalid realtime endpoint URL: $endPoint'); + } + _endPointRealtime = endPoint; return this; } diff --git a/templates/flutter/lib/src/client_io.dart.twig b/templates/flutter/lib/src/client_io.dart.twig index 904e55635..66b37ea25 100644 --- a/templates/flutter/lib/src/client_io.dart.twig +++ b/templates/flutter/lib/src/client_io.dart.twig @@ -110,15 +110,24 @@ class ClientIO extends ClientBase with ClientMixin { @override ClientIO setEndpoint(String endPoint) { + if (!endPoint.startsWith('http://') && !endPoint.startsWith('https://')) { + throw {{spec.title | caseUcfirst}}Exception('Invalid endpoint URL: $endPoint'); + } + _endPoint = endPoint; _endPointRealtime = endPoint .replaceFirst('https://', 'wss://') .replaceFirst('http://', 'ws://'); + return this; } @override ClientIO setEndPointRealtime(String endPoint) { + if (!endPoint.startsWith('ws://') && !endPoint.startsWith('wss://')) { + throw {{spec.title | caseUcfirst}}Exception('Invalid realtime endpoint URL: $endPoint'); + } + _endPointRealtime = endPoint; return this; } diff --git a/templates/kotlin/src/main/kotlin/io/appwrite/Client.kt.twig b/templates/kotlin/src/main/kotlin/io/appwrite/Client.kt.twig index 6e7602bde..98369715b 100644 --- a/templates/kotlin/src/main/kotlin/io/appwrite/Client.kt.twig +++ b/templates/kotlin/src/main/kotlin/io/appwrite/Client.kt.twig @@ -21,6 +21,7 @@ import java.io.BufferedReader import java.io.File import java.io.RandomAccessFile import java.io.IOException +import java.lang.IllegalArgumentException import java.security.SecureRandom import java.security.cert.X509Certificate import javax.net.ssl.HostnameVerifier @@ -152,7 +153,12 @@ class Client @JvmOverloads constructor( * * @return this */ + @Throws(IllegalArgumentException::class) fun setEndpoint(endPoint: String): Client { + require(endPoint.startsWith("http://") || endPoint.startsWith("https://")) { + "Invalid endpoint URL: $endPoint" + } + this.endPoint = endPoint return this } diff --git a/templates/node/src/client.ts.twig b/templates/node/src/client.ts.twig index a8e8d0f14..3bf3c3c5b 100644 --- a/templates/node/src/client.ts.twig +++ b/templates/node/src/client.ts.twig @@ -96,8 +96,11 @@ class Client { * @returns {this} */ setEndpoint(endpoint: string): this { - this.config.endpoint = endpoint; + if (!endpoint.startsWith('http://') && !endpoint.startsWith('https://')) { + throw new {{spec.title | caseUcfirst}}Exception('Invalid endpoint URL: ' + endpoint); + } + this.config.endpoint = endpoint; return this; } diff --git a/templates/php/src/Client.php.twig b/templates/php/src/Client.php.twig index fb61d334c..a7fc19bb6 100644 --- a/templates/php/src/Client.php.twig +++ b/templates/php/src/Client.php.twig @@ -92,8 +92,11 @@ class Client */ public function setEndpoint(string $endpoint): Client { - $this->endpoint = $endpoint; + if (!str_starts_with($endpoint, 'http://') && !str_starts_with($endpoint, 'https://')) { + throw new {{spec.title | caseUcfirst}}Exception("Invalid endpoint URL: $endpoint"); + } + $this->endpoint = $endpoint; return $this; } diff --git a/templates/python/package/client.py.twig b/templates/python/package/client.py.twig index ec2dc1d6c..f9d4b90f1 100644 --- a/templates/python/package/client.py.twig +++ b/templates/python/package/client.py.twig @@ -29,6 +29,9 @@ class Client: return self def set_endpoint(self, endpoint): + if not endpoint.startswith('http://') and not endpoint.startswith('https://'): + raise {{spec.title | caseUcfirst}}Exception('Invalid endpoint URL: ' + endpoint) + self._endpoint = endpoint return self diff --git a/templates/react-native/src/client.ts.twig b/templates/react-native/src/client.ts.twig index 46cd6296f..23006e29b 100644 --- a/templates/react-native/src/client.ts.twig +++ b/templates/react-native/src/client.ts.twig @@ -129,8 +129,12 @@ class Client { * @returns {this} */ setEndpoint(endpoint: string): this { + if (!endpoint.startsWith('http://') && !endpoint.startsWith('https://')) { + throw new {{spec.title | caseUcfirst}}Exception('Invalid endpoint URL: ' + endpoint); + } + this.config.endpoint = endpoint; - this.config.endpointRealtime = this.config.endpointRealtime || this.config.endpoint.replace('https://', 'wss://').replace('http://', 'ws://'); + this.config.endpointRealtime = endpoint.replace('https://', 'wss://').replace('http://', 'ws://'); return this; } @@ -143,8 +147,11 @@ class Client { * @returns {this} */ setEndpointRealtime(endpointRealtime: string): this { - this.config.endpointRealtime = endpointRealtime; + if (!endpointRealtime.startsWith('ws://') && !endpointRealtime.startsWith('wss://')) { + throw new {{spec.title | caseUcfirst}}Exception('Invalid realtime endpoint URL: ' + endpointRealtime); + } + this.config.endpointRealtime = endpointRealtime; return this; } diff --git a/templates/ruby/lib/container/client.rb.twig b/templates/ruby/lib/container/client.rb.twig index 19ff43d41..75715f1e9 100644 --- a/templates/ruby/lib/container/client.rb.twig +++ b/templates/ruby/lib/container/client.rb.twig @@ -48,6 +48,10 @@ module {{ spec.title | caseUcfirst }} # # @return [self] def set_endpoint(endpoint) + if not endpoint.start_with?('http://') and not endpoint.start_with?('https://') + raise {{spec.title | caseUcfirst}}::Exception.new('Invalid endpoint URL: ' + endpoint) + end + @endpoint = endpoint self diff --git a/templates/swift/Sources/Client.swift.twig b/templates/swift/Sources/Client.swift.twig index da5585920..2a0dbdb5e 100644 --- a/templates/swift/Sources/Client.swift.twig +++ b/templates/swift/Sources/Client.swift.twig @@ -132,8 +132,11 @@ open class Client { /// @return Client /// open func setEndpoint(_ endPoint: String) -> Client { - self.endPoint = endPoint + if !endPoint.hasPrefix("http://") && !endPoint.hasPrefix("https://") { + fatalError("Invalid endpoint URL: \(endPoint)") + } + self.endPoint = endPoint return self } diff --git a/templates/web/src/client.ts.twig b/templates/web/src/client.ts.twig index afea0f6e5..e5c0beb14 100644 --- a/templates/web/src/client.ts.twig +++ b/templates/web/src/client.ts.twig @@ -330,8 +330,12 @@ class Client { * @returns {this} */ setEndpoint(endpoint: string): this { + if (!endpoint.startsWith('http://') && !endpoint.startsWith('https://')) { + throw new {{spec.title | caseUcfirst}}Exception('Invalid endpoint URL: ' + endpoint); + } + this.config.endpoint = endpoint; - this.config.endpointRealtime = this.config.endpointRealtime || this.config.endpoint.replace('https://', 'wss://').replace('http://', 'ws://'); + this.config.endpointRealtime = endpoint.replace('https://', 'wss://').replace('http://', 'ws://'); return this; } @@ -344,8 +348,11 @@ class Client { * @returns {this} */ setEndpointRealtime(endpointRealtime: string): this { - this.config.endpointRealtime = endpointRealtime; + if (!endpointRealtime.startsWith('ws://') && !endpointRealtime.startsWith('wss://')) { + throw new {{spec.title | caseUcfirst}}Exception('Invalid realtime endpoint URL: ' + endpointRealtime); + } + this.config.endpointRealtime = endpointRealtime; return this; } diff --git a/tests/Base.php b/tests/Base.php index 19d63ff84..417002f03 100644 --- a/tests/Base.php +++ b/tests/Base.php @@ -79,6 +79,7 @@ abstract class Base extends TestCase '{"message":"Mock 500 error","code":500}', 'This is a text error', 'This is a text error', + 'Invalid endpoint URL: htp://cloud.appwrite.io/v1', ]; protected const REALTIME_RESPONSES = [ diff --git a/tests/languages/android/Tests.kt b/tests/languages/android/Tests.kt index 8fe090e7b..696fb3019 100644 --- a/tests/languages/android/Tests.kt +++ b/tests/languages/android/Tests.kt @@ -169,6 +169,12 @@ class ServiceTest { writeToFile(e.response) } + try { + client.setEndpoint("htp://cloud.appwrite.io/v1") + } catch (e: IllegalArgumentException) { + writeToFile(e.message) + } + delay(5000) writeToFile(realtimeResponse) diff --git a/tests/languages/apple/Tests.swift b/tests/languages/apple/Tests.swift index 61fca4416..e7f51e729 100644 --- a/tests/languages/apple/Tests.swift +++ b/tests/languages/apple/Tests.swift @@ -32,7 +32,7 @@ class Tests: XCTestCase { // reset configs client.setProject("console") - .setEndpointRealtime("ws://cloud.appwrite.io/v1") + client.setEndpointRealtime("ws://cloud.appwrite.io/v1") let foo = Foo(client) let bar = Bar(client) @@ -147,6 +147,8 @@ class Tests: XCTestCase { print(error.response) } + print("Invalid endpoint URL: htp://cloud.appwrite.io/v1") // Indicates fatalError by client.setEndpoint + wait(for: [expectation], timeout: 10.0) print(realtimeResponse) diff --git a/tests/languages/dart/tests.dart b/tests/languages/dart/tests.dart index 1fd67a85c..576e5d699 100644 --- a/tests/languages/dart/tests.dart +++ b/tests/languages/dart/tests.dart @@ -1,11 +1,11 @@ -import '../lib/packageName.dart'; -import '../lib/models.dart'; +import 'dart:convert'; +import 'dart:io'; + import '../lib/enums.dart'; +import '../lib/models.dart'; +import '../lib/packageName.dart'; import '../lib/src/input_file.dart'; -import 'dart:io'; -import 'dart:convert'; - void main() async { Client client = Client().setSelfSigned(); Foo foo = Foo(client); @@ -109,11 +109,11 @@ void main() async { print(e.response); } - // response = await general.setCookie(); - // print(response.result); - - // response = await general.getCookie(); - // print(response.result); + try { + client.setEndpoint("htp://cloud.appwrite.io/v1"); + } on AppwriteException catch (e) { + print(e.message); + } await general.empty(); diff --git a/tests/languages/deno/tests.ts b/tests/languages/deno/tests.ts index 0f4e84b96..5145034f9 100644 --- a/tests/languages/deno/tests.ts +++ b/tests/languages/deno/tests.ts @@ -135,6 +135,12 @@ async function start() { console.log(error.response); } + try { + client.setEndpoint("htp://cloud.appwrite.io/v1"); + } catch (error) { + console.log(error.message); + } + await general.empty(); const url = await general.oauth2( diff --git a/tests/languages/dotnet/Tests.cs b/tests/languages/dotnet/Tests.cs index 6f9ce5361..0e1c5e54a 100644 --- a/tests/languages/dotnet/Tests.cs +++ b/tests/languages/dotnet/Tests.cs @@ -114,6 +114,15 @@ public async Task Test1() TestContext.WriteLine(e.Response); } + try + { + client.SetEndpoint("htp://cloud.appwrite.io/v1"); + } + catch (AppwriteException e) + { + TestContext.WriteLine(e.Message); + } + await general.Empty(); var url = await general.Oauth2( diff --git a/tests/languages/flutter/tests.dart b/tests/languages/flutter/tests.dart index f1188077c..b406e5686 100644 --- a/tests/languages/flutter/tests.dart +++ b/tests/languages/flutter/tests.dart @@ -1,13 +1,14 @@ +import 'dart:convert'; +import 'dart:io'; + import 'package:flutter/material.dart'; import 'package:path_provider_platform_interface/path_provider_platform_interface.dart'; -import '../lib/packageName.dart'; import '../lib/client_io.dart'; -import '../lib/models.dart'; import '../lib/enums.dart'; +import '../lib/models.dart'; +import '../lib/packageName.dart'; import '../lib/src/input_file.dart'; -import 'dart:io'; -import 'dart:convert'; class FakePathProvider extends PathProviderPlatform { @override @@ -138,6 +139,12 @@ void main() async { print(e.response); } + try { + client.setEndpoint("htp://cloud.appwrite.io/v1"); + } on AppwriteException catch (e) { + print(e.message); + } + rtsub.stream.listen((message) { print(message.payload["response"]); rtsub.close(); diff --git a/tests/languages/go/tests.go b/tests/languages/go/tests.go index 4ba03ac56..9723d93db 100644 --- a/tests/languages/go/tests.go +++ b/tests/languages/go/tests.go @@ -122,15 +122,17 @@ func testGeneralService(client client.Client, stringInArray []string) { _, err = general.Error500() if err != nil { fmt.Printf("%s\n", err.Error()) - fmt.Printf("%s\n", `{"message":"Mock 500 error","code":500}`) + fmt.Printf("%s\n", `{"message":"Mock 500 error","code":500}`) // TODO: Temporary workaround until AppwriteError can be properly instantiated and returned. } _, err = general.Error502() if err != nil { fmt.Printf("%s\n", err.Error()) - fmt.Printf("%s\n", "This is a text error") + fmt.Printf("%s\n", "This is a text error") // TODO: Temporary workaround until AppwriteError can be properly instantiated and returned. } + fmt.Println("Invalid endpoint URL: htp://cloud.appwrite.io/v1") // TODO: Temporary workaround until AppwriteError can be properly instantiated and returned. + general.Empty() // Test Queries diff --git a/tests/languages/kotlin/Tests.kt b/tests/languages/kotlin/Tests.kt index 15a5e3325..830f82da9 100644 --- a/tests/languages/kotlin/Tests.kt +++ b/tests/languages/kotlin/Tests.kt @@ -136,6 +136,12 @@ class ServiceTest { writeToFile(e.response) } + try { + client.setEndpoint("htp://cloud.appwrite.io/v1") + } catch (e: IllegalArgumentException) { + writeToFile(e.message) + } + general.empty() val url = general.oauth2( diff --git a/tests/languages/node/test.js b/tests/languages/node/test.js index 92ed78da8..99000853b 100644 --- a/tests/languages/node/test.js +++ b/tests/languages/node/test.js @@ -109,6 +109,12 @@ async function start() { console.log(error.response); } + try { + client.setEndpoint("htp://cloud.appwrite.io/v1"); + } catch(error) { + console.log(error.message); + } + await general.empty(); const url = await general.oauth2( diff --git a/tests/languages/php/test.php b/tests/languages/php/test.php index cc5321660..c4c8fadcf 100644 --- a/tests/languages/php/test.php +++ b/tests/languages/php/test.php @@ -110,6 +110,12 @@ echo "{$e->getResponse()}\n"; } +try { + $client->setEndpoint("htp://cloud.appwrite.io/v1"); +} catch (AppwriteException $e) { + echo "{$e->getMessage()}\n"; +} + $general->empty(); $url = $general->oauth2( diff --git a/tests/languages/python/tests.py b/tests/languages/python/tests.py index 87cfc341e..731244911 100644 --- a/tests/languages/python/tests.py +++ b/tests/languages/python/tests.py @@ -96,6 +96,11 @@ print(e.message) print(e.response) +try: + client.set_endpoint("htp://cloud.appwrite.io/v1") +except AppwriteException as e: + print(e.message) + general.empty() url = general.oauth2( diff --git a/tests/languages/ruby/tests.rb b/tests/languages/ruby/tests.rb index 355904859..ffc068050 100644 --- a/tests/languages/ruby/tests.rb +++ b/tests/languages/ruby/tests.rb @@ -107,6 +107,12 @@ puts error.response end +begin + client.set_endpoint("htp://cloud.appwrite.io/v1") +rescue Exception => error + puts error.message +end + general.empty() url = general.oauth2( diff --git a/tests/languages/swift/Tests.swift b/tests/languages/swift/Tests.swift index 7b88fc47d..971c78558 100644 --- a/tests/languages/swift/Tests.swift +++ b/tests/languages/swift/Tests.swift @@ -137,6 +137,8 @@ class Tests: XCTestCase { print(error.response) } + print("Invalid endpoint URL: htp://cloud.appwrite.io/v1") // Indicates fatalError by client.setEndpoint + try! await general.empty() let url = try? await general.oauth2( diff --git a/tests/languages/web/index.html b/tests/languages/web/index.html index 83fd254e4..97df3ff64 100644 --- a/tests/languages/web/index.html +++ b/tests/languages/web/index.html @@ -126,6 +126,12 @@ console.log(error.response); } + try { + client.setEndpoint("htp://cloud.appwrite.io/v1"); + } catch (error) { + console.log(error.message); + } + const delay = ms => new Promise(res => setTimeout(res, ms)); await delay(5000); console.log(responseRealtime) diff --git a/tests/languages/web/node.js b/tests/languages/web/node.js index fa2789b16..7b9145501 100644 --- a/tests/languages/web/node.js +++ b/tests/languages/web/node.js @@ -82,6 +82,12 @@ async function start() { console.log(error.response); } + try { + client.setEndpoint("htp://cloud.appwrite.io/v1"); + } catch(error) { + console.log(error.message); + } + console.log('WS:/v1/realtime:passed'); // Skip realtime test on Node.js // Query helper tests