|
| 1 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +# All rights reserved. |
| 3 | + |
| 4 | +# This source code is licensed under the license found in the |
| 5 | +# LICENSE file in the root directory of this source tree. |
| 6 | + |
| 7 | +require 'minitest/autorun' |
| 8 | +require_relative '../lib/capi_param_builder' |
| 9 | +require 'test_etld_plus_one_resolver' |
| 10 | + |
| 11 | +ORIGINAL_VERSION_TESU = ReleaseConfig::VERSION |
| 12 | +ReleaseConfig.send(:remove_const, :VERSION) |
| 13 | +ReleaseConfig::VERSION = "1.0.1" |
| 14 | +Minitest.after_run do |
| 15 | + ReleaseConfig.send(:remove_const, :VERSION) |
| 16 | + ReleaseConfig::VERSION = ORIGINAL_VERSION_TESU |
| 17 | +end |
| 18 | + |
| 19 | +class FakeRackRequest |
| 20 | + attr_reader :env |
| 21 | + def initialize(env) |
| 22 | + @env = env |
| 23 | + end |
| 24 | +end |
| 25 | + |
| 26 | + |
| 27 | +# ============================================================================= |
| 28 | +# construct_event_source_url scheme variants |
| 29 | +# ============================================================================= |
| 30 | +class TestEventSourceUrlSchemeVariants < Minitest::Test |
| 31 | + def test_https_scheme_with_host_and_uri |
| 32 | + builder = ParamBuilder.new |
| 33 | + data = PlainDataObject.new( |
| 34 | + "example.com", {}, {}, nil, nil, nil, "https", "/path/to/page" |
| 35 | + ) |
| 36 | + builder.process_request_from_context(data) |
| 37 | + |
| 38 | + assert_equal("https://example.com/path/to/page", builder.get_event_source_url) |
| 39 | + end |
| 40 | + |
| 41 | + def test_http_scheme_with_host_and_uri |
| 42 | + builder = ParamBuilder.new |
| 43 | + data = PlainDataObject.new( |
| 44 | + "example.com", {}, {}, nil, nil, nil, "http", "/landing" |
| 45 | + ) |
| 46 | + builder.process_request_from_context(data) |
| 47 | + |
| 48 | + assert_equal("http://example.com/landing", builder.get_event_source_url) |
| 49 | + end |
| 50 | + |
| 51 | + def test_nil_scheme_returns_nil |
| 52 | + builder = ParamBuilder.new |
| 53 | + data = PlainDataObject.new( |
| 54 | + "example.com", {}, {}, nil, nil, nil, nil, "/some/path" |
| 55 | + ) |
| 56 | + builder.process_request_from_context(data) |
| 57 | + |
| 58 | + assert_nil(builder.get_event_source_url) |
| 59 | + end |
| 60 | + |
| 61 | + def test_empty_scheme_returns_nil |
| 62 | + builder = ParamBuilder.new |
| 63 | + data = PlainDataObject.new( |
| 64 | + "example.com", {}, {}, nil, nil, nil, "", "/some/path" |
| 65 | + ) |
| 66 | + builder.process_request_from_context(data) |
| 67 | + |
| 68 | + assert_nil(builder.get_event_source_url) |
| 69 | + end |
| 70 | +end |
| 71 | + |
| 72 | + |
| 73 | +# ============================================================================= |
| 74 | +# Nil host returns nil |
| 75 | +# ============================================================================= |
| 76 | +class TestEventSourceUrlNilHost < Minitest::Test |
| 77 | + def test_nil_host_returns_nil |
| 78 | + builder = ParamBuilder.new |
| 79 | + data = PlainDataObject.new( |
| 80 | + nil, {}, {}, nil, nil, nil, "https", "/path" |
| 81 | + ) |
| 82 | + builder.process_request_from_context(data) |
| 83 | + |
| 84 | + assert_nil(builder.get_event_source_url) |
| 85 | + end |
| 86 | + |
| 87 | + def test_empty_host_returns_nil |
| 88 | + builder = ParamBuilder.new |
| 89 | + data = PlainDataObject.new( |
| 90 | + "", {}, {}, nil, nil, nil, "https", "/path" |
| 91 | + ) |
| 92 | + builder.process_request_from_context(data) |
| 93 | + |
| 94 | + assert_nil(builder.get_event_source_url) |
| 95 | + end |
| 96 | +end |
| 97 | + |
| 98 | + |
| 99 | +# ============================================================================= |
| 100 | +# All nil returns nil |
| 101 | +# ============================================================================= |
| 102 | +class TestEventSourceUrlAllNil < Minitest::Test |
| 103 | + def test_all_nil_returns_nil |
| 104 | + builder = ParamBuilder.new |
| 105 | + data = PlainDataObject.new( |
| 106 | + nil, {}, {}, nil, nil, nil, nil, nil |
| 107 | + ) |
| 108 | + builder.process_request_from_context(data) |
| 109 | + |
| 110 | + assert_nil(builder.get_event_source_url) |
| 111 | + end |
| 112 | + |
| 113 | + def test_returns_nil_before_any_processing |
| 114 | + builder = ParamBuilder.new |
| 115 | + |
| 116 | + assert_nil(builder.get_event_source_url) |
| 117 | + end |
| 118 | +end |
| 119 | + |
| 120 | + |
| 121 | +# ============================================================================= |
| 122 | +# Host with port preserved |
| 123 | +# ============================================================================= |
| 124 | +class TestEventSourceUrlHostWithPort < Minitest::Test |
| 125 | + def test_host_with_port_preserved |
| 126 | + builder = ParamBuilder.new |
| 127 | + data = PlainDataObject.new( |
| 128 | + "example.com:8080", {}, {}, nil, nil, nil, "https", "/app" |
| 129 | + ) |
| 130 | + builder.process_request_from_context(data) |
| 131 | + |
| 132 | + assert_equal("https://example.com:8080/app", builder.get_event_source_url) |
| 133 | + end |
| 134 | + |
| 135 | + def test_host_with_port_no_uri |
| 136 | + builder = ParamBuilder.new |
| 137 | + data = PlainDataObject.new( |
| 138 | + "localhost:3000", {}, {}, nil, nil, nil, "http", nil |
| 139 | + ) |
| 140 | + builder.process_request_from_context(data) |
| 141 | + |
| 142 | + assert_equal("http://localhost:3000", builder.get_event_source_url) |
| 143 | + end |
| 144 | +end |
| 145 | + |
| 146 | + |
| 147 | +# ============================================================================= |
| 148 | +# Query string preserved |
| 149 | +# ============================================================================= |
| 150 | +class TestEventSourceUrlQueryString < Minitest::Test |
| 151 | + def test_query_string_in_request_uri_preserved |
| 152 | + builder = ParamBuilder.new |
| 153 | + data = PlainDataObject.new( |
| 154 | + "shop.example.com", {}, {}, nil, nil, nil, |
| 155 | + "https", "/products?category=shoes&page=2" |
| 156 | + ) |
| 157 | + builder.process_request_from_context(data) |
| 158 | + |
| 159 | + assert_equal( |
| 160 | + "https://shop.example.com/products?category=shoes&page=2", |
| 161 | + builder.get_event_source_url |
| 162 | + ) |
| 163 | + end |
| 164 | + |
| 165 | + def test_uri_without_query_string |
| 166 | + builder = ParamBuilder.new |
| 167 | + data = PlainDataObject.new( |
| 168 | + "example.com", {}, {}, nil, nil, nil, "https", "/clean-path" |
| 169 | + ) |
| 170 | + builder.process_request_from_context(data) |
| 171 | + |
| 172 | + assert_equal("https://example.com/clean-path", builder.get_event_source_url) |
| 173 | + end |
| 174 | +end |
| 175 | + |
| 176 | + |
| 177 | +# ============================================================================= |
| 178 | +# process_request sets event_source_url to nil |
| 179 | +# ============================================================================= |
| 180 | +class TestEventSourceUrlViaProcessRequest < Minitest::Test |
| 181 | + def test_process_request_returns_nil_for_event_source_url |
| 182 | + builder = ParamBuilder.new |
| 183 | + builder.process_request("example.com", { "fbclid" => "test" }, {}) |
| 184 | + |
| 185 | + assert_nil(builder.get_event_source_url) |
| 186 | + end |
| 187 | + |
| 188 | + def test_process_request_with_referer_still_returns_nil |
| 189 | + builder = ParamBuilder.new |
| 190 | + builder.process_request( |
| 191 | + "example.com", {}, {}, "https://facebook.com/ad" |
| 192 | + ) |
| 193 | + |
| 194 | + assert_nil(builder.get_event_source_url) |
| 195 | + end |
| 196 | +end |
| 197 | + |
| 198 | + |
| 199 | +# ============================================================================= |
| 200 | +# process_request_from_context sets event_source_url |
| 201 | +# ============================================================================= |
| 202 | +class TestEventSourceUrlViaProcessRequestFromContext < Minitest::Test |
| 203 | + def test_plain_data_object_sets_event_source_url |
| 204 | + builder = ParamBuilder.new |
| 205 | + data = PlainDataObject.new( |
| 206 | + "example.com", { "fbclid" => "ctx1" }, {}, nil, nil, nil, |
| 207 | + "https", "/checkout" |
| 208 | + ) |
| 209 | + builder.process_request_from_context(data) |
| 210 | + |
| 211 | + assert_equal("https://example.com/checkout", builder.get_event_source_url) |
| 212 | + end |
| 213 | + |
| 214 | + def test_plain_data_object_without_uri |
| 215 | + builder = ParamBuilder.new |
| 216 | + data = PlainDataObject.new( |
| 217 | + "example.com", {}, {}, nil, nil, nil, "https", nil |
| 218 | + ) |
| 219 | + builder.process_request_from_context(data) |
| 220 | + |
| 221 | + assert_equal("https://example.com", builder.get_event_source_url) |
| 222 | + end |
| 223 | + |
| 224 | + def test_rack_env_hash_sets_event_source_url |
| 225 | + builder = ParamBuilder.new |
| 226 | + env = { |
| 227 | + "HTTP_HOST" => "shop.example.com", |
| 228 | + "QUERY_STRING" => "fbclid=abc123", |
| 229 | + "REQUEST_SCHEME" => "https", |
| 230 | + "REQUEST_URI" => "/products?fbclid=abc123" |
| 231 | + } |
| 232 | + builder.process_request_from_context(env) |
| 233 | + |
| 234 | + assert_equal( |
| 235 | + "https://shop.example.com/products?fbclid=abc123", |
| 236 | + builder.get_event_source_url |
| 237 | + ) |
| 238 | + end |
| 239 | + |
| 240 | + def test_rack_env_hash_without_scheme_returns_nil |
| 241 | + builder = ParamBuilder.new |
| 242 | + env = { |
| 243 | + "HTTP_HOST" => "example.com", |
| 244 | + "QUERY_STRING" => "", |
| 245 | + "PATH_INFO" => "/page" |
| 246 | + } |
| 247 | + builder.process_request_from_context(env) |
| 248 | + |
| 249 | + assert_nil(builder.get_event_source_url) |
| 250 | + end |
| 251 | + |
| 252 | + def test_rack_request_object_sets_event_source_url |
| 253 | + builder = ParamBuilder.new |
| 254 | + request = FakeRackRequest.new( |
| 255 | + "HTTP_HOST" => "rack-app.com", |
| 256 | + "QUERY_STRING" => "", |
| 257 | + "REQUEST_SCHEME" => "https", |
| 258 | + "REQUEST_URI" => "/dashboard" |
| 259 | + ) |
| 260 | + builder.process_request_from_context(request) |
| 261 | + |
| 262 | + assert_equal("https://rack-app.com/dashboard", builder.get_event_source_url) |
| 263 | + end |
| 264 | +end |
| 265 | + |
| 266 | + |
| 267 | +# ============================================================================= |
| 268 | +# Reset between consecutive calls |
| 269 | +# ============================================================================= |
| 270 | +class TestEventSourceUrlResetBetweenCalls < Minitest::Test |
| 271 | + def test_event_source_url_resets_to_nil_when_scheme_missing |
| 272 | + builder = ParamBuilder.new |
| 273 | + data_with = PlainDataObject.new( |
| 274 | + "example.com", {}, {}, nil, nil, nil, "https", "/first" |
| 275 | + ) |
| 276 | + builder.process_request_from_context(data_with) |
| 277 | + assert_equal("https://example.com/first", builder.get_event_source_url) |
| 278 | + |
| 279 | + data_without = PlainDataObject.new( |
| 280 | + "example.com", {}, {}, nil, nil, nil, nil, "/second" |
| 281 | + ) |
| 282 | + builder.process_request_from_context(data_without) |
| 283 | + assert_nil(builder.get_event_source_url) |
| 284 | + end |
| 285 | + |
| 286 | + def test_event_source_url_updates_on_second_call |
| 287 | + builder = ParamBuilder.new |
| 288 | + data1 = PlainDataObject.new( |
| 289 | + "first.com", {}, {}, nil, nil, nil, "https", "/a" |
| 290 | + ) |
| 291 | + builder.process_request_from_context(data1) |
| 292 | + assert_equal("https://first.com/a", builder.get_event_source_url) |
| 293 | + |
| 294 | + data2 = PlainDataObject.new( |
| 295 | + "second.com", {}, {}, nil, nil, nil, "http", "/b" |
| 296 | + ) |
| 297 | + builder.process_request_from_context(data2) |
| 298 | + assert_equal("http://second.com/b", builder.get_event_source_url) |
| 299 | + end |
| 300 | + |
| 301 | + def test_event_source_url_resets_when_process_request_called |
| 302 | + builder = ParamBuilder.new |
| 303 | + data = PlainDataObject.new( |
| 304 | + "example.com", {}, {}, nil, nil, nil, "https", "/page" |
| 305 | + ) |
| 306 | + builder.process_request_from_context(data) |
| 307 | + assert_equal("https://example.com/page", builder.get_event_source_url) |
| 308 | + |
| 309 | + builder.process_request("example.com", {}, {}) |
| 310 | + assert_nil(builder.get_event_source_url) |
| 311 | + end |
| 312 | +end |
| 313 | + |
| 314 | + |
| 315 | +# ============================================================================= |
| 316 | +# Independence from referrer_url |
| 317 | +# ============================================================================= |
| 318 | +class TestEventSourceUrlIndependentFromReferrer < Minitest::Test |
| 319 | + def test_event_source_url_set_regardless_of_referer |
| 320 | + builder = ParamBuilder.new |
| 321 | + data = PlainDataObject.new( |
| 322 | + "example.com", {}, {}, "https://facebook.com/ad", nil, nil, |
| 323 | + "https", "/landing" |
| 324 | + ) |
| 325 | + builder.process_request_from_context(data) |
| 326 | + |
| 327 | + assert_equal("https://example.com/landing", builder.get_event_source_url) |
| 328 | + assert_equal("https://facebook.com/ad", builder.get_referrer_url) |
| 329 | + end |
| 330 | + |
| 331 | + def test_event_source_url_nil_does_not_affect_referer |
| 332 | + builder = ParamBuilder.new |
| 333 | + data = PlainDataObject.new( |
| 334 | + nil, {}, {}, "https://facebook.com/ad", nil, nil, "https", "/path" |
| 335 | + ) |
| 336 | + builder.process_request_from_context(data) |
| 337 | + |
| 338 | + assert_nil(builder.get_event_source_url) |
| 339 | + assert_equal("https://facebook.com/ad", builder.get_referrer_url) |
| 340 | + end |
| 341 | + |
| 342 | + def test_referer_nil_does_not_affect_event_source_url |
| 343 | + builder = ParamBuilder.new |
| 344 | + data = PlainDataObject.new( |
| 345 | + "example.com", {}, {}, nil, nil, nil, "https", "/page" |
| 346 | + ) |
| 347 | + builder.process_request_from_context(data) |
| 348 | + |
| 349 | + assert_equal("https://example.com/page", builder.get_event_source_url) |
| 350 | + assert_nil(builder.get_referrer_url) |
| 351 | + end |
| 352 | +end |
0 commit comments