summaryrefslogtreecommitdiff
path: root/http/codebird-php/test/replyparse_tests.php
blob: f1436fde84aca93386df6ff193d14f292867e921 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
<?php

namespace Codebird;
require_once ('test/codebirdm.php');

/**
 * A Twitter library in PHP.
 *
 * @package   codebird-test
 * @author    Jublo Solutions <support@jublo.net>
 * @copyright 2010-2016 Jublo Solutions <support@jublo.net>
 * @license   https://opensource.org/licenses/GPL-3.0 GNU General Public License 3.0
 * @link      https://github.com/jublonet/codebird-php
 */

/**
 * Reply parsing tests
 *
 * @package codebird-test
 */
class Replyparse_Test extends \PHPUnit_Framework_TestCase
{
  /**
   * Initialise Codebird class
   *
   * @return \Codebird\Codebird The Codebird class
   */
  protected function getCB()
  {
    Codebird::setConsumerKey('123', '456');
    $cb = new CodebirdM();

    return $cb;
  }

  /**
   * Tests _getHttpStatusFromHeaders
   */
  public function testGetHttpStatusFromHeaders()
  {
    $cb = $this->getCB();
    $this->assertEquals(
      '500',
      $cb->call('_getHttpStatusFromHeaders', [['']])
    );
    $this->assertEquals(
      '200',
      $cb->call('_getHttpStatusFromHeaders', [
        ['HTTP/1.1 200 OK'],
        ['X-Some-Data: test']
      ])
    );
    $this->assertEquals(
      '404',
      $cb->call('_getHttpStatusFromHeaders', [
        ['HTTP/1.1 404 Not Found'],
        ['X-Some-Data: test']
      ])
    );
  }

  /**
   * Tests _parseBearerReply
   */
  public function testParseBearerReply()
  {
    $cb = $this->getCB();
    $cb->setBearerToken(null);
    // get raw reply from mock collection
    $reply = $cb->getStatic('_mock_replies')['POST https://api.twitter.com/oauth2/token'];
    // check that bearer token is not yet set
    $this->assertNull($cb->getStatic('_bearer_token'));
    // parse it as object
    $result = $cb->call(
      '_parseBearerReply',
      [
        $reply['reply'],
        $reply['httpstatus']
      ]
    );
    $expected = new \stdClass;
    $expected->token_type = 'bearer';
    $expected->access_token = 'VqiO0n2HrKE';
    $expected->httpstatus = 200;
    $expected->rate = null;
    $this->assertEquals($expected, $result);
    // check that bearer token was actually set
    $this->assertNotNull($cb->getStatic('_bearer_token'));

    // array
    $cb->setReturnFormat(CODEBIRD_RETURNFORMAT_ARRAY);
    $cb->setBearerToken(null);
    $result = $cb->call(
      '_parseBearerReply',
      [
        $reply['reply'],
        $reply['httpstatus']
      ]
    );
    $expected = [
      'token_type'   => 'bearer',
      'access_token' => 'VqiO0n2HrKE',
      'httpstatus'   => 200,
      'rate'         => null
    ];
    $this->assertEquals($expected, $result);
    // check that bearer token was actually set
    $this->assertNotNull($cb->getStatic('_bearer_token'));

    // JSON
    $cb->setReturnFormat(CODEBIRD_RETURNFORMAT_JSON);
    $cb->setBearerToken(null);
    $result = $cb->call(
      '_parseBearerReply',
      [
        $reply['reply'],
        $reply['httpstatus']
      ]
    );
    $expected = '{"token_type":"bearer","access_token":"VqiO0n2HrKE"}';
    $this->assertEquals($expected, $result);
    // check that bearer token was actually set
    $this->assertNotNull($cb->getStatic('_bearer_token'));
  }

