Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -105,8 +105,8 @@ class RequestTest {
request.addHeader("X-Test", "value")
request.addHeader("Another-Header", "another-value")

assertEquals("X-Test", request.getHeader("X-Test"))
assertEquals("Another-Header", request.getHeader("Another-Header"))
assertEquals("value", request.getHeader("X-Test"))
assertEquals("another-value", request.getHeader("Another-Header"))
assertEquals(setOf("X-Test", "Another-Header"), request.headerNames)

request.removeHeader("X-Test")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public String getHeader(@NonNull String headerName) {
}

String headerValue = headers.get(headerName);
return headerValue != null ? headerName : "";
return headerValue != null ? headerValue : "";
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ internal class BugsnagEventMapper(
val exceptions = map["exceptions"] as? List<MutableMap<String, Any?>>
exceptions?.mapTo(event.errors) { Error(convertErrorInternal(it), this.logger) }

event.request = convertRequest(map["request"] as? Map<String, Any?>)
event.response = convertResponse(map["response"] as? Map<String, Any?>)

// populate user
event.userImpl = convertUser(map.readEntry("user"))

Expand Down Expand Up @@ -127,6 +130,51 @@ internal class BugsnagEventMapper(
)
}

@Suppress("UNCHECKED_CAST")
internal fun convertRequest(request: Map<String, Any?>?): Request? {
if (request == null) {
return null
}

val out = Request(
request["httpVersion"] as? String,
request["httpMethod"] as? String,
request["url"] as? String,
)

out.body = request["body"] as? String
out.bodyLength = (request["bodyLength"] as? Number)?.toLong() ?: 0

(request["headers"] as? Map<String, String>)?.forEach { (name, value) ->
out.addHeader(name, value)
}

(request["params"] as? Map<String, String>)?.forEach { (key, value) ->
out.addQueryParameter(key, value)
}

return out
}

@Suppress("UNCHECKED_CAST")
internal fun convertResponse(response: Map<String, Any?>?): Response? {
if (response == null) {
return null
}

val statusCode = (response["statusCode"] as? Number)?.toInt() ?: return null
val out = Response(statusCode)

out.body = response["body"] as? String
out.bodyLength = (response["bodyLength"] as? Number)?.toLong() ?: 0

(response["headers"] as? Map<String, String>)?.forEach { (name, value) ->
out.addHeader(name, value)
}

return out
}

internal fun convertUser(user: Map<String, Any?>): User {
return User(
user["id"] as? String,
Expand Down
30 changes: 15 additions & 15 deletions bugsnag-android-core/src/main/java/com/bugsnag/android/Request.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,11 @@ public void setUrl(@Nullable String url) {
int querySeparatorIndex = url.indexOf('?');

if (querySeparatorIndex > 0) {
setUrlWithQueryString(url);
try {
tryUrlWithQueryString(url);
} catch (RuntimeException re) {
this.url = url.substring(0, querySeparatorIndex);
}
} else {
this.url = url;
}
Expand All @@ -131,22 +135,18 @@ public void setUrl(@Nullable String url) {
}
}

private void setUrlWithQueryString(@NonNull String url) {
try {
Uri uri = Uri.parse(url);
private void tryUrlWithQueryString(@NonNull String url) {
Uri uri = Uri.parse(url);

params.clear();
for (String queryName : uri.getQueryParameterNames()) {
params.put(queryName, uri.getQueryParameter(queryName));
}

this.url = uri.buildUpon()
.clearQuery()
.build()
.toString();
} catch (RuntimeException ignored) {
this.url = url;
params.clear();
for (String queryName : uri.getQueryParameterNames()) {
params.put(queryName, uri.getQueryParameter(queryName));
}

this.url = uri.buildUpon()
.clearQuery()
.build()
.toString();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,27 @@ internal class EventSerializationTest {

createEvent {
createMetadataStressTest(it)
},

createEvent {
it.updateSeverityReason(SeverityReason.REASON_HTTP_ERROR)

it.request = Request("1.1", "POST", "http://example.com/").apply {
body = "some body"
bodyLength = 9

addHeader("content-type", "text/plain")
addHeader("content-length", "9")
addQueryParameter("message", "hello world")
}

it.response = Response(200).apply {
body = "some body"
bodyLength = 9

addHeader("content-type", "text/plain")
addHeader("content-length", "9")
}
}
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{
"metaData": {},
"severity": "warning",
"severityReason": {
"type": "httpError",
"unhandledOverridden": false
},
"unhandled": false,
"exceptions": [],
"request": {
"httpMethod": "POST",
"httpVersion": "1.1",
"url": "http://example.com/",
"body": "some body",
"bodyLength": 9,
"headers": {
"content-type": "text/plain",
"content-length": "9"
},
"params": {
"message": "hello world"
}
},
"response": {
"statusCode": 200,
"body": "some body",
"bodyLength": 9,
"headers": {
"content-type": "text/plain",
"content-length": "9"
}
},
"projectPackages":[
"com.example.foo"
],
"user": {},
"app": {
"type": "android",
"versionCode": 0
},
"device": {
"cpuAbi": [],
"manufacturer": "samsung",
"model": "s7",
"osName": "android",
"osVersion": "7.1",
"runtimeVersions": {
"osBuild": "bulldog",
"androidApiLevel": "24"
},
"totalMemory": 109230923452,
"freeDisk": 22234423124,
"freeMemory": 92340255592,
"orientation": "portrait",
"time": "1970-01-01T00:00:00.000Z"
},
"breadcrumbs": [],
"threads": [],
"featureFlags": []
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,18 @@ open class OkHttpInstrumentationScenario(
"Authorization".toPattern(Pattern.LITERAL or Pattern.CASE_INSENSITIVE),
".*password.*".toPattern(Pattern.CASE_INSENSITIVE)
)

config.addOnSend { event ->
if (event.request == null) {
event.errors.first().errorClass = "No request in OnSendCallback"
}

if (event.response == null) {
event.errors.first().errorClass = "No response in OnErrorCallback"
}

true
}
}

private fun requestType(): Pair<String, Int> {
Expand Down
Loading