See Release Notes
Long Term Support Release
Differences Between: [Versions 400 and 401]
1 <?php 2 // This file is part of Moodle - http://moodle.org/ 3 // 4 // Moodle is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // Moodle is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 16 17 namespace enrol_lti\local\ltiadvantage\lib; 18 19 /** 20 * Tests for the http_client class. 21 * 22 * @package enrol_lti 23 * @copyright 2022 Jake Dallimore <jrhdallimore@gmail.com> 24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 25 * @coversDefaultClass \enrol_lti\local\ltiadvantage\lib\http_client 26 */ 27 class http_client_test extends \advanced_testcase { 28 29 /** 30 * Verify the http_client delegates to curl during a "GET" request. 31 * 32 * @covers ::request 33 */ 34 public function test_client_get_request() { 35 global $CFG; 36 require_once($CFG->libdir . '/filelib.php'); 37 38 $mockcurl = $this->createMock(\curl::class); 39 $mockcurl->expects($this->once()) 40 ->method('get') 41 ->with( 42 $this->equalTo('https://example.com'), 43 $this->equalTo([]), 44 $this->equalTo(['CURLOPT_HEADER' => 1]) 45 ); 46 $mockcurl->expects($this->any()) 47 ->method('get_info') 48 ->willReturnCallback(function() { 49 return ['header_size' => 0, 'http_code' => 200]; 50 }); 51 $mockcurl->expects($this->once()) 52 ->method('setHeader') 53 ->with($this->equalTo(['someheader' => 'someheader: headervalue'])); 54 55 $client = new http_client($mockcurl); 56 $client->request('GET', 'https://example.com', ['headers' => ['someheader' => 'headervalue']]); 57 } 58 59 /** 60 * Verify the http_client delegates to curl during a "POST" request. 61 * 62 * @covers ::request 63 */ 64 public function test_client_post_request() { 65 global $CFG; 66 require_once($CFG->libdir . '/filelib.php'); 67 68 $mockcurl = $this->createMock(\curl::class); 69 $mockcurl->expects($this->once()) 70 ->method('post') 71 ->with( 72 $this->equalTo('https://example.com'), 73 $this->equalTo('examplebody'), 74 $this->equalTo(['CURLOPT_HEADER' => 1]) 75 ); 76 $mockcurl->expects($this->any()) 77 ->method('get_info') 78 ->willReturnCallback(function() { 79 return ['header_size' => 0, 'http_code' => 200]; 80 }); 81 $mockcurl->expects($this->once()) 82 ->method('setHeader') 83 ->with($this->equalTo(['someheader' => 'someheader: headervalue'])); 84 85 $client = new http_client($mockcurl); 86 $client->request('POST', 'https://example.com', ['headers' => ['someheader' => 'headervalue'], 'body' => 'examplebody']); 87 } 88 89 /** 90 * Test a few of the unsupported HTTP methods. 91 * 92 * @dataProvider unsupported_methods_provider 93 * @param string $httpmethod the http method. 94 * @covers ::request 95 */ 96 public function test_request_unsupported_method(string $httpmethod) { 97 global $CFG; 98 require_once($CFG->libdir . '/filelib.php'); 99 100 $mockcurl = $this->createMock(\curl::class); 101 102 $client = new http_client($mockcurl); 103 $this->expectException(\Exception::class); 104 $client->request($httpmethod, 'https://example.com', []); 105 } 106 107 /** 108 * Data provider for testing unsupported http methods. 109 * 110 * @return array the test case data. 111 */ 112 public function unsupported_methods_provider() { 113 return [ 114 'head' => ['HEAD'], 115 'put' => ['PUT'], 116 'delete' => ['DELETE'], 117 ]; 118 } 119 120 /** 121 * Verify that the response headers are properly read from curl, and exclude things like redirect headers, or 100-continues. 122 * @covers ::request 123 */ 124 public function test_header_parsing(): void { 125 $testurl = $this->getExternalTestFileUrl('/test_redir.php'); 126 $client = new http_client(new \curl()); 127 $response = $client->request('POST', "$testurl?redir=1", 128 ['headers' => ['Expect' => '100-continue'], 'body' => 'foo']); 129 $headers = $response->getHeaders(); 130 $this->assertEquals('200 OK', reset($headers)); 131 } 132 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body