  /**
   * Tests _getRateLimitInfo
   */
  public function testGetRateLimitInfo()
  {
    $cb = $this->getCB();
    $headers = [
      'content-length' => 68,
      'content-type'   => 'application/json;charset=utf-8',
      'date'           => 'Sun, 06 Dec 2015 14:43:28 GMT'
    ];
    $this->assertNull($cb->call('_getRateLimitInfo', [$headers]));

    // set rate-limit headers
    $headers['x-rate-limit-limit'] = 180;
    $headers['x-rate-limit-remaining'] = 123;
    $headers['x-rate-limit-reset'] = time() + 234;
    $rate = $cb->call('_getRateLimitInfo', [$headers]);
    $expected = new \stdClass;
    $expected->limit = $headers['x-rate-limit-limit'];
    $expected->remaining = $headers['x-rate-limit-remaining'];
    $expected->reset = $headers['x-rate-limit-reset'];
    $this->assertEquals($expected, $rate);

    // array
    $cb->setReturnFormat(CODEBIRD_RETURNFORMAT_ARRAY);
    $rate = $cb->call('_getRateLimitInfo', [$headers]);
    $expected = [
      'limit'     => $headers['x-rate-limit-limit'],
      'remaining' => $headers['x-rate-limit-remaining'],
      'reset'     => $headers['x-rate-limit-reset']
    ];
    $this->assertEquals($expected, $rate);
  }

  /**
   * Tests _validateSslCertificate
   */
  public function testValidateSslCertificate1()
  {
    $cb = $this->getCB();
    $cb->call('_validateSslCertificate', [0]);
  }

  /**
   * Tests _validateSslCertificate
   * @expectedException \Exception
   * @expectedExceptionMessage Error 58 while validating the Twitter API certificate.
   */
  public function testValidateSslCertificate2()
  {
    $cb = $this->getCB();
    $cb->call('_validateSslCertificate', [CURLE_SSL_CERTPROBLEM]);
  }

  /**
   * Tests _appendHttpStatusAndRate
   */
  public function testAppendHttpStatusAndRate()
  {
    $cb = $this->getCB();

    // object
    $reply = new \stdClass;
    $reply->somedata = 123;
    $reply->moredata = '456';
    $rate = new \stdClass;
    $rate->field1 = 180;
    $rate->field2 = 177;
    $expected = new \stdClass;
    $expected->somedata = 123;
    $expected->moredata = '456';
    $expected->httpstatus = 409;
    $expected->rate = new \stdClass;
    $expected->rate->field1 = 180;
    $expected->rate->field2 = 177;
    $this->assertEquals(
      $expected,
      $cb->call('_appendHttpStatusAndRate', [$reply, 409, $rate])
    );

    // array
    $reply            = (array) $reply;
    $rate             = (array) $rate;
    $expected         = (array) $expected;
    $expected['rate'] = (array) $expected['rate'];
    $cb->setReturnFormat(CODEBIRD_RETURNFORMAT_ARRAY);
    $this->assertEquals(
      $expected,
      $cb->call('_appendHttpStatusAndRate', [$reply, 409, $rate])
    );

    // JSON
    $reply    = '{"somedata":123,"moredata":"456"}';
    $expected = '{"somedata":123,"moredata":"456","httpstatus":409,'
      . '"rate":{"field1":180,"field2":177}}';
    $cb->setReturnFormat(CODEBIRD_RETURNFORMAT_JSON);
    $this->assertEquals(
      $expected,
      $cb->call('_appendHttpStatusAndRate', [$reply, 409, $rate])
    );
  }

  /**
   * Tests _parseApiHeaders
   */
  public function testParseApiHeaders()
  {
    $cb   = $this->getCB();
    $data = $cb->getStatic('_mock_replies')['default']['reply'];
    list($headers, $reply) = $cb->call('_parseApiHeaders', [$data]);
    $expected_headers = [
      'HTTP/1.1 404 Not Found' => '',
      'content-length' => '68',
      'content-type' => 'application/json;charset=utf-8',
      'date' => 'Sun, 06 Dec 2015 14:43:28 GMT',
      'server' => 'tsa_b',
      'set-cookie' => 'guest_id=v1%3A144941300885288055; Domain=.twitter.com'
        . '; Path=/; Expires=Tue, 05-Dec-2017 14:43:28 UTC',
      'strict-transport-security' => 'max-age=631138519',
      'x-connection-hash' => '12218aef9e9757609afb08e661fa3b9b',
      'x-response-time' => '2'
    ];
    $expected_reply = '{"errors":[{"message":"Sorry, that page does not exist","code":34}]}';
    $this->assertEquals($expected_headers, $headers);
    $this->assertEquals($expected_reply, $reply);

    // proxy
    $data = $cb->getStatic('_mock_replies')['proxy1']['reply'];
    list($headers, $reply) = $cb->call('_parseApiHeaders', [$data]);
    $this->assertEquals($expected_headers, $headers);
    $this->assertEquals($expected_reply, $reply);
  }

