Skip to content
Open
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
36 changes: 36 additions & 0 deletions bugsnag-android-core/api/bugsnag-android-core.api
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
public abstract class com/bugsnag/android/AbstractHttpEntity {
protected final field headers Ljava/util/Map;
public fun addHeader (Ljava/lang/String;Ljava/lang/String;)V
public fun getBody ()Ljava/lang/String;
public fun getBodyLength ()J
public fun getHeader (Ljava/lang/String;)Ljava/lang/String;
public fun getHeaderNames ()Ljava/util/Set;
public fun removeHeader (Ljava/lang/String;)V
public fun setBody (Ljava/lang/String;)V
public fun setBodyLength (J)V
}

public class com/bugsnag/android/App : com/bugsnag/android/JsonStream$Streamable {
public final fun getBinaryArch ()Ljava/lang/String;
public final fun getBuildUuid ()Ljava/lang/String;
Expand Down Expand Up @@ -411,6 +423,8 @@ public class com/bugsnag/android/Event : com/bugsnag/android/FeatureFlagAware, c
public fun getMetadata (Ljava/lang/String;)Ljava/util/Map;
public fun getMetadata (Ljava/lang/String;Ljava/lang/String;)Ljava/lang/Object;
public fun getOriginalError ()Ljava/lang/Throwable;
public fun getRequest ()Lcom/bugsnag/android/Request;
public fun getResponse ()Lcom/bugsnag/android/Response;
public fun getSeverity ()Lcom/bugsnag/android/Severity;
public fun getThreads ()Ljava/util/List;
public fun getUser ()Lcom/bugsnag/android/User;
Expand All @@ -424,6 +438,8 @@ public class com/bugsnag/android/Event : com/bugsnag/android/FeatureFlagAware, c
public fun setErrorReportingThread (Lcom/bugsnag/android/Thread;)V
public fun setGroupingDiscriminator (Ljava/lang/String;)Ljava/lang/String;
public fun setGroupingHash (Ljava/lang/String;)V
public fun setRequest (Lcom/bugsnag/android/Request;)V
public fun setResponse (Lcom/bugsnag/android/Response;)V
public fun setSeverity (Lcom/bugsnag/android/Severity;)V
public fun setTraceCorrelation (Ljava/util/UUID;J)V
public fun setUnhandled (Z)V
Expand Down Expand Up @@ -641,6 +657,26 @@ public abstract interface class com/bugsnag/android/Plugin {
public abstract fun unload ()V
}

public final class com/bugsnag/android/Request : com/bugsnag/android/AbstractHttpEntity, com/bugsnag/android/JsonStream$Streamable {
public fun addQueryParameter (Ljava/lang/String;Ljava/lang/String;)V
public fun getHttpMethod ()Ljava/lang/String;
public fun getHttpVersion ()Ljava/lang/String;
public fun getQueryParameter (Ljava/lang/String;)Ljava/lang/String;
public fun getQueryParameterNames ()Ljava/util/Set;
public fun getUrl ()Ljava/lang/String;
public fun removeQueryParameter (Ljava/lang/String;)V
public fun setHttpMethod (Ljava/lang/String;)V
public fun setHttpVersion (Ljava/lang/String;)V
public fun setUrl (Ljava/lang/String;)V
public fun toStream (Lcom/bugsnag/android/JsonStream;)V
}

public final class com/bugsnag/android/Response : com/bugsnag/android/AbstractHttpEntity, com/bugsnag/android/JsonStream$Streamable {
public fun getStatusCode ()I
public fun setStatusCode (I)V
public fun toStream (Lcom/bugsnag/android/JsonStream;)V
}

public final class com/bugsnag/android/Session : com/bugsnag/android/Deliverable, com/bugsnag/android/JsonStream$Streamable, com/bugsnag/android/UserAware {
public fun getApiKey ()Ljava/lang/String;
public fun getApp ()Lcom/bugsnag/android/App;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package com.bugsnag.android

import org.junit.Assert.assertEquals
import org.junit.Assert.assertNull
import org.junit.Test

class RequestTest {
private val testBodyString = "this is a body string with some content"
private val customBodyLength = 5000L

private val logger = NoopLogger

@Test
fun urlQueryIsExtracted() {
val request = Request(
logger,
"GET",
"1.1",
"http://localhost/test?t1=arg1&test2=argument+2"
)

assertEquals("http://localhost/test", request.url)
assertEquals("arg1", request.getQueryParameter("t1"))
assertEquals("argument 2", request.getQueryParameter("test2"))
assertEquals(setOf("t1", "test2"), request.queryParameterNames)
}

@Test
fun setBody() {
val request = Request(logger, "GET", "1.1", "http://localhost/")
request.body = testBodyString
assertEquals(testBodyString, request.body)
}

@Test
fun setBodyWithUserLength() {
val request = Request(logger, "GET", "1.1", "http://localhost/")
request.bodyLength = customBodyLength
request.body = testBodyString

assertEquals(testBodyString, request.body)
assertEquals(customBodyLength, request.bodyLength)
}

@Test
fun setNullBody() {
val request = Request(logger, "GET", "1.1", "http://localhost/")
request.body = testBodyString
request.body = null
assertNull(request.body)
}

@Test
fun setHttpMethod() {
val request = Request(logger, "GET", "1.1", "http://localhost/")
assertEquals("GET", request.httpMethod)

request.httpMethod = "POST"
assertEquals("POST", request.httpMethod)
}

@Test
fun setHttpVersion() {
val request = Request(logger, "1.1", "1.1", "http://localhost/")
assertEquals("1.1", request.httpVersion)

request.httpVersion = "1.0"
assertEquals("1.0", request.httpVersion)
}

@Test
fun setUrl() {
val request = Request(logger, "GET", "1.1", "http://localhost/")
assertEquals("http://localhost/", request.url)

request.url = "https://google.com"
assertEquals("https://google.com", request.url)
}

@Test
fun setUrlWithQuery() {
val request = Request(logger, "GET", "1.1", "http://localhost/")
request.url = "http://foo.com?a=1&b=2"
assertEquals("http://foo.com", request.url)
assertEquals("1", request.getQueryParameter("a"))
assertEquals("2", request.getQueryParameter("b"))
assertEquals(setOf("a", "b"), request.queryParameterNames)
}

@Test
fun queryParameters() {
val request = Request(logger, "GET", "1.1", "http://localhost/")
request.addQueryParameter("foo", "bar")
request.addQueryParameter("another", "param")

assertEquals("bar", request.getQueryParameter("foo"))
assertEquals("param", request.getQueryParameter("another"))
assertEquals(setOf("foo", "another"), request.queryParameterNames)

request.removeQueryParameter("foo")
assertNull(request.getQueryParameter("foo"))
assertEquals(setOf("another"), request.queryParameterNames)
}

@Test
fun headers() {
val request = Request(logger, "GET", "1.1", "http://localhost/")
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(setOf("X-Test", "Another-Header"), request.headerNames)

request.removeHeader("X-Test")
assertEquals("", request.getHeader("X-Test"))
assertEquals(setOf("Another-Header"), request.headerNames)
}

@Test
fun setBodyLength() {
val request = Request(logger, "GET", "1.1", "http://localhost/")
request.bodyLength = 1234
assertEquals(1234, request.bodyLength)

// test negative value is ignored
request.bodyLength = -5
assertEquals(1234, request.bodyLength)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package com.bugsnag.android;

import androidx.annotation.IntRange;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

@SuppressWarnings("ConstantValue")
public abstract class AbstractHttpEntity {
protected final Map<String, String> headers = new LinkedHashMap<>();

@Nullable
private String body;
private long bodyLength = -1L;

// package-protected constructor
AbstractHttpEntity() {
}

/**
* Add a header to this reported HTTP entity.
*
* @param name the name of the header
* @param value the value of the header
*/
public void addHeader(@NonNull String name, @NonNull String value) {
if (name == null || value == null) {
return;
}

headers.put(name, value);
}

/**
* Remove the specified header by name (case-sensitive).
*
* @param name the header to remove
*/
public void removeHeader(@NonNull String name) {
headers.remove(name);
}

/**
* Return the headers that are set for this HTTP entity.
*
* @return the header names
*/
@NonNull
public Set<String> getHeaderNames() {
return Collections.unmodifiableSet(headers.keySet());
}

/**
* Return the HTTP header by name or an empty string if the header is not present.
*
* @param headerName the header name (case-sensitive)
* @return the value of the header or an empty string
*/
@NonNull
public String getHeader(@NonNull String headerName) {
if (headerName == null) {
return "";
}

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

/**
* Return the captured HTTP body if one has been set, or null.
*
* @return the captured HTTP body
*/
@Nullable
public String getBody() {
return body;
}

/**
* Set or clear the captured body.
*
* @param body the body to report
*/
public void setBody(@Nullable String body) {
this.body = body;
}

/**
* Return the body length (as set with {@link #setBodyLength(long)}) or -1 if none has been set.
*
* @return the number of bytes in the body
*/
public long getBodyLength() {
return bodyLength;
}

/**
* Change the reported size of the request body size (in bytes).
*
* @param bodyLength the number of bytes in the request body
*/
public void setBodyLength(@IntRange(from = 0L) long bodyLength) {
if (bodyLength >= 0) {
this.bodyLength = bodyLength;
}
}
}
Loading