From 43980addd008aa78d353bc632c43f1ca3d3751e5 Mon Sep 17 00:00:00 2001
From: Travis Ralston <travpc@gmail.com>
Date: Thu, 25 Oct 2018 15:22:52 -0600
Subject: [PATCH] Add hostname sanity tests

In the event someone changes how the hostname parsing works.
---
 test/matrix-to-test.js | 85 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 85 insertions(+)

diff --git a/test/matrix-to-test.js b/test/matrix-to-test.js
index 3bf6b5af6f..dbe7fa55ff 100644
--- a/test/matrix-to-test.js
+++ b/test/matrix-to-test.js
@@ -143,4 +143,89 @@ describe('matrix-to', function () {
         expect(pickedServers[1]).toBe("second");
         expect(pickedServers[2]).toBe("third");
     });
+
+    it('should work with IPv4 hostnames', function () {
+        peg.get().getRoom = () => {
+            return {
+                getJoinedMembers: () => [
+                    {
+                        userId: "@alice:127.0.0.1",
+                        powerLevel: 100,
+                    }
+                ],
+            }
+        };
+        const pickedServers = pickServerCandidates("!somewhere:example.org");
+        expect(pickedServers).toExist();
+        expect(pickedServers.length).toBe(1);
+        expect(pickedServers[0]).toBe("127.0.0.1");
+    });
+
+    it('should work with IPv6 hostnames', function () {
+        peg.get().getRoom = () => {
+            return {
+                getJoinedMembers: () => [
+                    {
+                        userId: "@alice:[::1]",
+                        powerLevel: 100,
+                    }
+                ],
+            }
+        };
+        const pickedServers = pickServerCandidates("!somewhere:example.org");
+        expect(pickedServers).toExist();
+        expect(pickedServers.length).toBe(1);
+        expect(pickedServers[0]).toBe("[::1]");
+    });
+
+    it('should work with IPv4 hostnames with ports', function () {
+        peg.get().getRoom = () => {
+            return {
+                getJoinedMembers: () => [
+                    {
+                        userId: "@alice:127.0.0.1:8448",
+                        powerLevel: 100,
+                    }
+                ],
+            }
+        };
+        const pickedServers = pickServerCandidates("!somewhere:example.org");
+        expect(pickedServers).toExist();
+        expect(pickedServers.length).toBe(1);
+        expect(pickedServers[0]).toBe("127.0.0.1:8448");
+    });
+
+    it('should work with IPv6 hostnames with ports', function () {
+        peg.get().getRoom = () => {
+            return {
+                getJoinedMembers: () => [
+                    {
+                        userId: "@alice:[::1]:8448",
+                        powerLevel: 100,
+                    }
+                ],
+            }
+        };
+        const pickedServers = pickServerCandidates("!somewhere:example.org");
+        expect(pickedServers).toExist();
+        expect(pickedServers.length).toBe(1);
+        expect(pickedServers[0]).toBe("[::1]:8448");
+    });
+
+    it('should work with hostnames with ports', function () {
+        peg.get().getRoom = () => {
+            return {
+                getJoinedMembers: () => [
+                    {
+                        userId: "@alice:example.org:8448",
+                        powerLevel: 100,
+                    }
+                ],
+            }
+        };
+        const pickedServers = pickServerCandidates("!somewhere:example.org");
+        expect(pickedServers).toExist();
+        expect(pickedServers.length).toBe(1);
+        expect(pickedServers[0]).toBe("example.org:8448");
+    });
 });