|
4 | 4 |
|
5 | 5 | use PHPUnit\Framework\TestCase;
|
6 | 6 | use RemoteDataBlocks\Tests\Mocks\MockDataSource;
|
| 7 | +use RemoteDataBlocks\Tests\Mocks\MockValidator; |
7 | 8 | use WP_Error;
|
8 | 9 |
|
9 | 10 | class HttpDataSourceTest extends TestCase {
|
@@ -67,4 +68,158 @@ public function testMigrateConfigMethodCanBeOverridden_user_id_is_not_an_integer
|
67 | 68 | $this->assertInstanceOf( WP_Error::class, $this->http_data_source );
|
68 | 69 | $this->assertSame( 'testUserId must be an integer', $this->http_data_source->get_error_message() );
|
69 | 70 | }
|
| 71 | + |
| 72 | + public function testBearerAuthHeaderIsAdded(): void { |
| 73 | + $config = [ |
| 74 | + 'service_config' => [ |
| 75 | + '__version' => 1, |
| 76 | + 'display_name' => 'Mock Data Source', |
| 77 | + 'endpoint' => 'http://example.com', |
| 78 | + 'auth' => [ |
| 79 | + 'type' => 'bearer', |
| 80 | + 'value' => 'test-token', |
| 81 | + ], |
| 82 | + ], |
| 83 | + ]; |
| 84 | + $this->http_data_source = MockDataSource::create( $config ); |
| 85 | + |
| 86 | + $this->assertNotInstanceOf( WP_Error::class, $this->http_data_source ); |
| 87 | + $headers = $this->http_data_source->get_request_headers(); |
| 88 | + $this->assertIsArray( $headers ); |
| 89 | + $this->assertArrayHasKey( 'Authorization', $headers ); |
| 90 | + $this->assertSame( 'Bearer test-token', $headers['Authorization'] ); |
| 91 | + } |
| 92 | + |
| 93 | + public function testBasicAuthHeaderIsAdded(): void { |
| 94 | + $config = [ |
| 95 | + 'service_config' => [ |
| 96 | + '__version' => 1, |
| 97 | + 'display_name' => 'Mock Data Source', |
| 98 | + 'endpoint' => 'http://example.com', |
| 99 | + 'auth' => [ |
| 100 | + 'type' => 'basic', |
| 101 | + 'value' => 'user:pass', |
| 102 | + ], |
| 103 | + ], |
| 104 | + ]; |
| 105 | + $this->http_data_source = MockDataSource::create( $config ); |
| 106 | + |
| 107 | + $this->assertNotInstanceOf( WP_Error::class, $this->http_data_source ); |
| 108 | + $headers = $this->http_data_source->get_request_headers(); |
| 109 | + $this->assertIsArray( $headers ); |
| 110 | + $this->assertArrayHasKey( 'Authorization', $headers ); |
| 111 | + $this->assertSame( 'Basic ' . base64_encode( 'user:pass' ), $headers['Authorization'] ); |
| 112 | + } |
| 113 | + |
| 114 | + public function testApiKeyHeaderIsAdded(): void { |
| 115 | + $config = [ |
| 116 | + 'service_config' => [ |
| 117 | + '__version' => 1, |
| 118 | + 'display_name' => 'Mock Data Source', |
| 119 | + 'endpoint' => 'http://example.com', |
| 120 | + 'auth' => [ |
| 121 | + 'type' => 'api-key', |
| 122 | + 'add_to' => 'header', |
| 123 | + 'key' => 'X-Api-Key', |
| 124 | + 'value' => 'test-api-key', |
| 125 | + ], |
| 126 | + ], |
| 127 | + ]; |
| 128 | + $this->http_data_source = MockDataSource::create( $config ); |
| 129 | + |
| 130 | + $this->assertNotInstanceOf( WP_Error::class, $this->http_data_source ); |
| 131 | + $headers = $this->http_data_source->get_request_headers(); |
| 132 | + $this->assertIsArray( $headers ); |
| 133 | + $this->assertArrayHasKey( 'X-Api-Key', $headers ); |
| 134 | + $this->assertSame( 'test-api-key', $headers['X-Api-Key'] ); |
| 135 | + } |
| 136 | + |
| 137 | + public function testApiKeyQueryParamIsAdded(): void { |
| 138 | + $config = [ |
| 139 | + 'service_config' => [ |
| 140 | + '__version' => 1, |
| 141 | + 'display_name' => 'Mock Data Source', |
| 142 | + 'endpoint' => 'http://example.com/data', |
| 143 | + 'auth' => [ |
| 144 | + 'type' => 'api-key', |
| 145 | + 'add_to' => 'queryparams', |
| 146 | + 'key' => 'api_key', |
| 147 | + 'value' => 'test-api-key', |
| 148 | + ], |
| 149 | + ], |
| 150 | + ]; |
| 151 | + $this->http_data_source = MockDataSource::create( $config ); |
| 152 | + |
| 153 | + $this->assertNotInstanceOf( WP_Error::class, $this->http_data_source ); |
| 154 | + $endpoint = $this->http_data_source->get_endpoint(); |
| 155 | + $this->assertSame( 'http://example.com/data?api_key=test-api-key', $endpoint ); |
| 156 | + } |
| 157 | + |
| 158 | + public function testApiKeyQueryParamIsAppended(): void { |
| 159 | + $config = [ |
| 160 | + 'service_config' => [ |
| 161 | + '__version' => 1, |
| 162 | + 'display_name' => 'Mock Data Source', |
| 163 | + 'endpoint' => 'http://example.com/data?existing=true', |
| 164 | + 'auth' => [ |
| 165 | + 'type' => 'api-key', |
| 166 | + 'add_to' => 'queryparams', |
| 167 | + 'key' => 'api_key', |
| 168 | + 'value' => 'test-api-key', |
| 169 | + ], |
| 170 | + ], |
| 171 | + ]; |
| 172 | + $this->http_data_source = MockDataSource::create( $config ); |
| 173 | + |
| 174 | + $this->assertNotInstanceOf( WP_Error::class, $this->http_data_source ); |
| 175 | + $endpoint = $this->http_data_source->get_endpoint(); |
| 176 | + $this->assertSame( 'http://example.com/data?existing=true&api_key=test-api-key', $endpoint ); |
| 177 | + } |
| 178 | + |
| 179 | + public function testNoAuthLeavesConfigUnchanged(): void { |
| 180 | + $config = [ |
| 181 | + 'service_config' => [ |
| 182 | + '__version' => 1, |
| 183 | + 'display_name' => 'Mock Data Source', |
| 184 | + 'endpoint' => 'http://example.com/data', |
| 185 | + 'request_headers' => [ 'X-Custom' => 'value' ], |
| 186 | + ], |
| 187 | + ]; |
| 188 | + $this->http_data_source = MockDataSource::create( $config ); |
| 189 | + |
| 190 | + $this->assertNotInstanceOf( WP_Error::class, $this->http_data_source ); |
| 191 | + $headers = $this->http_data_source->get_request_headers(); |
| 192 | + $endpoint = $this->http_data_source->get_endpoint(); |
| 193 | + |
| 194 | + $this->assertIsArray( $headers ); |
| 195 | + $this->assertSame( [ 'X-Custom' => 'value' ], $headers ); |
| 196 | + $this->assertSame( 'http://example.com/data', $endpoint ); |
| 197 | + } |
| 198 | + |
| 199 | + public function testExistingHeadersAreMergedWithAuthHeaders(): void { |
| 200 | + $config = [ |
| 201 | + 'service_config' => [ |
| 202 | + '__version' => 1, |
| 203 | + 'display_name' => 'Mock Data Source', |
| 204 | + 'endpoint' => 'http://example.com', |
| 205 | + 'request_headers' => [ 'X-Custom' => 'value' ], |
| 206 | + 'auth' => [ |
| 207 | + 'type' => 'bearer', |
| 208 | + 'value' => 'test-token', |
| 209 | + ], |
| 210 | + ], |
| 211 | + ]; |
| 212 | + $this->http_data_source = MockDataSource::create( $config ); |
| 213 | + |
| 214 | + $this->assertNotInstanceOf( WP_Error::class, $this->http_data_source ); |
| 215 | + $headers = $this->http_data_source->get_request_headers(); |
| 216 | + $this->assertIsArray( $headers ); |
| 217 | + $this->assertSame( |
| 218 | + [ |
| 219 | + 'X-Custom' => 'value', |
| 220 | + 'Authorization' => 'Bearer test-token', |
| 221 | + ], |
| 222 | + $headers |
| 223 | + ); |
| 224 | + } |
70 | 225 | }
|
0 commit comments