Skip to content

Commit

Permalink
Send library version, compression as headers, test payload
Browse files Browse the repository at this point in the history
  • Loading branch information
macobo committed Nov 27, 2020
1 parent ed81ce9 commit 80624ca
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
25 changes: 25 additions & 0 deletions cypress/integration/capture.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
/// <reference types="cypress" />

import { version } from '../../package.json'
import { LZString } from '../../src/lz-string'

describe('Event capture', () => {
given('options', () => ({}))
given('sessionRecording', () => false)
Expand Down Expand Up @@ -118,4 +121,26 @@ describe('Event capture', () => {
cy.phCaptures().should('deep.equal', [])
})
})

describe('decoding the payload', () => {
it('contains the correct headers and payload after an event', () => {
start()

cy.get('[data-cy-custom-event-button]').click()
cy.phCaptures().should('deep.equal', ['$pageview', '$autocapture', 'custom-event'])

cy.wait('@capture').its('request.headers').should('deep.equal', {
'Content-Type': 'application/x-www-form-urlencoded',
PosthogJs: version,
PosthogCompression: 'lz64',
})

cy.get('@capture').should(({ request }) => {
const data = decodeURIComponent(request.body.match(/data=(.*)&compression=lz64/)[1])
const captures = JSON.parse(LZString.decompressFromBase64(data))

expect(captures.map(({ event }) => event)).to.deep.equal(['$pageview', '$autocapture', 'custom-event'])
})
})
})
})
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"@typescript-eslint/parser": "^3.5.0",
"babel-eslint": "^10.1.0",
"babel-jest": "^26.1.0",
"cypress": "^5.5.0",
"cypress": "^6.0.0",
"eslint": "^7.3.1",
"eslint-plugin-prettier": "^3.1.4",
"given2": "^2.1.7",
Expand Down
3 changes: 3 additions & 0 deletions src/posthog-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,7 @@ PostHogLib.prototype._send_request = function (url, data, options, callback) {
var args = {}
args['ip'] = this.get_config('ip') ? 1 : 0
args['_'] = new Date().getTime().toString()
const compression = data['compression'] || 'base64'

if (use_post) {
if (Array.isArray(data)) {
Expand Down Expand Up @@ -426,6 +427,8 @@ PostHogLib.prototype._send_request = function (url, data, options, callback) {
var headers = this.get_config('xhr_headers')
if (use_post) {
headers['Content-Type'] = 'application/x-www-form-urlencoded'
headers['PosthogJs'] = Config.LIB_VERSION
headers['PosthogCompression'] = compression
}
_.each(headers, function (headerValue, headerName) {
req.setRequestHeader(headerName, headerValue)
Expand Down

0 comments on commit 80624ca

Please sign in to comment.