  /**
   * Tests _parseApiReplyPrefillHeaders
   */
  public function testParseApiReplyPrefillHeaders()
  {
    $cb = $this->getCB();
    $headers = [
      'X-TON-Min-Chunk-Size' => '12345',
      'X-TON-Max-Chunk-Size' => '23456',
      'Range'                => 'bytes 0-1234567/2345678'
    ];

    // non-empty reply: no touching
    $this->assertEquals(
      '123',
      $cb->call('_parseApiReplyPrefillHeaders', [$headers, '123'])
    );

    // no location header: no touching
    $this->assertEquals(
      '',
      $cb->call('_parseApiReplyPrefillHeaders', [$headers, ''])
    );

    $headers['Location'] = 'https://twitter.com';
    $this->assertEquals(
      '{"Location":"https:\/\/twitter.com","X-TON-Min-Chunk-Size":"12345",'
      . '"X-TON-Max-Chunk-Size":"23456","Range":"bytes 0-1234567\/2345678"}',
      $cb->call('_parseApiReplyPrefillHeaders', [$headers, ''])
    );
  }

  /**
   * Tests _parseApiReply
   */
  public function testParseApiReply1()
  {
    $cb = $this->getCB();

    // object
    $this->assertEquals(new \stdClass, $cb->call('_parseApiReply', ['[]']));

    // array
    $cb->setReturnFormat(CODEBIRD_RETURNFORMAT_ARRAY);
    $this->assertEquals([], $cb->call('_parseApiReply', ['[]']));

    // JSON
    $cb->setReturnFormat(CODEBIRD_RETURNFORMAT_JSON);
    $this->assertEquals('{}', $cb->call('_parseApiReply', ['[]']));
  }

  /**
   * Tests _parseApiReply
   */
  public function testParseApiReply2()
  {
    $cb = $this->getCB();
    $reply = '{"id_str":"6253282","profile_location":null,'
      . '"status":{"created_at":"Tue Nov 24 08:56:07 +0000 2015","id":669077021138493440}}';

    // object
    $expected = new \stdClass;
    $expected->id_str = '6253282';
    $expected->profile_location = null;
    $expected->status = new \stdClass;
    $expected->status->created_at = 'Tue Nov 24 08:56:07 +0000 2015';
    $expected->status->id = 669077021138493440;
    $result = $cb->call('_parseApiReply', [$reply]);
    $this->assertEquals($expected, $result);
    $this->assertSame($expected->status->id, $result->status->id);

    // array
    $cb->setReturnFormat(CODEBIRD_RETURNFORMAT_ARRAY);
    $expected = (array) $expected;
    $expected['status'] = (array) $expected['status'];
    $this->assertEquals($expected, $cb->call('_parseApiReply', [$reply]));

    // JSON
    $cb->setReturnFormat(CODEBIRD_RETURNFORMAT_JSON);
    $this->assertEquals($reply, $cb->call('_parseApiReply', [$reply]));

    // query-string format
    $cb->setReturnFormat(CODEBIRD_RETURNFORMAT_ARRAY);
    $this->assertEquals(
      [
        'oauth_token' => 'ABC',
        'oauth_token_secret' => 'def',
        'oauth_callback_confirmed' => 'true'
      ],
      $cb->call(
        '_parseApiReply',
        ['oauth_token=ABC&oauth_token_secret=def&oauth_callback_confirmed=true']
      )
    );

    // message
    $this->assertEquals(
      ['message' => 'This is just a message.'],
      $cb->call('_parseApiReply', ['This is just a message.'])
    );
  }
}