From 9546b21d7ead7bae6ce942019de86f30a1ceddd6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bj=C3=B6rn=20Schie=C3=9Fle?= <bjoern@schiessle.org>
Date: Tue, 24 Nov 2015 13:07:40 +0100
Subject: [PATCH] always store server url without a trailing slash

---
 apps/federation/lib/dbhandler.php           |  1 +
 apps/federation/tests/lib/dbhandlertest.php | 22 ++++++++++++++++++---
 2 files changed, 20 insertions(+), 3 deletions(-)

diff --git a/apps/federation/lib/dbhandler.php b/apps/federation/lib/dbhandler.php
index 61ba5c87cfd..7606593f780 100644
--- a/apps/federation/lib/dbhandler.php
+++ b/apps/federation/lib/dbhandler.php
@@ -68,6 +68,7 @@ class DbHandler {
 	 */
 	public function addServer($url) {
 		$hash = $this->hash($url);
+		$url = rtrim($url, '/');
 		$query = $this->connection->getQueryBuilder();
 		$query->insert($this->dbTable)
 			->values(
diff --git a/apps/federation/tests/lib/dbhandlertest.php b/apps/federation/tests/lib/dbhandlertest.php
index e47df092f8c..123eaaee450 100644
--- a/apps/federation/tests/lib/dbhandlertest.php
+++ b/apps/federation/tests/lib/dbhandlertest.php
@@ -67,17 +67,33 @@ class DbHandlerTest extends TestCase {
 		$query->execute();
 	}
 
-	public function testAddServer() {
-		$id = $this->dbHandler->addServer('server1');
+	/**
+	 * @dataProvider dataTestAddServer
+	 *
+	 * @param string $url passed to the method
+	 * @param string $expectedUrl the url we expect to be written to the db
+	 * @param string $expectedHash the hash value we expect to be written to the db
+	 */
+	public function testAddServer($url, $expectedUrl, $expectedHash) {
+		$id = $this->dbHandler->addServer($url);
 
 		$query = $this->connection->getQueryBuilder()->select('*')->from($this->dbTable);
 		$result = $query->execute()->fetchAll();
 		$this->assertSame(1, count($result));
-		$this->assertSame('server1', $result[0]['url']);
+		$this->assertSame($expectedUrl, $result[0]['url']);
 		$this->assertSame($id, (int)$result[0]['id']);
+		$this->assertSame($expectedHash, $result[0]['url_hash']);
 		$this->assertSame(TrustedServers::STATUS_PENDING, (int)$result[0]['status']);
 	}
 
+	public function dataTestAddServer() {
+		return [
+				['http://owncloud.org', 'http://owncloud.org', md5('owncloud.org')],
+				['https://owncloud.org', 'https://owncloud.org', md5('owncloud.org')],
+				['http://owncloud.org/', 'http://owncloud.org', md5('owncloud.org')],
+		];
+	}
+
 	public function testRemove() {
 		$id1 = $this->dbHandler->addServer('server1');
 		$id2 = $this->dbHandler->addServer('server2');
-- 
GitLab