-
-
Notifications
You must be signed in to change notification settings - Fork 453
Add Request to the Scope. #1270
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,12 @@ | ||
package io.sentry.protocol; | ||
|
||
import io.sentry.IUnknownPropertiesConsumer; | ||
import io.sentry.util.CollectionUtils; | ||
import java.util.Map; | ||
import org.jetbrains.annotations.ApiStatus; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
import org.jetbrains.annotations.TestOnly; | ||
|
||
/** | ||
* Http request information. | ||
|
@@ -40,7 +44,7 @@ | |
* csrftoken=u32t4o3tb3gg43; _gat=1;", "headers": { "content-type": "text/html" }, "env": { | ||
* "REMOTE_ADDR": "192.168.0.1" } } } ``` | ||
*/ | ||
public final class Request implements IUnknownPropertiesConsumer { | ||
public final class Request implements Cloneable, IUnknownPropertiesConsumer { | ||
/** | ||
* The URL of the request if available. | ||
* | ||
|
@@ -159,9 +163,38 @@ public void setOthers(Map<String, String> other) { | |
this.other = other; | ||
} | ||
|
||
/** | ||
* the Request's unknown fields | ||
* | ||
* @return the unknown map | ||
*/ | ||
@TestOnly | ||
@Nullable | ||
Map<String, Object> getUnknown() { | ||
return unknown; | ||
} | ||
|
||
@ApiStatus.Internal | ||
@Override | ||
public void acceptUnknownProperties(Map<String, Object> unknown) { | ||
this.unknown = unknown; | ||
} | ||
|
||
/** | ||
* Clones an User aka deep copy | ||
* | ||
* @return the cloned User | ||
* @throws CloneNotSupportedException if the User is not cloneable | ||
*/ | ||
@Override | ||
public @NotNull Request clone() throws CloneNotSupportedException { | ||
final Request clone = (Request) super.clone(); | ||
|
||
clone.headers = CollectionUtils.shallowCopy(headers); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you mean to check if with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, but if it's a list/map and items are added/removed, it would mutate both, right, that's what we could avoid. |
||
clone.env = CollectionUtils.shallowCopy(env); | ||
clone.other = CollectionUtils.shallowCopy(other); | ||
clone.unknown = CollectionUtils.shallowCopy(unknown); | ||
|
||
return clone; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package io.sentry.protocol | ||
|
||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertNotNull | ||
import kotlin.test.assertNotSame | ||
import kotlin.test.assertNull | ||
|
||
class RequestTest { | ||
@Test | ||
fun `cloning request wont have the same references`() { | ||
val request = createRequest() | ||
val clone = request.clone() | ||
|
||
assertNotNull(clone) | ||
assertNotSame(request, clone) | ||
|
||
assertNotSame(request.others, clone.others) | ||
|
||
assertNotSame(request.unknown, clone.unknown) | ||
} | ||
|
||
@Test | ||
fun `cloning request will have the same values`() { | ||
val request = createRequest() | ||
val clone = request.clone() | ||
|
||
assertEquals("get", clone.method) | ||
assertEquals("http://localhost:8080", clone.url) | ||
assertEquals("?foo=bar", clone.queryString) | ||
assertEquals("envs", clone.envs!!["envs"]) | ||
assertEquals("others", clone.others!!["others"]) | ||
assertEquals("unknown", clone.unknown!!["unknown"]) | ||
} | ||
|
||
@Test | ||
fun `cloning request and changing the original values wont change the clone values`() { | ||
val request = createRequest() | ||
val clone = request.clone() | ||
|
||
request.method = "post" | ||
request.url = "http://another-host:8081/" | ||
request.queryString = "?xxx=yyy" | ||
request.envs!!["envs"] = "newEnvs" | ||
request.others!!["others"] = "newOthers" | ||
request.others!!["anotherOne"] = "anotherOne" | ||
val newUnknown = mapOf(Pair("unknown", "newUnknown"), Pair("otherUnknown", "otherUnknown")) | ||
request.acceptUnknownProperties(newUnknown) | ||
|
||
assertEquals("get", clone.method) | ||
assertEquals("http://localhost:8080", clone.url) | ||
assertEquals("?foo=bar", clone.queryString) | ||
assertEquals("envs", clone.envs!!["envs"]) | ||
assertEquals(1, clone.envs!!.size) | ||
assertEquals("others", clone.others!!["others"]) | ||
assertEquals(1, clone.others!!.size) | ||
assertEquals("unknown", clone.unknown!!["unknown"]) | ||
assertEquals(1, clone.unknown!!.size) | ||
} | ||
|
||
@Test | ||
fun `setting null others do not crash`() { | ||
val request = createRequest() | ||
request.others = null | ||
|
||
assertNull(request.others) | ||
} | ||
|
||
private fun createRequest(): Request { | ||
return Request().apply { | ||
method = "get" | ||
url = "http://localhost:8080" | ||
queryString = "?foo=bar" | ||
envs = mutableMapOf(Pair("envs", "envs")) | ||
val others = mutableMapOf(Pair("others", "others")) | ||
setOthers(others) | ||
val unknown = mapOf(Pair("unknown", "unknown")) | ||
acceptUnknownProperties(unknown) | ||
} | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.