1
+ <?php
2
+
3
+ namespace Bilfeldt \LaravelHttpClientLogger \Tests ;
4
+
5
+ use Bilfeldt \LaravelHttpClientLogger \HttpLogger ;
6
+ use Bilfeldt \LaravelHttpClientLogger \MessageAccessor ;
7
+ use Bilfeldt \LaravelHttpClientLogger \PsrMessageToStringConverter ;
8
+ use GuzzleHttp \Psr7 \Request ;
9
+ use GuzzleHttp \Psr7 \Response ;
10
+ use Illuminate \Support \Facades \Http ;
11
+ use Illuminate \Support \Facades \Log ;
12
+ use Illuminate \Support \Str ;
13
+ use Psr \Http \Message \MessageInterface ;
14
+ use TiMacDonald \Log \LogEntry ;
15
+ use TiMacDonald \Log \LogFake ;
16
+
17
+ class HttpLoggerE2eTest extends TestCase
18
+ {
19
+ protected HttpLogger $ logger ;
20
+
21
+ public function setUp (): void
22
+ {
23
+ parent ::setUp ();
24
+
25
+ Http::preventStrayRequests ();
26
+ Http::fake ([
27
+ 'https://api.example.com/login?username=SECRET_USER&pass=SECRET_PASSWORD ' => Http::response (['authentication_token ' => 'SECRET_TOKEN ' ], 200 ),
28
+ 'https://api.example.com/documents ' => function ($ request ) {
29
+ $ authorizationHeader = $ request ->header ('Authorization ' );
30
+
31
+ if (($ authorizationHeader [0 ] ?? '' ) === 'Bearer SECRET_TOKEN ' ) {
32
+ return Http::response ([
33
+ ['id ' => '1 ' , 'title ' => 'Document Title 1 ' , 'author ' => 'Author Name 1 ' ],
34
+ ['id ' => '2 ' , 'title ' => 'Document Title 2 ' , 'author ' => 'Author Name 2 ' ]
35
+ ], 200 );
36
+ } else {
37
+ return Http::response (['error ' => 'Unauthorized ' ], 401 );
38
+ }
39
+ },
40
+ '* ' => Http::response (['error ' => 'Not Found ' ], 404 ),
41
+ ]);
42
+ }
43
+
44
+ public function test_accessor_adhoc_config ()
45
+ {
46
+ LogFake::bind ();
47
+
48
+ $ pendingRequest = Http::log (
49
+ [],
50
+ [ 'replace_json ' => [ 'authentication_token ' ],
51
+ 'replace_headers ' => ['Authorization ' ],
52
+ 'replace_query ' => ['username ' , 'pass ' ]
53
+ ]
54
+ );
55
+
56
+ $ responses = [
57
+ $ pendingRequest ->get ('https://api.example.com/login?username=SECRET_USER&pass=SECRET_PASSWORD ' ),
58
+ $ pendingRequest ->withToken ("SECRET_TOKEN " )->get ('https://api.example.com/documents ' )
59
+ ];
60
+
61
+ Log::assertLogged (fn (LogEntry $ log ) => !Str::contains ($ log ->message , 'SECRET_ ' ));
62
+
63
+ }
64
+
65
+ public function test_accessor_custom_class ()
66
+ {
67
+ LogFake::bind ();
68
+
69
+ Http::log ([], [
70
+ 'message_accessor_class ' => MockMessageAccessor::class,
71
+ ])->get ('https://api.example.com/login?username=SECRET_USER&pass=SECRET_PASSWORD ' );
72
+
73
+ Log::assertLogged (fn (LogEntry $ log ) => Str::contains ($ log ->message , 'TOP SECRET ' ));
74
+ }
75
+ }
76
+
77
+ class MockMessageAccessor extends MessageAccessor
78
+ {
79
+ public function getContent (MessageInterface $ message ) : string
80
+ {
81
+ return "TOP SECRET " ;
82
+ }
83
+ }
0 commit comments