3 use MediaWiki\Http\HttpRequestFactory
;
6 * @covers MediaWiki\Http\HttpRequestFactory
8 class HttpRequestFactoryTest
extends MediaWikiTestCase
{
11 * @return HttpRequestFactory
13 private function newFactory() {
14 return new HttpRequestFactory();
18 * @return HttpRequestFactory
20 private function newFactoryWithFakeRequest(
25 $factory = $this->getMockBuilder( HttpRequestFactory
::class )
26 ->setMethods( [ 'create' ] )
29 $factory->method( 'create' )
31 function ( $url, array $options = [], $caller = __METHOD__
)
32 use ( $req, $expectedUrl, $expectedOptions )
34 $this->assertSame( $url, $expectedUrl );
36 foreach ( $expectedOptions as $opt => $exp ) {
37 $this->assertArrayHasKey( $opt, $options );
38 $this->assertSame( $exp, $options[$opt] );
49 * @return MWHttpRequest
51 private function newFakeRequest( $result ) {
52 $req = $this->getMockBuilder( MWHttpRequest
::class )
53 ->disableOriginalConstructor()
54 ->setMethods( [ 'getContent', 'execute' ] )
57 if ( $result instanceof Status
) {
58 $req->method( 'getContent' )
59 ->willReturn( $result->getValue() );
60 $req->method( 'execute' )
61 ->willReturn( $result );
63 $req->method( 'getContent' )
64 ->willReturn( $result );
65 $req->method( 'execute' )
66 ->willReturn( Status
::newGood( $result ) );
72 public function testCreate() {
73 $factory = $this->newFactory();
74 $this->assertInstanceOf( 'MWHttpRequest', $factory->create( 'http://example.test' ) );
77 public function testGetUserAgent() {
78 $factory = $this->newFactory();
79 $this->assertStringStartsWith( 'MediaWiki/', $factory->getUserAgent() );
82 public function testGet() {
83 $req = $this->newFakeRequest( __METHOD__
);
84 $factory = $this->newFactoryWithFakeRequest(
85 $req, 'https://example.test', [ 'method' => 'GET' ]
88 $this->assertSame( __METHOD__
, $factory->get( 'https://example.test' ) );
91 public function testPost() {
92 $req = $this->newFakeRequest( __METHOD__
);
93 $factory = $this->newFactoryWithFakeRequest(
94 $req, 'https://example.test', [ 'method' => 'POST' ]
97 $this->assertSame( __METHOD__
, $factory->post( 'https://example.test' ) );
100 public function testRequest() {
101 $req = $this->newFakeRequest( __METHOD__
);
102 $factory = $this->newFactoryWithFakeRequest(
103 $req, 'https://example.test', [ 'method' => 'GET' ]
106 $this->assertSame( __METHOD__
, $factory->request( 'GET', 'https://example.test' ) );
109 public function testRequest_failed() {
110 $status = Status
::newFatal( 'testing' );
111 $req = $this->newFakeRequest( $status );
112 $factory = $this->newFactoryWithFakeRequest(
113 $req, 'https://example.test', [ 'method' => 'POST' ]
116 $this->assertNull( $factory->request( 'POST', 'https://example.test' ) );