|
| 1 | +/* eslint-disable require-jsdoc */ |
| 2 | +var Countly = require("../../lib/countly"); |
| 3 | +var hp = require("../support/helper"); |
| 4 | + |
| 5 | +/** |
| 6 | + * Tests for the fake_request_handler feature |
| 7 | + * This allows injecting a mock server in tests for controlled request/response handling |
| 8 | + */ |
| 9 | +describe("Fake request handler", () => { |
| 10 | + it("captures requests and provides custom responses via init config", () => { |
| 11 | + hp.haltAndClearStorage(() => { |
| 12 | + var capturedRequests = []; |
| 13 | + |
| 14 | + Countly.init({ |
| 15 | + app_key: hp.appKey, |
| 16 | + url: "https://test.count.ly", |
| 17 | + debug: true, |
| 18 | + fake_request_handler: function(req) { |
| 19 | + capturedRequests.push(req); |
| 20 | + // Return success response |
| 21 | + return { status: 200, responseText: '{"result":"Success"}' }; |
| 22 | + } |
| 23 | + }); |
| 24 | + |
| 25 | + Countly.add_event({ key: "test_event", count: 1 }); |
| 26 | + Countly.begin_session(); |
| 27 | + |
| 28 | + cy.wait(500).then(() => { |
| 29 | + // Verify requests were captured |
| 30 | + expect(capturedRequests.length).to.be.greaterThan(0); |
| 31 | + |
| 32 | + // Check that we have different request types |
| 33 | + var functionNames = capturedRequests.map(r => r.functionName); |
| 34 | + expect(functionNames).to.include("send_request_queue"); |
| 35 | + |
| 36 | + // Verify request structure |
| 37 | + var hasBeginSession = capturedRequests.some(r => |
| 38 | + r.params && r.params.begin_session === 1 |
| 39 | + ); |
| 40 | + expect(hasBeginSession).to.be.true; |
| 41 | + }); |
| 42 | + }); |
| 43 | + }); |
| 44 | + |
| 45 | + it("allows simulating error responses", () => { |
| 46 | + hp.haltAndClearStorage(() => { |
| 47 | + var requestCount = 0; |
| 48 | + |
| 49 | + Countly.init({ |
| 50 | + app_key: hp.appKey, |
| 51 | + url: "https://test.count.ly", |
| 52 | + debug: true, |
| 53 | + fake_request_handler: function() { |
| 54 | + requestCount++; |
| 55 | + // Simulate server error |
| 56 | + return { status: 500, responseText: 'Internal Server Error' }; |
| 57 | + } |
| 58 | + }); |
| 59 | + |
| 60 | + Countly.begin_session(); |
| 61 | + |
| 62 | + cy.wait(300).then(() => { |
| 63 | + // Requests should still be captured even on error |
| 64 | + expect(requestCount).to.be.greaterThan(0); |
| 65 | + }); |
| 66 | + }); |
| 67 | + }); |
| 68 | + |
| 69 | + it("can be set at runtime via _internals", () => { |
| 70 | + hp.haltAndClearStorage(() => { |
| 71 | + var capturedRequests = []; |
| 72 | + |
| 73 | + Countly.init({ |
| 74 | + app_key: hp.appKey, |
| 75 | + url: "https://test.count.ly", |
| 76 | + debug: true, |
| 77 | + test_mode: true // prevent queue processing initially |
| 78 | + }); |
| 79 | + |
| 80 | + // Set handler at runtime |
| 81 | + Countly._internals.setFakeRequestHandler(function(req) { |
| 82 | + capturedRequests.push(req); |
| 83 | + return { status: 200, responseText: '{"result":"Success"}' }; |
| 84 | + }); |
| 85 | + |
| 86 | + // Verify it was set |
| 87 | + var handler = Countly._internals.getFakeRequestHandler(); |
| 88 | + expect(handler).to.be.a("function"); |
| 89 | + |
| 90 | + // Enable request processing |
| 91 | + Countly.test_mode_rq(false); |
| 92 | + |
| 93 | + Countly.add_event({ key: "runtime_event", count: 1 }); |
| 94 | + |
| 95 | + cy.wait(500).then(() => { |
| 96 | + expect(capturedRequests.length).to.be.greaterThan(0); |
| 97 | + }); |
| 98 | + }); |
| 99 | + }); |
| 100 | + |
| 101 | + it("returns default success when handler returns undefined", () => { |
| 102 | + hp.haltAndClearStorage(() => { |
| 103 | + Countly.init({ |
| 104 | + app_key: hp.appKey, |
| 105 | + url: "https://test.count.ly", |
| 106 | + debug: true, |
| 107 | + fake_request_handler: function() { |
| 108 | + // Return nothing - should default to success |
| 109 | + return undefined; |
| 110 | + } |
| 111 | + }); |
| 112 | + |
| 113 | + Countly.begin_session(); |
| 114 | + |
| 115 | + cy.wait(300).then(() => { |
| 116 | + // Session should start successfully with default response |
| 117 | + expect(Countly._internals.testingGetRequests().length).to.be.greaterThan(0); |
| 118 | + }); |
| 119 | + }); |
| 120 | + }); |
| 121 | + |
| 122 | + it("skips callback when handler returns false", () => { |
| 123 | + hp.haltAndClearStorage(() => { |
| 124 | + var capturedRequests = []; |
| 125 | + |
| 126 | + Countly.init({ |
| 127 | + app_key: hp.appKey, |
| 128 | + url: "https://test.count.ly", |
| 129 | + debug: true, |
| 130 | + fake_request_handler: function(req) { |
| 131 | + capturedRequests.push(req); |
| 132 | + // Return false to skip callback entirely |
| 133 | + return false; |
| 134 | + } |
| 135 | + }); |
| 136 | + |
| 137 | + Countly.begin_session(); |
| 138 | + |
| 139 | + cy.wait(300).then(() => { |
| 140 | + // Requests should still be captured |
| 141 | + expect(capturedRequests.length).to.be.greaterThan(0); |
| 142 | + }); |
| 143 | + }); |
| 144 | + }); |
| 145 | + |
| 146 | + it("works with helper createFakeRequestHandler utility", () => { |
| 147 | + hp.haltAndClearStorage(() => { |
| 148 | + // Use the helper utility for cleaner test setup |
| 149 | + var fakeServer = hp.createFakeRequestHandler({ |
| 150 | + onRequest: function(req) { |
| 151 | + // Custom logic per request |
| 152 | + if (req.params && req.params.begin_session) { |
| 153 | + return { status: 200, responseText: '{"result":"Session started"}' }; |
| 154 | + } |
| 155 | + return { status: 200, responseText: '{"result":"OK"}' }; |
| 156 | + } |
| 157 | + }); |
| 158 | + |
| 159 | + Countly.init({ |
| 160 | + app_key: hp.appKey, |
| 161 | + url: "https://test.count.ly", |
| 162 | + debug: true, |
| 163 | + fake_request_handler: fakeServer.handler |
| 164 | + }); |
| 165 | + |
| 166 | + Countly.begin_session(); |
| 167 | + Countly.add_event({ key: "helper_event", count: 1 }); |
| 168 | + |
| 169 | + cy.wait(500).then(() => { |
| 170 | + // Use helper methods for easy querying |
| 171 | + var sessionReqs = fakeServer.findByParam("begin_session", 1); |
| 172 | + expect(sessionReqs.length).to.equal(1); |
| 173 | + |
| 174 | + var allReqs = fakeServer.getRequests(); |
| 175 | + expect(allReqs.length).to.be.greaterThan(0); |
| 176 | + |
| 177 | + // Clear for next test section |
| 178 | + fakeServer.clear(); |
| 179 | + expect(fakeServer.getRequests().length).to.equal(0); |
| 180 | + }); |
| 181 | + }); |
| 182 | + }); |
| 183 | + |
| 184 | + it("provides full request details for validation", () => { |
| 185 | + hp.haltAndClearStorage(() => { |
| 186 | + var lastRequest = null; |
| 187 | + |
| 188 | + Countly.init({ |
| 189 | + app_key: hp.appKey, |
| 190 | + url: "https://test.count.ly", |
| 191 | + debug: true, |
| 192 | + fake_request_handler: function(req) { |
| 193 | + lastRequest = req; |
| 194 | + return { status: 200, responseText: '{"result":"Success"}' }; |
| 195 | + } |
| 196 | + }); |
| 197 | + |
| 198 | + Countly.begin_session(); |
| 199 | + |
| 200 | + cy.wait(300).then(() => { |
| 201 | + expect(lastRequest).to.not.be.null; |
| 202 | + |
| 203 | + // Verify request structure has all expected fields |
| 204 | + expect(lastRequest).to.have.property("functionName"); |
| 205 | + expect(lastRequest).to.have.property("url"); |
| 206 | + expect(lastRequest).to.have.property("params"); |
| 207 | + |
| 208 | + // URL should match configured server |
| 209 | + expect(lastRequest.url).to.include("test.count.ly"); |
| 210 | + |
| 211 | + // Params should have app_key |
| 212 | + expect(lastRequest.params).to.have.property("app_key", hp.appKey); |
| 213 | + }); |
| 214 | + }); |
| 215 | + }); |
| 216 | +}); |
0 commit comments