From 48cc45ec6f30b5fe76b5b3b218b9d191052fec70 Mon Sep 17 00:00:00 2001 From: Kenneth Tilton Date: Mon, 23 Jul 2018 05:47:59 -0400 Subject: [PATCH] [Fix #3604] ui, screens, browser, events: deduplicate browser history hence chat elements --- src/status_im/ui/screens/browser/events.cljs | 7 +++++-- src/status_im/utils/http.cljs | 11 ++++++++++- test/cljs/status_im/test/utils/http.cljs | 17 ++++++++++++++++- 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/src/status_im/ui/screens/browser/events.cljs b/src/status_im/ui/screens/browser/events.cljs index 494782453f..168ce179cd 100644 --- a/src/status_im/ui/screens/browser/events.cljs +++ b/src/status_im/ui/screens/browser/events.cljs @@ -73,10 +73,13 @@ :open-url-in-browser [re-frame/trim-v] (fn [cofx [url]] - (let [browser {:browser-id (random/id) + (let [normalized-url (http/normalize-and-decode-url url) + browser {:browser-id (or (http/url-host normalized-url) + ;; purely as a fallback should url not parse... + (random/id)) :name (i18n/label :t/browser) :history-index 0 - :history [(http/normalize-and-decode-url url)]}] + :history [normalized-url]}] (model/update-browser-and-navigate cofx browser)))) (handlers/register-handler-fx diff --git a/src/status_im/utils/http.cljs b/src/status_im/utils/http.cljs index 522dc45d69..cc5952961f 100644 --- a/src/status_im/utils/http.cljs +++ b/src/status_im/utils/http.cljs @@ -1,7 +1,9 @@ (ns status-im.utils.http (:require [status-im.utils.utils :as utils] [status-im.react-native.js-dependencies :as rn-dependencies] - [taoensso.timbre :as log]) + [taoensso.timbre :as log] + [goog.Uri :as uri] + [clojure.string :as string]) (:refer-clojure :exclude [get])) ;; Default HTTP request timeout ms @@ -65,6 +67,13 @@ (def normalize-and-decode-url (comp js/decodeURI normalize-url)) +(defn url-host [url] + (try + (when-let [host (.getDomain (goog.Uri. url))] + (when-not (string/blank? host) + host)) + (catch :default _ nil))) + (defn parse-payload [o] (when o (try diff --git a/test/cljs/status_im/test/utils/http.cljs b/test/cljs/status_im/test/utils/http.cljs index 003f747736..ac9461b78e 100644 --- a/test/cljs/status_im/test/utils/http.cljs +++ b/test/cljs/status_im/test/utils/http.cljs @@ -37,4 +37,19 @@ (testing "https://www.etheremon.com/assets/images/mons'>origin/025.png" (testing "it returns false" - (is (not (http/url-sanitized? "https://www.etheremon.com/assets/images/mons'>origin/025.png")))))) \ No newline at end of file + (is (not (http/url-sanitized? "https://www.etheremon.com/assets/images/mons'>origin/025.png")))))) + +(deftest url-host-check + (testing "Extract host/domain from URL" + (testing "Valid URL with endpoint" + (is (= "status.im" (http/url-host "https://status.im/testing")))) + (testing "Valid URL" + (is (= "status.im" (http/url-host "http://status.im")))) + (testing "Blank domainlocalhost" + (is (nil? (http/url-host "localhost:3000/testing"))))) + (testing "Return nil for Invalid URL" + (testing "Bad scheme" + (is (nil? (http/url-host "invalid//status.im/testing")))) + (testing "No scheme" + (is (nil? (http/url-host "status.im/testing")))))) +