Skip to content

Commit 6cb730a

Browse files
author
bryanadamloh97
committed
Added tests for 3 new drivers
1 parent 4c1f8b5 commit 6cb730a

File tree

10 files changed

+357
-0
lines changed

10 files changed

+357
-0
lines changed

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ Powerful URL shortening tool using different drivers for your Laravel projects
1919
- [Usage](#usage)
2020
- [Instantiate Shortener](#instantiate-shortener)
2121
- [Changing driver](#changing-driver)
22+
- [Available Drivers](#available-drivers)
23+
- [Changelog](#changelog)
24+
- [Security](#security)
25+
- [Credits](#credits)
2226
- [License](#license)
2327

2428
## Overview
@@ -119,5 +123,29 @@ Method | Description
119123
You can change the default driver by setting `URL_SHORTENER_DRIVER={driver}` in your environment file
120124
or publish the config file and make your changes there directly.
121125

126+
## Available Drivers
127+
There are total of 8 drivers available in this package. Below is a list of drivers with their respective specs:
128+
129+
Service | API Token | Driver name | Analytics | Monetization
130+
-------------------------------|-----------|-------------|-----------|-------------
131+
[Bit.ly](#bitly) | yes | `bit_ly` | yes | no
132+
[TinyURL](#tinyurl) | yes | `tiny_url` | no | no
133+
[Shorte.st](#shortest) | yes | `shorte_st` | yes | yes
134+
[Is.gd](#isgd) | no | `is_gd` | yes | no
135+
[Cutt.ly](#cuttly) | yes | `cutt_ly` | yes | no
136+
[HideUri](#hideuri) | no | `hide_uri` | no | no
137+
[Firebase Links](#firebase) | yes | `firebase` | yes | no
138+
[Ouo.io](#ouoio) | no | `ouo_io` | yes | yes
139+
140+
## Changelog
141+
Please see [CHANGELOG](CHANGELOG.md) for more information what has been changed in recent versions.
142+
143+
## Security
144+
If you discover any security related issues, please email one of the authors instead of using the issue tracker. You can
145+
find the author emails in the [composer.json](composer.json).
146+
147+
## Credits
148+
- [Bryan Adam Loh](https://github.com/bryanadamloh)
149+
122150
## License
123151
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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, 24 Jun 2021 12:43:12 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+
{
19+
"shortLink": "https://codeofdigital.page.link/CBNYHKkLc6FZmuYU9",
20+
"warning": [
21+
{
22+
"warningCode": "UNRECOGNIZED_PARAM",
23+
"warningMessage": "There is no configuration to prevent phishing on this domain https://codeofdigital.page.link. Setup URL patterns to whitelist in the Firebase Dynamic Links console. [https://support.google.com/firebase/answer/9021429]"
24+
}
25+
],
26+
"previewLink": "https://codeofdigital.page.link/CBNYHKkLc6FZmuYU9?d=1"
27+
}
28+
'));
29+
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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, 24 Jun 2021 12:43:12 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+
{
19+
"error": {
20+
"code": 400,
21+
"message": "API key not valid. Please pass a valid API key.",
22+
"status": "INVALID_ARGUMENT",
23+
"details": [
24+
{
25+
"type": "type.googleapis.com/google.rpc.ErrorInfo",
26+
"reason": "API_KEY_INVALID",
27+
"domain": "googleapis.com",
28+
"metadata": {
29+
"service": "firebasedynamiclinks.googleapis.com"
30+
}
31+
}
32+
]
33+
}
34+
}
35+
'));
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, 24 Jun 2021 12:43:12 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+
{"result_url":"https:\/\/hideuri.com\/Oky9AE"}
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 400 Bad Request
7+
Server: nginx
8+
Date: Mon, 24 Jun 2021 12:43:12 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": "API Error: URL is invalid (check #1)"}
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, 24 Jun 2021 12:43:12 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://ouo.io/5FhZHP
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://ouo.io
19+
'));
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
namespace CodeOfDigital\LaravelUrlShortener\Tests\Unit;
4+
5+
use CodeOfDigital\LaravelUrlShortener\Drivers\FirebaseDriverShortener;
6+
use CodeOfDigital\LaravelUrlShortener\Exceptions\BadRequestException;
7+
use CodeOfDigital\LaravelUrlShortener\Exceptions\InvalidApiTokenException;
8+
use CodeOfDigital\LaravelUrlShortener\Exceptions\ShortUrlException;
9+
10+
class FirebaseShortenerTest extends TestCase
11+
{
12+
protected $shortener;
13+
14+
protected function setUp(): void
15+
{
16+
parent::setUp();
17+
$this->shortener = new FirebaseDriverShortener($this->client, 'API_TOKEN', 'URI_PREFIX', 'UNGUESSABLE');
18+
}
19+
20+
/**
21+
* Test the shortening of URLs through Firebase
22+
*
23+
* @throws ShortUrlException
24+
*/
25+
public function testUrlShortening()
26+
{
27+
$this->client->queue(require __DIR__ . '/../Responses/firebase/http-200.php');
28+
29+
$shortUrl = $this->shortener->shorten('https://laravel.com');
30+
$request = $this->client->getRequest(0);
31+
32+
$this->assertNotNull($request);
33+
$this->assertEquals('POST', $request->getMethod());
34+
$this->assertEquals('firebasedynamiclinks.googleapis.com', $request->getUri()->getHost());
35+
$this->assertEquals('/v1/shortLinks?key=API_TOKEN', $request->getRequestTarget());
36+
$this->assertEquals('application/json', $request->getHeader('Content-Type')[0]);
37+
$this->assertEquals('application/json', $request->getHeader('Accept')[0]);
38+
39+
$expected = '{"dynamicLinkInfo":{"domainUriPrefix":"URI_PREFIX","link":"https:\/\/laravel.com"},"suffix":{"option":"UNGUESSABLE"}}';
40+
$this->assertJsonStringEqualsJsonString($expected, $request->getBody()->getContents());
41+
42+
$this->assertEquals('https://codeofdigital.page.link/CBNYHKkLc6FZmuYU9', $shortUrl);
43+
}
44+
45+
/**
46+
* Test failure to authenticate with Firebase
47+
*
48+
* @throws ShortUrlException
49+
*/
50+
public function testUnauthorized()
51+
{
52+
$this->client->queue(require __DIR__ . '/../Responses/firebase/http-400.php');
53+
$this->expectException(BadRequestException::class);
54+
$this->shortener->shorten('https://laravel.com');
55+
}
56+
57+
/**
58+
* Test failure if parsed URL is invalid or incorrect format
59+
*
60+
* @throws ShortUrlException
61+
*/
62+
public function testInvalidUrl()
63+
{
64+
$this->expectException(ShortUrlException::class);
65+
$this->shortener->shorten('some-string.com');
66+
}
67+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
namespace CodeOfDigital\LaravelUrlShortener\Tests\Unit;
4+
5+
use CodeOfDigital\LaravelUrlShortener\Drivers\HideUriDriverShortener;
6+
use CodeOfDigital\LaravelUrlShortener\Exceptions\BadRequestException;
7+
use CodeOfDigital\LaravelUrlShortener\Exceptions\ShortUrlException;
8+
9+
class HideUriShortenerTest extends TestCase
10+
{
11+
protected $shortener;
12+
13+
protected function setUp(): void
14+
{
15+
parent::setUp();
16+
$this->shortener = new HideUriDriverShortener($this->client);
17+
}
18+
19+
/**
20+
* Test the URL Shortening through Hide.uri
21+
*
22+
* @throws ShortUrlException
23+
*/
24+
public function testUrlShortening()
25+
{
26+
$this->client->queue(require __DIR__ . '/../Responses/hide_uri/http-200.php');
27+
28+
$shortUrl = $this->shortener->shorten('https://laravel.com');
29+
$request = $this->client->getRequest(0);
30+
31+
$this->assertNotNull($request);
32+
$this->assertEquals('POST', $request->getMethod());
33+
$this->assertEquals('hideuri.com', $request->getUri()->getHost());
34+
$this->assertEquals('/api/v1/shorten', $request->getRequestTarget());
35+
36+
$this->assertEquals('https://hideuri.com/Oky9AE', $shortUrl);
37+
}
38+
39+
/**
40+
* Test failure if there is invalid syntax in request
41+
*
42+
* @throws ShortUrlException
43+
*/
44+
public function testFailure()
45+
{
46+
$this->client->queue(require __DIR__ . '/../Responses/hide_uri/http-400.php');
47+
$this->expectException(BadRequestException::class);
48+
$this->shortener->shorten('https://laravel,com');
49+
}
50+
51+
/**
52+
* Test failure if parsed URL is invalid or incorrect format
53+
*
54+
* @throws ShortUrlException
55+
*/
56+
public function testInvalidUrl()
57+
{
58+
$this->expectException(ShortUrlException::class);
59+
$this->shortener->shorten('some-string.com');
60+
}
61+
}

tests/Unit/OuoIoShortenerTest.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
namespace CodeOfDigital\LaravelUrlShortener\Tests\Unit;
4+
5+
use CodeOfDigital\LaravelUrlShortener\Drivers\OuoIoDriverShortener;
6+
use CodeOfDigital\LaravelUrlShortener\Exceptions\InvalidApiTokenException;
7+
use CodeOfDigital\LaravelUrlShortener\Exceptions\ShortUrlException;
8+
9+
class OuoIoShortenerTest extends TestCase
10+
{
11+
protected $shortener;
12+
13+
protected function setUp(): void
14+
{
15+
parent::setUp();
16+
$this->shortener = new OuoIoDriverShortener($this->client, 'API_TOKEN');
17+
}
18+
19+
/**
20+
* Test the URL Shortening through Ouo.io
21+
*
22+
* @throws ShortUrlException
23+
*/
24+
public function testShortening()
25+
{
26+
$this->client->queue(require __DIR__ . '/../Responses/ouo_io/http-200.php');
27+
28+
$shortUrl = $this->shortener->shorten('https://laravel.com');
29+
$request = $this->client->getRequest(0);
30+
31+
$this->assertNotNull($request);
32+
$this->assertEquals('GET', $request->getMethod());
33+
$this->assertEquals('ouo.io', $request->getUri()->getHost());
34+
$this->assertEquals('/api/API_TOKEN?s=https%253A%252F%252Flaravel.com', $request->getRequestTarget());
35+
36+
$this->assertEquals('https://ouo.io/5FhZHP', $shortUrl);
37+
}
38+
39+
/**
40+
* Test failure if there is invalid syntax in request
41+
*
42+
* @throws ShortUrlException
43+
*/
44+
public function testUnauthorized()
45+
{
46+
$this->client->queue(require __DIR__ . '/../Responses/ouo_io/http-302.php');
47+
$this->expectException(InvalidApiTokenException::class);
48+
$this->shortener->shorten('https://laravel.com');
49+
}
50+
51+
/**
52+
* Test failure if parsed URL is invalid or incorrect format
53+
*
54+
* @throws ShortUrlException
55+
*/
56+
public function testInvalidUrl()
57+
{
58+
$this->expectException(ShortUrlException::class);
59+
$this->shortener->shorten('some-string.com');
60+
}
61+
}

0 commit comments

Comments
 (0)