1
+ <?php
2
+
3
+ namespace Spatie \WebhookClient \Tests \Unit ;
4
+
5
+ use Illuminate \Http \Request ;
6
+ use Spatie \WebhookClient \Models \WebhookCall ;
7
+ use Spatie \WebhookClient \SignatureValidator \DefaultSignatureValidator ;
8
+ use Spatie \WebhookClient \Tests \TestCase ;
9
+ use Spatie \WebhookClient \Tests \TestClasses \ProcessWebhookJobTestClass ;
10
+ use Spatie \WebhookClient \WebhookConfig ;
11
+ use Spatie \WebhookClient \WebhookProfile \ProcessEverythingWebhookProfile ;
12
+
13
+ class WebhookCallTest extends TestCase
14
+ {
15
+ /** @test */
16
+ public function it_stores_url_without_truncation_when_under_255_characters ()
17
+ {
18
+ $ url = 'https://example.com/webhook?param=value ' ;
19
+ $ request = Request::create ($ url );
20
+ $ config = new WebhookConfig ([
21
+ 'name ' => 'test ' ,
22
+ 'signing_secret ' => 'test ' ,
23
+ 'signature_header_name ' => 'test ' ,
24
+ 'signature_validator ' => DefaultSignatureValidator::class,
25
+ 'webhook_profile ' => ProcessEverythingWebhookProfile::class,
26
+ 'webhook_response ' => null ,
27
+ 'webhook_model ' => WebhookCall::class,
28
+ 'store_headers ' => [],
29
+ 'process_webhook_job ' => ProcessWebhookJobTestClass::class
30
+ ]);
31
+
32
+ $ webhookCall = WebhookCall::storeWebhook ($ config , $ request );
33
+
34
+ $ this ->assertEquals ($ url , $ webhookCall ->url );
35
+ }
36
+
37
+ /** @test */
38
+ public function it_truncates_url_when_exceeding_255_characters ()
39
+ {
40
+ $ baseUrl = 'https://example.com/webhook? ' ;
41
+ $ params = [];
42
+ for ($ i = 0 ; $ i < 100 ; $ i ++) {
43
+ $ params [] = "param {$ i }= " . str_repeat ('x ' , 10 );
44
+ }
45
+ $ longUrl = $ baseUrl . implode ('& ' , $ params );
46
+ $ request = Request::create ($ longUrl );
47
+ $ config = new WebhookConfig ([
48
+ 'name ' => 'test ' ,
49
+ 'signing_secret ' => 'test ' ,
50
+ 'signature_header_name ' => 'test ' ,
51
+ 'signature_validator ' => DefaultSignatureValidator::class,
52
+ 'webhook_profile ' => ProcessEverythingWebhookProfile::class,
53
+ 'webhook_response ' => null ,
54
+ 'webhook_model ' => WebhookCall::class,
55
+ 'store_headers ' => [],
56
+ 'process_webhook_job ' => ProcessWebhookJobTestClass::class
57
+ ]);
58
+
59
+ $ webhookCall = WebhookCall::storeWebhook ($ config , $ request );
60
+
61
+ $ this ->assertLessThanOrEqual (255 , strlen ($ webhookCall ->url ));
62
+ $ this ->assertStringEndsWith ('... ' , $ webhookCall ->url );
63
+ }
64
+ }
0 commit comments