1 | <?php |
---|
2 | /** |
---|
3 | * Copyright 2011 Bas de Nooijer. All rights reserved. |
---|
4 | * |
---|
5 | * Redistribution and use in source and binary forms, with or without |
---|
6 | * modification, are permitted provided that the following conditions are met: |
---|
7 | * |
---|
8 | * 1. Redistributions of source code must retain the above copyright notice, |
---|
9 | * this list of conditions and the following disclaimer. |
---|
10 | * |
---|
11 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
---|
12 | * this listof conditions and the following disclaimer in the documentation |
---|
13 | * and/or other materials provided with the distribution. |
---|
14 | * |
---|
15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS" |
---|
16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
---|
19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
25 | * POSSIBILITY OF SUCH DAMAGE. |
---|
26 | * |
---|
27 | * The views and conclusions contained in the software and documentation are |
---|
28 | * those of the authors and should not be interpreted as representing official |
---|
29 | * policies, either expressed or implied, of the copyright holder. |
---|
30 | */ |
---|
31 | |
---|
32 | class Solarium_Client_Adapter_HttpTest extends PHPUnit_Framework_TestCase |
---|
33 | { |
---|
34 | /** |
---|
35 | * @var Solarium_Client_Adapter_Http |
---|
36 | */ |
---|
37 | protected $_adapter; |
---|
38 | |
---|
39 | public function setUp() |
---|
40 | { |
---|
41 | $this->_adapter = new Solarium_Client_Adapter_Http(); |
---|
42 | } |
---|
43 | |
---|
44 | public function testExecute() |
---|
45 | { |
---|
46 | $data = 'test123'; |
---|
47 | |
---|
48 | $request = new Solarium_Client_Request(); |
---|
49 | $request->setMethod(Solarium_Client_Request::METHOD_GET); |
---|
50 | |
---|
51 | $mock = $this->getMock('Solarium_Client_Adapter_Http', array('_getData','check')); |
---|
52 | $mock->expects($this->once()) |
---|
53 | ->method('_getData') |
---|
54 | ->with($this->equalTo('http://127.0.0.1:8983/solr/?'), $this->isType('resource')) |
---|
55 | ->will($this->returnValue(array($data, array('HTTP 1.1 200 OK')))); |
---|
56 | |
---|
57 | $mock->execute($request); |
---|
58 | } |
---|
59 | |
---|
60 | public function testExecuteErrorResponse() |
---|
61 | { |
---|
62 | $data = 'test123'; |
---|
63 | |
---|
64 | $request = new Solarium_Client_Request(); |
---|
65 | |
---|
66 | $mock = $this->getMock('Solarium_Client_Adapter_Http', array('_getData','check')); |
---|
67 | $mock->expects($this->once()) |
---|
68 | ->method('_getData') |
---|
69 | ->with($this->equalTo('http://127.0.0.1:8983/solr/?'), $this->isType('resource')) |
---|
70 | ->will($this->returnValue(array($data, array('HTTP 1.1 200 OK')))); |
---|
71 | $mock->expects($this->once()) |
---|
72 | ->method('check') |
---|
73 | ->will($this->throwException(new Solarium_Client_HttpException("HTTP request failed"))); |
---|
74 | |
---|
75 | $this->setExpectedException('Solarium_Client_HttpException'); |
---|
76 | $mock->execute($request); |
---|
77 | } |
---|
78 | |
---|
79 | public function testCheckError() |
---|
80 | { |
---|
81 | $this->setExpectedException('Solarium_Client_HttpException'); |
---|
82 | $this->_adapter->check(false, array()); |
---|
83 | |
---|
84 | } |
---|
85 | |
---|
86 | public function testCheckOk() |
---|
87 | { |
---|
88 | $value = $this->_adapter->check('dummydata',array('HTTP 1.1 200 OK')); |
---|
89 | |
---|
90 | $this->assertEquals( |
---|
91 | null, |
---|
92 | $value |
---|
93 | ); |
---|
94 | } |
---|
95 | |
---|
96 | public function testCreateContextGetRequest() |
---|
97 | { |
---|
98 | $timeout = 13; |
---|
99 | $method = Solarium_Client_Request::METHOD_HEAD; |
---|
100 | |
---|
101 | $request = new Solarium_Client_Request(); |
---|
102 | $request->setMethod($method); |
---|
103 | $this->_adapter->setTimeout($timeout); |
---|
104 | |
---|
105 | $context = $this->_adapter->createContext($request); |
---|
106 | |
---|
107 | $this->assertEquals( |
---|
108 | array('http' => array('method' => $method, 'timeout' => $timeout)), |
---|
109 | stream_context_get_options($context) |
---|
110 | ); |
---|
111 | } |
---|
112 | |
---|
113 | public function testCreateContextWithHeaders() |
---|
114 | { |
---|
115 | $timeout = 13; |
---|
116 | $method = Solarium_Client_Request::METHOD_HEAD; |
---|
117 | $header1 = 'Content-Type: text/xml; charset=UTF-8'; |
---|
118 | $header2 = 'X-MyHeader: dummyvalue'; |
---|
119 | |
---|
120 | $request = new Solarium_Client_Request(); |
---|
121 | $request->setMethod($method); |
---|
122 | $request->addHeader($header1); |
---|
123 | $request->addHeader($header2); |
---|
124 | $this->_adapter->setTimeout($timeout); |
---|
125 | |
---|
126 | $context = $this->_adapter->createContext($request); |
---|
127 | |
---|
128 | $this->assertEquals( |
---|
129 | array('http' => array('method' => $method, 'timeout' => $timeout, 'header' => $header1."\r\n".$header2)), |
---|
130 | stream_context_get_options($context) |
---|
131 | ); |
---|
132 | } |
---|
133 | |
---|
134 | public function testCreateContextPostRequest() |
---|
135 | { |
---|
136 | $timeout = 13; |
---|
137 | $method = Solarium_Client_Request::METHOD_POST; |
---|
138 | $data = 'test123'; |
---|
139 | |
---|
140 | $request = new Solarium_Client_Request(); |
---|
141 | $request->setMethod($method); |
---|
142 | $request->setRawData($data); |
---|
143 | $this->_adapter->setTimeout($timeout); |
---|
144 | |
---|
145 | $context = $this->_adapter->createContext($request); |
---|
146 | |
---|
147 | $this->assertEquals( |
---|
148 | array('http' => array('method' => $method, 'timeout' => $timeout, 'content' => $data, 'header' => 'Content-Type: text/xml; charset=UTF-8')), |
---|
149 | stream_context_get_options($context) |
---|
150 | ); |
---|
151 | } |
---|
152 | |
---|
153 | } |
---|