Skip to content

Commit ca48caa

Browse files
Automatically truncating the user agent string to 255 characters #2
1 parent 5763ab3 commit ca48caa

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed

Changelog.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Eloquent History Changelog
22

3+
## Unreleased
4+
##### 2024-XX-YY
5+
6+
- Fixed MySQL/Postgres truncation errors when the user-agent string is longer than 255 characters by truncating the string to 255 chars before inserting to the DB
7+
38
## 1.6.0
49
##### 2024-11-28
510

src/JobTracker.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Illuminate\Support\Facades\Auth;
2222
use Illuminate\Support\Facades\Event;
2323
use Illuminate\Support\Facades\Request;
24+
use Illuminate\Support\Str;
2425
use Konekt\History\Contracts\JobExecution;
2526
use Konekt\History\Contracts\JobExecutionLog;
2627
use Konekt\History\Contracts\JobStatus;
@@ -203,7 +204,7 @@ protected static function commonFields(TrackableJob $job): array
203204
'tracking_id' => $job->getJobTrackingId(),
204205
'user_id' => Auth::id(),
205206
'ip_address' => Request::ip(),
206-
'user_agent' => Request::userAgent(),
207+
'user_agent' => Str::limit(Request::userAgent(), 255, ''),
207208
];
208209
}
209210

tests/UserAgentTest.php

+27-5
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616

1717
use Illuminate\Http\Request;
1818
use Konekt\History\History;
19+
use Konekt\History\JobTracker;
20+
use Konekt\History\Models\JobExecution;
1921
use Konekt\History\Tests\Dummies\SampleTask;
22+
use Konekt\History\Tests\Dummies\SampleTrackableJob;
2023

2124
class UserAgentTest extends TestCase
2225
{
@@ -25,16 +28,35 @@ class UserAgentTest extends TestCase
2528
/** @test */
2629
public function it_truncates_user_agent_strings_longer_than_255_characters_without_errors()
2730
{
28-
$request = Request::create('/example-route', 'GET', [], [], [], [
29-
'HTTP_USER_AGENT' => self::STUPID_UA_STRING,
30-
]);
31-
app()->forgetInstance('request');
32-
app()->singleton('request', fn () => $request);
31+
$this->forgeRequestWithStupidUserAgent();
3332

3433
$task = SampleTask::create(['title' => 'Task With a stupid Browser Agent String', 'status' => 'in-progress']);
3534
$entry = History::begin($task);
3635
$entry = $entry->fresh();
3736

3837
$this->assertEquals(substr(self::STUPID_UA_STRING, 0, 255), $entry->user_agent);
3938
}
39+
40+
/** @test */
41+
public function it_truncates_the_user_agent_strings_longer_than_255_characters_when_using_the_job_tracker()
42+
{
43+
$this->forgeRequestWithStupidUserAgent();
44+
45+
JobExecution::ofJobClass(SampleTrackableJob::class)->delete();
46+
47+
$job = new SampleTrackableJob(new SampleTask());
48+
$job->generateJobTrackingId();
49+
$entry = JobTracker::createFor($job);
50+
51+
$this->assertEquals(substr(self::STUPID_UA_STRING, 0, 255), $entry->user_agent);
52+
}
53+
54+
private function forgeRequestWithStupidUserAgent(): void
55+
{
56+
$request = Request::create('/example-route', 'GET', [], [], [], [
57+
'HTTP_USER_AGENT' => self::STUPID_UA_STRING,
58+
]);
59+
app()->forgetInstance('request');
60+
app()->singleton('request', fn () => $request);
61+
}
4062
}

0 commit comments

Comments
 (0)