Skip to content

Commit ef5f113

Browse files
author
bryanadamloh97
committed
Added more tests for other drivers
1 parent 1e9f27e commit ef5f113

16 files changed

+419
-8
lines changed

src/Drivers/CuttLyDriverShortener.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use CodeOfDigital\LaravelUrlShortener\Exceptions\InvalidDataException;
88
use CodeOfDigital\LaravelUrlShortener\Exceptions\InvalidResponseException;
99
use CodeOfDigital\LaravelUrlShortener\Exceptions\ShortUrlException;
10+
use CodeOfDigital\LaravelUrlShortener\Exceptions\TooManyRequestException;
1011
use GuzzleHttp\ClientInterface;
1112
use GuzzleHttp\Exception\RequestException;
1213
use GuzzleHttp\Psr7\Request;
@@ -39,14 +40,15 @@ public function shortenAsync($url, array $options = [])
3940
if (!Str::startsWith($url, ['http://', 'https://']))
4041
throw new ShortUrlException('The given URL must begin with http:// or https://');
4142

42-
$options = array_merge_recursive(Arr::add($this->object, 'query.short', urlencode($url)), ['query' => $options]);
43+
$options = array_merge_recursive(Arr::add($this->object, 'query.short', $url), ['query' => $options]);
4344
$request = new Request('GET', '/api/api.php');
4445

