@@ -46,6 +46,67 @@ public function testSuccessfulResponseEmitsEnd()
46
46
$ loop ->run ();
47
47
}
48
48
49
+ /** @group internet */
50
+ public function testPostDataReturnsData ()
51
+ {
52
+ $ loop = Factory::create ();
53
+ $ client = new Client ($ loop );
54
+
55
+ $ data = str_repeat ('. ' , 33000 );
56
+ $ request = $ client ->request ('POST ' , 'https:// ' . (mt_rand (0 , 1 ) === 0 ? 'eu. ' : '' ) . 'httpbin.org/post ' , array ('Content-Length ' => strlen ($ data )));
57
+
58
+ $ buffer = '' ;
59
+ $ request ->on ('response ' , function (Response $ response ) use (&$ buffer ) {
60
+ $ response ->on ('data ' , function ($ chunk ) use (&$ buffer ) {
61
+ $ buffer .= $ chunk ;
62
+ });
63
+ });
64
+
65
+ $ request ->on ('error ' , 'printf ' );
66
+ $ request ->on ('error ' , $ this ->expectCallableNever ());
67
+
68
+ $ request ->end ($ data );
69
+
70
+ $ loop ->run ();
71
+
72
+ $ this ->assertNotEquals ('' , $ buffer );
73
+
74
+ $ parsed = json_decode ($ buffer , true );
75
+ $ this ->assertTrue (is_array ($ parsed ) && isset ($ parsed ['data ' ]));
76
+ $ this ->assertEquals (strlen ($ data ), strlen ($ parsed ['data ' ]));
77
+ $ this ->assertEquals ($ data , $ parsed ['data ' ]);
78
+ }
79
+
80
+ /** @group internet */
81
+ public function testPostJsonReturnsData ()
82
+ {
83
+ $ loop = Factory::create ();
84
+ $ client = new Client ($ loop );
85
+
86
+ $ data = json_encode (array ('numbers ' => range (1 , 50 )));
87
+ $ request = $ client ->request ('POST ' , 'https://httpbin.org/post ' , array ('Content-Length ' => strlen ($ data ), 'Content-Type ' => 'application/json ' ));
88
+
89
+ $ buffer = '' ;
90
+ $ request ->on ('response ' , function (Response $ response ) use (&$ buffer ) {
91
+ $ response ->on ('data ' , function ($ chunk ) use (&$ buffer ) {
92
+ $ buffer .= $ chunk ;
93
+ });
94
+ });
95
+
96
+ $ request ->on ('error ' , 'printf ' );
97
+ $ request ->on ('error ' , $ this ->expectCallableNever ());
98
+
99
+ $ request ->end ($ data );
100
+
101
+ $ loop ->run ();
102
+
103
+ $ this ->assertNotEquals ('' , $ buffer );
104
+
105
+ $ parsed = json_decode ($ buffer , true );
106
+ $ this ->assertTrue (is_array ($ parsed ) && isset ($ parsed ['json ' ]));
107
+ $ this ->assertEquals (json_decode ($ data , true ), $ parsed ['json ' ]);
108
+ }
109
+
49
110
/** @group internet */
50
111
public function testCancelPendingConnectionEmitsClose ()
51
112
{
0 commit comments