| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * Calendar App | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @author Georg Ehrke | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @copyright 2016 Georg Ehrke <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * This library is free software; you can redistribute it and/or | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * License as published by the Free Software Foundation; either | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * version 3 of the License, or any later version. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * This library is distributed in the hope that it will be useful, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * GNU AFFERO GENERAL PUBLIC LICENSE for more details. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * You should have received a copy of the GNU Affero General Public | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * License along with this library.  If not, see <http://www.gnu.org/licenses/>. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | namespace OCA\Calendar\Controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | class ProxyControllerTest extends \PHPUnit_Framework_TestCase { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 	private $appName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 	private $request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 	private $client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 	private $newClient; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 	private $response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 	private $exception; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 	private $controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 	public function setUp() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 		$this->appName = 'calendar'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 		$this->request = $this->getMockBuilder('\OCP\IRequest') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 			->disableOriginalConstructor() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 			->getMock(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 		$this->client = $this->getMockBuilder('\OCP\Http\Client\IClientService') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 			->disableOriginalConstructor() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 			->getMock(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 		$this->newClient = $this->getMockBuilder('\OCP\Http\Client\IClient') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 			->disableOriginalConstructor() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 			->getMock(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 		$this->response = $this->getMockBuilder('\OCP\Http\Client\IResponse') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 			->disableOriginalConstructor() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 			->getMock(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 		$this->exception = $this->getMockBuilder('\GuzzleHttp\Exception\ClientException') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 			->disableOriginalConstructor() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 			->getMock(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 		$this->controller = new ProxyController($this->appName, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 			$this->request, $this->client); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  | 	public function testProxy() { | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  | 		$testUrl = 'http://abc.def/foobar?123'; | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  | 		$this->client->expects($this->once()) | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  | 			->method('newClient') | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  | 			->will($this->returnValue($this->newClient)); | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  | 		$this->newClient->expects($this->once()) | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  | 			->method('get') | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  | 			->with($testUrl, [ | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  | 				'stream' => true, | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  | 			]) | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  | 			->will($this->returnValue($this->response)); | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  | 		$actual = $this->controller->proxy($testUrl); | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  | 		$this->assertInstanceOf('OCA\Calendar\Http\StreamResponse', $actual); | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  | 		$this->assertEquals('text/calendar', $actual->getHeaders()['Content-Type']); | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 	public function testProxyException() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 		$testUrl = 'http://abc.def/foobar?123'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 		$this->client->expects($this->once()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 			->method('newClient') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 			->will($this->returnValue($this->newClient)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 		$this->newClient->expects($this->once()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 			->method('get') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 			->with($testUrl, [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 				'stream' => true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 			]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 			->will($this->throwException($this->exception)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 		$this->exception->expects($this->once()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 			->method('getResponse') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 			->will($this->returnValue($this->response)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 		$this->response->expects($this->once()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 			->method('getStatusCode') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 			->will($this->returnValue(403)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 		$actual = $this->controller->proxy($testUrl); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 		$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | 		$this->assertEquals('403', $actual->getStatus()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 103 |  |  | 	} | 
            
                                                        
            
                                    
            
            
                | 104 |  |  | } |