Skip to content
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

RUMM-2342 Add support to v2 request #99

Merged
merged 3 commits into from
Jul 20, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,22 @@ package com.datadog.gradle.plugin.internal

import com.datadog.gradle.plugin.DdAndroidGradlePlugin.Companion.LOGGER
import com.datadog.gradle.plugin.RepositoryInfo
import java.io.File
import java.lang.IllegalStateException
import java.net.HttpURLConnection
import java.util.concurrent.TimeUnit
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody
import okhttp3.RequestBody.Companion.asRequestBody
import okhttp3.RequestBody.Companion.toRequestBody
import okhttp3.Response
import okio.Buffer
import org.json.JSONObject
import java.io.File
import java.io.IOException
import java.net.HttpURLConnection
import java.nio.charset.Charset
import java.util.Locale
import java.util.concurrent.TimeUnit

internal class OkHttpUploader : Uploader {

Expand All @@ -30,6 +36,7 @@ internal class OkHttpUploader : Uploader {
.Builder()
.callTimeout(NETWORK_TIMEOUT_MS, TimeUnit.MILLISECONDS)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it seems call timeout already includes time for both write and connect stages, so shouldn't we either keep only call timeout, or remove it and use a set of connect, write, read timeouts (read timeout probably doesn't matter)?

.writeTimeout(NETWORK_TIMEOUT_MS, TimeUnit.MILLISECONDS)
.connectTimeout(NETWORK_TIMEOUT_MS, TimeUnit.MILLISECONDS)
.build()

@Suppress("TooGenericExceptionCaught")
Expand Down Expand Up @@ -81,12 +88,16 @@ internal class OkHttpUploader : Uploader {
.format(mappingFile.absolutePath)
)
}

val eventJson = JSONObject()
eventJson.put("version", identifier.version)
eventJson.put("service", identifier.serviceName)
eventJson.put("variant", identifier.variant)
eventJson.put("type", TYPE_JVM_MAPPING_FILE)

val builder = MultipartBody.Builder()
builder.setType(MultipartBody.FORM)
.addFormDataPart("version", identifier.version)
.addFormDataPart("service", identifier.serviceName)
.addFormDataPart("variant", identifier.variant)
.addFormDataPart("type", TYPE_JVM_MAPPING_FILE)
.addFormDataPart("event", "event", eventJson.toString(0).toRequestBody(MEDIA_TYPE_JSON))
.addFormDataPart("jvm_mapping_file", mappingFile.name, mappingFileBody)
xgouchet marked this conversation as resolved.
Show resolved Hide resolved

