📄 http_socket.test.php
字号:
$this->Socket->config['request']['uri']['host'] = 'bakery.cakephp.org'; $url = $this->Socket->url(); $this->assertIdentical($url, 'http://bakery.cakephp.org/'); $this->Socket->configUri('http://www.cakephp.org'); $url = $this->Socket->url('/search?q=bar'); $this->assertIdentical($url, 'http://www.cakephp.org/search?q=bar'); $url = $this->Socket->url(array('host' => 'www.foobar.org', 'query' => array('q' => 'bar'))); $this->assertIdentical($url, 'http://www.foobar.org/?q=bar'); $url = $this->Socket->url(array('path' => '/supersearch', 'query' => array('q' => 'bar'))); $this->assertIdentical($url, 'http://www.cakephp.org/supersearch?q=bar'); $this->Socket->configUri('http://www.google.com'); $url = $this->Socket->url('/search?q=socket'); $this->assertIdentical($url, 'http://www.google.com/search?q=socket'); $url = $this->Socket->url(); $this->assertIdentical($url, 'http://www.google.com/'); $this->Socket->configUri('https://www.google.com'); $url = $this->Socket->url('/search?q=socket'); $this->assertIdentical($url, 'https://www.google.com/search?q=socket'); $this->Socket->reset(); $this->Socket->configUri('www.google.com:443'); $url = $this->Socket->url('/search?q=socket'); $this->assertIdentical($url, 'https://www.google.com/search?q=socket'); $this->Socket->reset(); $this->Socket->configUri('www.google.com:8080'); $url = $this->Socket->url('/search?q=socket'); $this->assertIdentical($url, 'http://www.google.com:8080/search?q=socket'); }/** * testGet method * * @access public * @return void */ function testGet() { $this->RequestSocket->reset(); $this->RequestSocket->expect('request', a(array('method' => 'GET', 'uri' => 'http://www.google.com/'))); $this->RequestSocket->get('http://www.google.com/'); $this->RequestSocket->expect('request', a(array('method' => 'GET', 'uri' => 'http://www.google.com/?foo=bar'))); $this->RequestSocket->get('http://www.google.com/', array('foo' => 'bar')); $this->RequestSocket->expect('request', a(array('method' => 'GET', 'uri' => 'http://www.google.com/?foo=bar'))); $this->RequestSocket->get('http://www.google.com/', 'foo=bar'); $this->RequestSocket->expect('request', a(array('method' => 'GET', 'uri' => 'http://www.google.com/?foo=23&foobar=42'))); $this->RequestSocket->get('http://www.google.com/?foo=bar', array('foobar' => '42', 'foo' => '23')); $this->RequestSocket->expect('request', a(array('method' => 'GET', 'uri' => 'http://www.google.com/', 'auth' => array('user' => 'foo', 'pass' => 'bar')))); $this->RequestSocket->get('http://www.google.com/', null, array('auth' => array('user' => 'foo', 'pass' => 'bar'))); }/** * testPostPutDelete method * * @access public * @return void */ function testPostPutDelete() { $this->RequestSocket->reset(); foreach (array('POST', 'PUT', 'DELETE') as $method) { $this->RequestSocket->expect('request', a(array('method' => $method, 'uri' => 'http://www.google.com/', 'body' => array()))); $this->RequestSocket->{low($method)}('http://www.google.com/'); $this->RequestSocket->expect('request', a(array('method' => $method, 'uri' => 'http://www.google.com/', 'body' => array('Foo' => 'bar')))); $this->RequestSocket->{low($method)}('http://www.google.com/', array('Foo' => 'bar')); $this->RequestSocket->expect('request', a(array('method' => $method, 'uri' => 'http://www.google.com/', 'body' => null, 'line' => 'Hey Server'))); $this->RequestSocket->{low($method)}('http://www.google.com/', null, array('line' => 'Hey Server')); } }/** * Enter description here... * */ function testParseResponse() { $this->Socket->reset(); $r = $this->Socket->parseResponse(array('foo' => 'bar')); $this->assertIdentical($r, array('foo' => 'bar')); $r = $this->Socket->parseResponse(true); $this->assertIdentical($r, false); $r = $this->Socket->parseResponse("HTTP Foo\r\nBar: La"); $this->assertIdentical($r, false); $tests = array( 'simple-request' => array( 'response' => array( 'status-line' => "HTTP/1.x 200 OK\r\n", 'header' => "Date: Mon, 16 Apr 2007 04:14:16 GMT\r\nServer: CakeHttp Server\r\n", 'body' => "<h1>Hello World</h1>\r\n<p>It's good to be html</p>" ) , 'expectations' => array( 'status.http-version' => 'HTTP/1.x', 'status.code' => 200, 'status.reason-phrase' => 'OK', 'header' => $this->Socket->parseHeader("Date: Mon, 16 Apr 2007 04:14:16 GMT\r\nServer: CakeHttp Server\r\n"), 'body' => "<h1>Hello World</h1>\r\n<p>It's good to be html</p>" ) ), 'no-header' => array( 'response' => array( 'status-line' => "HTTP/1.x 404 OK\r\n", 'header' => null, ) , 'expectations' => array( 'status.code' => 404, 'header' => array() ) ), 'chunked' => array( 'response' => array( 'header' => "Transfer-Encoding: chunked\r\n", 'body' => "19\r\nThis is a chunked message\r\n0\r\n" ), 'expectations' => array( 'body' => "This is a chunked message", 'header' => $this->Socket->parseHeader("Transfer-Encoding: chunked\r\n") ) ), 'enitity-header' => array( 'response' => array( 'body' => "19\r\nThis is a chunked message\r\n0\r\nFoo: Bar\r\n" ), 'expectations' => array( 'header' => $this->Socket->parseHeader("Transfer-Encoding: chunked\r\nFoo: Bar\r\n") ) ), 'enitity-header-combine' => array( 'response' => array( 'header' => "Transfer-Encoding: chunked\r\nFoo: Foobar\r\n" ), 'expectations' => array( 'header' => $this->Socket->parseHeader("Transfer-Encoding: chunked\r\nFoo: Foobar\r\nFoo: Bar\r\n") ) ) ); $testResponse = array(); $expectations = array(); foreach ($tests as $name => $test) { $testResponse = array_merge($testResponse, $test['response']); $testResponse['response'] = $testResponse['status-line'].$testResponse['header']."\r\n".$testResponse['body']; $r = $this->Socket->parseResponse($testResponse['response']); $expectations = array_merge($expectations, $test['expectations']); foreach ($expectations as $property => $expectedVal) { $val = Set::extract($r, $property); $this->assertIdentical($val, $expectedVal, 'Test "'.$name.'": response.'.$property.' - %s'); } foreach (array('status-line', 'header', 'body', 'response') as $field) { $this->assertIdentical($r['raw'][$field], $testResponse[$field], 'Test response.raw.'.$field.': %s'); } } }/** * Enter description here... * */ function testDecodeBody() { $this->Socket->reset(); $r = $this->Socket->decodeBody(true); $this->assertIdentical($r, false); $r = $this->Socket->decodeBody('Foobar', false); $this->assertIdentical($r, array('body' => 'Foobar', 'header' => false)); $encodings = array( 'chunked' => array( 'encoded' => "19\r\nThis is a chunked message\r\n0\r\n", 'decoded' => array('body' => "This is a chunked message", 'header' => false) ), 'foo-coded' => array( 'encoded' => '!Foobar!', 'decoded' => array('body' => '!Foobar!', 'header' => false), 'error' => new PatternExpectation('/unknown encoding: foo-coded/i') ) ); foreach ($encodings as $encoding => $sample) { if (isset($sample['error'])) { $this->expectError($sample['error']); } $r = $this->Socket->decodeBody($sample['encoded'], $encoding); $this->assertIdentical($r, $sample['decoded']); if (isset($sample['error'])) { $this->Socket->quirksMode = true; $r = $this->Socket->decodeBody($sample['encoded'], $encoding); $this->assertIdentical($r, $sample['decoded']); $this->Socket->quirksMode = false; } } }/** * Enter description here... * */ function testDecodeChunkedBody() { $this->Socket->reset(); $r = $this->Socket->decodeChunkedBody(true); $this->assertIdentical($r, false); $encoded = "19\r\nThis is a chunked message\r\n0\r\n"; $decoded = "This is a chunked message"; $r = $this->Socket->decodeChunkedBody($encoded); $this->assertIdentical($r['body'], $decoded); $this->assertIdentical($r['header'], false); $encoded = "19 \r\nThis is a chunked message\r\n0\r\n"; $r = $this->Socket->decodeChunkedBody($encoded); $this->assertIdentical($r['body'], $decoded); $encoded = "19\r\nThis is a chunked message\r\nE\r\n\nThat is cool\n\r\n0\r\n"; $decoded = "This is a chunked message\nThat is cool\n"; $r = $this->Socket->decodeChunkedBody($encoded); $this->assertIdentical($r['body'], $decoded); $this->assertIdentical($r['header'], false); $encoded = "19\r\nThis is a chunked message\r\nE;foo-chunk=5\r\n\nThat is cool\n\r\n0\r\n"; $r = $this->Socket->decodeChunkedBody($encoded); $this->assertIdentical($r['body'], $decoded); $this->assertIdentical($r['header'], false); $encoded = "19\r\nThis is a chunked message\r\nE\r\n\nThat is cool\n\r\n0\r\nfoo-header: bar\r\ncake: PHP\r\n\r\n"; $r = $this->Socket->decodeChunkedBody($encoded); $this->assertIdentical($r['body'], $decoded); $this->assertIdentical($r['header'], array('Foo-Header' => 'bar', 'Cake' => 'PHP')); $encoded = "19\r\nThis is a chunked message\r\nE\r\n\nThat is cool\n\r\n"; $this->expectError(new PatternExpectation('/activate quirks mode/i')); $r = $this->Socket->decodeChunkedBody($encoded); $this->assertIdentical($r, false); $this->Socket->quirksMode = true; $r = $this->Socket->decodeChunkedBody($encoded); $this->assertIdentical($r['body'], $decoded); $this->assertIdentical($r['header'], false); $encoded = "19\r\nThis is a chunked message\r\nE\r\n\nThat is cool\n\r\nfoo-header: bar\r\ncake: PHP\r\n\r\n"; $r = $this->Socket->decodeChunkedBody($encoded); $this->assertIdentical($r['body'], $decoded); $this->assertIdentical($r['header'], array('Foo-Header' => 'bar', 'Cake' => 'PHP')); }/** * testBuildRequestLine method * * @access public * @return void */ function testBuildRequestLine() { $this->Socket->reset(); $this->expectError(new PatternExpectation('/activate quirks mode/i')); $r = $this->Socket->buildRequestLine('Foo'); $this->assertIdentical($r, false); $this->Socket->quirksMode = true; $r = $this->Socket->buildRequestLine('Foo'); $this->assertIdentical($r, 'Foo'); $this->Socket->quirksMode = false; $r = $this->Socket->buildRequestLine(true); $this->assertIdentical($r, false); $r = $this->Socket->buildRequestLine(array('foo' => 'bar', 'method' => 'foo')); $this->assertIdentical($r, false); $r = $this->Socket->buildRequestLine(array('method' => 'GET', 'uri' => 'http://www.cakephp.org/search?q=socket')); $this->assertIdentical($r, "GET /search?q=socket HTTP/1.1\r\n"); $request = array( 'method' => 'GET', 'uri' => array( 'path' => '/search', 'query' => array('q' => 'socket') ) ); $r = $this->Socket->buildRequestLine($request); $this->assertIdentical($r, "GET /search?q=socket HTTP/1.1\r\n"); unset($request['method']); $r = $this->Socket->buildRequestLine($request); $this->assertIdentical($r, "GET /search?q=socket HTTP/1.1\r\n"); $r = $this->Socket->buildRequestLine($request, 'CAKE-HTTP/0.1'); $this->assertIdentical($r, "GET /search?q=socket CAKE-HTTP/0.1\r\n"); $request = array('method' => 'OPTIONS', 'uri' => '*'); $r = $this->Socket->buildRequestLine($request); $this->assertIdentical($r, "OPTIONS * HTTP/1.1\r\n"); $request['method'] = 'GET'; $this->expectError(new PatternExpectation('/activate quirks mode/i')); $r = $this->Socket->buildRequestLine($request); $this->assertIdentical($r, false); $this->expectError(new PatternExpectation('/activate quirks mode/i')); $r = $this->Socket->buildRequestLine("GET * HTTP/1.1\r\n"); $this->assertIdentical($r, false); $this->Socket->quirksMode = true; $r = $this->Socket->buildRequestLine($request); $this->assertIdentical($r, "GET * HTTP/1.1\r\n"); $r = $this->Socket->buildRequestLine("GET * HTTP/1.1\r\n"); $this->assertIdentical($r, "GET * HTTP/1.1\r\n"); }/** * Asserts that HttpSocket::parseUri is working properly * */ function testParseUri() { $this->Socket->reset(); $uri = $this->Socket->parseUri(array('invalid' => 'uri-string')); $this->assertIdentical($uri, false); $uri = $this->Socket->parseUri(array('invalid' => 'uri-string'), array('host' => 'somehost')); $this->assertIdentical($uri, array('host' => 'somehost', 'invalid' => 'uri-string')); $uri = $this->Socket->parseUri(false); $this->assertIdentical($uri, false); $uri = $this->Socket->parseUri('/my-cool-path'); $this->assertIdentical($uri, array('path' => '/my-cool-path')); $uri = $this->Socket->parseUri('http://bob:foo123@www.cakephp.org:40/search?q=dessert#results'); $this->assertIdentical($uri, array( 'scheme' => 'http', 'host' => 'www.cakephp.org', 'port' => 40, 'user' => 'bob', 'pass' => 'foo123', 'path' => '/search', 'query' => array('q' => 'dessert'), 'fragment' => 'results' )); $uri = $this->Socket->parseUri('http://www.cakephp.org/'); $this->assertIdentical($uri, array( 'scheme' => 'http', 'host' => 'www.cakephp.org', 'path' => '/', )); $uri = $this->Socket->parseUri('http://www.cakephp.org', true); $this->assertIdentical($uri, array( 'scheme' => 'http', 'host' => 'www.cakephp.org', 'port' => 80, 'user' => null, 'pass' => null, 'path' => '/', 'query' => array(), 'fragment' => null )); $uri = $this->Socket->parseUri('https://www.cakephp.org', true); $this->assertIdentical($uri, array( 'scheme' => 'https', 'host' => 'www.cakephp.org', 'port' => 443, 'user' => null, 'pass' => null, 'path' => '/', 'query' => array(), 'fragment' => null )); $uri = $this->Socket->parseUri('www.cakephp.org:443/query?foo', true); $this->assertIdentical($uri, array( 'scheme' => 'https', 'host' => 'www.cakephp.org', 'port' => 443, 'user' => null, 'pass' => null, 'path' => '/query', 'query' => array('foo' => ""), 'fragment' => null )); $uri = $this->Socket->parseUri('http://www.cakephp.org', array('host' => 'piephp.org', 'user' => 'bob', 'fragment' => 'results')); $this->assertIdentical($uri, array( 'host' => 'www.cakephp.org', 'user' => 'bob', 'fragment' => 'results', 'scheme' => 'http' )); $uri = $this->Socket->parseUri('https://www.cakephp.org', array('scheme' => 'http', 'port' => 23)); $this->assertIdentical($uri, array( 'scheme' => 'https', 'port' => 23, 'host' => 'www.cakephp.org' )); $uri = $this->Socket->parseUri('www.cakephp.org:59', array('scheme' => array('http', 'https'), 'port' => 80)); $this->assertIdentical($uri, array( 'scheme' => 'http', 'port' => 59, 'host' => 'www.cakephp.org' )); $uri = $this->Socket->parseUri(array('scheme' => 'http', 'host' => 'www.google.com', 'port' => 8080), array('scheme' => array('http', 'https'), 'host' => 'www.google.com', 'port' => array(80, 443))); $this->assertIdentical($uri, array( 'scheme' => 'http', 'host' => 'www.google.com', 'port' => 8080, )); }/** * Tests that HttpSocket::buildUri can turn all kinds of uri arrays (and strings) into fully or partially qualified URI's * */ function testBuildUri() { $this->Socket->reset();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -