1414 */
1515class ClientTest extends TestCase
1616{
17- public function testDefaultHeaders (): void
17+ public function testBaseUrlUsesStagehandAPIUrlEnv (): void
1818 {
19- $ transporter = new Client ;
20- $ mockRsp = Psr17FactoryDiscovery:: findResponseFactory ()
21- -> createResponse ()
22- -> withStatus ( 200 )
23- -> withHeader ( ' Content-Type ' , ' application/json ' )
24- -> withBody (Psr17FactoryDiscovery:: findStreamFactory ()-> createStream ( json_encode ([], flags: Util:: JSON_ENCODE_FLAGS ) ?: '' ))
25- ;
19+ $ this -> withEnv (
20+ [
21+ ' STAGEHAND_API_URL ' => ' http://localhost:5000/from-api-env ' ,
22+ ' STAGEHAND_BASE_URL ' => ' http://localhost:5000/from-base-env ' ,
23+ ],
24+ function (): void {
25+ $ transporter = $ this -> mockTransport () ;
2626
27- $ transporter ->setDefaultResponse ($ mockRsp );
27+ $ client = new \Stagehand \Client (
28+ browserbaseAPIKey: 'My Browserbase API Key ' ,
29+ browserbaseProjectID: 'My Browserbase Project ID ' ,
30+ modelAPIKey: 'My Model API Key ' ,
31+ requestOptions: ['transporter ' => $ transporter ],
32+ );
33+
34+ $ client ->sessions ->start (modelName: 'openai/gpt-5.4-mini ' );
35+
36+ $ this ->assertNotFalse ($ requested = $ transporter ->getRequests ()[0 ] ?? false );
37+ $ this ->assertSame (
38+ 'http://localhost:5000/from-api-env/v1/sessions/start ' ,
39+ (string ) $ requested ->getUri ()
40+ );
41+ },
42+ );
43+ }
44+
45+ public function testBaseUrlUsesLegacyStagehandBaseUrlEnv (): void
46+ {
47+ $ this ->withEnv (
48+ [
49+ 'STAGEHAND_API_URL ' => null ,
50+ 'STAGEHAND_BASE_URL ' => 'http://localhost:5000/from-base-env ' ,
51+ ],
52+ function (): void {
53+ $ transporter = $ this ->mockTransport ();
54+
55+ $ client = new \Stagehand \Client (
56+ browserbaseAPIKey: 'My Browserbase API Key ' ,
57+ browserbaseProjectID: 'My Browserbase Project ID ' ,
58+ modelAPIKey: 'My Model API Key ' ,
59+ requestOptions: ['transporter ' => $ transporter ],
60+ );
61+
62+ $ client ->sessions ->start (modelName: 'openai/gpt-5.4-mini ' );
63+
64+ $ this ->assertNotFalse ($ requested = $ transporter ->getRequests ()[0 ] ?? false );
65+ $ this ->assertSame (
66+ 'http://localhost:5000/from-base-env/v1/sessions/start ' ,
67+ (string ) $ requested ->getUri ()
68+ );
69+ },
70+ );
71+ }
72+
73+ public function testDefaultHeaders (): void
74+ {
75+ $ transporter = $ this ->mockTransport ();
2876
2977 $ client = new \Stagehand \Client (
3078 baseUrl: 'http://localhost ' ,
@@ -43,4 +91,43 @@ public function testDefaultHeaders(): void
4391 $ this ->assertNotEmpty ($ sent );
4492 }
4593 }
94+
95+ private function mockTransport (): Client
96+ {
97+ $ transporter = new Client ;
98+ $ mockRsp = Psr17FactoryDiscovery::findResponseFactory ()
99+ ->createResponse ()
100+ ->withStatus (200 )
101+ ->withHeader ('Content-Type ' , 'application/json ' )
102+ ->withBody (Psr17FactoryDiscovery::findStreamFactory ()->createStream (json_encode ([], flags: Util::JSON_ENCODE_FLAGS ) ?: '' ))
103+ ;
104+
105+ $ transporter ->setDefaultResponse ($ mockRsp );
106+
107+ return $ transporter ;
108+ }
109+
110+ /**
111+ * @param array<string, string|null> $vars
112+ */
113+ private function withEnv (array $ vars , callable $ callback ): void
114+ {
115+ $ oldValues = [];
116+ foreach ($ vars as $ key => $ _ ) {
117+ $ value = getenv ($ key );
118+ $ oldValues [$ key ] = false === $ value ? null : $ value ;
119+ }
120+
121+ try {
122+ foreach ($ vars as $ key => $ value ) {
123+ null === $ value ? putenv ($ key ) : putenv ("{$ key }= {$ value }" );
124+ }
125+
126+ $ callback ();
127+ } finally {
128+ foreach ($ oldValues as $ key => $ value ) {
129+ null === $ value ? putenv ($ key ) : putenv ("{$ key }= {$ value }" );
130+ }
131+ }
132+ }
46133}
0 commit comments