@@ -298,6 +298,77 @@ public function testCanGrabGetRawJSON()
298298 $ this ->assertEquals ($ expected , $ request ->getJSON (true ));
299299 }
300300
301+ public function testCanGetAVariableFromJson ()
302+ {
303+ $ jsonObj = [
304+ 'foo ' => 'bar ' ,
305+ 'baz ' => [
306+ 'fizz ' => 'buzz ' ,
307+ ],
308+ ];
309+ $ json = json_encode ($ jsonObj );
310+
311+ $ config = new App ();
312+ $ config ->baseURL = 'http://example.com/ ' ;
313+
314+ $ request = new IncomingRequest ($ config , new URI (), $ json , new UserAgent ());
315+
316+ $ this ->assertEquals ('bar ' , $ request ->getJsonVar ('foo ' ));
317+ $ jsonVar = $ request ->getJsonVar ('baz ' );
318+ $ this ->assertIsObject ($ jsonVar );
319+ $ this ->assertEquals ('buzz ' , $ jsonVar ->fizz );
320+ $ this ->assertEquals ('buzz ' , $ request ->getJsonVar ('baz.fizz ' ));
321+ }
322+
323+ public function testGetJsonVarAsArray ()
324+ {
325+ $ jsonObj = [
326+ 'baz ' => [
327+ 'fizz ' => 'buzz ' ,
328+ 'foo ' => 'bar ' ,
329+ ],
330+ ];
331+ $ json = json_encode ($ jsonObj );
332+
333+ $ config = new App ();
334+ $ config ->baseURL = 'http://example.com/ ' ;
335+
336+ $ request = new IncomingRequest ($ config , new URI (), $ json , new UserAgent ());
337+
338+ $ jsonVar = $ request ->getJsonVar ('baz ' , true );
339+ $ this ->assertIsArray ($ jsonVar );
340+ $ this ->assertEquals ('buzz ' , $ jsonVar ['fizz ' ]);
341+ $ this ->assertEquals ('bar ' , $ jsonVar ['foo ' ]);
342+ }
343+
344+ public function testGetVarWorksWithJson ()
345+ {
346+ $ jsonObj = [
347+ 'foo ' => 'bar ' ,
348+ 'fizz ' => 'buzz ' ,
349+ ];
350+ $ json = json_encode ($ jsonObj );
351+
352+ $ config = new App ();
353+ $ config ->baseURL = 'http://example.com/ ' ;
354+
355+ $ request = new IncomingRequest ($ config , new URI (), $ json , new UserAgent ());
356+ $ request ->setHeader ('Content-Type ' , 'application/json ' );
357+
358+ $ this ->assertEquals ('bar ' , $ request ->getVar ('foo ' ));
359+ $ this ->assertEquals ('buzz ' , $ request ->getVar ('fizz ' ));
360+
361+ $ multiple = $ request ->getVar (['foo ' , 'fizz ' ]);
362+ $ this ->assertIsArray ($ multiple );
363+ $ this ->assertEquals ('bar ' , $ multiple ['foo ' ]);
364+ $ this ->assertEquals ('buzz ' , $ multiple ['fizz ' ]);
365+
366+ $ all = $ request ->getVar ();
367+ $ this ->assertIsObject ($ all );
368+ $ this ->assertEquals ('bar ' , $ all ->foo );
369+ $ this ->assertEquals ('buzz ' , $ all ->fizz );
370+ }
371+
301372 public function testCanGrabGetRawInput ()
302373 {
303374 $ rawstring = 'username=admin001&role=administrator&usepass=0 ' ;
0 commit comments