Skip to content

Commit a0900e7

Browse files
boekpocmo
authored andcommitted
For mozilla-mobile#191 - Test that getSuggestions throws an exception with bad json
1 parent b6aca32 commit a0900e7

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

components/browser/search/src/main/java/mozilla/components/browser/search/SearchSuggestionClient.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package mozilla.components.browser.search
22

3-
import mozilla.components.browser.search.parser.defaultParser
43
import mozilla.components.browser.search.parser.selectParser
54

65
typealias SearchSuggestionFetcher = suspend (url: String) -> String?

components/browser/search/src/test/java/mozilla/components/browser/search/SearchSuggestionClientTest.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package mozilla.components.browser.search
66

77
import kotlinx.coroutines.experimental.runBlocking
88
import org.json.JSONArray
9+
import org.json.JSONException
910
import org.junit.Test
1011
import org.junit.runner.RunWith
1112
import org.junit.Assert.assertEquals
@@ -17,6 +18,7 @@ class SearchSuggestionClientTest {
1718
companion object {
1819
val GOOGLE_MOCK_RESPONSE: SearchSuggestionFetcher = { "[\"firefox\",[\"firefox\",\"firefox for mac\",\"firefox quantum\",\"firefox update\",\"firefox esr\",\"firefox focus\",\"firefox addons\",\"firefox extensions\",\"firefox nightly\",\"firefox clear cache\"]]" }
1920
val QWANT_MOCK_RESPONSE: SearchSuggestionFetcher = { "{\"status\":\"success\",\"data\":{\"items\":[{\"value\":\"firefox (video game)\",\"suggestType\":3},{\"value\":\"firefox addons\",\"suggestType\":12},{\"value\":\"firefox\",\"suggestType\":2},{\"value\":\"firefox quantum\",\"suggestType\":12},{\"value\":\"firefox focus\",\"suggestType\":12}],\"special\":[],\"availableQwick\":[]}}" }
21+
val SERVER_ERROR_RESPONSE: SearchSuggestionFetcher = { "Server error. Try again later" }
2022
}
2123

2224
@Test
@@ -50,4 +52,17 @@ class SearchSuggestionClientTest {
5052
assertEquals(expectedResults, results)
5153
}
5254
}
55+
56+
@Test(expected = JSONException::class)
57+
fun `Check that a bad response will throw an exception`() {
58+
val searchEngine = SearchEngineParser().load(
59+
RuntimeEnvironment.application.assets,
60+
"google", "searchplugins/google-nocodes.xml")
61+
62+
val client = SearchSuggestionClient(searchEngine, SERVER_ERROR_RESPONSE)
63+
64+
runBlocking {
65+
client.getSuggestions("firefox")
66+
}
67+
}
5368
}

0 commit comments

Comments
 (0)