|
| 1 | +goog.module('measurementLibrary.eventProcessor.testing.GoogleAnalyticsEventProcessor.buildRequestUrl'); |
| 2 | +goog.setTestOnly(); |
| 3 | + |
| 4 | +const GoogleAnalyticsEventProcessor = goog.require('measurementLibrary.eventProcessor.GoogleAnalyticsEventProcessor'); |
| 5 | + |
| 6 | +describe('The `buildRequestUrl` method ' + |
| 7 | + 'of GoogleAnalyticsEventProcessor', () => { |
| 8 | + /** |
| 9 | + * Tests the `buildRequestUrl` function by passing in an API secret, |
| 10 | + * measurement ID, and measurement URL to construct the event processor |
| 11 | + * with and comparing the result of the function with the expected result. |
| 12 | + * @param {string} apiSecret |
| 13 | + * @param {string} measurementId |
| 14 | + * @param {string} measurementUrl |
| 15 | + * @return {string} expected |
| 16 | + */ |
| 17 | + function expectRequestUrl(apiSecret, measurementId, measurementUrl) { |
| 18 | + const eventProcessor = new GoogleAnalyticsEventProcessor({ |
| 19 | + 'api_secret': apiSecret, |
| 20 | + 'measurement_id': measurementId, |
| 21 | + 'measurement_url': measurementUrl, |
| 22 | + }); |
| 23 | + |
| 24 | + return expect(eventProcessor.buildRequestUrl_()); |
| 25 | + } |
| 26 | + |
| 27 | + it('creates the correct query string when using the default ' + |
| 28 | + 'measurement url', () => { |
| 29 | + expectRequestUrl( |
| 30 | + /* apiSecret */ '123', |
| 31 | + /* measurementId */ '456', |
| 32 | + /* measurementUrl */ undefined) |
| 33 | + .toBe('https://www.google-analytics.com/mp/collect?' + |
| 34 | + 'measurement_id=456&api_secret=123'); |
| 35 | + |
| 36 | + expectRequestUrl( |
| 37 | + /* apiSecret */ '', |
| 38 | + /* measurementId */ '456', |
| 39 | + /* measurementUrl */ undefined) |
| 40 | + .toBe('https://www.google-analytics.com/mp/collect?measurement_id=456'); |
| 41 | + |
| 42 | + expectRequestUrl( |
| 43 | + /* apiSecret */ '123', |
| 44 | + /* measurementId */ '', |
| 45 | + /* measurementUrl */ undefined) |
| 46 | + .toBe('https://www.google-analytics.com/mp/collect?api_secret=123'); |
| 47 | + |
| 48 | + expectRequestUrl( |
| 49 | + /* apiSecret */ '', |
| 50 | + /* measurementId */ '', |
| 51 | + /* measurementUrl */ undefined) |
| 52 | + .toBe('https://www.google-analytics.com/mp/collect'); |
| 53 | + }); |
| 54 | + |
| 55 | + it('creates the correct query string when using an overriden ' + |
| 56 | + 'measurement url', () => { |
| 57 | + expectRequestUrl( |
| 58 | + /* apiSecret */ '123', |
| 59 | + /* measurementId */ '456', |
| 60 | + /* measurementUrl */ 'https://test.com') |
| 61 | + .toBe('https://test.com?measurement_id=456&api_secret=123'); |
| 62 | + |
| 63 | + expectRequestUrl( |
| 64 | + /* apiSecret */ '', |
| 65 | + /* measurementId */ '456', |
| 66 | + /* measurementUrl */ 'https://test.com') |
| 67 | + .toBe('https://test.com?measurement_id=456'); |
| 68 | + |
| 69 | + expectRequestUrl( |
| 70 | + /* apiSecret */ '123', |
| 71 | + /* measurementId */ '', |
| 72 | + /* measurementUrl */ 'https://test.com') |
| 73 | + .toBe('https://test.com?api_secret=123'); |
| 74 | + |
| 75 | + expectRequestUrl( |
| 76 | + /* apiSecret */ '', |
| 77 | + /* measurementId */ '', |
| 78 | + /* measurementUrl */ 'https://test.com') |
| 79 | + .toBe('https://test.com'); |
| 80 | + }); |
| 81 | + |
| 82 | + it('encodes query parameters', () => { |
| 83 | + expectRequestUrl( |
| 84 | + /* apiSecret */ '12 3', |
| 85 | + /* measurementId */ '45 6', |
| 86 | + /* measurementUrl */ 'https://test.com') |
| 87 | + .toBe('https://test.com?measurement_id=45%206&api_secret=12%203'); |
| 88 | + }); |
| 89 | +}); |
0 commit comments