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

Tests: Multiple Uids Cookies Support #3691

Merged
Merged
Changes from 3 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
@@ -16,11 +16,11 @@ class UidsCookie {
Map<BidderName, UidWithExpiry> tempUIDs
Boolean optout

static UidsCookie getDefaultUidsCookie(BidderName bidder = GENERIC) {
static UidsCookie getDefaultUidsCookie(BidderName bidder = GENERIC, Integer expireDays = 2) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

You should choose one name for expiration day(expireDays or plusDays)

new UidsCookie().tap {
uids = [(bidder): UUID.randomUUID().toString()]
tempUIDs = [(bidder): new UidWithExpiry(uid: UUID.randomUUID().toString(),
expires: ZonedDateTime.now(Clock.systemUTC()).plusDays(2))]
expires: ZonedDateTime.now(Clock.systemUTC()).plusDays(expireDays))]
}
}
}
Original file line number Diff line number Diff line change
@@ -23,9 +23,9 @@ class SetuidRequest {
String account

static SetuidRequest getDefaultSetuidRequest() {
def request = new SetuidRequest()
request.bidder = GENERIC
request.gdpr = "0"
request
new SetuidRequest().tap {
bidder = GENERIC
gdpr = "0"
}
}
}
Original file line number Diff line number Diff line change
@@ -11,10 +11,10 @@ class UidWithExpiry {
String uid
ZonedDateTime expires

static UidWithExpiry getDefaultUidWithExpiry() {
static UidWithExpiry getDefaultUidWithExpiry(Integer plusDays = 2) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This one is related to the comment above.

new UidWithExpiry().tap {
uid = UUID.randomUUID().toString()
expires = ZonedDateTime.now(Clock.systemUTC()).plusDays(2)
expires = ZonedDateTime.now(Clock.systemUTC()).plusDays(plusDays)
}
}
}
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@ import org.prebid.server.functional.model.UidsCookie
@ToString(includeNames = true, ignoreNulls = true)
class SetuidResponse {

Map<String, String> headers
LinkedHashMap<String, List<String>> headers
Copy link
Collaborator

Choose a reason for hiding this comment

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

I believe here Map should be fine.

UidsCookie uidsCookie
Byte[] responseBody
}
Original file line number Diff line number Diff line change
@@ -167,12 +167,21 @@ class PrebidServerService implements ObjectMapperWrapper {
}

SetuidResponse sendSetUidRequest(SetuidRequest request, UidsCookie uidsCookie, Map header = [:]) {
def uidsCookieAsJson = encode(uidsCookie)
def uidsCookieAsEncodedJson = Base64.urlEncoder.encodeToString(uidsCookieAsJson.bytes)
def response = given(requestSpecification).cookie(UIDS_COOKIE_NAME, uidsCookieAsEncodedJson)
.queryParams(toMap(request))
.headers(header)
.get(SET_UID_ENDPOINT)
sendSetUidRequest(request, [uidsCookie], header)
}

SetuidResponse sendSetUidRequest(SetuidRequest request, List<UidsCookie> uidsCookies, Map header = [:]) {
def cookies = uidsCookies.withIndex().collectEntries { group, index ->
def uidsCookieAsJson = encode(group)
def uidsCookieAsEncodedJson = Base64.urlEncoder.encodeToString(uidsCookieAsJson.bytes)
["${UIDS_COOKIE_NAME}${index > 0 ? index + 1 : ''}": uidsCookieAsEncodedJson]
}

def response = given(requestSpecification)
.cookies(cookies)
.queryParams(toMap(request))
.headers(header)
.get(SET_UID_ENDPOINT)

checkResponseStatusCode(response)

@@ -344,16 +353,32 @@ class PrebidServerService implements ObjectMapperWrapper {
}
}

private static Map<String, String> getHeaders(Response response) {
response.headers().collectEntries { [it.name, it.value] }
private static Map<String, List<String>> getHeaders(Response response) {
response.headers().groupBy { it.name }.collectEntries { [(it.key): it.value*.value] }
}

private static UidsCookie getDecodedUidsCookie(Response response) {
def uids = response.detailedCookie(UIDS_COOKIE_NAME)?.value
if (uids) {
return decode(new String(Base64.urlDecoder.decode(uids)), UidsCookie)
} else {
throw new IllegalStateException("uids cookie is missing in response")
def sortedCookies = response.detailedCookies()
.findAll { cookie -> !(cookie =~ /\buids\d*=\s*;/) }
.sort { a, b ->
def aMatch = (a.name =~ /uids(\d*)/)[0]
def bMatch = (b.name =~ /uids(\d*)/)[0]

def aNumber = (aMatch?.getAt(1) ? aMatch[1].toInteger() : 0)
def bNumber = (bMatch?.getAt(1) ? bMatch[1].toInteger() : 0)

aNumber <=> bNumber
}

def decodedCookiesList = sortedCookies.collect { cookie ->
def uid = (cookie =~ /uids\d*=(\S+?);/)[0][1]
decodeWithBase64(uid as String, UidsCookie)
}

decodedCookiesList.inject(new UidsCookie()) { uidsCookie, decodedCookie ->
uidsCookie.uids = (uidsCookie.uids ?: new LinkedHashMap()) + (decodedCookie.uids ?: new LinkedHashMap())
uidsCookie.tempUIDs = (uidsCookie.tempUIDs ?: new LinkedHashMap()) + (decodedCookie.tempUIDs ?: new LinkedHashMap())
uidsCookie
}
}

Loading