1<?php 2 3namespace Sabre\HTTP; 4 5class MessageTest extends \PHPUnit_Framework_TestCase { 6 7 function testConstruct() { 8 9 $message = new MessageMock(); 10 $this->assertInstanceOf('Sabre\HTTP\Message', $message); 11 12 } 13 14 function testStreamBody() { 15 16 $body = 'foo'; 17 $h = fopen('php://memory', 'r+'); 18 fwrite($h, $body); 19 rewind($h); 20 21 $message = new MessageMock(); 22 $message->setBody($h); 23 24 $this->assertEquals($body, $message->getBodyAsString()); 25 rewind($h); 26 $this->assertEquals($body, stream_get_contents($message->getBodyAsStream())); 27 rewind($h); 28 $this->assertEquals($body, stream_get_contents($message->getBody())); 29 30 } 31 32 function testStringBody() { 33 34 $body = 'foo'; 35 36 $message = new MessageMock(); 37 $message->setBody($body); 38 39 $this->assertEquals($body, $message->getBodyAsString()); 40 $this->assertEquals($body, stream_get_contents($message->getBodyAsStream())); 41 $this->assertEquals($body, $message->getBody()); 42 43 } 44 45 46 function testGetEmptyBodyStream() { 47 48 $message = new MessageMock(); 49 $body = $message->getBodyAsStream(); 50 51 $this->assertEquals('', stream_get_contents($body)); 52 53 } 54 55 function testGetEmptyBodyString() { 56 57 $message = new MessageMock(); 58 $body = $message->getBodyAsString(); 59 60 $this->assertEquals('', $body); 61 62 } 63 64 function testHeaders() { 65 66 $message = new MessageMock(); 67 $message->setHeader('X-Foo', 'bar'); 68 69 // Testing caselessness 70 $this->assertEquals('bar', $message->getHeader('X-Foo')); 71 $this->assertEquals('bar', $message->getHeader('x-fOO')); 72 73 $this->assertTrue( 74 $message->removeHeader('X-FOO') 75 ); 76 $this->assertNull($message->getHeader('X-Foo')); 77 $this->assertFalse( 78 $message->removeHeader('X-FOO') 79 ); 80 81 } 82 83 function testSetHeaders() { 84 85 $message = new MessageMock(); 86 87 $headers = [ 88 'X-Foo' => ['1'], 89 'X-Bar' => ['2'], 90 ]; 91 92 $message->setHeaders($headers); 93 $this->assertEquals($headers, $message->getHeaders()); 94 95 $message->setHeaders([ 96 'X-Foo' => ['3', '4'], 97 'X-Bar' => '5', 98 ]); 99 100 $expected = [ 101 'X-Foo' => ['3','4'], 102 'X-Bar' => ['5'], 103 ]; 104 105 $this->assertEquals($expected, $message->getHeaders()); 106 107 } 108 109 function testAddHeaders() { 110 111 $message = new MessageMock(); 112 113 $headers = [ 114 'X-Foo' => ['1'], 115 'X-Bar' => ['2'], 116 ]; 117 118 $message->addHeaders($headers); 119 $this->assertEquals($headers, $message->getHeaders()); 120 121 $message->addHeaders([ 122 'X-Foo' => ['3', '4'], 123 'X-Bar' => '5', 124 ]); 125 126 $expected = [ 127 'X-Foo' => ['1','3','4'], 128 'X-Bar' => ['2','5'], 129 ]; 130 131 $this->assertEquals($expected, $message->getHeaders()); 132 133 } 134 135 function testSendBody() { 136 137 $message = new MessageMock(); 138 139 // String 140 $message->setBody('foo'); 141 142 // Stream 143 $h = fopen('php://memory', 'r+'); 144 fwrite($h, 'bar'); 145 rewind($h); 146 $message->setBody($h); 147 148 $body = $message->getBody(); 149 rewind($body); 150 151 $this->assertEquals('bar', stream_get_contents($body)); 152 153 } 154 155 function testMultipleHeaders() { 156 157 $message = new MessageMock(); 158 $message->setHeader('a', '1'); 159 $message->addHeader('A', '2'); 160 161 $this->assertEquals( 162 "1,2", 163 $message->getHeader('A') 164 ); 165 $this->assertEquals( 166 "1,2", 167 $message->getHeader('a') 168 ); 169 170 $this->assertEquals( 171 ['1', '2'], 172 $message->getHeaderAsArray('a') 173 ); 174 $this->assertEquals( 175 ['1', '2'], 176 $message->getHeaderAsArray('A') 177 ); 178 $this->assertEquals( 179 [], 180 $message->getHeaderAsArray('B') 181 ); 182 183 } 184 185 function testHasHeaders() { 186 187 $message = new MessageMock(); 188 189 $this->assertFalse($message->hasHeader('X-Foo')); 190 $message->setHeader('X-Foo', 'Bar'); 191 $this->assertTrue($message->hasHeader('X-Foo')); 192 193 } 194 195} 196 197class MessageMock extends Message { } 198