if (repositoryFile != null) {
Expand Down Expand Up @@ -116,12 +127,19 @@ internal class OkHttpUploader : Uploader {
)
statusCode == HttpURLConnection.HTTP_FORBIDDEN -> throw IllegalStateException(
"Unable to upload mapping file for $identifier; " +
"verify that you're using a valid API Key"
"verify that you're using a valid API Key"
)
statusCode >= HttpURLConnection.HTTP_BAD_REQUEST -> throw IllegalStateException(
"Unable to upload mapping file for $identifier; " +
"it can be because the mapping file already exist for this version"
statusCode == HttpURLConnection.HTTP_CLIENT_TIMEOUT -> throw RuntimeException(
"Unable to upload mapping file for $identifier because of a request timeout; " +
"check your network connection"
)
statusCode >= HttpURLConnection.HTTP_BAD_REQUEST -> {
throw IllegalStateException(
"Unable to upload mapping file for $identifier ($statusCode); " +
"it can be because the mapping file already exist for this version.\n" +
"${response.body?.string()}"
)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,19 +105,26 @@ internal class OkHttpUploaderTest {
}

@Test
fun `M use an OkHttpClient W callTimeout { 45 seconds }`() {
fun `M set client callTimeout W init()`() {
assertThat(testedUploader.client.callTimeoutMillis).isEqualTo(
OkHttpUploader.NETWORK_TIMEOUT_MS.toInt()
)
}

@Test
fun `M use an OkHttpClient W writeTimeout { 45 seconds }`() {
fun `M set client writeTimeout W init()`() {
assertThat(testedUploader.client.writeTimeoutMillis).isEqualTo(
OkHttpUploader.NETWORK_TIMEOUT_MS.toInt()
)
}

@Test
fun `M set client connectTimeout W init()`() {
assertThat(testedUploader.client.connectTimeoutMillis).isEqualTo(
OkHttpUploader.NETWORK_TIMEOUT_MS.toInt()
)
}

@Test
fun `𝕄 upload proper request 𝕎 upload()`() {
// Given
Expand All @@ -139,12 +146,17 @@ internal class OkHttpUploaderTest {
assertThat(mockWebServer.requestCount).isEqualTo(1)
assertThat(dispatchedRequest)
.hasMethod("POST")
.containsFormData("version", fakeIdentifier.version)
.containsFormData("service", fakeIdentifier.serviceName)
.containsFormData("variant", fakeIdentifier.variant)
.containsFormData("type", OkHttpUploader.TYPE_JVM_MAPPING_FILE)
.containsFormData("git_repository_url", fakeRepositoryInfo.url)
.containsFormData("git_commit_sha", fakeRepositoryInfo.hash)
.containsMultipartFile(
"event",
"event",
"{\"service\":\"${fakeIdentifier.serviceName}\"," +
"\"variant\":\"${fakeIdentifier.variant}\"," +
"\"type\":\"${OkHttpUploader.TYPE_JVM_MAPPING_FILE}\"," +
"\"version\":\"${fakeIdentifier.version}\"}",
"application/json; charset=utf-8"
)
.containsMultipartFile(
"jvm_mapping_file",
fakeMappingFileName,
Expand Down Expand Up @@ -180,10 +192,15 @@ internal class OkHttpUploaderTest {
assertThat(mockWebServer.requestCount).isEqualTo(1)
assertThat(dispatchedRequest)
.hasMethod("POST")
.containsFormData("version", fakeIdentifier.version)
.containsFormData("service", fakeIdentifier.serviceName)
.containsFormData("variant", fakeIdentifier.variant)
.containsFormData("type", OkHttpUploader.TYPE_JVM_MAPPING_FILE)
.containsMultipartFile(
"event",
"event",
"{\"service\":\"${fakeIdentifier.serviceName}\"," +
"\"variant\":\"${fakeIdentifier.variant}\"," +
"\"type\":\"${OkHttpUploader.TYPE_JVM_MAPPING_FILE}\"," +
"\"version\":\"${fakeIdentifier.version}\"}",
"application/json; charset=utf-8"
)
.containsMultipartFile(
"jvm_mapping_file",
fakeMappingFileName,
Expand Down Expand Up @@ -218,12 +235,17 @@ internal class OkHttpUploaderTest {
assertThat(mockWebServer.requestCount).isEqualTo(1)
assertThat(dispatchedRequest)
.hasMethod("POST")
.containsFormData("version", fakeIdentifier.version)
.containsFormData("service", fakeIdentifier.serviceName)
.containsFormData("variant", fakeIdentifier.variant)
.containsFormData("type", OkHttpUploader.TYPE_JVM_MAPPING_FILE)
.containsFormData("git_repository_url", fakeRepositoryInfo.url)
.containsFormData("git_commit_sha", fakeRepositoryInfo.hash)
.containsMultipartFile(
"event",
"event",
"{\"service\":\"${fakeIdentifier.serviceName}\"," +
"\"variant\":\"${fakeIdentifier.variant}\"," +
"\"type\":\"${OkHttpUploader.TYPE_JVM_MAPPING_FILE}\"," +
"\"version\":\"${fakeIdentifier.version}\"}",
"application/json; charset=utf-8"
)
.containsMultipartFile(
"jvm_mapping_file",
fakeMappingFileName,
Expand Down Expand Up @@ -267,12 +289,17 @@ internal class OkHttpUploaderTest {
assertThat(mockWebServer.requestCount).isEqualTo(1)
assertThat(dispatchedRequest)
.hasMethod("POST")
.containsFormData("version", fakeIdentifier.version)
.containsFormData("service", fakeIdentifier.serviceName)
.containsFormData("variant", fakeIdentifier.variant)
.containsFormData("type", OkHttpUploader.TYPE_JVM_MAPPING_FILE)
.containsFormData("git_repository_url", fakeRepositoryInfo.url)
.containsFormData("git_commit_sha", fakeRepositoryInfo.hash)
.containsMultipartFile(
"event",
"event",
"{\"service\":\"${fakeIdentifier.serviceName}\"," +
"\"variant\":\"${fakeIdentifier.variant}\"," +
"\"type\":\"${OkHttpUploader.TYPE_JVM_MAPPING_FILE}\"," +
"\"version\":\"${fakeIdentifier.version}\"}",
"application/json; charset=utf-8"
)
.containsMultipartFile(
"jvm_mapping_file",
fakeMappingFileName,
Expand Down