4546
return $this->client->sendAsync($request, $options)->then(
4647
function (ResponseInterface $response) {
47-
$statusCode = json_decode($response->getBody()->getContents())->url->status;
48+
$data = json_decode($response->getBody()->getContents());
49+
$statusCode = $data->url->status;
4850
if ($statusCode != 7) $this->getErrorMessage($statusCode);
49-
return str_replace('http://', 'https://', json_decode($response->getBody()->getContents())->url->shortLink);
51+
return str_replace('http://', 'https://', $data->url->shortLink);
5052
},
5153
function (RequestException $e) {
5254
$this->getErrorMessage($e->getCode(), $e->getMessage());
@@ -65,7 +67,9 @@ protected function getErrorMessage($code, $message = null)
6567
throw new InvalidDataException("The preferred link name is already taken.");
6668
case 4:
6769
case 401:
68-
throw new InvalidApiTokenException("Your API Key is invalid and incorrect.");
70+
throw new InvalidApiTokenException($message ?? "Your API Key is invalid and incorrect.");
71+
case 429:
72+
throw new TooManyRequestException($message);
6973
case 5:
7074
throw new InvalidDataException("The link has not passed validated. There is invalid characters.");
7175
case 6:

src/Drivers/IsGdDriverShortener.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function __construct(ClientInterface $client, $statistic)
2525
'allow_redirects' => false,
2626
'base_uri' => 'https://is.gd',
2727
'query' => [
28-
'format' => 'json',
28+
'format' => 'simple',
2929
'logstats' => intval($statistic)
3030
]
3131
];
@@ -44,7 +44,7 @@ public function shortenAsync($url, array $options = [])
4444

4545
return $this->client->sendAsync($request, $options)->then(
4646
function (ResponseInterface $response) {
47-
return str_replace('http://', 'https://', json_decode($response->getBody()->getContents())->shorturl);
47+
return str_replace('http://', 'https://', $response->getBody()->getContents());
4848
},
4949
function (RequestException $e) {
5050
$this->getErrorMessage($e->getCode(), $e->getMessage());
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace CodeOfDigital\LaravelUrlShortener\Exceptions;
4+
5+
class TooManyRequestException extends \RuntimeException
6+
{
7+
}

tests/Responses/cutt_ly/http-200.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
use GuzzleHttp\Psr7\Message;
4+
5+
return Message::parseResponse(trim('
6+
HTTP/1.1 200 OK
7+
Server: nginx
8+
Date: Mon, 14 Jun 2021 16:55:09 GMT
9+
Content-Type: application/json
10+
Content-Length: 263
11+
Connection: keep-alive
12+
Strict-Transport-Security: max-age=31536000; includeSubDomains
13+
X-XSS-Protection: 1; mode=blockFilter
14+
X-Content-Type-Options: nosniff
15+
X-Frame-Options: DENY
16+
Content-Security-Policy: default-src \'none
17+
18+
{"url":{"status":7,"fullLink":"https://laravel.com","date":"2021-06-15","shortLink":"https://cutt.ly/lnHBR1m","title":"Laravel - The PHP Framework For Web Artisans"}}
19+
'));

tests/Responses/cutt_ly/http-401.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
use GuzzleHttp\Psr7\Message;
4+
5+
return Message::parseResponse(trim('
6+
HTTP/1.1 401 Unauthorized
7+
Server: nginx
8+
Date: Mon, 14 Jun 2021 16:55:09 GMT
9+
Content-Type: application/json
10+
Content-Length: 263
11+
Connection: keep-alive
12+
Strict-Transport-Security: max-age=31536000; includeSubDomains
13+
X-XSS-Protection: 1; mode=blockFilter
14+
X-Content-Type-Options: nosniff
15+
X-Frame-Options: DENY
16+
Content-Security-Policy: default-src \'none
17+
18+
Your API Key is invalid and incorrect.
19+
'));

tests/Responses/is_gd/http-200.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
use GuzzleHttp\Psr7\Message;
4+
5+
return Message::parseResponse(trim('
6+
HTTP/1.1 200 OK
7+
Server: nginx
8+
Date: Mon, 14 Jun 2021 16:55:09 GMT
9+
Content-Type: application/json
10+
Content-Length: 263
11+
Connection: keep-alive
12+
Strict-Transport-Security: max-age=31536000; includeSubDomains
13+
X-XSS-Protection: 1; mode=blockFilter
14+
X-Content-Type-Options: nosniff
15+
X-Frame-Options: DENY
16+
Content-Security-Policy: default-src \'none
17+
18+
https://is.gd/Qn8dwg
19+
'));

tests/Responses/is_gd/http-400.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
use GuzzleHttp\Psr7\Message;
4+
5+
return Message::parseResponse(trim('
6+
HTTP/1.1 400 Bad Request
7+
Server: nginx
8+
Date: Mon, 14 Jun 2021 16:55:09 GMT
9+
Content-Type: application/json
10+
Content-Length: 263
11+
Connection: keep-alive
12+
Strict-Transport-Security: max-age=31536000; includeSubDomains
13+
X-XSS-Protection: 1; mode=blockFilter
14+
X-Content-Type-Options: nosniff
15+
X-Frame-Options: DENY
16+
Content-Security-Policy: default-src \'none
17+
18+
Error: Please enter a valid URL to shorten
19+
'));
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
use GuzzleHttp\Psr7\Message;
4+
5+
return Message::parseResponse(trim('
6+
HTTP/1.1 200 OK
7+
Server: nginx
8+
Date: Mon, 14 Jun 2021 16:55:09 GMT
9+
Content-Type: application/json
10+
Content-Length: 263
11+
Connection: keep-alive
12+
Strict-Transport-Security: max-age=31536000; includeSubDomains
13+
X-XSS-Protection: 1; mode=blockFilter
14+
X-Content-Type-Options: nosniff
15+
X-Frame-Options: DENY
16+
Content-Security-Policy: default-src \'none
17+
18+
{"status":"ok","shortenedUrl":"http:\/\/gestyy.com\/ei4Z9l"}
19+
'));
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
use GuzzleHttp\Psr7\Message;
4+
5+
return Message::parseResponse(trim('
6+
HTTP/1.1 302 Found
7+
Server: nginx
8+
Date: Mon, 14 Jun 2021 16:55:09 GMT
9+
Content-Type: application/json
10+
Content-Length: 263
11+
Connection: keep-alive
12+
Strict-Transport-Security: max-age=31536000; includeSubDomains
13+
X-XSS-Protection: 1; mode=blockFilter
14+
X-Content-Type-Options: nosniff
15+
X-Frame-Options: DENY
16+
Content-Security-Policy: default-src \'none
17+
18+
Location: https://api.shorte.st/login/
19+
'));

tests/Responses/tiny_url/http-200.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
use GuzzleHttp\Psr7\Message;
4+
5+
return Message::parseResponse(trim('
6+
HTTP/1.1 200 OK
7+
Server: nginx
8+
Date: Mon, 14 Jun 2021 16:55:09 GMT
9+
Content-Type: application/json
10+
Content-Length: 263
11+
Connection: keep-alive
12+
Strict-Transport-Security: max-age=31536000; includeSubDomains
13+
X-XSS-Protection: 1; mode=blockFilter
14+
X-Content-Type-Options: nosniff
15+
X-Frame-Options: DENY
16+
Content-Security-Policy: default-src \'none
17+
18+
{"code":0,"data":{"url":"https://laravel.com","domain":"tinyurl.com","alias":"3ffwsk3p","tags":[],"tiny_url":"https://tinyurl.com/3ffwsk3p"},"errors":[]}
19+
'));

0 commit comments

Comments
 (0)