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
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,6 @@
"wrangler": "^4.95.0"
},
"dependencies": {
"@monetizationos/proxy": "1.4.3"
"@monetizationos/proxy": "1.5.2"
}
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const proxy = new MOSProxyBuilder()
}
},
})
.withClientIP((request) => request.headers.get('CF-Connecting-IP') ?? undefined)
.build()

export default {
Expand Down
71 changes: 70 additions & 1 deletion test/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { env, fetchMock, SELF } from 'cloudflare:test'
import { afterEach, beforeAll, describe, expect, it } from 'vitest'
import { afterEach, beforeAll, describe, expect, it, vi } from 'vitest'
import { mockOriginFetch, mockSurfaceDecisionsFetch, surfaceDecisionsResponse } from './helpers'

describe('MonetizationOS Proxy', () => {
Expand Down Expand Up @@ -102,6 +102,75 @@ describe('MonetizationOS Proxy', () => {
)
})

it('sends CF-Connecting-IP as http.clientIP in surface decisions payload', async () => {
mockOriginFetch()
const mockSurfaceDecision = mockSurfaceDecisionsFetch()

const req = new Request('https://test.example/index.html', {
headers: { 'CF-Connecting-IP': '203.0.113.10' },
})
await SELF.fetch(req)

expect(mockSurfaceDecision).toHaveBeenCalledWith(
expect.objectContaining({
http: expect.objectContaining({ clientIP: '203.0.113.10' }),
}),
)
})

it('sends Referer header in http.referer in surface decisions payload', async () => {
mockOriginFetch()
const mockSurfaceDecision = mockSurfaceDecisionsFetch()

const req = new Request('https://test.example/index.html', {
headers: { Referer: 'https://test.example/previous-page' },
})
await SELF.fetch(req)

expect(mockSurfaceDecision).toHaveBeenCalledWith(
expect.objectContaining({
http: expect.objectContaining({ referer: 'https://test.example/previous-page' }),
}),
)
})

it('sends Referer header in http.referer for offer redemption requests', async () => {
const mockOfferRedemption = vi.fn()
fetchMock
.get('https://api.monetizationos.com')
.intercept({ path: '/api/v1/offer-redemptions', method: 'POST' })
.reply((data) => {
mockOfferRedemption(JSON.parse(data.body as string))
return {
statusCode: 200,
data: JSON.stringify({ success: true }),
responseOptions: { headers: { 'Content-Type': 'application/json' } },
}
})

const req = new Request('https://test.example/mos-api/offer-redemptions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Cookie: 'anon-session=the-session',
Referer: 'https://test.example/article/tombstone',
},
body: JSON.stringify({ offerToken: 'offer.abc' }),
})
const res = await SELF.fetch(req)

expect(res.status).toBe(200)
expect(mockOfferRedemption).toHaveBeenCalledWith(
expect.objectContaining({
offerToken: 'offer.abc',
http: expect.objectContaining({
url: 'https://test.example/mos-api/offer-redemptions',
referer: 'https://test.example/article/tombstone',
}),
}),
)
})

it('sends origin status code in http.proxyOrigin.status in surface decisions payload', async () => {
mockOriginFetch({ status: 404, responseBody: '<html><body>Not Found</body></html>' })
const mockSurfaceDecision = mockSurfaceDecisionsFetch()
Expand Down
Loading