Repository navigation
Feature/tests #53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Feature/tests #53
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a8b2c12
Add tests
01a2424
Update README.md
40adb7b
Revert version changes
8632321
Add continue-on-error for Gradle Plugin Portal publishing validation job
4be7a03
Add publish-unit-test-result-action for PR test reporting
9edae06
Bring back test results uploading
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
lib/src/integrationTest/kotlin/com/appswithlove/loco/http/DefaultLocoHttpClientTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| package com.appswithlove.loco.http | ||
|
|
||
| import com.appswithlove.loco.plugin.LocoConfig | ||
| import com.sun.net.httpserver.HttpServer | ||
| import io.kotest.matchers.string.shouldContain | ||
| import io.kotest.matchers.string.shouldNotContain | ||
| import org.junit.jupiter.api.AfterEach | ||
| import org.junit.jupiter.api.BeforeEach | ||
| import org.junit.jupiter.api.Test | ||
| import java.net.InetSocketAddress | ||
|
|
||
| class DefaultLocoHttpClientTest { | ||
|
|
||
| private lateinit var server: HttpServer | ||
| private var capturedUri: String = "" | ||
|
|
||
| @BeforeEach | ||
| fun setUp() { | ||
| server = HttpServer.create(InetSocketAddress(0), 0) | ||
| server.createContext("/") { exchange -> | ||
| capturedUri = exchange.requestURI.toString() | ||
| val response = "<resources></resources>".toByteArray() | ||
| exchange.sendResponseHeaders(200, response.size.toLong()) | ||
| exchange.responseBody.use { it.write(response) } | ||
| } | ||
| server.start() | ||
| } | ||
|
|
||
| @AfterEach | ||
| fun tearDown() { | ||
| server.stop(0) | ||
| } | ||
|
|
||
| private fun config(block: LocoConfig.() -> Unit = {}): LocoConfig = LocoConfig().apply { | ||
| apiKey = "test-key" | ||
| locoBaseUrl = "http://localhost:${server.address.port}" | ||
| block() | ||
| } | ||
|
|
||
| @Test | ||
| fun fetchTranslationWithTagsIncludesFilterParam() { | ||
| val config = config { tags = "android" } | ||
| DefaultLocoHttpClient().fetchTranslation(config, "en") | ||
|
|
||
| capturedUri shouldContain "filter=android" | ||
| } | ||
|
|
||
| @Test | ||
| fun fetchTranslationWithFallbackLangIncludesFallbackParam() { | ||
| val config = config { fallbackLang = "en" } | ||
| DefaultLocoHttpClient().fetchTranslation(config, "de") | ||
|
|
||
| capturedUri shouldContain "fallback=en" | ||
| } | ||
|
|
||
| @Test | ||
| fun fetchTranslationWithOrderByAssetIdIncludesOrderParam() { | ||
| val config = config { orderByAssetId = true } | ||
| DefaultLocoHttpClient().fetchTranslation(config, "en") | ||
|
|
||
| capturedUri shouldContain "order=id" | ||
| } | ||
|
|
||
| @Test | ||
| fun fetchTranslationWithOrderByAssetIdFalseOmitsOrderParam() { | ||
| val config = config { orderByAssetId = false } | ||
| DefaultLocoHttpClient().fetchTranslation(config, "en") | ||
|
|
||
| capturedUri shouldNotContain "order=id" | ||
| } | ||
|
|
||
| @Test | ||
| fun fetchTranslationWithHideCommentsSetsNoCommentsTrue() { | ||
| val config = config { hideComments = true } | ||
| DefaultLocoHttpClient().fetchTranslation(config, "en") | ||
|
|
||
| capturedUri shouldContain "no-comments=true" | ||
| } | ||
|
|
||
| @Test | ||
| fun fetchTranslationWithIndexIncludesIndexParam() { | ||
| val config = config { index = "name" } | ||
| DefaultLocoHttpClient().fetchTranslation(config, "en") | ||
|
|
||
| capturedUri shouldContain "index=name" | ||
| } | ||
|
|
||
| @Test | ||
| fun fetchTranslationWithStatusIncludesStatusParam() { | ||
| val config = config { status = "translated" } | ||
| DefaultLocoHttpClient().fetchTranslation(config, "en") | ||
|
|
||
| capturedUri shouldContain "status=translated" | ||
| } | ||
| } |
47 changes: 47 additions & 0 deletions
47
lib/src/main/kotlin/com/appswithlove/loco/http/DefaultLocoHttpClient.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| package com.appswithlove.loco.http | ||
|
|
||
| import com.appswithlove.loco.Constants | ||
| import com.appswithlove.loco.plugin.LocoConfig | ||
| import java.net.HttpURLConnection | ||
| import java.net.URL | ||
|
|
||
| internal class DefaultLocoHttpClient : LocoHttpClient { | ||
|
|
||
| override fun fetchLocales(apiKey: String): String { | ||
| val connection = | ||
| (URL(Constants.LOCO_LOCALES_URL).openConnection() as HttpURLConnection).apply { | ||
| requestMethod = "GET" | ||
| setRequestProperty("Authorization", "Loco $apiKey") | ||
| setRequestProperty("Accept", "application/json") | ||
| connectTimeout = 10000 | ||
| readTimeout = 10000 | ||
| } | ||
| return connection.inputStream.bufferedReader(Charsets.UTF_8).use { it.readText() } | ||
| } | ||
|
|
||
| override fun fetchTranslation(locoConfig: LocoConfig, language: String): String { | ||
| var parameter = "?no-comments=${locoConfig.hideComments}" | ||
| locoConfig.tags?.let { parameter += "&filter=$it" } | ||
| locoConfig.index?.let { parameter += "&index=$it" } | ||
| locoConfig.status?.let { parameter += "&status=$it" } | ||
| locoConfig.fallbackLang?.let { parameter += "&fallback=$it" } | ||
| if (locoConfig.orderByAssetId) parameter += "&order=id" | ||
|
|
||
| val connection = | ||
| (URL("${locoConfig.locoBaseUrl}/$language.xml$parameter").openConnection() as HttpURLConnection).apply { | ||
| requestMethod = "GET" | ||
| addRequestProperty("Authorization", "Loco ${locoConfig.apiKey}") | ||
| addRequestProperty("Accept-Charset", "utf-8") | ||
| connectTimeout = 10000 | ||
| readTimeout = 10000 | ||
| } | ||
|
|
||
| if (connection.responseCode != 200) { | ||
| val error = (connection.errorStream ?: connection.inputStream).bufferedReader() | ||
| .use { it.readText() } | ||
| throw IllegalStateException("Request failed with code ${connection.responseCode}: $error") | ||
| } | ||
|
|
||
| return connection.inputStream.bufferedReader(Charsets.UTF_8).use { it.readText() } | ||
| } | ||
| } |
8 changes: 8 additions & 0 deletions
8
lib/src/main/kotlin/com/appswithlove/loco/http/LocoHttpClient.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package com.appswithlove.loco.http | ||
|
|
||
| import com.appswithlove.loco.plugin.LocoConfig | ||
|
|
||
| internal interface LocoHttpClient { | ||
| fun fetchLocales(apiKey: String): String | ||
| fun fetchTranslation(locoConfig: LocoConfig, language: String): String | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice improvement!